Skip to content

Commit e53be9c

Browse files
committed
feat: add cached property for device configuration and handle UnsupportedOperation in mode change methods
1 parent 4815dd6 commit e53be9c

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/blinkstick/clients/blinkstick.py

Lines changed: 21 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
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":
@@ -78,6 +80,15 @@ def wrapper(*args, **kwargs):
7880
return wrapper
7981
return attr
8082

83+
@cached_property
84+
def _config(self) -> Configuration:
85+
"""
86+
Get the hardware configuration of the connected device, using the reported variant.
87+
88+
@rtype: Configuration
89+
"""
90+
return _get_device_config(self.get_variant())
91+
8192
def get_serial(self) -> str:
8293
"""
8394
Returns the serial number of backend.::
@@ -372,6 +383,10 @@ def set_mode(self, mode: int) -> None:
372383
@type mode: int
373384
@param mode: Device mode to set
374385
"""
386+
if not self._config.mode_change_support:
387+
raise UnsupportedOperation(
388+
"This operation is only supported on BlinkStick Pro devices"
389+
)
375390
control_string = bytes(bytearray([4, mode]))
376391

377392
self.backend.control_transfer(0x20, 0x9, 0x0004, 0, control_string)
@@ -391,6 +406,10 @@ def get_mode(self) -> int:
391406
@rtype: int
392407
@return: Device mode
393408
"""
409+
if not self._config.mode_change_support:
410+
raise UnsupportedOperation(
411+
"This operation is only supported on BlinkStick Pro devices"
412+
)
394413

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

0 commit comments

Comments
 (0)