Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Vallox ventilation unit switches."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any
7 
8 from vallox_websocket_api import Vallox
9 
10 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN
17 from .coordinator import ValloxDataUpdateCoordinator
18 from .entity import ValloxEntity
19 
20 
22  """Representation of a Vallox switch."""
23 
24  entity_description: ValloxSwitchEntityDescription
25  _attr_entity_category = EntityCategory.CONFIG
26 
27  def __init__(
28  self,
29  name: str,
30  coordinator: ValloxDataUpdateCoordinator,
31  description: ValloxSwitchEntityDescription,
32  client: Vallox,
33  ) -> None:
34  """Initialize the Vallox switch."""
35  super().__init__(name, coordinator)
36 
37  self.entity_descriptionentity_description = description
38 
39  self._attr_unique_id_attr_unique_id = f"{self._device_uuid}-{description.key}"
40  self._client_client = client
41 
42  @property
43  def is_on(self) -> bool | None:
44  """Return true if the switch is on."""
45  if (
46  value := self.coordinator.data.get(self.entity_descriptionentity_description.metric_key)
47  ) is None:
48  return None
49  return value == 1
50 
51  async def async_turn_on(self, **kwargs: Any) -> None:
52  """Turn on."""
53  await self._set_value_set_value(True)
54 
55  async def async_turn_off(self, **kwargs: Any) -> None:
56  """Turn off."""
57  await self._set_value_set_value(False)
58 
59  async def _set_value(self, value: bool) -> None:
60  """Update the current value."""
61  metric_key = self.entity_descriptionentity_description.metric_key
62  await self._client_client.set_values({metric_key: 1 if value else 0})
63  await self.coordinator.async_request_refresh()
64 
65 
66 @dataclass(frozen=True, kw_only=True)
68  """Describes Vallox switch entity."""
69 
70  metric_key: str
71 
72 
73 SWITCH_ENTITIES: tuple[ValloxSwitchEntityDescription, ...] = (
75  key="bypass_locked",
76  translation_key="bypass_locked",
77  metric_key="A_CYC_BYPASS_LOCKED",
78  ),
79 )
80 
81 
83  hass: HomeAssistant,
84  entry: ConfigEntry,
85  async_add_entities: AddEntitiesCallback,
86 ) -> None:
87  """Set up the switches."""
88 
89  data = hass.data[DOMAIN][entry.entry_id]
90 
93  data["name"], data["coordinator"], description, data["client"]
94  )
95  for description in SWITCH_ENTITIES
96  )
None __init__(self, str name, ValloxDataUpdateCoordinator coordinator, ValloxSwitchEntityDescription description, Vallox client)
Definition: switch.py:33
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:86