diff --git a/homeassistant/components/wattwaechter/coordinator.py b/homeassistant/components/wattwaechter/coordinator.py index f1da39f0e71b..8a21d0764cbc 100644 --- a/homeassistant/components/wattwaechter/coordinator.py +++ b/homeassistant/components/wattwaechter/coordinator.py @@ -64,7 +64,13 @@ class WattwaechterCoordinator(DataUpdateCoordinator[WattwaechterData]): LEGACY_MODEL_IDS.get(model, model) if model else None ) self.mac: str = config_entry.data[CONF_MAC] - self.fw_version: str | None = config_entry.data.get(CONF_FW_VERSION) + # The entry only stores the version from the initial setup. + device = dr.async_get(hass).async_get_device_by_identifier( + (DOMAIN, self.device_id), config_entry.entry_id + ) + self.fw_version: str | None = ( + device.sw_version if device else None + ) or config_entry.data.get(CONF_FW_VERSION) super().__init__( hass, @@ -121,7 +127,7 @@ class WattwaechterCoordinator(DataUpdateCoordinator[WattwaechterData]): ota = None if system is not None: - self._async_update_device_model(system) + self._async_update_device_info(system) return WattwaechterData(meter=data, system=system, ota=ota) @@ -137,17 +143,20 @@ class WattwaechterCoordinator(DataUpdateCoordinator[WattwaechterData]): ) return model, model_id - def _async_update_device_model(self, system: SystemInfo) -> None: - """Update the registered model after a medium change or late system info.""" + def _async_update_device_info(self, system: SystemInfo) -> None: + """Update the registered device after a firmware update, a medium change or late system info.""" + if fw_version := system.get_value("esp", "os_version"): + self.fw_version = fw_version model, model_id = self.device_model(system) device_registry = dr.async_get(self.hass) device = device_registry.async_get_device_by_identifier( (DOMAIN, self.device_id), self.config_entry.entry_id ) - if device is not None and (device.model, device.model_id) != ( - model, - model_id, - ): + if device is not None and ( + device.model, + device.model_id, + device.sw_version, + ) != (model, model_id, self.fw_version): device_registry.async_update_device( - device.id, model=model, model_id=model_id + device.id, model=model, model_id=model_id, sw_version=self.fw_version ) diff --git a/tests/components/wattwaechter/test_init.py b/tests/components/wattwaechter/test_init.py index d1e19a8b1120..d6b189bc49db 100644 --- a/tests/components/wattwaechter/test_init.py +++ b/tests/components/wattwaechter/test_init.py @@ -12,13 +12,22 @@ from aio_wattwaechter import ( WattwaechterError, WattwaechterNoDataError, ) +from aio_wattwaechter.models import SystemInfo, _parse_system_info from freezegun.api import FrozenDateTimeFactory import pytest -from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed +from pytest_homeassistant.common import ( + MockConfigEntry, + async_fire_time_changed, + load_json_object_fixture, +) from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.update import DOMAIN as UPDATE_DOMAIN -from homeassistant.components.wattwaechter.const import DEFAULT_SCAN_INTERVAL, DOMAIN +from homeassistant.components.wattwaechter.const import ( + CONF_FW_VERSION, + DEFAULT_SCAN_INTERVAL, + DOMAIN, +) from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState from homeassistant.const import CONF_DEVICE_ID, CONF_MODEL from homeassistant.core import HomeAssistant @@ -215,6 +224,55 @@ async def test_device_model_follows_system_info( assert device_registry.async_get(device.id).model == "WattWächter Gas" +def _system_info_with_version(version: str) -> SystemInfo: + """Return the Plus system info reporting the given firmware version.""" + data = load_json_object_fixture("system_info.json", DOMAIN) + data["esp"] = [ + {**item, "value": version} if item["name"] == "os_version" else item + for item in data["esp"] + ] + return _parse_system_info(data) + + +async def test_device_sw_version_follows_firmware( + hass: HomeAssistant, + mock_client: AsyncMock, + device_registry: dr.DeviceRegistry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test the firmware version is updated after a firmware update.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={**MOCK_CONFIG_DATA, CONF_FW_VERSION: "1.1.5"}, + unique_id=MOCK_DEVICE_ID, + minor_version=2, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + # The version stored at setup is replaced by the one the device reports + device = device_registry.async_get_device_by_identifier( + (DOMAIN, MOCK_DEVICE_ID), entry.entry_id + ) + assert device is not None + assert device.sw_version == "1.2.3" + + # The device installs a firmware update + mock_client.system_info.return_value = _system_info_with_version("1.3.0") + freezer.tick(timedelta(seconds=DEFAULT_SCAN_INTERVAL)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + assert device_registry.async_get(device.id).sw_version == "1.3.0" + + # The last known version is kept when the system info is unavailable on reload + mock_client.system_info.side_effect = WattwaechterConnectionError("offline") + await hass.config_entries.async_reload(entry.entry_id) + await hass.async_block_till_done() + assert entry.state is ConfigEntryState.LOADED + assert device_registry.async_get(device.id).sw_version == "1.3.0" + + async def test_migrate_gw_entry_to_bare_device_id( hass: HomeAssistant, mock_client: AsyncMock,