diff --git a/homeassistant/components/wled/sensor.py b/homeassistant/components/wled/sensor.py index 74aed519c705..d36a0989a12f 100644 --- a/homeassistant/components/wled/sensor.py +++ b/homeassistant/components/wled/sensor.py @@ -8,6 +8,7 @@ from typing import override from wled import Device as WLEDDevice from homeassistant.components.sensor import ( + DOMAIN as SENSOR_DOMAIN, SensorDeviceClass, SensorEntity, SensorEntityDescription, @@ -20,7 +21,8 @@ from homeassistant.const import ( UnitOfElectricCurrent, UnitOfInformation, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.typing import StateType from homeassistant.util.dt import utcnow @@ -135,11 +137,41 @@ async def async_setup_entry( ) -> None: """Set up WLED sensor based on a config entry.""" coordinator = entry.runtime_data - async_add_entities( - WLEDSensorEntity(coordinator, description) - for description in SENSORS - if description.exists_fn(coordinator.data) - ) + added: set[str] = set() + + # Sensors added before stay, even when the device doesn't report what they + # need right now, like the current while the light is off. + registered = { + registry_entry.unique_id + for registry_entry in er.async_entries_for_config_entry( + er.async_get(hass), entry.entry_id + ) + if registry_entry.domain == SENSOR_DOMAIN + } + mac_address = coordinator.data.info.mac_address + + @callback + def _async_add_sensors() -> None: + """Add the sensors the device reports, as soon as it does. + + Some only show up later: WLED reports no current while the light is + off, so a device that's off when set up gets those sensors once it's on. + """ + sensors = [ + WLEDSensorEntity(coordinator, description) + for description in SENSORS + if description.key not in added + and ( + description.exists_fn(coordinator.data) + or f"{mac_address}_{description.key}" in registered + ) + ] + added.update(sensor.entity_description.key for sensor in sensors) + if sensors: + async_add_entities(sensors) + + _async_add_sensors() + entry.async_on_unload(coordinator.async_add_listener(_async_add_sensors)) class WLEDSensorEntity(WLEDEntity, SensorEntity): diff --git a/tests/components/wled/test_sensor.py b/tests/components/wled/test_sensor.py index e7065d91c8dc..4b9f088bd7b5 100644 --- a/tests/components/wled/test_sensor.py +++ b/tests/components/wled/test_sensor.py @@ -106,6 +106,65 @@ async def test_no_current_measurement( assert hass.states.get("sensor.wled_rgb_light_estimated_current") is None +async def test_current_measurement_once_reported( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_wled: MagicMock, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the current sensors are added once the device reports a current.""" + # WLED reports no current while the light is off. + device = mock_wled.update.return_value + device.info.leds.max_power = 0 + device.info.leds.power = 0 + + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert hass.states.get("sensor.wled_rgb_light_max_current") is None + assert hass.states.get("sensor.wled_rgb_light_estimated_current") is None + + device.info.leds.max_power = 850 + device.info.leds.power = 470 + await mock_config_entry.runtime_data.async_refresh() + await hass.async_block_till_done() + + assert (state := hass.states.get("sensor.wled_rgb_light_max_current")) + assert state.state == "850" + assert (state := hass.states.get("sensor.wled_rgb_light_estimated_current")) + assert state.state == "470" + + # Later updates don't add them again. + await mock_config_entry.runtime_data.async_refresh() + await hass.async_block_till_done() + assert "does not generate unique IDs" not in caplog.text + + +async def test_current_measurement_kept_when_not_reported( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_wled: MagicMock, +) -> None: + """Test current sensors added before stay while the device reports none.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + assert hass.states.get("sensor.wled_rgb_light_estimated_current") + + # The light is off when Home Assistant starts again. + device = mock_wled.update.return_value + device.info.leds.max_power = 0 + device.info.leds.power = 0 + await hass.config_entries.async_reload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert (state := hass.states.get("sensor.wled_rgb_light_max_current")) + assert state.state == "0" + assert (state := hass.states.get("sensor.wled_rgb_light_estimated_current")) + assert state.state == "0" + + async def test_current_measurement_with_per_output_limiters( hass: HomeAssistant, mock_config_entry: MockConfigEntry,