Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Platform for switch integration."""
2 
3 import asyncio
4 from typing import Any
5 
6 from smarttub import SpaPump
7 
8 from homeassistant.components.switch import SwitchEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import API_TIMEOUT, ATTR_PUMPS, DOMAIN, SMARTTUB_CONTROLLER
14 from .entity import SmartTubEntity
15 from .helpers import get_spa_name
16 
17 
19  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
20 ) -> None:
21  """Set up switch entities for the pumps on the tub."""
22 
23  controller = hass.data[DOMAIN][entry.entry_id][SMARTTUB_CONTROLLER]
24 
25  entities = [
26  SmartTubPump(controller.coordinator, pump)
27  for spa in controller.spas
28  for pump in controller.coordinator.data[spa.id][ATTR_PUMPS].values()
29  ]
30 
31  async_add_entities(entities)
32 
33 
35  """A pump on a spa."""
36 
37  def __init__(self, coordinator, pump: SpaPump) -> None:
38  """Initialize the entity."""
39  super().__init__(coordinator, pump.spa, "pump")
40  self.pump_idpump_id = pump.id
41  self.pump_typepump_type = pump.type
42  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{super().unique_id}-{pump.id}"
43 
44  @property
45  def pump(self) -> SpaPump:
46  """Return the underlying SpaPump object for this entity."""
47  return self.coordinator.data[self.spaspa.id][ATTR_PUMPS][self.pump_idpump_id]
48 
49  @property
50  def name(self) -> str:
51  """Return a name for this pump entity."""
52  spa_name = get_spa_name(self.spaspa)
53  if self.pump_typepump_type == SpaPump.PumpType.CIRCULATION:
54  return f"{spa_name} Circulation Pump"
55  if self.pump_typepump_type == SpaPump.PumpType.JET:
56  return f"{spa_name} Jet {self.pump_id}"
57  return f"{spa_name} pump {self.pump_id}"
58 
59  @property
60  def is_on(self) -> bool:
61  """Return True if the pump is on."""
62  return self.pumppump.state != SpaPump.PumpState.OFF
63 
64  async def async_turn_on(self, **kwargs: Any) -> None:
65  """Turn the pump on."""
66 
67  # the API only supports toggling
68  if not self.is_onis_onis_on:
69  await self.async_toggleasync_toggleasync_toggle()
70 
71  async def async_turn_off(self, **kwargs: Any) -> None:
72  """Turn the pump off."""
73 
74  # the API only supports toggling
75  if self.is_onis_onis_on:
76  await self.async_toggleasync_toggleasync_toggle()
77 
78  async def async_toggle(self, **kwargs: Any) -> None:
79  """Toggle the pump on or off."""
80  async with asyncio.timeout(API_TIMEOUT):
81  await self.pumppump.toggle()
82  await self.coordinator.async_request_refresh()
None __init__(self, coordinator, SpaPump pump)
Definition: switch.py:37
None async_toggle(self, **Any kwargs)
Definition: entity.py:1721
None toggle(self, **Any kwargs)
Definition: entity.py:1714
str get_spa_name(smarttub.Spa spa)
Definition: helpers.py:6
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:20