mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Only create WLED current sensors when available (#68116)
This commit is contained in:
parent
a29afc3731
commit
6aacaa744e
@ -44,6 +44,8 @@ class WLEDSensorEntityDescription(
|
|||||||
):
|
):
|
||||||
"""Describes WLED sensor entity."""
|
"""Describes WLED sensor entity."""
|
||||||
|
|
||||||
|
exists_fn: Callable[[WLEDDevice], bool] = lambda _: True
|
||||||
|
|
||||||
|
|
||||||
SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
|
SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
|
||||||
WLEDSensorEntityDescription(
|
WLEDSensorEntityDescription(
|
||||||
@ -54,6 +56,7 @@ SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
|
|||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
value_fn=lambda device: device.info.leds.power,
|
value_fn=lambda device: device.info.leds.power,
|
||||||
|
exists_fn=lambda device: bool(device.info.leds.max_power),
|
||||||
),
|
),
|
||||||
WLEDSensorEntityDescription(
|
WLEDSensorEntityDescription(
|
||||||
key="info_leds_count",
|
key="info_leds_count",
|
||||||
@ -68,6 +71,7 @@ SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
|
|||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
device_class=SensorDeviceClass.CURRENT,
|
device_class=SensorDeviceClass.CURRENT,
|
||||||
value_fn=lambda device: device.info.leds.max_power,
|
value_fn=lambda device: device.info.leds.max_power,
|
||||||
|
exists_fn=lambda device: bool(device.info.leds.max_power),
|
||||||
),
|
),
|
||||||
WLEDSensorEntityDescription(
|
WLEDSensorEntityDescription(
|
||||||
key="uptime",
|
key="uptime",
|
||||||
@ -132,7 +136,9 @@ async def async_setup_entry(
|
|||||||
"""Set up WLED sensor based on a config entry."""
|
"""Set up WLED sensor based on a config entry."""
|
||||||
coordinator: WLEDDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: WLEDDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
WLEDSensorEntity(coordinator, description) for description in SENSORS
|
WLEDSensorEntity(coordinator, description)
|
||||||
|
for description in SENSORS
|
||||||
|
if description.exists_fn(coordinator.data)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -189,3 +189,20 @@ async def test_no_wifi_support(
|
|||||||
state = hass.states.get(f"sensor.wled_rgb_light_wifi_{key}")
|
state = hass.states.get(f"sensor.wled_rgb_light_wifi_{key}")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == STATE_UNKNOWN
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user