1 """Support for SLZB-06 sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime, timedelta
8 from itertools
import chain
10 from pysmlight
import Info, Sensors
15 SensorEntityDescription,
24 from .
import SmConfigEntry
25 from .const
import UPTIME_DEVIATION
26 from .coordinator
import SmDataUpdateCoordinator
27 from .entity
import SmEntity
30 @dataclass(frozen=True, kw_only=True)
32 """Class describing SMLIGHT sensor entities."""
34 value_fn: Callable[[Sensors], float |
None]
37 @dataclass(frozen=True, kw_only=True)
39 """Class describing SMLIGHT information entities."""
41 value_fn: Callable[[Info], StateType]
44 INFO: list[SmInfoEntityDescription] = [
47 translation_key=
"device_mode",
48 device_class=SensorDeviceClass.ENUM,
49 options=[
"eth",
"wifi",
"usb"],
50 value_fn=
lambda x: x.coord_mode,
53 key=
"firmware_channel",
54 translation_key=
"firmware_channel",
55 device_class=SensorDeviceClass.ENUM,
56 options=[
"dev",
"release"],
57 value_fn=
lambda x: x.fw_channel,
61 translation_key=
"zigbee_type",
62 device_class=SensorDeviceClass.ENUM,
63 options=[
"coordinator",
"router",
"thread"],
64 value_fn=
lambda x: x.zb_type,
69 SENSORS: list[SmSensorEntityDescription] = [
71 key=
"core_temperature",
72 translation_key=
"core_temperature",
73 device_class=SensorDeviceClass.TEMPERATURE,
74 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
75 state_class=SensorStateClass.MEASUREMENT,
76 suggested_display_precision=1,
77 value_fn=
lambda x: x.esp32_temp,
80 key=
"zigbee_temperature",
81 translation_key=
"zigbee_temperature",
82 device_class=SensorDeviceClass.TEMPERATURE,
83 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
84 state_class=SensorStateClass.MEASUREMENT,
85 suggested_display_precision=1,
86 value_fn=
lambda x: x.zb_temp,
90 translation_key=
"ram_usage",
91 device_class=SensorDeviceClass.DATA_SIZE,
92 native_unit_of_measurement=UnitOfInformation.KILOBYTES,
93 entity_registry_enabled_default=
False,
94 value_fn=
lambda x: x.ram_usage,
98 translation_key=
"fs_usage",
99 device_class=SensorDeviceClass.DATA_SIZE,
100 native_unit_of_measurement=UnitOfInformation.KILOBYTES,
101 entity_registry_enabled_default=
False,
102 value_fn=
lambda x: x.fs_used,
106 UPTIME: list[SmSensorEntityDescription] = [
109 translation_key=
"core_uptime",
110 device_class=SensorDeviceClass.TIMESTAMP,
111 entity_registry_enabled_default=
False,
112 value_fn=
lambda x: x.uptime,
116 translation_key=
"socket_uptime",
117 device_class=SensorDeviceClass.TIMESTAMP,
118 entity_registry_enabled_default=
False,
119 value_fn=
lambda x: x.socket_uptime,
126 entry: SmConfigEntry,
127 async_add_entities: AddEntitiesCallback,
129 """Set up SMLIGHT sensor based on a config entry."""
130 coordinator = entry.runtime_data.data
135 (
SmSensorEntity(coordinator, description)
for description
in SENSORS),
142 """Representation of a slzb sensor."""
144 coordinator: SmDataUpdateCoordinator
145 entity_description: SmSensorEntityDescription
146 _attr_entity_category = EntityCategory.DIAGNOSTIC
150 coordinator: SmDataUpdateCoordinator,
151 description: SmSensorEntityDescription,
153 """Initiate slzb sensor."""
161 """Return the sensor value."""
166 """Representation of a slzb info sensor."""
168 coordinator: SmDataUpdateCoordinator
169 entity_description: SmInfoEntityDescription
170 _attr_entity_category = EntityCategory.DIAGNOSTIC
174 coordinator: SmDataUpdateCoordinator,
175 description: SmInfoEntityDescription,
177 """Initiate slzb sensor."""
185 """Return the sensor value."""
189 if isinstance(value, int)
and options
is not None:
190 value = options[value]
if 0 <= value < len(options)
else None
196 """Representation of a slzb uptime sensor."""
200 coordinator: SmDataUpdateCoordinator,
201 description: SmSensorEntityDescription,
203 "Initialize uptime sensor instance."
204 super().
__init__(coordinator, description)
207 def get_uptime(self, uptime: float |
None) -> datetime |
None:
208 """Return device uptime or zigbee socket uptime.
210 Converts uptime from seconds to a datetime value, allow up to 5
211 seconds deviation. This avoids unnecessary updates to sensor state,
212 that may be caused by clock jitter.
223 or abs(new_uptime - self.
_last_uptime_last_uptime) > UPTIME_DEVIATION
231 """Return the sensor value."""
StateType native_value(self)
None __init__(self, SmDataUpdateCoordinator coordinator, SmInfoEntityDescription description)
None __init__(self, SmDataUpdateCoordinator coordinator, SmSensorEntityDescription description)
datetime|str|float|None native_value(self)
datetime|None get_uptime(self, float|None uptime)
None __init__(self, SmDataUpdateCoordinator coordinator, SmSensorEntityDescription description)
datetime|None native_value(self)
None async_setup_entry(HomeAssistant hass, SmConfigEntry entry, AddEntitiesCallback async_add_entities)