diff --git a/lib/logitech_receiver/device.py b/lib/logitech_receiver/device.py index 6636c313..b7be8095 100644 --- a/lib/logitech_receiver/device.py +++ b/lib/logitech_receiver/device.py @@ -305,6 +305,21 @@ class Device: self._profiles = _hidpp20.get_profiles(self) return self._profiles + def set_configuration(self, configuration, no_reply=False): + if self.online and self.protocol >= 2.0: + _hidpp20.config_change(self, configuration, no_reply=no_reply) + + def reset(self, no_reply=False): + self.set_configuration(0, no_reply) + + @property + def persister(self): + if not self._persister: + with self._persister_lock: + if not self._persister: + self._persister = _configuration.persister(self) + return self._persister + @property def settings(self): if not self._settings: @@ -328,21 +343,6 @@ class Device: self._feature_settings_checked = _check_feature_settings(self, self._settings) return self._settings - def set_configuration(self, configuration, no_reply=False): - if self.online and self.protocol >= 2.0: - _hidpp20.config_change(self, configuration, no_reply=no_reply) - - def reset(self, no_reply=False): - self.set_configuration(0, no_reply) - - @property - def persister(self): - if not self._persister: - with self._persister_lock: - if not self._persister: - self._persister = _configuration.persister(self) - return self._persister - def battery(self): # None or level, next, status, voltage if self.protocol < 2.0: return _hidpp10.get_battery(self) @@ -368,6 +368,8 @@ class Device: changed = self.battery_info != info self.battery_info, old_info = info, self.battery_info + if old_info is None: + old_info = Battery(None, None, None, None) alert, reason = ALERT.NONE, None if not info.ok(): @@ -387,30 +389,6 @@ class Device: battery = self.battery() self.set_battery_info(battery if battery is not None else Battery(None, None, None, None)) - def enable_connection_notifications(self, enable=True): - """Enable or disable device (dis)connection notifications on this - receiver.""" - if not bool(self.receiver) or self.protocol >= 2.0: - return False - - if enable: - set_flag_bits = ( - hidpp10_constants.NOTIFICATION_FLAG.battery_status - | hidpp10_constants.NOTIFICATION_FLAG.ui - | hidpp10_constants.NOTIFICATION_FLAG.configuration_complete - ) - else: - set_flag_bits = 0 - ok = _hidpp10.set_notification_flags(self, set_flag_bits) - if not ok: - logger.warning("%s: failed to %s device notifications", self, "enable" if enable else "disable") - - flag_bits = _hidpp10.get_notification_flags(self) - if logger.isEnabledFor(logging.INFO): - flag_names = None if flag_bits is None else tuple(hidpp10_constants.NOTIFICATION_FLAG.flag_names(flag_bits)) - logger.info("%s: device notifications %s %s", self, "enabled" if enable else "disabled", flag_names) - return flag_bits if ok else None - def changed(self, active=None, alert=ALERT.NONE, reason=None, push=False): """The status of the device had changed, so invoke the status callback. Also push notifications and settings to the device when necessary.""" @@ -442,6 +420,30 @@ class Device: if self.status_callback is not None: self.status_callback(self, alert, reason) + def enable_connection_notifications(self, enable=True): + """Enable or disable device (dis)connection notifications on this + receiver.""" + if not bool(self.receiver) or self.protocol >= 2.0: + return False + + if enable: + set_flag_bits = ( + hidpp10_constants.NOTIFICATION_FLAG.battery_status + | hidpp10_constants.NOTIFICATION_FLAG.ui + | hidpp10_constants.NOTIFICATION_FLAG.configuration_complete + ) + else: + set_flag_bits = 0 + ok = _hidpp10.set_notification_flags(self, set_flag_bits) + if not ok: + logger.warning("%s: failed to %s device notifications", self, "enable" if enable else "disable") + + flag_bits = _hidpp10.get_notification_flags(self) + if logger.isEnabledFor(logging.INFO): + flag_names = None if flag_bits is None else tuple(hidpp10_constants.NOTIFICATION_FLAG.flag_names(flag_bits)) + logger.info("%s: device notifications %s %s", self, "enabled" if enable else "disabled", flag_names) + return flag_bits if ok else None + def add_notification_handler(self, id: str, fn): """Adds the notification handling callback `fn` to this device under name `id`. If a callback has already been registered under this name, it's replaced with