From e3478a8b0c98f86ad2be2476433e2f86092c5371 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 29 Nov 2021 18:40:06 -1000 Subject: [PATCH] Fix brightness reporting on 0xA1 models (#196) --- flux_led/base_device.py | 19 +++++++++++++------ flux_led/device.py | 4 ++-- tests.py | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/flux_led/base_device.py b/flux_led/base_device.py index bcbf5471..e3e1e55e 100644 --- a/flux_led/base_device.py +++ b/flux_led/base_device.py @@ -125,6 +125,12 @@ } +PATTERN_CODE_TO_EFFECT = { + PRESET_MUSIC_MODE: MODE_MUSIC, + EFFECT_CUSTOM_CODE: EFFECT_CUSTOM, +} + + class DeviceType(Enum): Bulb = 0 Switch = 1 @@ -342,13 +348,14 @@ def effect_list(self) -> List[str]: @property def effect(self) -> Optional[str]: """Return the current effect.""" + return PATTERN_CODE_TO_EFFECT.get(self.preset_pattern_num, self._named_effect) + + @property + def _named_effect(self) -> Optional[str]: + """Returns the named effect.""" assert self.raw_state is not None - pattern_code = self.preset_pattern_num - if pattern_code == PRESET_MUSIC_MODE: - return MODE_MUSIC - if pattern_code == EFFECT_CUSTOM_CODE: - return EFFECT_CUSTOM mode = self.raw_state.mode + pattern_code = self.preset_pattern_num protocol = self.protocol if protocol in OLD_EFFECTS_PROTOCOLS: effect_id = (pattern_code << 8) + mode - 99 @@ -386,7 +393,7 @@ def brightness(self) -> int: raw_state = self.raw_state assert raw_state is not None - if self._mode == MODE_PRESET: + if self._named_effect: if self.dimmable_effects: if ( self.protocol in NEW_EFFECTS_PROTOCOLS diff --git a/flux_led/device.py b/flux_led/device.py index 95d67e20..968f83af 100644 --- a/flux_led/device.py +++ b/flux_led/device.py @@ -143,7 +143,7 @@ def setRgbw( persist: bool = True, brightness: Optional[int] = None, w2: Optional[int] = None, - retry: int = 2, + retry: int = DEFAULT_RETRIES, ) -> None: self.set_levels(r, g, b, w, w2, persist, brightness, retry=retry) @@ -156,7 +156,7 @@ def set_levels( w2: Optional[int] = None, persist: bool = True, brightness: Optional[int] = None, - retry: Optional[int] = None, + retry: int = DEFAULT_RETRIES, ) -> None: self._process_levels_change( *self._generate_levels_change( diff --git a/tests.py b/tests.py index a40b8766..48b815a8 100644 --- a/tests.py +++ b/tests.py @@ -1707,6 +1707,11 @@ def read_data(expected): return bytearray( b"\x81\xA1#\x00\xa1\x01\x64\x00\x00\x00\x04\x00\xf0\x3f" ) + if calls == 4: + self.assertEqual(expected, 14) + return bytearray( + b"\x81\xA1\x23\x00\x61\x64\x07\x00\x21\x03\x03\x01\x2C\x65" + ) raise ValueError("Too many calls") mock_read.side_effect = read_data @@ -1764,3 +1769,14 @@ def read_data(expected): assert light.getSpeed() == 1 light.set_effect("random", 50) self.assertEqual(mock_send.call_count, 5) + + light.set_levels(128, 0, 0) + self.assertEqual(mock_read.call_count, 3) + self.assertEqual(mock_send.call_count, 6) + self.assertEqual( + mock_send.call_args, + mock.call(bytearray(b"1\x80\x00\x00\x00\x00\xf0\x0f\xb0")), + ) + light.update_state() + assert light.effect is None + assert light.brightness == 128