Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for AquaLogic switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aqualogic.core import States
8 import voluptuous as vol
9 
11  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
12  SwitchEntity,
13 )
14 from homeassistant.const import CONF_MONITORED_CONDITIONS
15 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.dispatcher import async_dispatcher_connect
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
20 
21 from . import DOMAIN, UPDATE_TOPIC, AquaLogicProcessor
22 
23 SWITCH_TYPES = {
24  "lights": "Lights",
25  "filter": "Filter",
26  "filter_low_speed": "Filter Low Speed",
27  "aux_1": "Aux 1",
28  "aux_2": "Aux 2",
29  "aux_3": "Aux 3",
30  "aux_4": "Aux 4",
31  "aux_5": "Aux 5",
32  "aux_6": "Aux 6",
33  "aux_7": "Aux 7",
34 }
35 
36 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
37  {
38  vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SWITCH_TYPES)): vol.All(
39  cv.ensure_list, [vol.In(SWITCH_TYPES)]
40  )
41  }
42 )
43 
44 
46  hass: HomeAssistant,
47  config: ConfigType,
48  async_add_entities: AddEntitiesCallback,
49  discovery_info: DiscoveryInfoType | None = None,
50 ) -> None:
51  """Set up the switch platform."""
52  processor: AquaLogicProcessor = hass.data[DOMAIN]
53 
55  AquaLogicSwitch(processor, switch_type)
56  for switch_type in config[CONF_MONITORED_CONDITIONS]
57  )
58 
59 
61  """Switch implementation for the AquaLogic component."""
62 
63  _attr_should_poll = False
64 
65  def __init__(self, processor: AquaLogicProcessor, switch_type: str) -> None:
66  """Initialize switch."""
67  self._processor_processor = processor
68  self._state_name_state_name = {
69  "lights": States.LIGHTS,
70  "filter": States.FILTER,
71  "filter_low_speed": States.FILTER_LOW_SPEED,
72  "aux_1": States.AUX_1,
73  "aux_2": States.AUX_2,
74  "aux_3": States.AUX_3,
75  "aux_4": States.AUX_4,
76  "aux_5": States.AUX_5,
77  "aux_6": States.AUX_6,
78  "aux_7": States.AUX_7,
79  }[switch_type]
80  self._attr_name_attr_name = f"AquaLogic {SWITCH_TYPES[switch_type]}"
81 
82  @property
83  def is_on(self) -> bool:
84  """Return true if device is on."""
85  if (panel := self._processor_processor.panel) is None:
86  return False
87  return panel.get_state(self._state_name_state_name) # type: ignore[no-any-return]
88 
89  def turn_on(self, **kwargs: Any) -> None:
90  """Turn the device on."""
91  if (panel := self._processor_processor.panel) is None:
92  return
93  panel.set_state(self._state_name_state_name, True)
94 
95  def turn_off(self, **kwargs: Any) -> None:
96  """Turn the device off."""
97  if (panel := self._processor_processor.panel) is None:
98  return
99  panel.set_state(self._state_name_state_name, False)
100 
101  async def async_added_to_hass(self) -> None:
102  """Register callbacks."""
103  self.async_on_removeasync_on_remove(
104  async_dispatcher_connect(self.hasshass, UPDATE_TOPIC, self.async_write_ha_stateasync_write_ha_state)
105  )
None __init__(self, AquaLogicProcessor processor, str switch_type)
Definition: switch.py:65
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:50
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103