Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for DROP switches."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 import logging
8 from typing import Any
9 
10 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import (
16  CONF_DEVICE_TYPE,
17  DEV_FILTER,
18  DEV_HUB,
19  DEV_PROTECTION_VALVE,
20  DEV_SOFTENER,
21  DOMAIN,
22 )
23 from .coordinator import DROPDeviceDataUpdateCoordinator
24 from .entity import DROPEntity
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 SWITCH_VALUE: dict[int | None, bool] = {0: False, 1: True}
29 
30 # Switch type constants
31 WATER_SWITCH = "water"
32 BYPASS_SWITCH = "bypass"
33 
34 
35 @dataclass(kw_only=True, frozen=True)
37  """Describes DROP switch entity."""
38 
39  value_fn: Callable[[DROPDeviceDataUpdateCoordinator], int | None]
40  set_fn: Callable[[DROPDeviceDataUpdateCoordinator, int], Awaitable[Any]]
41 
42 
43 SWITCHES: list[DROPSwitchEntityDescription] = [
45  key=WATER_SWITCH,
46  translation_key=WATER_SWITCH,
47  value_fn=lambda device: device.drop_api.water(),
48  set_fn=lambda device, value: device.set_water(value),
49  ),
51  key=BYPASS_SWITCH,
52  translation_key=BYPASS_SWITCH,
53  value_fn=lambda device: device.drop_api.bypass(),
54  set_fn=lambda device, value: device.set_bypass(value),
55  ),
56 ]
57 
58 # Defines which switches are used by each device type
59 DEVICE_SWITCHES: dict[str, list[str]] = {
60  DEV_FILTER: [BYPASS_SWITCH],
61  DEV_HUB: [WATER_SWITCH, BYPASS_SWITCH],
62  DEV_PROTECTION_VALVE: [WATER_SWITCH],
63  DEV_SOFTENER: [BYPASS_SWITCH],
64 }
65 
66 
68  hass: HomeAssistant,
69  config_entry: ConfigEntry,
70  async_add_entities: AddEntitiesCallback,
71 ) -> None:
72  """Set up the DROP switches from config entry."""
73  _LOGGER.debug(
74  "Set up switch for device type %s with entry_id is %s",
75  config_entry.data[CONF_DEVICE_TYPE],
76  config_entry.entry_id,
77  )
78 
79  if config_entry.data[CONF_DEVICE_TYPE] in DEVICE_SWITCHES:
81  DROPSwitch(hass.data[DOMAIN][config_entry.entry_id], switch)
82  for switch in SWITCHES
83  if switch.key in DEVICE_SWITCHES[config_entry.data[CONF_DEVICE_TYPE]]
84  )
85 
86 
88  """Representation of a DROP switch."""
89 
90  entity_description: DROPSwitchEntityDescription
91 
92  def __init__(
93  self,
94  coordinator: DROPDeviceDataUpdateCoordinator,
95  entity_description: DROPSwitchEntityDescription,
96  ) -> None:
97  """Initialize the switch."""
98  super().__init__(entity_description.key, coordinator)
99  self.entity_descriptionentity_description = entity_description
100 
101  @property
102  def is_on(self) -> bool | None:
103  """Return the state of the binary sensor."""
104  return SWITCH_VALUE.get(self.entity_descriptionentity_description.value_fn(self.coordinator))
105 
106  async def async_turn_on(self, **kwargs: Any) -> None:
107  """Turn switch on."""
108  await self.entity_descriptionentity_description.set_fn(self.coordinator, 1)
109 
110  async def async_turn_off(self, **kwargs: Any) -> None:
111  """Turn switch off."""
112  await self.entity_descriptionentity_description.set_fn(self.coordinator, 0)
None __init__(self, DROPDeviceDataUpdateCoordinator coordinator, DROPSwitchEntityDescription entity_description)
Definition: switch.py:96
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:71