1 """Support for TPLink Omada binary sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
8 from tplink_omada_client.definitions
import GatewayPortMode, LinkStatus, PoEMode
9 from tplink_omada_client.devices
import (
11 OmadaGatewayPortConfig,
12 OmadaGatewayPortStatus,
16 BinarySensorDeviceClass,
18 BinarySensorEntityDescription,
23 from .
import OmadaConfigEntry
24 from .controller
import OmadaGatewayCoordinator
25 from .entity
import OmadaDeviceEntity
30 config_entry: OmadaConfigEntry,
31 async_add_entities: AddEntitiesCallback,
33 """Set up binary sensors."""
34 controller = config_entry.runtime_data
36 gateway_coordinator = controller.gateway_coordinator
37 if not gateway_coordinator:
40 entities: list[OmadaDeviceEntity] = []
41 for gateway
in gateway_coordinator.data.values():
44 gateway_coordinator, gateway, p.port_number, desc
46 for p
in gateway.port_configs
47 for desc
in GATEWAY_PORT_SENSORS
48 if desc.exists_func(p)
54 @dataclass(frozen=True, kw_only=True)
56 """Entity description for a binary status derived from a gateway port."""
58 exists_func: Callable[[OmadaGatewayPortConfig], bool] =
lambda _:
True
59 update_func: Callable[[OmadaGatewayPortStatus], bool]
62 GATEWAY_PORT_SENSORS: list[GatewayPortBinarySensorEntityDescription] = [
65 translation_key=
"wan_link",
66 device_class=BinarySensorDeviceClass.CONNECTIVITY,
67 exists_func=
lambda p: p.port_status.mode == GatewayPortMode.WAN,
68 update_func=
lambda p: p.wan_connected,
71 key=
"online_detection",
72 translation_key=
"online_detection",
73 device_class=BinarySensorDeviceClass.CONNECTIVITY,
74 exists_func=
lambda p: p.port_status.mode == GatewayPortMode.WAN,
75 update_func=
lambda p: p.online_detection,
79 translation_key=
"lan_status",
80 device_class=BinarySensorDeviceClass.CONNECTIVITY,
81 exists_func=
lambda p: p.port_status.mode == GatewayPortMode.LAN,
82 update_func=
lambda p: p.link_status == LinkStatus.LINK_UP,
86 translation_key=
"poe_delivery",
87 device_class=BinarySensorDeviceClass.POWER,
88 exists_func=
lambda p: (
89 p.port_status.mode == GatewayPortMode.LAN
and p.poe_mode == PoEMode.ENABLED
91 update_func=
lambda p: p.poe_active,
97 OmadaDeviceEntity[OmadaGatewayCoordinator], BinarySensorEntity
99 """Binary status of a property on an internet gateway."""
101 entity_description: GatewayPortBinarySensorEntityDescription
105 coordinator: OmadaGatewayCoordinator,
108 entity_description: GatewayPortBinarySensorEntityDescription,
110 """Initialize the gateway port binary sensor."""
111 super().
__init__(coordinator, device)
114 self.
_attr_unique_id_attr_unique_id = f
"{device.mac}_{port_number}_{entity_description.key}"
118 """When entity is added to hass."""
123 gateway = self.coordinator.data[self.device.mac]
126 p
for p
in gateway.port_status
if p.port_number == self.
_port_number_port_number
133 """Handle updated data from the coordinator."""
_attr_translation_placeholders
None __init__(self, OmadaGatewayCoordinator coordinator, OmadaDevice device, int port_number, GatewayPortBinarySensorEntityDescription entity_description)
None _handle_coordinator_update(self)
None async_added_to_hass(self)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, OmadaConfigEntry config_entry, AddEntitiesCallback async_add_entities)