Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for SwitchBee switch."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from switchbee.api.central_unit import SwitchBeeDeviceOfflineError, SwitchBeeError
8 from switchbee.device import (
9  ApiStateCommand,
10  SwitchBeeGroupSwitch,
11  SwitchBeeSwitch,
12  SwitchBeeTimedSwitch,
13  SwitchBeeTimerSwitch,
14 )
15 
16 from homeassistant.components.switch import SwitchEntity
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.core import HomeAssistant, callback
19 from homeassistant.exceptions import HomeAssistantError
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .const import DOMAIN
23 from .coordinator import SwitchBeeCoordinator
24 from .entity import SwitchBeeDeviceEntity
25 
26 
28  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
29 ) -> None:
30  """Set up Switchbee switch."""
31  coordinator: SwitchBeeCoordinator = hass.data[DOMAIN][entry.entry_id]
32 
34  SwitchBeeSwitchEntity(device, coordinator)
35  for device in coordinator.data.values()
36  if isinstance(
37  device,
38  (
39  SwitchBeeTimedSwitch,
40  SwitchBeeGroupSwitch,
41  SwitchBeeSwitch,
42  SwitchBeeTimerSwitch,
43  ),
44  )
45  )
46 
47 
49  _DeviceTypeT: SwitchBeeTimedSwitch
50  | SwitchBeeGroupSwitch
51  | SwitchBeeSwitch
52  | SwitchBeeTimerSwitch
53 ](SwitchBeeDeviceEntity[_DeviceTypeT], SwitchEntity):
54  """Representation of a Switchbee switch."""
55 
56  def __init__(
57  self,
58  device: _DeviceTypeT,
59  coordinator: SwitchBeeCoordinator,
60  ) -> None:
61  """Initialize the Switchbee switch."""
62  super().__init__(device, coordinator)
63  self._attr_is_on = False
64 
65  @callback
66  def _handle_coordinator_update(self) -> None:
67  """Handle updated data from the coordinator."""
68  self._update_from_coordinator()
70 
71  def _update_from_coordinator(self) -> None:
72  """Update the entity attributes from the coordinator data."""
73 
74  coordinator_device = self._get_coordinator_device()
75 
76  if coordinator_device.state == -1:
77  self._check_if_became_offline()
78  return
79 
80  self._check_if_became_online()
81 
82  # timed power switch state is an integer representing the number of minutes left until it goes off
83  # regulare switches state is ON/OFF (1/0 respectively)
84  self._attr_is_on = coordinator_device.state != ApiStateCommand.OFF
85 
86  async def async_turn_on(self, **kwargs: Any) -> None:
87  """Async function to set on to switch."""
88  return await self._async_set_state(ApiStateCommand.ON)
89 
90  async def async_turn_off(self, **kwargs: Any) -> None:
91  """Async function to set off to switch."""
92  return await self._async_set_state(ApiStateCommand.OFF)
93 
94  async def _async_set_state(self, state: str) -> None:
95  try:
96  await self.coordinator.api.set_state(self._device.id, state)
97  except (SwitchBeeError, SwitchBeeDeviceOfflineError) as exp:
98  await self.coordinator.async_refresh()
99  raise HomeAssistantError(
100  f"Failed to set {self._attr_name} state {state}, {exp!s}"
101  ) from exp
102 
103  await self.coordinator.async_refresh()
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:29
None _async_set_state(self, str state)
Definition: switch.py:94
None async_turn_on(self, **Any kwargs)
Definition: switch.py:86
None async_turn_off(self, **Any kwargs)
Definition: switch.py:90
None __init__(self, _DeviceTypeT device, SwitchBeeCoordinator coordinator)
Definition: switch.py:60