Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Overkiz switches."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from pyoverkiz.enums import OverkizCommand, OverkizCommandParam, OverkizState
10 from pyoverkiz.enums.ui import UIClass, UIWidget
11 from pyoverkiz.types import StateType as OverkizStateType
12 
14  SwitchDeviceClass,
15  SwitchEntity,
16  SwitchEntityDescription,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.const import EntityCategory, Platform
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from . import HomeAssistantOverkizData
24 from .const import DOMAIN
25 from .entity import OverkizDescriptiveEntity
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """Class to describe an Overkiz switch."""
31 
32  turn_on: str
33  turn_off: str
34  is_on: Callable[[Callable[[str], OverkizStateType]], bool] | None = None
35  turn_on_args: OverkizStateType | list[OverkizStateType] | None = None
36  turn_off_args: OverkizStateType | list[OverkizStateType] | None = None
37 
38 
39 SWITCH_DESCRIPTIONS: list[OverkizSwitchDescription] = [
41  key=UIWidget.DOMESTIC_HOT_WATER_TANK,
42  turn_on=OverkizCommand.SET_FORCE_HEATING,
43  turn_on_args=OverkizCommandParam.ON,
44  turn_off=OverkizCommand.SET_FORCE_HEATING,
45  turn_off_args=OverkizCommandParam.OFF,
46  is_on=lambda select_state: (
47  select_state(OverkizState.IO_FORCE_HEATING) == OverkizCommandParam.ON
48  ),
49  icon="mdi:water-boiler",
50  ),
52  key=UIClass.ON_OFF,
53  turn_on=OverkizCommand.ON,
54  turn_off=OverkizCommand.OFF,
55  is_on=lambda select_state: (
56  select_state(OverkizState.CORE_ON_OFF) == OverkizCommandParam.ON
57  ),
58  device_class=SwitchDeviceClass.OUTLET,
59  ),
61  key=UIClass.SWIMMING_POOL,
62  turn_on=OverkizCommand.ON,
63  turn_off=OverkizCommand.OFF,
64  is_on=lambda select_state: (
65  select_state(OverkizState.CORE_ON_OFF) == OverkizCommandParam.ON
66  ),
67  icon="mdi:pool",
68  ),
70  key=UIWidget.RTD_INDOOR_SIREN,
71  turn_on=OverkizCommand.ON,
72  turn_off=OverkizCommand.OFF,
73  icon="mdi:bell",
74  ),
76  key=UIWidget.RTD_OUTDOOR_SIREN,
77  turn_on=OverkizCommand.ON,
78  turn_off=OverkizCommand.OFF,
79  icon="mdi:bell",
80  ),
82  key=UIWidget.STATELESS_ALARM_CONTROLLER,
83  turn_on=OverkizCommand.ALARM_ON,
84  turn_off=OverkizCommand.ALARM_OFF,
85  icon="mdi:shield-lock",
86  ),
88  key=UIWidget.STATELESS_EXTERIOR_HEATING,
89  turn_on=OverkizCommand.ON,
90  turn_off=OverkizCommand.OFF,
91  icon="mdi:radiator",
92  ),
94  key=UIWidget.MY_FOX_SECURITY_CAMERA,
95  name="Camera shutter",
96  turn_on=OverkizCommand.OPEN,
97  turn_off=OverkizCommand.CLOSE,
98  icon="mdi:camera-lock",
99  is_on=lambda select_state: (
100  select_state(OverkizState.MYFOX_SHUTTER_STATUS)
101  == OverkizCommandParam.OPENED
102  ),
103  entity_category=EntityCategory.CONFIG,
104  ),
105 ]
106 
107 SUPPORTED_DEVICES = {
108  description.key: description for description in SWITCH_DESCRIPTIONS
109 }
110 
111 
113  hass: HomeAssistant,
114  entry: ConfigEntry,
115  async_add_entities: AddEntitiesCallback,
116 ) -> None:
117  """Set up the Overkiz switch from a config entry."""
118  data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id]
119 
122  device.device_url,
123  data.coordinator,
124  description,
125  )
126  for device in data.platforms[Platform.SWITCH]
127  if (
128  description := SUPPORTED_DEVICES.get(device.widget)
129  or SUPPORTED_DEVICES.get(device.ui_class)
130  )
131  )
132 
133 
135  """Representation of an Overkiz Switch."""
136 
137  entity_description: OverkizSwitchDescription
138 
139  @property
140  def is_on(self) -> bool | None:
141  """Return True if entity is on."""
142  if self.entity_descriptionentity_description.is_on:
143  return self.entity_descriptionentity_description.is_on(self.executorexecutor.select_state)
144 
145  return None
146 
147  async def async_turn_on(self, **kwargs: Any) -> None:
148  """Turn the entity on."""
149  await self.executorexecutor.async_execute_command(
150  self.entity_descriptionentity_description.turn_on,
151  self.entity_descriptionentity_description.turn_on_args,
152  )
153 
154  async def async_turn_off(self, **kwargs: Any) -> None:
155  """Turn the entity off."""
156  await self.executorexecutor.async_execute_command(
157  self.entity_descriptionentity_description.turn_off,
158  self.entity_descriptionentity_description.turn_off_args,
159  )
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:116