Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Aqualink pool feature switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from iaqualink.device import AqualinkSwitch
8 
9 from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import refresh_system
15 from .const import DOMAIN as AQUALINK_DOMAIN
16 from .entity import AqualinkEntity
17 from .utils import await_or_reraise
18 
19 PARALLEL_UPDATES = 0
20 
21 
23  hass: HomeAssistant,
24  config_entry: ConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up discovered switches."""
29  (HassAqualinkSwitch(dev) for dev in hass.data[AQUALINK_DOMAIN][SWITCH_DOMAIN]),
30  True,
31  )
32 
33 
34 class HassAqualinkSwitch(AqualinkEntity, SwitchEntity):
35  """Representation of a switch."""
36 
37  def __init__(self, dev: AqualinkSwitch) -> None:
38  """Initialize AquaLink switch."""
39  super().__init__(dev)
40  name = self._attr_name_attr_name = dev.label
41  if name == "Cleaner":
42  self._attr_icon_attr_icon = "mdi:robot-vacuum"
43  elif name == "Waterfall" or name.endswith("Dscnt"):
44  self._attr_icon_attr_icon = "mdi:fountain"
45  elif name.endswith(("Pump", "Blower")):
46  self._attr_icon_attr_icon = "mdi:fan"
47  if name.endswith("Heater"):
48  self._attr_icon_attr_icon = "mdi:radiator"
49 
50  @property
51  def is_on(self) -> bool:
52  """Return whether the switch is on or not."""
53  return self.devdev.is_on
54 
55  @refresh_system
56  async def async_turn_on(self, **kwargs: Any) -> None:
57  """Turn on the switch."""
58  await await_or_reraise(self.devdev.turn_on())
59 
60  @refresh_system
61  async def async_turn_off(self, **kwargs: Any) -> None:
62  """Turn off the switch."""
63  await await_or_reraise(self.devdev.turn_off())