Skip to content

Commit 25a08ee

Browse files
committed
device: add present flag, unset when internal error occurs, set when notification appears
1 parent 277a23e commit 25a08ee

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

lib/logitech_receiver/device.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def __init__(
147147
self.battery_info = None
148148
self.link_encrypted = None
149149
self._active = None # lags self.online - is used to help determine when to setup devices
150+
self.present = True # used for devices that are integral with their receiver but that separately be disconnected
150151

151152
self._feature_settings_checked = False
152153
self._gestures_lock = threading.Lock()
@@ -432,6 +433,8 @@ def read_battery(self):
432433
def changed(self, active=None, alert=Alert.NONE, reason=None, push=False):
433434
"""The status of the device had changed, so invoke the status callback.
434435
Also push notifications and settings to the device when necessary."""
436+
if logger.isEnabledFor(logging.DEBUG):
437+
logger.debug("device %d changing: active=%s %s present=%s", self.number, active, self._active, self.present)
435438
if active is not None:
436439
self.online = active
437440
was_active, self._active = self._active, active
@@ -533,7 +536,8 @@ def feature_request(self, feature, function=0x00, *params, no_reply=False):
533536
return hidpp20.feature_request(self, feature, function, *params, no_reply=no_reply)
534537

535538
def ping(self):
536-
"""Checks if the device is online, returns True of False"""
539+
"""Checks if the device is online and present, returns True of False.
540+
Some devices are integral with their receiver but may not be present even if the receiver responds to ping."""
537541
long = self.hidpp_long is True or (
538542
self.hidpp_long is None and (self.bluetooth or self._protocol is not None and self._protocol >= 2.0)
539543
)
@@ -542,9 +546,11 @@ def ping(self):
542546
protocol = self.low_level.ping(handle, self.number, long_message=long)
543547
except exceptions.NoReceiver: # if ping fails, device is offline
544548
protocol = None
545-
self.online = protocol is not None
549+
self.online = protocol is not None and self.present
546550
if protocol:
547551
self._protocol = protocol
552+
if logger.isEnabledFor(logging.DEBUG):
553+
logger.debug("pinged %s: online %s protocol %s present %s", self.online, protocol, self.present)
548554
return self.online
549555

550556
def notify_devices(self): # no need to notify, as there are none

lib/logitech_receiver/notifications.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def _process_hidpp10_notification(device: Device, notification: HIDPPNotificatio
238238

239239

240240
def _process_feature_notification(device: Device, notification: HIDPPNotification):
241+
old_present, device.present = device.present, True # the device is generating a feature notification so it must be present
241242
try:
242243
feature = device.features.get_feature(notification.sub_id)
243244
except IndexError:
@@ -277,10 +278,11 @@ def _process_feature_notification(device: Device, notification: HIDPPNotificatio
277278
elif feature == SupportedFeature.ADC_MEASUREMENT:
278279
if notification.address == 0x00:
279280
result = hidpp20.decipher_adc_measurement(notification.data)
280-
if result: # this may be the only message signalling that the device has become active
281+
if result: # if good data and the device was not present then a push is needed
281282
device.set_battery_info(result[1])
282-
device.changed(active=True, alert=Alert.ALL, reason=_("ADC measurement notification"))
283+
device.changed(active=True, alert=Alert.ALL, reason=_("ADC measurement notification"), push=not old_present)
283284
else: # this feature is also used to signal device becoming inactive
285+
device.present = False # exception to device presence
284286
device.changed(active=False)
285287
else:
286288
logger.warning("%s: unknown ADC MEASUREMENT %s", device, notification)

lib/logitech_receiver/settings_templates.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1936,8 +1936,9 @@ def check_feature_settings(device, already_known) -> bool:
19361936
isinstance(err, exceptions.FeatureCallError)
19371937
and err.error == hidpp20_constants.ErrorCode.LOGITECH_ERROR
19381938
):
1939-
logger.warning(f"HID++ internal error when checking feature {sclass.name}: make device offline")
1939+
logger.warning(f"HID++ internal error checking feature {sclass.name}: make device not present")
19401940
device.online = False
1941+
device.present = False
19411942
return False
19421943
else:
19431944
logger.warning(f"ignore feature {sclass.name} because of error {err}")

0 commit comments

Comments
 (0)