Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switch platform for the Flipr's Hub."""
2 
3 import logging
4 from typing import Any
5 
6 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 
10 from . import FliprConfigEntry
11 from .entity import FliprEntity
12 
13 _LOGGER = logging.getLogger(__name__)
14 
15 SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
17  key="hubState",
18  name=None,
19  ),
20 )
21 
22 
24  hass: HomeAssistant,
25  config_entry: FliprConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up switch for Flipr hub."""
29  coordinators = config_entry.runtime_data.hub_coordinators
30 
32  FliprHubSwitch(coordinator, description, True)
33  for description in SWITCH_TYPES
34  for coordinator in coordinators
35  )
36 
37 
39  """Switch representing Hub state."""
40 
41  @property
42  def is_on(self) -> bool:
43  """Return state of the switch."""
44  _LOGGER.debug("coordinator data = %s", self.coordinator.data)
45  return self.coordinator.data["state"]
46 
47  async def async_turn_off(self, **kwargs: Any) -> None:
48  """Turn off the switch."""
49  _LOGGER.debug("Switching off %s", self.device_iddevice_iddevice_id)
50  data = await self.hasshasshass.async_add_executor_job(
51  self.coordinator.client.set_hub_state,
52  self.device_iddevice_iddevice_id,
53  False,
54  )
55  _LOGGER.debug("New hub infos are %s", data)
56  self.coordinator.async_set_updated_data(data)
57 
58  async def async_turn_on(self, **kwargs: Any) -> None:
59  """Turn on the switch."""
60  _LOGGER.debug("Switching on %s", self.device_iddevice_iddevice_id)
61  data = await self.hasshasshass.async_add_executor_job(
62  self.coordinator.client.set_hub_state,
63  self.device_iddevice_iddevice_id,
64  True,
65  )
66  _LOGGER.debug("New hub infos are %s", data)
67  self.coordinator.async_set_updated_data(data)
None async_setup_entry(HomeAssistant hass, FliprConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:27