From 53b3b34e3662cb98c852ee3248195a3adbf27695 Mon Sep 17 00:00:00 2001 From: "Peter F. Patel-Schneider" Date: Tue, 13 Feb 2024 03:14:40 -0500 Subject: [PATCH] device: improve features araay --- lib/logitech_receiver/hidpp20.py | 36 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/logitech_receiver/hidpp20.py b/lib/logitech_receiver/hidpp20.py index 42d0f545d9..0a9978e790 100644 --- a/lib/logitech_receiver/hidpp20.py +++ b/lib/logitech_receiver/hidpp20.py @@ -24,7 +24,7 @@ from struct import pack as _pack from struct import unpack as _unpack -from typing import List +from typing import List, Optional import yaml as _yaml @@ -247,14 +247,16 @@ class FeaturesArray(dict): def __init__(self, device): assert device is not None - self.supported = True + self.supported = True # Actually don't know whether it is supported yet self.device = device self.inverse = {} self.version = {} self.count = 0 - def _check(self): - if self.supported is False or not self.device.online: + def _check(self) -> Optional[bool]: + if not self.device.online: + return False + if self.supported is False: return False if self.device.protocol and self.device.protocol < 2.0: self.supported = False @@ -263,14 +265,14 @@ def _check(self): return True reply = self.device.request(0x0000, _pack('!H', FEATURE.FEATURE_SET)) if reply is not None: - fs_index = ord(reply[0:1]) + fs_index = reply[0] if fs_index: count = self.device.request(fs_index << 8) if count is None: logger.warn('FEATURE_SET found, but failed to read features count') return False else: - self.count = ord(count[:1]) + 1 # ROOT feature not included in count + self.count = count[0] + 1 # ROOT feature not included in count self[FEATURE.ROOT] = 0 self[FEATURE.FEATURE_SET] = fs_index return True @@ -278,7 +280,7 @@ def _check(self): self.supported = False return False - def get_feature(self, index): + def get_feature(self, index: int) -> Optional[_NamedInt]: feature = self.inverse.get(index) if feature is not None: return feature @@ -296,13 +298,15 @@ def enumerate(self): # return all features and their index, ordered by index feature = self.get_feature(index) yield feature, index - def get_feature_version(self, feature): + def get_feature_version(self, feature: _NamedInt) -> Optional[int]: if self[feature]: return self.version.get(feature, 0) - __bool__ = __nonzero__ = _check + def __contains__(self, feature: _NamedInt) -> bool: + index = self.__getitem__(feature) + return index is not None and index is not False - def __getitem__(self, feature): + def __getitem__(self, feature: _NamedInt) -> Optional[int]: index = super().get(feature) if index is not None: return index @@ -322,17 +326,13 @@ def __setitem__(self, feature, index): self.inverse[index] = feature def __delitem__(self, feature): - if type(super().get(feature)) == int: - self.inverse.pop(super().get(feature)) - super().__delitem__(feature) + raise Exception("Don't delete features from FeatureArray") - def __contains__(self, feature): # is a feature present - index = self.__getitem__(feature) - return index is not None and index is not False - - def __len__(self): + def __len__(self) -> int: return self.count + __bool__ = __nonzero__ = _check + class ReprogrammableKey: """Information about a control present on a device with the `REPROG_CONTROLS` feature.