Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for OpenTherm Gateway switches."""
2 
3 from collections.abc import Awaitable, Callable
4 from dataclasses import dataclass
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_ID, EntityCategory
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import OpenThermGatewayHub
14 from .const import DATA_GATEWAYS, DATA_OPENTHERM_GW, GATEWAY_DEVICE_DESCRIPTION
15 from .entity import OpenThermEntity, OpenThermEntityDescription
16 
17 
18 @dataclass(frozen=True, kw_only=True)
20  OpenThermEntityDescription, SwitchEntityDescription
21 ):
22  """Describes an opentherm_gw switch entity."""
23 
24  turn_off_action: Callable[[OpenThermGatewayHub], Awaitable[int | None]]
25  turn_on_action: Callable[[OpenThermGatewayHub], Awaitable[int | None]]
26 
27 
28 SWITCH_DESCRIPTIONS: tuple[OpenThermSwitchEntityDescription, ...] = (
30  key="central_heating_1_override",
31  translation_key="central_heating_override_n",
32  translation_placeholders={"circuit_number": "1"},
33  device_description=GATEWAY_DEVICE_DESCRIPTION,
34  turn_off_action=lambda hub: hub.gateway.set_ch_enable_bit(0),
35  turn_on_action=lambda hub: hub.gateway.set_ch_enable_bit(1),
36  ),
38  key="central_heating_2_override",
39  translation_key="central_heating_override_n",
40  translation_placeholders={"circuit_number": "2"},
41  device_description=GATEWAY_DEVICE_DESCRIPTION,
42  turn_off_action=lambda hub: hub.gateway.set_ch2_enable_bit(0),
43  turn_on_action=lambda hub: hub.gateway.set_ch2_enable_bit(1),
44  ),
45 )
46 
47 
49  hass: HomeAssistant,
50  config_entry: ConfigEntry,
51  async_add_entities: AddEntitiesCallback,
52 ) -> None:
53  """Set up the OpenTherm Gateway switches."""
54  gw_hub = hass.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][config_entry.data[CONF_ID]]
55 
57  OpenThermSwitch(gw_hub, description) for description in SWITCH_DESCRIPTIONS
58  )
59 
60 
62  """Represent an OpenTherm Gateway switch."""
63 
64  _attr_assumed_state = True
65  _attr_entity_category = EntityCategory.CONFIG
66  _attr_entity_registry_enabled_default = False
67  entity_description: OpenThermSwitchEntityDescription
68 
69  async def async_turn_off(self, **kwargs: Any) -> None:
70  """Turn the switch off."""
71  value = await self.entity_descriptionentity_description.turn_off_action(self._gateway_gateway)
72  self._attr_is_on_attr_is_on = bool(value) if value is not None else None
73  self.async_write_ha_stateasync_write_ha_state()
74 
75  async def async_turn_on(self, **kwargs: Any) -> None:
76  """Turn the switch on."""
77  value = await self.entity_descriptionentity_description.turn_on_action(self._gateway_gateway)
78  self._attr_is_on_attr_is_on = bool(value) if value is not None else None
79  self.async_write_ha_stateasync_write_ha_state()
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:52