Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switch integration microBees."""
2 
3 from typing import Any
4 
5 from homeassistant.components.switch import SwitchEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.core import HomeAssistant
8 from homeassistant.exceptions import HomeAssistantError
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 
11 from .const import DOMAIN
12 from .coordinator import MicroBeesUpdateCoordinator
13 from .entity import MicroBeesActuatorEntity
14 
15 SOCKET_TRANSLATIONS = {46: "socket_it", 38: "socket_eu"}
16 SWITCH_PRODUCT_IDS = {25, 26, 27, 35, 38, 46, 63, 64, 65, 86}
17 
18 
20  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
21 ) -> None:
22  """Config entry."""
23  coordinator = hass.data[DOMAIN][entry.entry_id].coordinator
24 
26  MBSwitch(coordinator, bee_id, switch.id)
27  for bee_id, bee in coordinator.data.bees.items()
28  if bee.productID in SWITCH_PRODUCT_IDS
29  for switch in bee.actuators
30  )
31 
32 
34  """Representation of a microBees switch."""
35 
36  def __init__(
37  self,
38  coordinator: MicroBeesUpdateCoordinator,
39  bee_id: int,
40  actuator_id: int,
41  ) -> None:
42  """Initialize the microBees switch."""
43  super().__init__(coordinator, bee_id, actuator_id)
44  self._attr_translation_key_attr_translation_key = SOCKET_TRANSLATIONS.get(self.beebee.productID)
45 
46  @property
47  def name(self) -> str:
48  """Name of the switch."""
49  return self.actuatoractuator.name
50 
51  @property
52  def is_on(self) -> bool:
53  """Status of the switch."""
54  return self.actuatoractuator.value
55 
56  async def async_turn_on(self, **kwargs: Any) -> None:
57  """Turn on the switch."""
58  send_command = await self.coordinator.microbees.sendCommand(self.actuator_idactuator_id, 1)
59  if not send_command:
60  raise HomeAssistantError(f"Failed to turn on {self.name}")
61 
62  self.actuatoractuator.value = True
63  self.async_write_ha_stateasync_write_ha_state()
64 
65  async def async_turn_off(self, **kwargs: Any) -> None:
66  """Turn off the switch."""
67  send_command = await self.coordinator.microbees.sendCommand(self.actuator_idactuator_id, 0)
68  if not send_command:
69  raise HomeAssistantError(f"Failed to turn off {self.name}")
70 
71  self.actuatoractuator.value = False
72  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, MicroBeesUpdateCoordinator coordinator, int bee_id, int actuator_id)
Definition: switch.py:41
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:21