1 """SFR Box sensor platform."""
3 from collections.abc
import Callable
4 from dataclasses
import dataclass
5 from typing
import TYPE_CHECKING
7 from sfrbox_api.models
import DslInfo, SystemInfo, WanInfo
12 SensorEntityDescription,
17 SIGNAL_STRENGTH_DECIBELS,
20 UnitOfElectricPotential,
29 from .const
import DOMAIN
30 from .coordinator
import SFRDataUpdateCoordinator
31 from .models
import DomainData
34 @dataclass(frozen=True, kw_only=True)
36 """Description for SFR Box sensors."""
38 value_fn: Callable[[_T], StateType]
41 DSL_SENSOR_TYPES: tuple[SFRBoxSensorEntityDescription[DslInfo], ...] = (
42 SFRBoxSensorEntityDescription[DslInfo](
44 entity_category=EntityCategory.DIAGNOSTIC,
45 entity_registry_enabled_default=
False,
46 translation_key=
"dsl_linemode",
47 value_fn=
lambda x: x.linemode,
49 SFRBoxSensorEntityDescription[DslInfo](
51 entity_category=EntityCategory.DIAGNOSTIC,
52 entity_registry_enabled_default=
False,
53 translation_key=
"dsl_counter",
54 value_fn=
lambda x: x.counter,
56 SFRBoxSensorEntityDescription[DslInfo](
58 entity_category=EntityCategory.DIAGNOSTIC,
59 entity_registry_enabled_default=
False,
60 translation_key=
"dsl_crc",
61 value_fn=
lambda x: x.crc,
63 SFRBoxSensorEntityDescription[DslInfo](
65 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
66 entity_category=EntityCategory.DIAGNOSTIC,
67 entity_registry_enabled_default=
False,
68 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
69 state_class=SensorStateClass.MEASUREMENT,
70 translation_key=
"dsl_noise_down",
71 value_fn=
lambda x: x.noise_down,
73 SFRBoxSensorEntityDescription[DslInfo](
75 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
76 entity_category=EntityCategory.DIAGNOSTIC,
77 entity_registry_enabled_default=
False,
78 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
79 state_class=SensorStateClass.MEASUREMENT,
80 translation_key=
"dsl_noise_up",
81 value_fn=
lambda x: x.noise_up,
83 SFRBoxSensorEntityDescription[DslInfo](
84 key=
"attenuation_down",
85 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
86 entity_category=EntityCategory.DIAGNOSTIC,
87 entity_registry_enabled_default=
False,
88 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
89 state_class=SensorStateClass.MEASUREMENT,
90 translation_key=
"dsl_attenuation_down",
91 value_fn=
lambda x: x.attenuation_down,
93 SFRBoxSensorEntityDescription[DslInfo](
95 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
96 entity_category=EntityCategory.DIAGNOSTIC,
97 entity_registry_enabled_default=
False,
98 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
99 state_class=SensorStateClass.MEASUREMENT,
100 translation_key=
"dsl_attenuation_up",
101 value_fn=
lambda x: x.attenuation_up,
103 SFRBoxSensorEntityDescription[DslInfo](
105 device_class=SensorDeviceClass.DATA_RATE,
106 native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND,
107 state_class=SensorStateClass.MEASUREMENT,
108 translation_key=
"dsl_rate_down",
109 value_fn=
lambda x: x.rate_down,
111 SFRBoxSensorEntityDescription[DslInfo](
113 device_class=SensorDeviceClass.DATA_RATE,
114 native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND,
115 state_class=SensorStateClass.MEASUREMENT,
116 translation_key=
"dsl_rate_up",
117 value_fn=
lambda x: x.rate_up,
119 SFRBoxSensorEntityDescription[DslInfo](
121 device_class=SensorDeviceClass.ENUM,
122 entity_category=EntityCategory.DIAGNOSTIC,
123 entity_registry_enabled_default=
False,
129 "loss_of_signal_quality",
132 translation_key=
"dsl_line_status",
135 SFRBoxSensorEntityDescription[DslInfo](
137 device_class=SensorDeviceClass.ENUM,
138 entity_category=EntityCategory.DIAGNOSTIC,
139 entity_registry_enabled_default=
False,
144 "g_922_channel_analysis",
145 "g_992_message_exchange",
147 "g_993_channel_analysis",
148 "g_993_message_exchange",
152 translation_key=
"dsl_training",
156 SYSTEM_SENSOR_TYPES: tuple[SFRBoxSensorEntityDescription[SystemInfo], ...] = (
157 SFRBoxSensorEntityDescription[SystemInfo](
159 device_class=SensorDeviceClass.ENUM,
160 entity_category=EntityCategory.DIAGNOSTIC,
161 entity_registry_enabled_default=
False,
168 translation_key=
"net_infra",
169 value_fn=
lambda x: x.net_infra,
171 SFRBoxSensorEntityDescription[SystemInfo](
173 device_class=SensorDeviceClass.VOLTAGE,
174 entity_category=EntityCategory.DIAGNOSTIC,
175 entity_registry_enabled_default=
False,
176 native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT,
177 value_fn=
lambda x: x.alimvoltage,
179 SFRBoxSensorEntityDescription[SystemInfo](
181 device_class=SensorDeviceClass.TEMPERATURE,
182 entity_category=EntityCategory.DIAGNOSTIC,
183 entity_registry_enabled_default=
False,
184 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
188 WAN_SENSOR_TYPES: tuple[SFRBoxSensorEntityDescription[WanInfo], ...] = (
189 SFRBoxSensorEntityDescription[WanInfo](
191 device_class=SensorDeviceClass.ENUM,
192 entity_category=EntityCategory.DIAGNOSTIC,
193 entity_registry_enabled_default=
False,
201 translation_key=
"wan_mode",
202 value_fn=
lambda x: x.mode.replace(
"/",
"_"),
210 return value.lower().replace(
" ",
"_").replace(
".",
"_")
214 if value
is None or value < 1000:
220 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
222 """Set up the sensors."""
223 data: DomainData = hass.data[DOMAIN][entry.entry_id]
224 system_info = data.system.data
226 assert system_info
is not None
228 entities: list[SFRBoxSensor] = [
230 for description
in SYSTEM_SENSOR_TYPES
234 for description
in WAN_SENSOR_TYPES
236 if system_info.net_infra ==
"adsl":
239 for description
in DSL_SENSOR_TYPES
245 class SFRBoxSensor[_T](CoordinatorEntity[SFRDataUpdateCoordinator[_T]], SensorEntity):
246 """SFR Box sensor."""
248 entity_description: SFRBoxSensorEntityDescription[_T]
249 _attr_has_entity_name =
True
253 coordinator: SFRDataUpdateCoordinator[_T],
254 description: SFRBoxSensorEntityDescription,
255 system_info: SystemInfo,
257 """Initialize the sensor."""
261 f
"{system_info.mac_addr}_{coordinator.name}_{description.key}"
264 identifiers={(DOMAIN, system_info.mac_addr)},
269 """Return the native value of the device."""
270 if self.coordinator.data
is None:
None __init__(self, SFRDataUpdateCoordinator[_T] coordinator, SFRBoxSensorEntityDescription description, SystemInfo system_info)
StateType native_value(self)
str|None _value_to_option(str|None value)
float|None _get_temperature(float|None value)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)