1 """SFR Box sensor platform."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from typing
import TYPE_CHECKING
9 from sfrbox_api.models
import DslInfo, FtthInfo, SystemInfo, WanInfo
12 BinarySensorDeviceClass,
14 BinarySensorEntityDescription,
23 from .const
import DOMAIN
24 from .coordinator
import SFRDataUpdateCoordinator
25 from .models
import DomainData
28 @dataclass(frozen=True, kw_only=True)
30 """Description for SFR Box binary sensors."""
32 value_fn: Callable[[_T], bool |
None]
35 DSL_SENSOR_TYPES: tuple[SFRBoxBinarySensorEntityDescription[DslInfo], ...] = (
36 SFRBoxBinarySensorEntityDescription[DslInfo](
38 device_class=BinarySensorDeviceClass.CONNECTIVITY,
39 entity_category=EntityCategory.DIAGNOSTIC,
40 value_fn=
lambda x: x.status ==
"up",
41 translation_key=
"dsl_status",
44 FTTH_SENSOR_TYPES: tuple[SFRBoxBinarySensorEntityDescription[FtthInfo], ...] = (
45 SFRBoxBinarySensorEntityDescription[FtthInfo](
47 device_class=BinarySensorDeviceClass.CONNECTIVITY,
48 entity_category=EntityCategory.DIAGNOSTIC,
49 value_fn=
lambda x: x.status ==
"up",
50 translation_key=
"ftth_status",
53 WAN_SENSOR_TYPES: tuple[SFRBoxBinarySensorEntityDescription[WanInfo], ...] = (
54 SFRBoxBinarySensorEntityDescription[WanInfo](
56 device_class=BinarySensorDeviceClass.CONNECTIVITY,
57 entity_category=EntityCategory.DIAGNOSTIC,
58 value_fn=
lambda x: x.status ==
"up",
59 translation_key=
"wan_status",
65 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
67 """Set up the sensors."""
68 data: DomainData = hass.data[DOMAIN][entry.entry_id]
69 system_info = data.system.data
71 assert system_info
is not None
73 entities: list[SFRBoxBinarySensor] = [
75 for description
in WAN_SENSOR_TYPES
77 if (net_infra := system_info.net_infra) ==
"adsl":
80 for description
in DSL_SENSOR_TYPES
82 elif net_infra ==
"ftth":
85 for description
in FTTH_SENSOR_TYPES
92 CoordinatorEntity[SFRDataUpdateCoordinator[_T]], BinarySensorEntity
96 entity_description: SFRBoxBinarySensorEntityDescription[_T]
97 _attr_has_entity_name =
True
101 coordinator: SFRDataUpdateCoordinator[_T],
102 description: SFRBoxBinarySensorEntityDescription,
103 system_info: SystemInfo,
105 """Initialize the sensor."""
109 f
"{system_info.mac_addr}_{coordinator.name}_{description.key}"
112 identifiers={(DOMAIN, system_info.mac_addr)},
117 """Return the native value of the device."""
118 if self.coordinator.data
is None:
None __init__(self, SFRDataUpdateCoordinator[_T] coordinator, SFRBoxBinarySensorEntityDescription description, SystemInfo system_info)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)