Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for switch entities."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 import logging
7 from typing import Any
8 
9 from thinqconnect import DeviceType
10 from thinqconnect.devices.const import Property as ThinQProperty
11 from thinqconnect.integration import ActiveMode
12 
14  SwitchDeviceClass,
15  SwitchEntity,
16  SwitchEntityDescription,
17 )
18 from homeassistant.const import EntityCategory
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from . import ThinqConfigEntry
23 from .entity import ThinQEntity
24 
25 
26 @dataclass(frozen=True, kw_only=True)
28  """Describes ThinQ switch entity."""
29 
30  on_key: str | None = None
31  off_key: str | None = None
32 
33 
34 DEVICE_TYPE_SWITCH_MAP: dict[DeviceType, tuple[ThinQSwitchEntityDescription, ...]] = {
35  DeviceType.AIR_CONDITIONER: (
37  key=ThinQProperty.POWER_SAVE_ENABLED,
38  translation_key=ThinQProperty.POWER_SAVE_ENABLED,
39  on_key="true",
40  off_key="false",
41  entity_category=EntityCategory.CONFIG,
42  ),
43  ),
44  DeviceType.AIR_PURIFIER_FAN: (
46  key=ThinQProperty.AIR_FAN_OPERATION_MODE, translation_key="operation_power"
47  ),
49  key=ThinQProperty.UV_NANO,
50  translation_key=ThinQProperty.UV_NANO,
51  on_key="on",
52  off_key="off",
53  entity_category=EntityCategory.CONFIG,
54  ),
56  key=ThinQProperty.WARM_MODE,
57  translation_key=ThinQProperty.WARM_MODE,
58  on_key="warm_on",
59  off_key="warm_off",
60  entity_category=EntityCategory.CONFIG,
61  ),
62  ),
63  DeviceType.AIR_PURIFIER: (
65  key=ThinQProperty.AIR_PURIFIER_OPERATION_MODE,
66  translation_key="operation_power",
67  ),
68  ),
69  DeviceType.DEHUMIDIFIER: (
71  key=ThinQProperty.DEHUMIDIFIER_OPERATION_MODE,
72  translation_key="operation_power",
73  ),
74  ),
75  DeviceType.HUMIDIFIER: (
77  key=ThinQProperty.HUMIDIFIER_OPERATION_MODE,
78  translation_key="operation_power",
79  ),
81  key=ThinQProperty.WARM_MODE,
82  translation_key="humidity_warm_mode",
83  on_key="warm_on",
84  off_key="warm_off",
85  entity_category=EntityCategory.CONFIG,
86  ),
88  key=ThinQProperty.MOOD_LAMP_STATE,
89  translation_key=ThinQProperty.MOOD_LAMP_STATE,
90  on_key="on",
91  off_key="off",
92  entity_category=EntityCategory.CONFIG,
93  ),
95  key=ThinQProperty.AUTO_MODE,
96  translation_key=ThinQProperty.AUTO_MODE,
97  on_key="auto_on",
98  off_key="auto_off",
99  entity_category=EntityCategory.CONFIG,
100  ),
102  key=ThinQProperty.SLEEP_MODE,
103  translation_key=ThinQProperty.SLEEP_MODE,
104  on_key="sleep_on",
105  off_key="sleep_off",
106  entity_category=EntityCategory.CONFIG,
107  ),
108  ),
109  DeviceType.REFRIGERATOR: (
111  key=ThinQProperty.EXPRESS_MODE,
112  translation_key=ThinQProperty.EXPRESS_MODE,
113  on_key="true",
114  off_key="false",
115  entity_category=EntityCategory.CONFIG,
116  ),
118  key=ThinQProperty.RAPID_FREEZE,
119  translation_key=ThinQProperty.RAPID_FREEZE,
120  on_key="true",
121  off_key="false",
122  entity_category=EntityCategory.CONFIG,
123  ),
124  ),
125  DeviceType.SYSTEM_BOILER: (
127  key=ThinQProperty.HOT_WATER_MODE,
128  translation_key=ThinQProperty.HOT_WATER_MODE,
129  on_key="on",
130  off_key="off",
131  entity_category=EntityCategory.CONFIG,
132  ),
133  ),
134  DeviceType.WINE_CELLAR: (
136  key=ThinQProperty.OPTIMAL_HUMIDITY,
137  translation_key=ThinQProperty.OPTIMAL_HUMIDITY,
138  on_key="on",
139  off_key="off",
140  entity_category=EntityCategory.CONFIG,
141  ),
142  ),
143 }
144 
145 _LOGGER = logging.getLogger(__name__)
146 
147 
149  hass: HomeAssistant,
150  entry: ThinqConfigEntry,
151  async_add_entities: AddEntitiesCallback,
152 ) -> None:
153  """Set up an entry for switch platform."""
154  entities: list[ThinQSwitchEntity] = []
155  for coordinator in entry.runtime_data.coordinators.values():
156  if (
157  descriptions := DEVICE_TYPE_SWITCH_MAP.get(
158  coordinator.api.device.device_type
159  )
160  ) is not None:
161  for description in descriptions:
162  entities.extend(
163  ThinQSwitchEntity(coordinator, description, property_id)
164  for property_id in coordinator.api.get_active_idx(
165  description.key, ActiveMode.READ_WRITE
166  )
167  )
168 
169  if entities:
170  async_add_entities(entities)
171 
172 
174  """Represent a thinq switch platform."""
175 
176  entity_description: ThinQSwitchEntityDescription
177  _attr_device_class = SwitchDeviceClass.SWITCH
178 
179  def _update_status(self) -> None:
180  """Update status itself."""
181  super()._update_status()
182 
183  if (key := self.entity_descriptionentity_description.on_key) is not None:
184  self._attr_is_on_attr_is_on = self.datadatadatadata.value == key
185  else:
186  self._attr_is_on_attr_is_on = self.datadatadatadata.is_on
187 
188  _LOGGER.debug(
189  "[%s:%s] update status: %s -> %s",
190  self.coordinator.device_name,
191  self.property_idproperty_id,
192  self.datadatadatadata.is_on,
193  self.is_onis_on,
194  )
195 
196  async def async_turn_on(self, **kwargs: Any) -> None:
197  """Turn on the switch."""
198  _LOGGER.debug(
199  "[%s:%s] async_turn_on id: %s",
200  self.coordinator.device_name,
201  self.namenamename,
202  self.property_idproperty_id,
203  )
204  if (on_command := self.entity_descriptionentity_description.on_key) is not None:
205  await self.async_call_apiasync_call_api(
206  self.coordinator.api.post(self.property_idproperty_id, on_command)
207  )
208  else:
209  await self.async_call_apiasync_call_api(
210  self.coordinator.api.async_turn_on(self.property_idproperty_id)
211  )
212 
213  async def async_turn_off(self, **kwargs: Any) -> None:
214  """Turn off the switch."""
215  _LOGGER.debug(
216  "[%s:%s] async_turn_off id: %s",
217  self.coordinator.device_name,
218  self.namenamename,
219  self.property_idproperty_id,
220  )
221  if (off_command := self.entity_descriptionentity_description.off_key) is not None:
222  await self.async_call_apiasync_call_api(
223  self.coordinator.api.post(self.property_idproperty_id, off_command)
224  )
225  else:
226  await self.async_call_apiasync_call_api(
227  self.coordinator.api.async_turn_off(self.property_idproperty_id)
228  )
None async_call_api(self, Coroutine[Any, Any, Any] target, Callable[[], None]|None on_fail_method=None)
Definition: entity.py:101
str|UndefinedType|None name(self)
Definition: entity.py:738
None async_setup_entry(HomeAssistant hass, ThinqConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:152