diff --git a/homeassistant/components/airthings_ble/coordinator.py b/homeassistant/components/airthings_ble/coordinator.py index 487fe635fa07..b1c071c89458 100644 --- a/homeassistant/components/airthings_ble/coordinator.py +++ b/homeassistant/components/airthings_ble/coordinator.py @@ -13,6 +13,7 @@ from homeassistant.components.bluetooth import BluetoothReachabilityIntent from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import ( @@ -112,4 +113,12 @@ class AirthingsBLEDataUpdateCoordinator(DataUpdateCoordinator[AirthingsDevice]): translation_key="incomplete_read", ) + device_registry = dr.async_get(self.hass) + if ( + device := device_registry.async_get_device_by_connection( + (dr.CONNECTION_BLUETOOTH, data.address), self.config_entry.entry_id + ) + ) and device.sw_version != data.sw_version: + device_registry.async_update_device(device.id, sw_version=data.sw_version) + return data diff --git a/tests/components/airthings_ble/test_sensor.py b/tests/components/airthings_ble/test_sensor.py index 53595d62ae87..1e72947d4f5d 100644 --- a/tests/components/airthings_ble/test_sensor.py +++ b/tests/components/airthings_ble/test_sensor.py @@ -470,3 +470,43 @@ async def test_default_scan_interval_migration( async_fire_time_changed(hass) await hass.async_block_till_done() assert mock_update.call_count == 3 + + +async def test_device_registry_sw_version_updates_on_refresh( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, +) -> None: + """Test the device firmware version follows a firmware upgrade.""" + first_device = deepcopy(WAVE_DEVICE_INFO) + second_device = deepcopy(WAVE_DEVICE_INFO) + first_device.sw_version = "G-BLE-1.5.3-master+0" + second_device.sw_version = "G-BLE-2.2.3-master+0" + + entry = MockConfigEntry( + domain=DOMAIN, + unique_id=WAVE_SERVICE_INFO.address, + data={DEVICE_MODEL: first_device.model.value}, + ) + entry.add_to_hass(hass) + + inject_bluetooth_service_info(hass, WAVE_SERVICE_INFO) + + with ( + patch_async_ble_device_from_address(WAVE_SERVICE_INFO.device), + patch_airthings_ble(side_effect=[first_device, second_device]), + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + device = device_registry.async_get_device_by_connection( + (dr.CONNECTION_BLUETOOTH, WAVE_DEVICE_INFO.address), entry.entry_id + ) + assert device is not None + assert device.sw_version == "G-BLE-1.5.3-master+0" + + await entry.runtime_data.async_refresh() + await hass.async_block_till_done() + + device = device_registry.async_get(device.id) + assert device is not None + assert device.sw_version == "G-BLE-2.2.3-master+0"