device: use FRIENDLY NAME for codename if needed and available
This commit is contained in:
parent
254e2cb77e
commit
7a7aad8977
|
@ -195,16 +195,17 @@ class Device(object):
|
||||||
@property
|
@property
|
||||||
def codename(self):
|
def codename(self):
|
||||||
if not self._codename:
|
if not self._codename:
|
||||||
codename = self.receiver.read_register(_R.receiver_info, 0x40 + self.number - 1) if self.receiver else None
|
if self.online and self.protocol >= 2.0:
|
||||||
if codename:
|
self._codename = _hidpp20.get_friendly_name(self)
|
||||||
codename_length = ord(codename[1:2])
|
elif self.receiver:
|
||||||
codename = codename[2:2 + codename_length]
|
codename = self.receiver.read_register(_R.receiver_info, 0x40 + self.number - 1)
|
||||||
self._codename = codename.decode('ascii')
|
if codename:
|
||||||
# if _log.isEnabledFor(_DEBUG):
|
codename_length = ord(codename[1:2])
|
||||||
# _log.debug("device %d codename %s", self.number, self._codename)
|
codename = codename[2:2 + codename_length]
|
||||||
else:
|
self._codename = codename.decode('utf-8')
|
||||||
self._codename = '? (%s)' % (self.wpid or self.product_id)
|
elif self.protocol < 2.0:
|
||||||
return self._codename
|
self._codename = '? (%s)' % (self.wpid or self.product_id)
|
||||||
|
return self._codename if self._codename else '?? (%s)' % (self.wpid or self.product_id)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
|
|
@ -1100,7 +1100,30 @@ def get_name(device):
|
||||||
_log.error('failed to read whole name of %s (expected %d chars)', device, name_length)
|
_log.error('failed to read whole name of %s (expected %d chars)', device, name_length)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return name.decode('ascii')
|
return name.decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
def get_friendly_name(device):
|
||||||
|
"""Reads a device's friendly name.
|
||||||
|
|
||||||
|
:returns: a string with the device name, or ``None`` if the device is not
|
||||||
|
available or does not support the ``DEVICE_NAME`` feature.
|
||||||
|
"""
|
||||||
|
name_length = feature_request(device, FEATURE.DEVICE_FRIENDLY_NAME)
|
||||||
|
if name_length:
|
||||||
|
name_length = ord(name_length[:1])
|
||||||
|
|
||||||
|
name = b''
|
||||||
|
while len(name) < name_length:
|
||||||
|
fragment = feature_request(device, FEATURE.DEVICE_FRIENDLY_NAME, 0x10, len(name))
|
||||||
|
if fragment:
|
||||||
|
initial_null = 0 if fragment[0] else 1 # initial null actually seen on a device
|
||||||
|
name += fragment[initial_null:name_length + initial_null - len(name)]
|
||||||
|
else:
|
||||||
|
_log.error('failed to read whole name of %s (expected %d chars)', device, name_length)
|
||||||
|
return None
|
||||||
|
|
||||||
|
return name.decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
def get_battery(device):
|
def get_battery(device):
|
||||||
|
|
|
@ -460,10 +460,9 @@ def _device_row(receiver_path, device_number, device=None):
|
||||||
icon_name = _icons.device_icon_name(device.name, device.kind)
|
icon_name = _icons.device_icon_name(device.name, device.kind)
|
||||||
status_text = None
|
status_text = None
|
||||||
status_icon = None
|
status_icon = None
|
||||||
codename = device.codename if device.codename and device.codename[0] != '?' else (
|
row_data = (
|
||||||
device.name.split()[0] if device.name.split() else device.codename
|
receiver_path, device_number, bool(device.online), device.codename, icon_name, status_text, status_icon, device
|
||||||
)
|
)
|
||||||
row_data = (receiver_path, device_number, bool(device.online), codename, icon_name, status_text, status_icon, device)
|
|
||||||
assert len(row_data) == len(_TREE_SEPATATOR)
|
assert len(row_data) == len(_TREE_SEPATATOR)
|
||||||
if _log.isEnabledFor(_DEBUG):
|
if _log.isEnabledFor(_DEBUG):
|
||||||
_log.debug('new device row %s at index %d', row_data, new_child_index)
|
_log.debug('new device row %s at index %d', row_data, new_child_index)
|
||||||
|
|
Loading…
Reference in New Issue