Skip to content

Commit

Permalink
Fix brightness reporting on 0xA1 models (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 30, 2021
1 parent 2d73454 commit e3478a8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
19 changes: 13 additions & 6 deletions flux_led/base_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions flux_led/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit e3478a8

Please sign in to comment.