Cleanups related to improved typing on radios objects (#141455)

* Improved handling of radio objects

* Drop get_radio helper

* Remove mock of get_radio in tests
This commit is contained in:
TimL 2025-03-26 21:45:07 +11:00 committed by GitHub
parent 043603c9be
commit 57d02d7a17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 23 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations
from pysmlight import Api2, Info, Radio
from pysmlight import Api2
from homeassistant.const import CONF_HOST, Platform
from homeassistant.core import HomeAssistant
@ -50,9 +50,3 @@ async def async_setup_entry(hass: HomeAssistant, entry: SmConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: SmConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
def get_radio(info: Info, idx: int) -> Radio:
"""Get the radio object from the info."""
assert info.radios is not None
return info.radios[idx]

View File

@ -22,7 +22,6 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import get_radio
from .const import LOGGER
from .coordinator import SmConfigEntry, SmFirmwareUpdateCoordinator, SmFwData
from .entity import SmEntity
@ -56,7 +55,7 @@ CORE_UPDATE_ENTITY = SmUpdateEntityDescription(
ZB_UPDATE_ENTITY = SmUpdateEntityDescription(
key="zigbee_update",
translation_key="zigbee_update",
installed_version=lambda x, idx: get_radio(x, idx).zb_version,
installed_version=lambda x, idx: x.radios[idx].zb_version,
latest_version=zigbee_latest_version,
)
@ -75,7 +74,6 @@ async def async_setup_entry(
entities = [SmUpdateEntity(coordinator, CORE_UPDATE_ENTITY)]
radios = coordinator.data.info.radios
assert radios is not None
entities.extend(
SmUpdateEntity(coordinator, ZB_UPDATE_ENTITY, idx)

View File

@ -154,10 +154,9 @@ async def test_update_zigbee2_firmware(
mock_smlight_client: MagicMock,
) -> None:
"""Test update of zigbee2 firmware where available."""
mock_info = Info.from_dict(load_json_object_fixture("info-MR1.json", DOMAIN))
mock_smlight_client.get_info.side_effect = None
mock_smlight_client.get_info.return_value = Info.from_dict(
load_json_object_fixture("info-MR1.json", DOMAIN)
)
mock_smlight_client.get_info.return_value = mock_info
await setup_integration(hass, mock_config_entry)
entity_id = "update.mock_title_zigbee_firmware_2"
state = hass.states.get(entity_id)
@ -177,17 +176,17 @@ async def test_update_zigbee2_firmware(
event_function = get_mock_event_function(mock_smlight_client, SmEvents.FW_UPD_done)
event_function(MOCK_FIRMWARE_DONE)
with patch(
"homeassistant.components.smlight.update.get_radio", return_value=MOCK_RADIO
):
freezer.tick(timedelta(seconds=5))
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
assert state.attributes[ATTR_INSTALLED_VERSION] == "20240716"
assert state.attributes[ATTR_LATEST_VERSION] == "20240716"
mock_info.radios[1] = MOCK_RADIO
freezer.tick(timedelta(seconds=5))
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
assert state.attributes[ATTR_INSTALLED_VERSION] == "20240716"
assert state.attributes[ATTR_LATEST_VERSION] == "20240716"
async def test_update_legacy_firmware_v2(