Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for binary sensor entities."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 import logging
7 
8 from thinqconnect import DeviceType
9 from thinqconnect.devices.const import Property as ThinQProperty
10 from thinqconnect.integration import ActiveMode
11 
13  BinarySensorDeviceClass,
14  BinarySensorEntity,
15  BinarySensorEntityDescription,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import ThinqConfigEntry
21 from .entity import ThinQEntity
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """Describes ThinQ sensor entity."""
27 
28  on_key: str | None = None
29 
30 
31 BINARY_SENSOR_DESC: dict[ThinQProperty, ThinQBinarySensorEntityDescription] = {
32  ThinQProperty.RINSE_REFILL: ThinQBinarySensorEntityDescription(
33  key=ThinQProperty.RINSE_REFILL,
34  translation_key=ThinQProperty.RINSE_REFILL,
35  ),
36  ThinQProperty.ECO_FRIENDLY_MODE: ThinQBinarySensorEntityDescription(
37  key=ThinQProperty.ECO_FRIENDLY_MODE,
38  translation_key=ThinQProperty.ECO_FRIENDLY_MODE,
39  ),
40  ThinQProperty.POWER_SAVE_ENABLED: ThinQBinarySensorEntityDescription(
41  key=ThinQProperty.POWER_SAVE_ENABLED,
42  translation_key=ThinQProperty.POWER_SAVE_ENABLED,
43  ),
44  ThinQProperty.REMOTE_CONTROL_ENABLED: ThinQBinarySensorEntityDescription(
45  key=ThinQProperty.REMOTE_CONTROL_ENABLED,
46  translation_key=ThinQProperty.REMOTE_CONTROL_ENABLED,
47  ),
48  ThinQProperty.SABBATH_MODE: ThinQBinarySensorEntityDescription(
49  key=ThinQProperty.SABBATH_MODE,
50  translation_key=ThinQProperty.SABBATH_MODE,
51  ),
52  ThinQProperty.DOOR_STATE: ThinQBinarySensorEntityDescription(
53  key=ThinQProperty.DOOR_STATE,
54  device_class=BinarySensorDeviceClass.DOOR,
55  on_key="open",
56  ),
57  ThinQProperty.MACHINE_CLEAN_REMINDER: ThinQBinarySensorEntityDescription(
58  key=ThinQProperty.MACHINE_CLEAN_REMINDER,
59  translation_key=ThinQProperty.MACHINE_CLEAN_REMINDER,
60  on_key="mcreminder_on",
61  ),
62  ThinQProperty.SIGNAL_LEVEL: ThinQBinarySensorEntityDescription(
63  key=ThinQProperty.SIGNAL_LEVEL,
64  translation_key=ThinQProperty.SIGNAL_LEVEL,
65  on_key="signallevel_on",
66  ),
67  ThinQProperty.CLEAN_LIGHT_REMINDER: ThinQBinarySensorEntityDescription(
68  key=ThinQProperty.CLEAN_LIGHT_REMINDER,
69  translation_key=ThinQProperty.CLEAN_LIGHT_REMINDER,
70  on_key="cleanlreminder_on",
71  ),
72  ThinQProperty.HOOD_OPERATION_MODE: ThinQBinarySensorEntityDescription(
73  key=ThinQProperty.HOOD_OPERATION_MODE,
74  translation_key="operation_mode",
75  on_key="power_on",
76  ),
77  ThinQProperty.WATER_HEATER_OPERATION_MODE: ThinQBinarySensorEntityDescription(
78  key=ThinQProperty.WATER_HEATER_OPERATION_MODE,
79  translation_key="operation_mode",
80  on_key="power_on",
81  ),
82  ThinQProperty.ONE_TOUCH_FILTER: ThinQBinarySensorEntityDescription(
83  key=ThinQProperty.ONE_TOUCH_FILTER,
84  translation_key=ThinQProperty.ONE_TOUCH_FILTER,
85  on_key="on",
86  ),
87 }
88 
89 DEVICE_TYPE_BINARY_SENSOR_MAP: dict[
90  DeviceType, tuple[ThinQBinarySensorEntityDescription, ...]
91 ] = {
92  DeviceType.COOKTOP: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],),
93  DeviceType.DISH_WASHER: (
94  BINARY_SENSOR_DESC[ThinQProperty.DOOR_STATE],
95  BINARY_SENSOR_DESC[ThinQProperty.RINSE_REFILL],
96  BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],
97  BINARY_SENSOR_DESC[ThinQProperty.MACHINE_CLEAN_REMINDER],
98  BINARY_SENSOR_DESC[ThinQProperty.SIGNAL_LEVEL],
99  BINARY_SENSOR_DESC[ThinQProperty.CLEAN_LIGHT_REMINDER],
100  ),
101  DeviceType.DRYER: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],),
102  DeviceType.HOOD: (BINARY_SENSOR_DESC[ThinQProperty.HOOD_OPERATION_MODE],),
103  DeviceType.OVEN: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],),
104  DeviceType.REFRIGERATOR: (
105  BINARY_SENSOR_DESC[ThinQProperty.DOOR_STATE],
106  BINARY_SENSOR_DESC[ThinQProperty.ECO_FRIENDLY_MODE],
107  BINARY_SENSOR_DESC[ThinQProperty.POWER_SAVE_ENABLED],
108  BINARY_SENSOR_DESC[ThinQProperty.SABBATH_MODE],
109  ),
110  DeviceType.KIMCHI_REFRIGERATOR: (
111  BINARY_SENSOR_DESC[ThinQProperty.ONE_TOUCH_FILTER],
112  ),
113  DeviceType.STYLER: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],),
114  DeviceType.WASHCOMBO_MAIN: (
115  BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],
116  ),
117  DeviceType.WASHCOMBO_MINI: (
118  BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],
119  ),
120  DeviceType.WASHER: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],),
121  DeviceType.WASHTOWER_DRYER: (
122  BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],
123  ),
124  DeviceType.WASHTOWER: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],),
125  DeviceType.WASHTOWER_WASHER: (
126  BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],
127  ),
128  DeviceType.WATER_HEATER: (
129  BINARY_SENSOR_DESC[ThinQProperty.WATER_HEATER_OPERATION_MODE],
130  ),
131  DeviceType.WINE_CELLAR: (BINARY_SENSOR_DESC[ThinQProperty.SABBATH_MODE],),
132 }
133 _LOGGER = logging.getLogger(__name__)
134 
135 
137  hass: HomeAssistant,
138  entry: ThinqConfigEntry,
139  async_add_entities: AddEntitiesCallback,
140 ) -> None:
141  """Set up an entry for binary sensor platform."""
142  entities: list[ThinQBinarySensorEntity] = []
143  for coordinator in entry.runtime_data.coordinators.values():
144  if (
145  descriptions := DEVICE_TYPE_BINARY_SENSOR_MAP.get(
146  coordinator.api.device.device_type
147  )
148  ) is not None:
149  for description in descriptions:
150  entities.extend(
151  ThinQBinarySensorEntity(coordinator, description, property_id)
152  for property_id in coordinator.api.get_active_idx(
153  description.key, ActiveMode.READ_ONLY
154  )
155  )
156 
157  if entities:
158  async_add_entities(entities)
159 
160 
162  """Represent a thinq binary sensor platform."""
163 
164  entity_description: ThinQBinarySensorEntityDescription
165 
166  def _update_status(self) -> None:
167  """Update status itself."""
168  super()._update_status()
169 
170  if (key := self.entity_descriptionentity_description.on_key) is not None:
171  self._attr_is_on_attr_is_on = self.datadatadatadata.value == key
172  else:
173  self._attr_is_on_attr_is_on = self.datadatadatadata.is_on
174 
175  _LOGGER.debug(
176  "[%s:%s] update status: %s -> %s",
177  self.coordinator.device_name,
178  self.property_idproperty_id,
179  self.datadatadatadata.value,
180  self.is_on,
181  )
None async_setup_entry(HomeAssistant hass, ThinqConfigEntry entry, AddEntitiesCallback async_add_entities)