Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for TPLink binary sensors."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Final, cast
7 
8 from kasa import Feature
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13  BinarySensorEntityDescription,
14 )
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import TPLinkConfigEntry
19 from .entity import CoordinatedTPLinkFeatureEntity, TPLinkFeatureEntityDescription
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  BinarySensorEntityDescription, TPLinkFeatureEntityDescription
25 ):
26  """Base class for a TPLink feature based sensor entity description."""
27 
28 
29 BINARY_SENSOR_DESCRIPTIONS: Final = (
31  key="overheated",
32  device_class=BinarySensorDeviceClass.PROBLEM,
33  ),
35  key="battery_low",
36  device_class=BinarySensorDeviceClass.BATTERY,
37  ),
39  key="cloud_connection",
40  device_class=BinarySensorDeviceClass.CONNECTIVITY,
41  ),
42  # To be replaced & disabled per default by the upcoming update platform.
44  key="update_available",
45  device_class=BinarySensorDeviceClass.UPDATE,
46  ),
48  key="temperature_warning",
49  ),
51  key="humidity_warning",
52  ),
54  key="is_open",
55  device_class=BinarySensorDeviceClass.DOOR,
56  ),
58  key="water_alert",
59  device_class=BinarySensorDeviceClass.MOISTURE,
60  ),
62  key="motion_detected",
63  device_class=BinarySensorDeviceClass.MOTION,
64  ),
65 )
66 
67 BINARYSENSOR_DESCRIPTIONS_MAP = {desc.key: desc for desc in BINARY_SENSOR_DESCRIPTIONS}
68 
69 
71  hass: HomeAssistant,
72  config_entry: TPLinkConfigEntry,
73  async_add_entities: AddEntitiesCallback,
74 ) -> None:
75  """Set up sensors."""
76  data = config_entry.runtime_data
77  parent_coordinator = data.parent_coordinator
78  children_coordinators = data.children_coordinators
79  device = parent_coordinator.device
80 
81  entities = CoordinatedTPLinkFeatureEntity.entities_for_device_and_its_children(
82  hass=hass,
83  device=device,
84  coordinator=parent_coordinator,
85  feature_type=Feature.Type.BinarySensor,
86  entity_class=TPLinkBinarySensorEntity,
87  descriptions=BINARYSENSOR_DESCRIPTIONS_MAP,
88  child_coordinators=children_coordinators,
89  )
90  async_add_entities(entities)
91 
92 
94  """Representation of a TPLink binary sensor."""
95 
96  entity_description: TPLinkBinarySensorEntityDescription
97 
98  @callback
99  def _async_update_attrs(self) -> None:
100  """Update the entity's attributes."""
101  self._attr_is_on_attr_is_on = cast(bool | None, self._feature_feature.value)