diff --git a/homeassistant/components/tradfri/entity.py b/homeassistant/components/tradfri/entity.py index 9792d77a514d..306e361743d0 100644 --- a/homeassistant/components/tradfri/entity.py +++ b/homeassistant/components/tradfri/entity.py @@ -6,6 +6,7 @@ from functools import wraps from typing import Any, cast, override from pytradfri.command import Command +from pytradfri.const import ATTR_DEVICE_FIRMWARE_VERSION from pytradfri.device import Device from pytradfri.error import RequestError @@ -61,7 +62,7 @@ class TradfriBaseEntity(CoordinatorEntity[TradfriDeviceDataUpdateCoordinator]): manufacturer=info.manufacturer, model=info.model_number, name=self._device.name, - sw_version=info.firmware_version, + sw_version=info.raw.get(ATTR_DEVICE_FIRMWARE_VERSION), via_device_id=dr.async_get_device_id_by_identifier( device_coordinator.hass, (DOMAIN, gateway_id), diff --git a/tests/components/tradfri/test_sensor.py b/tests/components/tradfri/test_sensor.py index eee1830f066c..401bf6f52f6e 100644 --- a/tests/components/tradfri/test_sensor.py +++ b/tests/components/tradfri/test_sensor.py @@ -1,9 +1,12 @@ """Tradfri sensor platform tests.""" +import json + import pytest from pytradfri.const import ( ATTR_AIR_PURIFIER_AIR_QUALITY, ATTR_DEVICE_BATTERY, + ATTR_DEVICE_FIRMWARE_VERSION, ATTR_DEVICE_INFO, ATTR_REACHABLE_STATE, ROOT_AIR_PURIFIER, @@ -27,7 +30,7 @@ from homeassistant.const import ( UnitOfTime, ) from homeassistant.core import HomeAssistant -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import device_registry as dr, entity_registry as er from . import GATEWAY_ID from .common import CommandStore, setup_integration @@ -41,6 +44,14 @@ def remote_control() -> str: return load_fixture("remote_control.json", DOMAIN) +@pytest.fixture(scope="module") +def remote_control_without_firmware(remote_control: str) -> str: + """Return a remote control response that reports no firmware version.""" + device = json.loads(remote_control) + del device[ATTR_DEVICE_INFO][ATTR_DEVICE_FIRMWARE_VERSION] + return json.dumps(device) + + @pytest.mark.parametrize("device", ["remote_control"], indirect=True) async def test_battery_sensor( hass: HomeAssistant, @@ -70,6 +81,26 @@ async def test_battery_sensor( assert state.attributes[ATTR_STATE_CLASS] == SensorStateClass.MEASUREMENT +@pytest.mark.parametrize("device", ["remote_control_without_firmware"], indirect=True) +@pytest.mark.usefixtures("device") +async def test_battery_sensor_without_firmware_version( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, +) -> None: + """Test that a device that reports no firmware version still gets its sensor.""" + entry = await setup_integration(hass) + + state = hass.states.get("sensor.test_battery") + assert state + assert state.state == "87" + + device_entry = device_registry.async_get_device_by_identifier( + (DOMAIN, f"{GATEWAY_ID}-65536"), entry.entry_id + ) + assert device_entry + assert device_entry.sw_version is None + + @pytest.mark.parametrize("device", ["blind"], indirect=True) async def test_cover_battery_sensor( hass: HomeAssistant,