Only create WLED current sensors when available ()

pull/68139/head
Franck Nijhof 2022-03-14 11:49:16 +01:00 committed by GitHub
parent a29afc3731
commit 6aacaa744e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions
homeassistant/components/wled
tests/components/wled

View File

@ -44,6 +44,8 @@ class WLEDSensorEntityDescription(
):
"""Describes WLED sensor entity."""
exists_fn: Callable[[WLEDDevice], bool] = lambda _: True
SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
WLEDSensorEntityDescription(
@ -54,6 +56,7 @@ SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda device: device.info.leds.power,
exists_fn=lambda device: bool(device.info.leds.max_power),
),
WLEDSensorEntityDescription(
key="info_leds_count",
@ -68,6 +71,7 @@ SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
entity_category=EntityCategory.DIAGNOSTIC,
device_class=SensorDeviceClass.CURRENT,
value_fn=lambda device: device.info.leds.max_power,
exists_fn=lambda device: bool(device.info.leds.max_power),
),
WLEDSensorEntityDescription(
key="uptime",
@ -132,7 +136,9 @@ async def async_setup_entry(
"""Set up WLED sensor based on a config entry."""
coordinator: WLEDDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
WLEDSensorEntity(coordinator, description) for description in SENSORS
WLEDSensorEntity(coordinator, description)
for description in SENSORS
if description.exists_fn(coordinator.data)
)

View File

@ -189,3 +189,20 @@ async def test_no_wifi_support(
state = hass.states.get(f"sensor.wled_rgb_light_wifi_{key}")
assert state
assert state.state == STATE_UNKNOWN
async def test_no_current_measurement(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_wled: MagicMock,
) -> None:
"""Test missing current information when no max power is defined."""
device = mock_wled.update.return_value
device.info.leds.max_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