Skip to content

Commit c36a331

Browse files
committed
feat: add cached property for device configuration and handle UnsupportedOperation in mode change methods
1 parent 67fb5df commit c36a331

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/blinkstick/clients/blinkstick.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import time
55
import warnings
6+
from functools import cached_property
67
from typing import Callable
78

89
from blinkstick.colors import (
@@ -12,10 +13,11 @@
1213
remap_rgb_value_reverse,
1314
ColorFormat,
1415
)
15-
from blinkstick.decorators import no_backend_required
16+
from blinkstick.configs import _get_device_config
1617
from blinkstick.devices import BlinkStickDevice
1718
from blinkstick.enums import BlinkStickVariant, Mode
18-
from blinkstick.exceptions import NotConnected
19+
from blinkstick.exceptions import NotConnected, UnsupportedOperation
20+
from blinkstick.models import Configuration
1921
from blinkstick.utilities import string_to_info_block_data
2022

2123
if sys.platform == "win32":
@@ -94,6 +96,15 @@ def __str__(self):
9496
return "Blinkstick - Not connected"
9597
return f"{variant} ({serial})"
9698

99+
@cached_property
100+
def _config(self) -> Configuration:
101+
"""
102+
Get the hardware configuration of the connected device, using the reported variant.
103+
104+
@rtype: Configuration
105+
"""
106+
return _get_device_config(self.get_variant())
107+
97108
def get_serial(self) -> str:
98109
"""
99110
Returns the serial number of backend.::
@@ -388,6 +399,11 @@ def set_mode(self, mode: Mode | int) -> None:
388399
@type mode: int
389400
@param mode: Device mode to set
390401
"""
402+
if not self._config.mode_change_support:
403+
raise UnsupportedOperation(
404+
"This operation is only supported on BlinkStick Pro devices"
405+
)
406+
391407
# If mode is an enum, get the value
392408
# this will allow the user to pass in the enum directly, and also gate the value to the enum values
393409
if not isinstance(mode, int):
@@ -411,6 +427,10 @@ def get_mode(self) -> int:
411427
@rtype: int
412428
@return: Device mode
413429
"""
430+
if not self._config.mode_change_support:
431+
raise UnsupportedOperation(
432+
"This operation is only supported on BlinkStick Pro devices"
433+
)
414434

415435
device_bytes = self.backend.control_transfer(0x80 | 0x20, 0x1, 0x0004, 0, 2)
416436

0 commit comments

Comments
 (0)