From 6b1fc00910e776dba53f6c5fd297a892cbd9057b Mon Sep 17 00:00:00 2001 From: TimL Date: Fri, 6 Sep 2024 23:46:08 +1000 Subject: [PATCH] Improve handling of old firmware versions (#125406) * Update Info fixture with new fields from pysmlight 0.0.14 * Create repair if device is running unsupported firmware * Add test for legacy firmware info * Add strings for repair issue --- .../components/smlight/coordinator.py | 22 +++++++++++- homeassistant/components/smlight/strings.json | 6 ++++ tests/components/smlight/fixtures/info.json | 4 ++- .../smlight/snapshots/test_init.ambr | 2 +- tests/components/smlight/test_init.py | 34 ++++++++++++++++++- 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/smlight/coordinator.py b/homeassistant/components/smlight/coordinator.py index 380644c81d1e1..094c6ec9cdb60 100644 --- a/homeassistant/components/smlight/coordinator.py +++ b/homeassistant/components/smlight/coordinator.py @@ -9,8 +9,10 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.helpers import issue_registry as ir from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import format_mac +from homeassistant.helpers.issue_registry import IssueSeverity from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import DOMAIN, LOGGER, SCAN_INTERVAL @@ -40,6 +42,7 @@ def __init__(self, hass: HomeAssistant, host: str) -> None: self.unique_id: str | None = None self.client = Api2(host=host, session=async_get_clientsession(hass)) + self.legacy_api: int = 0 async def _async_setup(self) -> None: """Authenticate if needed during initial setup.""" @@ -62,11 +65,28 @@ async def _async_setup(self) -> None: info = await self.client.get_info() self.unique_id = format_mac(info.MAC) + if info.legacy_api: + self.legacy_api = info.legacy_api + ir.async_create_issue( + self.hass, + DOMAIN, + "unsupported_firmware", + is_fixable=False, + is_persistent=False, + learn_more_url="https://smlight.tech/flasher/#SLZB-06", + severity=IssueSeverity.ERROR, + translation_key="unsupported_firmware", + ) + async def _async_update_data(self) -> SmData: """Fetch data from the SMLIGHT device.""" try: + sensors = Sensors() + if not self.legacy_api: + sensors = await self.client.get_sensors() + return SmData( - sensors=await self.client.get_sensors(), + sensors=sensors, info=await self.client.get_info(), ) except SmlightAuthError as err: diff --git a/homeassistant/components/smlight/strings.json b/homeassistant/components/smlight/strings.json index e3e8fee0d4d89..8628a49a13c68 100644 --- a/homeassistant/components/smlight/strings.json +++ b/homeassistant/components/smlight/strings.json @@ -92,5 +92,11 @@ "name": "LED night mode" } } + }, + "issues": { + "unsupported_firmware": { + "title": "SLZB core firmware update required", + "description": "Your SMLIGHT SLZB-06x device is running an unsupported core firmware version. Please update it to the latest version to enjoy all the features of this integration." + } } } diff --git a/tests/components/smlight/fixtures/info.json b/tests/components/smlight/fixtures/info.json index 72bb7c1ed9b6c..070232512f338 100644 --- a/tests/components/smlight/fixtures/info.json +++ b/tests/components/smlight/fixtures/info.json @@ -3,10 +3,12 @@ "device_ip": "192.168.1.161", "fs_total": 3456, "fw_channel": "dev", + "legacy_api": 0, + "hostname": "SLZB-06p7", "MAC": "AA:BB:CC:DD:EE:FF", "model": "SLZB-06p7", "ram_total": 296, - "sw_version": "v2.3.1.dev", + "sw_version": "v2.3.6", "wifi_mode": 0, "zb_flash_size": 704, "zb_hw": "CC2652P7", diff --git a/tests/components/smlight/snapshots/test_init.ambr b/tests/components/smlight/snapshots/test_init.ambr index 528a7b7b340f6..bb6a6c50f9b59 100644 --- a/tests/components/smlight/snapshots/test_init.ambr +++ b/tests/components/smlight/snapshots/test_init.ambr @@ -27,7 +27,7 @@ 'primary_config_entry': , 'serial_number': None, 'suggested_area': None, - 'sw_version': 'core: v2.3.1.dev / zigbee: -1', + 'sw_version': 'core: v2.3.6 / zigbee: -1', 'via_device_id': None, }) # --- diff --git a/tests/components/smlight/test_init.py b/tests/components/smlight/test_init.py index 1323c93e6bf36..eb7b6396d2609 100644 --- a/tests/components/smlight/test_init.py +++ b/tests/components/smlight/test_init.py @@ -3,15 +3,17 @@ from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory +from pysmlight import Info from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError, SmlightError import pytest from syrupy.assertion import SnapshotAssertion -from homeassistant.components.smlight.const import SCAN_INTERVAL +from homeassistant.components.smlight.const import DOMAIN, SCAN_INTERVAL from homeassistant.config_entries import ConfigEntryState from homeassistant.const import STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.issue_registry import IssueRegistry from .conftest import setup_integration @@ -110,3 +112,33 @@ async def test_device_info( ) assert device_entry is not None assert device_entry == snapshot + + +async def test_device_legacy_firmware( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_smlight_client: MagicMock, + device_registry: dr.DeviceRegistry, + issue_registry: IssueRegistry, +) -> None: + """Test device setup for old firmware version that dont support required API.""" + LEGACY_VERSION = "v2.3.1" + mock_smlight_client.get_sensors.side_effect = SmlightError + mock_smlight_client.get_info.return_value = Info( + legacy_api=1, sw_version=LEGACY_VERSION, MAC="AA:BB:CC:DD:EE:FF" + ) + entry = await setup_integration(hass, mock_config_entry) + + assert entry.unique_id == "aa:bb:cc:dd:ee:ff" + + device_entry = device_registry.async_get_device( + connections={(dr.CONNECTION_NETWORK_MAC, entry.unique_id)} + ) + assert LEGACY_VERSION in device_entry.sw_version + + issue = issue_registry.async_get_issue( + domain=DOMAIN, issue_id="unsupported_firmware" + ) + assert issue is not None + assert issue.domain == DOMAIN + assert issue.issue_id == "unsupported_firmware"