Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Toon switches."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any
7 
8 from toonapi import (
9  ACTIVE_STATE_AWAY,
10  ACTIVE_STATE_HOLIDAY,
11  PROGRAM_STATE_OFF,
12  PROGRAM_STATE_ON,
13 )
14 
15 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import DOMAIN
21 from .coordinator import ToonDataUpdateCoordinator
22 from .entity import ToonDisplayDeviceEntity, ToonEntity, ToonRequiredKeysMixin
23 from .helpers import toon_exception_handler
24 
25 
27  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
28 ) -> None:
29  """Set up a Toon switches based on a config entry."""
30  coordinator = hass.data[DOMAIN][entry.entry_id]
31 
33  [description.cls(coordinator, description) for description in SWITCH_ENTITIES]
34  )
35 
36 
38  """Defines an Toon switch."""
39 
40  entity_description: ToonSwitchEntityDescription
41 
42  def __init__(
43  self,
44  coordinator: ToonDataUpdateCoordinator,
45  description: ToonSwitchEntityDescription,
46  ) -> None:
47  """Initialize the Toon switch."""
48  self.entity_descriptionentity_description = description
49  super().__init__(coordinator)
50 
51  self._attr_unique_id_attr_unique_id = (
52  f"{coordinator.data.agreement.agreement_id}_{description.key}"
53  )
54 
55  @property
56  def is_on(self) -> bool:
57  """Return the status of the binary sensor."""
58  section = getattr(self.coordinator.data, self.entity_descriptionentity_description.section)
59  return getattr(section, self.entity_descriptionentity_description.measurement)
60 
61 
63  """Defines a Toon program switch."""
64 
65  @toon_exception_handler
66  async def async_turn_off(self, **kwargs: Any) -> None:
67  """Turn off the Toon program switch."""
68  await self.coordinator.toon.set_active_state(
69  ACTIVE_STATE_AWAY, PROGRAM_STATE_OFF
70  )
71 
72  @toon_exception_handler
73  async def async_turn_on(self, **kwargs: Any) -> None:
74  """Turn on the Toon program switch."""
75  await self.coordinator.toon.set_active_state(
76  ACTIVE_STATE_AWAY, PROGRAM_STATE_ON
77  )
78 
79 
81  """Defines a Toon Holiday mode switch."""
82 
83  @toon_exception_handler
84  async def async_turn_off(self, **kwargs: Any) -> None:
85  """Turn off the Toon holiday mode switch."""
86  await self.coordinator.toon.set_active_state(
87  ACTIVE_STATE_AWAY, PROGRAM_STATE_ON
88  )
89 
90  @toon_exception_handler
91  async def async_turn_on(self, **kwargs: Any) -> None:
92  """Turn on the Toon holiday mode switch."""
93  await self.coordinator.toon.set_active_state(
94  ACTIVE_STATE_HOLIDAY, PROGRAM_STATE_OFF
95  )
96 
97 
98 @dataclass(frozen=True)
100  """Mixin for switch required keys."""
101 
102  cls: type[ToonSwitch]
103 
104 
105 @dataclass(frozen=True)
107  """Describes Toon switch entity."""
108 
109 
110 SWITCH_ENTITIES: tuple[ToonSwitchEntityDescription, ...] = (
111  ToonSwitchEntityDescription(
112  key="thermostat_holiday_mode",
113  name="Holiday Mode",
114  section="thermostat",
115  measurement="holiday_mode",
116  icon="mdi:airport",
117  cls=ToonHolidayModeSwitch,
118  ),
119  ToonSwitchEntityDescription(
120  key="thermostat_program",
121  name="Thermostat Program",
122  section="thermostat",
123  measurement="program",
124  icon="mdi:calendar-clock",
125  cls=ToonProgramSwitch,
126  ),
127 )
None __init__(self, ToonDataUpdateCoordinator coordinator, ToonSwitchEntityDescription description)
Definition: switch.py:46
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:28