Skip to content

Commit 419c819

Browse files
authored
Merge pull request #122 from robberwick/mode-enum
Introduce Mode enum
2 parents 3221a02 + 4cc8be2 commit 419c819

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/blinkstick/clients/blinkstick.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
)
1515
from blinkstick.decorators import no_backend_required
1616
from blinkstick.devices import BlinkStickDevice
17-
from blinkstick.enums import BlinkStickVariant
17+
from blinkstick.enums import BlinkStickVariant, Mode
1818
from blinkstick.exceptions import NotConnected
1919
from blinkstick.utilities import string_to_info_block_data
2020

@@ -373,7 +373,7 @@ def get_led_data(self, count: int) -> list[int]:
373373

374374
return device_bytes[2 : 2 + count * 3]
375375

376-
def set_mode(self, mode: int) -> None:
376+
def set_mode(self, mode: Mode | int) -> None:
377377
"""
378378
Set backend mode for BlinkStick Pro. Device currently supports the following modes:
379379
@@ -388,6 +388,10 @@ def set_mode(self, mode: int) -> None:
388388
@type mode: int
389389
@param mode: Device mode to set
390390
"""
391+
# If mode is an enum, get the value
392+
# this will allow the user to pass in the enum directly, and also gate the value to the enum values
393+
if not isinstance(mode, int):
394+
mode = Mode(mode).value
391395
control_string = bytes(bytearray([4, mode]))
392396

393397
self.backend.control_transfer(0x20, 0x9, 0x0004, 0, control_string)

src/blinkstick/enums.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from enum import Enum
3+
from enum import Enum, IntEnum
44

55

66
class BlinkStickVariant(Enum):
@@ -38,3 +38,9 @@ def from_version_attrs(
3838
elif version_attribute == 0x203:
3939
return BlinkStickVariant.BLINKSTICK_FLEX
4040
return BlinkStickVariant.UNKNOWN
41+
42+
43+
class Mode(IntEnum):
44+
RGB = 1
45+
RGB_INVERSE = 2
46+
ADDRESSABLE = 3

tests/clients/test_blinkstick.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from blinkstick.colors import ColorFormat
6-
from blinkstick.enums import BlinkStickVariant
6+
from blinkstick.enums import BlinkStickVariant, Mode
77
from blinkstick.clients.blinkstick import BlinkStick
88
from pytest_mock import MockFixture
99

@@ -310,3 +310,26 @@ def test_inverse_does_not_affect_max_rgb_value(make_blinkstick):
310310
bs.set_max_rgb_value(100)
311311
bs.set_inverse(True)
312312
assert bs.get_max_rgb_value() == 100
313+
314+
315+
@pytest.mark.parametrize(
316+
"mode, is_valid",
317+
[
318+
(1, True),
319+
(2, True),
320+
(3, True),
321+
(4, False),
322+
(-1, False),
323+
(Mode.RGB, True),
324+
(Mode.RGB_INVERSE, True),
325+
(Mode.ADDRESSABLE, True),
326+
],
327+
)
328+
def test_set_mode_raises_on_invalid_mode(make_blinkstick, mode, is_valid):
329+
"""Test that set_mode raises an exception when an invalid mode is passed."""
330+
bs = make_blinkstick()
331+
if is_valid:
332+
bs.set_mode(mode)
333+
else:
334+
with pytest.raises(ValueError):
335+
bs.set_mode("invalid_mode") # noqa

0 commit comments

Comments
 (0)