Fix loading of SMLIGHT integration when no internet is available (#136497)

* Don't fail to load integration if internet unavailable

* Add test case for no internet

* Also test we recover after internet returns
pull/137390/head
TimL 2025-01-30 12:01:39 +11:00 committed by Bram Kragten
parent f391438d0a
commit 55ac0b0f37
3 changed files with 53 additions and 9 deletions

View File

@ -144,11 +144,15 @@ class SmFirmwareUpdateCoordinator(SmBaseDataUpdateCoordinator[SmFwData]):
async def _internal_update_data(self) -> SmFwData:
"""Fetch data from the SMLIGHT device."""
info = await self.client.get_info()
esp_firmware = None
zb_firmware = None
return SmFwData(
info=info,
esp_firmware=await self.client.get_firmware_version(info.fw_channel),
zb_firmware=await self.client.get_firmware_version(
try:
esp_firmware = await self.client.get_firmware_version(info.fw_channel)
zb_firmware = await self.client.get_firmware_version(
info.fw_channel, device=info.model, mode="zigbee"
),
)
)
except SmlightConnectionError as err:
self.async_set_update_error(err)
return SmFwData(info=info, esp_firmware=esp_firmware, zb_firmware=zb_firmware)

View File

@ -8,9 +8,14 @@ from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError, Smlig
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.smlight.const import DOMAIN, SCAN_INTERVAL
from homeassistant.components.smlight.const import (
DOMAIN,
SCAN_FIRMWARE_INTERVAL,
SCAN_INTERVAL,
)
from homeassistant.components.update import ATTR_INSTALLED_VERSION
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.issue_registry import IssueRegistry
@ -73,6 +78,41 @@ async def test_async_setup_missing_credentials(
assert progress[0]["context"]["unique_id"] == "aa:bb:cc:dd:ee:ff"
async def test_async_setup_no_internet(
hass: HomeAssistant,
mock_config_entry_host: MockConfigEntry,
mock_smlight_client: MagicMock,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test we still load integration when no internet is available."""
mock_smlight_client.get_firmware_version.side_effect = SmlightConnectionError
await setup_integration(hass, mock_config_entry_host)
entity = hass.states.get("update.mock_title_core_firmware")
assert entity is not None
assert entity.state == STATE_UNKNOWN
freezer.tick(SCAN_FIRMWARE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
entity = hass.states.get("update.mock_title_core_firmware")
assert entity is not None
assert entity.state == STATE_UNKNOWN
mock_smlight_client.get_firmware_version.side_effect = None
freezer.tick(SCAN_FIRMWARE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
entity = hass.states.get("update.mock_title_core_firmware")
assert entity is not None
assert entity.state == STATE_ON
assert entity.attributes[ATTR_INSTALLED_VERSION] == "v2.3.6"
@pytest.mark.parametrize("error", [SmlightConnectionError, SmlightAuthError])
async def test_update_failed(
hass: HomeAssistant,

View File

@ -81,7 +81,7 @@ async def test_update_setup(
mock_config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test setup of SMLIGHT switches."""
"""Test setup of SMLIGHT update entities."""
entry = await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)