Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Elmax switch platform."""
2 
3 import asyncio
4 import logging
5 from typing import Any
6 
7 from elmax_api.model.command import SwitchCommand
8 from elmax_api.model.panel import PanelStatus
9 
10 from homeassistant.components.switch import SwitchEntity
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 DOMAIN
16 from .coordinator import ElmaxCoordinator
17 from .entity import ElmaxEntity
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
23  hass: HomeAssistant,
24  config_entry: ConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up the Elmax switch platform."""
28  coordinator: ElmaxCoordinator = hass.data[DOMAIN][config_entry.entry_id]
29  known_devices = set()
30 
31  def _discover_new_devices():
32  panel_status: PanelStatus = coordinator.data
33  # In case the panel is offline, its status will be None. In that case, simply do nothing
34  if panel_status is None:
35  return
36 
37  # Otherwise, add all the entities we found
38  entities = []
39  for actuator in panel_status.actuators:
40  # Skip already handled devices
41  if actuator.endpoint_id in known_devices:
42  continue
43  entity = ElmaxSwitch(
44  elmax_device=actuator,
45  panel_version=panel_status.release,
46  coordinator=coordinator,
47  )
48  entities.append(entity)
49 
50  if entities:
51  async_add_entities(entities)
52  known_devices.update([entity.unique_id for entity in entities])
53 
54  # Register a listener for the discovery of new devices
55  remove_handle = coordinator.async_add_listener(_discover_new_devices)
56  config_entry.async_on_unload(remove_handle)
57 
58  # Immediately run a discovery, so we don't need to wait for the next update
59  _discover_new_devices()
60 
61 
63  """Implement the Elmax switch entity."""
64 
65  @property
66  def is_on(self) -> bool:
67  """Return True if entity is on."""
68  return self.coordinator.get_actuator_state(self._device_device.endpoint_id).opened
69 
70  async def _wait_for_state_change(self) -> bool:
71  """Refresh data and wait until the state changes."""
72  old_state = self.coordinator.get_actuator_state(self._device_device.endpoint_id).opened
73 
74  # Wait a bit at first to let Elmax cloud assimilate the new state.
75  await asyncio.sleep(2.0)
76  await self.coordinator.async_refresh()
77  new_state = self.coordinator.get_actuator_state(self._device_device.endpoint_id).opened
78 
79  # First check attempt.
80  if new_state == old_state:
81  # Otherwise sleep a bit more and then trigger a final update.
82  await asyncio.sleep(5.0)
83  await self.coordinator.async_refresh()
84  new_state = self.coordinator.get_actuator_state(
85  self._device_device.endpoint_id
86  ).opened
87 
88  return new_state != old_state
89 
90  async def async_turn_on(self, **kwargs: Any) -> None:
91  """Turn the entity on."""
92  await self.coordinator.http_client.execute_command(
93  endpoint_id=self._device_device.endpoint_id, command=SwitchCommand.TURN_ON
94  )
95  if await self._wait_for_state_change_wait_for_state_change():
96  self.async_write_ha_stateasync_write_ha_state()
97 
98  async def async_turn_off(self, **kwargs: Any) -> None:
99  """Turn the entity off."""
100  await self.coordinator.http_client.execute_command(
101  endpoint_id=self._device_device.endpoint_id, command=SwitchCommand.TURN_OFF
102  )
103  if await self._wait_for_state_change_wait_for_state_change():
104  self.async_write_ha_stateasync_write_ha_state()
None async_turn_off(self, **Any kwargs)
Definition: switch.py:98
None async_turn_on(self, **Any kwargs)
Definition: switch.py:90
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:26