device: when finding name or codename ping if not known to be online

This commit is contained in:
Peter F. Patel-Schneider 2023-04-15 10:44:57 -04:00
parent 46a06f3870
commit 5b1d542d79
1 changed files with 8 additions and 11 deletions

View File

@ -170,35 +170,32 @@ class Device:
@property @property
def protocol(self): def protocol(self):
if not self._protocol and self.online: if not self._protocol:
self._protocol = _base.ping( self.ping()
self.handle or self.receiver.handle, self.number, long_message=self.bluetooth or self.hidpp_short is False
)
# if the ping failed, the peripheral is (almost) certainly offline
self.online = self._protocol is not None
# if _log.isEnabledFor(_DEBUG):
# _log.debug("device %d protocol %s", self.number, self._protocol)
return self._protocol or 0 return self._protocol or 0
@property @property
def codename(self): def codename(self):
if not self._codename: if not self._codename:
if not self.online: # be very defensive
self.ping()
if self.online and self.protocol >= 2.0: if self.online and self.protocol >= 2.0:
self._codename = _hidpp20.get_friendly_name(self) self._codename = _hidpp20.get_friendly_name(self)
if not self._codename: if not self._codename:
self._codename = self.name.split(' ', 1)[0] if self.name else None self._codename = self.name.split(' ', 1)[0] if self.name else None
elif self.receiver: if not self._codename and self.receiver:
codename = self.receiver.device_codename(self.number) codename = self.receiver.device_codename(self.number)
if codename: if codename:
self._codename = codename self._codename = codename
elif self.protocol < 2.0: elif self.protocol < 2.0:
self._codename = '? (%s)' % (self.wpid or self.product_id) self._codename = '? (%s)' % (self.wpid or self.product_id)
return self._codename if self._codename else '?? (%s)' % (self.wpid or self.product_id) return self._codename or '?? (%s)' % (self.wpid or self.product_id)
@property @property
def name(self): def name(self):
if not self._name: if not self._name:
if not self.online: # be very defensive
self.ping()
if self.online and self.protocol >= 2.0: if self.online and self.protocol >= 2.0:
self._name = _hidpp20.get_name(self) self._name = _hidpp20.get_name(self)
return self._name or self._codename or ('Unknown device %s' % (self.wpid or self.product_id)) return self._name or self._codename or ('Unknown device %s' % (self.wpid or self.product_id))