Skip to content

Commit 1bc89cc

Browse files
bieniubdraco
andauthored
Check if firmware is supported by device model (#632)
* Check supported firmware by device model * Add comment * Cleaning * Fix fixture.py file * Cleaning * Add is_firmware_supported function * Add test * Additional condition * switch to dataclass * model names * compat * cleanup * preserve const * preen * preen * preen * add tests for constru * Update tests/test_const.py * fixes --------- Co-authored-by: J. Nick Koston <nick@koston.org>
1 parent af37c74 commit 1bc89cc

File tree

8 files changed

+839
-170
lines changed

8 files changed

+839
-170
lines changed

aioshelly/block_device/device.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
from aiohttp import ClientResponse, ClientResponseError, ClientSession, ClientTimeout
1313
from yarl import URL
1414

15-
from ..common import ConnectionOptions, IpOrOptionsType, get_info, process_ip_or_options
15+
from ..common import (
16+
ConnectionOptions,
17+
IpOrOptionsType,
18+
get_info,
19+
is_firmware_supported,
20+
process_ip_or_options,
21+
)
1622
from ..const import (
1723
CONNECT_ERRORS,
1824
DEFAULT_HTTP_PORT,
1925
DEVICE_IO_TIMEOUT,
20-
FIRMWARE_PATTERN,
21-
GEN1_LIGHT_TRANSITION_MIN_FIRMWARE_DATE,
22-
GEN1_MIN_FIRMWARE_DATE,
23-
GEN1_MODELS_SUPPORTING_LIGHT_TRANSITION,
24-
GEN1_MODELS_UNSUPPORTED,
2526
HTTP_CALL_TIMEOUT,
2627
MODEL_RGBW2,
2728
)
@@ -451,22 +452,7 @@ def last_error(self) -> ShellyError | None:
451452
@property
452453
def firmware_supported(self) -> bool:
453454
"""Return True if device firmware version is supported."""
454-
if self.model in GEN1_MODELS_UNSUPPORTED:
455-
return False
456-
457-
if self.model in GEN1_MODELS_SUPPORTING_LIGHT_TRANSITION:
458-
fw_ver = GEN1_LIGHT_TRANSITION_MIN_FIRMWARE_DATE
459-
else:
460-
fw_ver = GEN1_MIN_FIRMWARE_DATE
461-
462-
match = FIRMWARE_PATTERN.search(self.firmware_version)
463-
464-
if match is None:
465-
return False
466-
467-
# We compare firmware release dates because Shelly version numbering is
468-
# inconsistent, sometimes the word is used as the version number.
469-
return int(match[0]) >= fw_ver
455+
return is_firmware_supported(self.gen, self.model, self.firmware_version)
470456

471457

472458
class Block:

aioshelly/common.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
from aiohttp import BasicAuth, ClientSession, ClientTimeout
1313
from yarl import URL
1414

15-
from .const import CONNECT_ERRORS, DEFAULT_HTTP_PORT, DEVICE_IO_TIMEOUT
15+
from .const import (
16+
CONNECT_ERRORS,
17+
DEFAULT_HTTP_PORT,
18+
DEVICE_IO_TIMEOUT,
19+
DEVICES,
20+
FIRMWARE_PATTERN,
21+
MIN_FIRMWARE_DATES,
22+
)
1623
from .exceptions import (
1724
DeviceConnectionError,
1825
MacAddressMismatchError,
@@ -91,3 +98,23 @@ async def get_info(
9198
raise mac_err
9299

93100
return result
101+
102+
103+
def is_firmware_supported(gen: int, model: str, firmware_version: str) -> bool:
104+
"""Return True if firmware is supported."""
105+
fw_ver: int | None
106+
if device := DEVICES.get(model):
107+
# Specific model is known
108+
if not device.supported:
109+
return False
110+
fw_ver = device.min_fw_date
111+
elif not (fw_ver := MIN_FIRMWARE_DATES.get(gen)):
112+
# Protection against future generations of devices.
113+
return False
114+
115+
match = FIRMWARE_PATTERN.search(firmware_version)
116+
if match is None:
117+
return False
118+
# We compare firmware release dates because Shelly version numbering is
119+
# inconsistent, sometimes the word is used as the version number.
120+
return int(match[0]) >= fw_ver

0 commit comments

Comments
 (0)