Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switch platform for Sensibo integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Mapping
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from pysensibo.model import SensiboDevice
10 
12  SwitchDeviceClass,
13  SwitchEntity,
14  SwitchEntityDescription,
15 )
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import HomeAssistantError
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import SensiboConfigEntry
21 from .const import DOMAIN
22 from .coordinator import SensiboDataUpdateCoordinator
23 from .entity import SensiboDeviceBaseEntity, async_handle_api_call
24 
25 PARALLEL_UPDATES = 0
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """Describes Sensibo Switch entity."""
31 
32  value_fn: Callable[[SensiboDevice], bool | None]
33  extra_fn: Callable[[SensiboDevice], dict[str, str | bool | None]] | None
34  command_on: str
35  command_off: str
36  data_key: str
37 
38 
39 DEVICE_SWITCH_TYPES: tuple[SensiboDeviceSwitchEntityDescription, ...] = (
41  key="timer_on_switch",
42  translation_key="timer_on_switch",
43  device_class=SwitchDeviceClass.SWITCH,
44  value_fn=lambda data: data.timer_on,
45  extra_fn=lambda data: {"id": data.timer_id, "turn_on": data.timer_state_on},
46  command_on="async_turn_on_timer",
47  command_off="async_turn_off_timer",
48  data_key="timer_on",
49  ),
51  key="climate_react_switch",
52  translation_key="climate_react_switch",
53  device_class=SwitchDeviceClass.SWITCH,
54  value_fn=lambda data: data.smart_on,
55  extra_fn=lambda data: {"type": data.smart_type},
56  command_on="async_turn_on_off_smart",
57  command_off="async_turn_on_off_smart",
58  data_key="smart_on",
59  ),
60 )
61 
62 PURE_SWITCH_TYPES: tuple[SensiboDeviceSwitchEntityDescription, ...] = (
64  key="pure_boost_switch",
65  translation_key="pure_boost_switch",
66  device_class=SwitchDeviceClass.SWITCH,
67  value_fn=lambda data: data.pure_boost_enabled,
68  extra_fn=None,
69  command_on="async_turn_on_off_pure_boost",
70  command_off="async_turn_on_off_pure_boost",
71  data_key="pure_boost_enabled",
72  ),
73 )
74 
75 DESCRIPTION_BY_MODELS = {"pure": PURE_SWITCH_TYPES}
76 
77 
79  hass: HomeAssistant,
80  entry: SensiboConfigEntry,
81  async_add_entities: AddEntitiesCallback,
82 ) -> None:
83  """Set up Sensibo Switch platform."""
84 
85  coordinator = entry.runtime_data
86 
88  SensiboDeviceSwitch(coordinator, device_id, description)
89  for device_id, device_data in coordinator.data.parsed.items()
90  for description in DESCRIPTION_BY_MODELS.get(
91  device_data.model, DEVICE_SWITCH_TYPES
92  )
93  )
94 
95 
97  """Representation of a Sensibo Device Switch."""
98 
99  entity_description: SensiboDeviceSwitchEntityDescription
100 
101  def __init__(
102  self,
103  coordinator: SensiboDataUpdateCoordinator,
104  device_id: str,
105  entity_description: SensiboDeviceSwitchEntityDescription,
106  ) -> None:
107  """Initiate Sensibo Device Switch."""
108  super().__init__(
109  coordinator,
110  device_id,
111  )
112  self.entity_descriptionentity_description = entity_description
113  self._attr_unique_id_attr_unique_id = f"{device_id}-{entity_description.key}"
114 
115  @property
116  def is_on(self) -> bool | None:
117  """Return True if entity is on."""
118  return self.entity_descriptionentity_description.value_fn(self.device_datadevice_data)
119 
120  async def async_turn_on(self, **kwargs: Any) -> None:
121  """Turn the entity on."""
122  func = getattr(SensiboDeviceSwitch, self.entity_descriptionentity_description.command_on)
123  await func(
124  self,
125  key=self.entity_descriptionentity_description.data_key,
126  value=True,
127  )
128 
129  async def async_turn_off(self, **kwargs: Any) -> None:
130  """Turn the entity off."""
131  func = getattr(SensiboDeviceSwitch, self.entity_descriptionentity_description.command_off)
132  await func(
133  self,
134  key=self.entity_descriptionentity_description.data_key,
135  value=False,
136  )
137 
138  @property
139  def extra_state_attributes(self) -> Mapping[str, Any] | None:
140  """Return additional attributes."""
141  if self.entity_descriptionentity_description.extra_fn:
142  return self.entity_descriptionentity_description.extra_fn(self.device_datadevice_data)
143  return None
144 
145  @async_handle_api_call
146  async def async_turn_on_timer(self, key: str, value: bool) -> bool:
147  """Make service call to api for setting timer."""
148  new_state = not self.device_datadevice_data.device_on
149  data = {
150  "minutesFromNow": 60,
151  "acState": {**self.device_datadevice_data.ac_states, "on": new_state},
152  }
153  result = await self._client_client.async_set_timer(self._device_id_device_id, data)
154  return bool(result.get("status") == "success")
155 
156  @async_handle_api_call
157  async def async_turn_off_timer(self, key: str, value: bool) -> bool:
158  """Make service call to api for deleting timer."""
159  result = await self._client_client.async_del_timer(self._device_id_device_id)
160  return bool(result.get("status") == "success")
161 
162  @async_handle_api_call
163  async def async_turn_on_off_pure_boost(self, key: str, value: bool) -> bool:
164  """Make service call to api for setting Pure Boost."""
165  data: dict[str, Any] = {"enabled": value}
166  if self.device_datadevice_data.pure_measure_integration is None:
167  data["sensitivity"] = "N"
168  data["measurementsIntegration"] = True
169  data["acIntegration"] = False
170  data["geoIntegration"] = False
171  data["primeIntegration"] = False
172  result = await self._client_client.async_set_pureboost(self._device_id_device_id, data)
173  return bool(result.get("status") == "success")
174 
175  @async_handle_api_call
176  async def async_turn_on_off_smart(self, key: str, value: bool) -> bool:
177  """Make service call to api for setting Climate React."""
178  if self.device_datadevice_data.smart_type is None:
179  raise HomeAssistantError(
180  translation_domain=DOMAIN,
181  translation_key="climate_react_not_available",
182  )
183  data: dict[str, Any] = {"enabled": value}
184  result = await self._client_client.async_enable_climate_react(self._device_id_device_id, data)
185  return bool(result.get("status") == "success")
bool async_turn_on_off_pure_boost(self, str key, bool value)
Definition: switch.py:163
bool async_turn_on_timer(self, str key, bool value)
Definition: switch.py:146
bool async_turn_on_off_smart(self, str key, bool value)
Definition: switch.py:176
None __init__(self, SensiboDataUpdateCoordinator coordinator, str device_id, SensiboDeviceSwitchEntityDescription entity_description)
Definition: switch.py:106
bool async_turn_off_timer(self, str key, bool value)
Definition: switch.py:157
None async_setup_entry(HomeAssistant hass, SensiboConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:82