1 """Support for WLED sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime
9 from wled
import Device
as WLEDDevice
14 SensorEntityDescription,
19 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
21 UnitOfElectricCurrent,
29 from .
import WLEDConfigEntry
30 from .coordinator
import WLEDDataUpdateCoordinator
31 from .entity
import WLEDEntity
34 @dataclass(frozen=True, kw_only=True)
36 """Describes WLED sensor entity."""
38 exists_fn: Callable[[WLEDDevice], bool] =
lambda _:
True
39 value_fn: Callable[[WLEDDevice], datetime | StateType]
42 SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
44 key=
"estimated_current",
45 translation_key=
"estimated_current",
46 native_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
47 device_class=SensorDeviceClass.CURRENT,
48 state_class=SensorStateClass.MEASUREMENT,
49 entity_category=EntityCategory.DIAGNOSTIC,
50 value_fn=
lambda device: device.info.leds.power,
51 exists_fn=
lambda device:
bool(device.info.leds.max_power),
54 key=
"info_leds_count",
55 translation_key=
"info_leds_count",
56 entity_category=EntityCategory.DIAGNOSTIC,
57 value_fn=
lambda device: device.info.leds.count,
60 key=
"info_leds_max_power",
61 translation_key=
"info_leds_max_power",
62 native_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
63 entity_category=EntityCategory.DIAGNOSTIC,
64 device_class=SensorDeviceClass.CURRENT,
65 value_fn=
lambda device: device.info.leds.max_power,
66 exists_fn=
lambda device:
bool(device.info.leds.max_power),
70 translation_key=
"uptime",
71 device_class=SensorDeviceClass.TIMESTAMP,
72 entity_category=EntityCategory.DIAGNOSTIC,
73 entity_registry_enabled_default=
False,
74 value_fn=
lambda device: (
utcnow() - device.info.uptime),
78 translation_key=
"free_heap",
79 native_unit_of_measurement=UnitOfInformation.BYTES,
80 state_class=SensorStateClass.MEASUREMENT,
81 device_class=SensorDeviceClass.DATA_SIZE,
82 entity_category=EntityCategory.DIAGNOSTIC,
83 entity_registry_enabled_default=
False,
84 value_fn=
lambda device: device.info.free_heap,
88 translation_key=
"wifi_signal",
89 native_unit_of_measurement=PERCENTAGE,
90 state_class=SensorStateClass.MEASUREMENT,
91 entity_category=EntityCategory.DIAGNOSTIC,
92 entity_registry_enabled_default=
False,
93 value_fn=
lambda device: device.info.wifi.signal
if device.info.wifi
else None,
97 translation_key=
"wifi_rssi",
98 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
99 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
100 state_class=SensorStateClass.MEASUREMENT,
101 entity_category=EntityCategory.DIAGNOSTIC,
102 entity_registry_enabled_default=
False,
103 value_fn=
lambda device: device.info.wifi.rssi
if device.info.wifi
else None,
107 translation_key=
"wifi_channel",
108 entity_category=EntityCategory.DIAGNOSTIC,
109 entity_registry_enabled_default=
False,
110 value_fn=
lambda device: device.info.wifi.channel
if device.info.wifi
else None,
114 translation_key=
"wifi_bssid",
115 entity_category=EntityCategory.DIAGNOSTIC,
116 entity_registry_enabled_default=
False,
117 value_fn=
lambda device: device.info.wifi.bssid
if device.info.wifi
else None,
121 translation_key=
"ip",
122 entity_category=EntityCategory.DIAGNOSTIC,
123 value_fn=
lambda device: device.info.ip,
130 entry: WLEDConfigEntry,
131 async_add_entities: AddEntitiesCallback,
133 """Set up WLED sensor based on a config entry."""
134 coordinator = entry.runtime_data
137 for description
in SENSORS
138 if description.exists_fn(coordinator.data)
143 """Defines a WLED sensor entity."""
145 entity_description: WLEDSensorEntityDescription
149 coordinator: WLEDDataUpdateCoordinator,
150 description: WLEDSensorEntityDescription,
152 """Initialize a WLED sensor entity."""
153 super().
__init__(coordinator=coordinator)
155 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.data.info.mac_address}_{description.key}"
159 """Return the state of the sensor."""
datetime|StateType native_value(self)
None __init__(self, WLEDDataUpdateCoordinator coordinator, WLEDSensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, WLEDConfigEntry entry, AddEntitiesCallback async_add_entities)