Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Platform for switch integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from devolo_home_control_api.devices.zwave import Zwave
8 from devolo_home_control_api.homecontrol import HomeControl
9 
10 from homeassistant.components.switch import SwitchEntity
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import DevoloHomeControlConfigEntry
15 from .entity import DevoloDeviceEntity
16 
17 
19  hass: HomeAssistant,
20  entry: DevoloHomeControlConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Get all devices and setup the switch devices via config entry."""
24 
27  homecontrol=gateway,
28  device_instance=device,
29  element_uid=binary_switch,
30  )
31  for gateway in entry.runtime_data
32  for device in gateway.binary_switch_devices
33  for binary_switch in device.binary_switch_property
34  # Exclude the binary switch which also has multi_level_switches here,
35  # because those are implemented as light entities now.
36  if not hasattr(device, "multi_level_switch_property")
37  )
38 
39 
41  """Representation of a switch."""
42 
43  _attr_name = None
44 
45  def __init__(
46  self, homecontrol: HomeControl, device_instance: Zwave, element_uid: str
47  ) -> None:
48  """Initialize a devolo Switch."""
49  super().__init__(
50  homecontrol=homecontrol,
51  device_instance=device_instance,
52  element_uid=element_uid,
53  )
54  self._binary_switch_property_binary_switch_property = self._device_instance_device_instance.binary_switch_property[
55  self._attr_unique_id_attr_unique_id # type: ignore[index]
56  ]
57  self._attr_is_on_attr_is_on = self._binary_switch_property_binary_switch_property.state
58 
59  def turn_on(self, **kwargs: Any) -> None:
60  """Switch on the device."""
61  self._binary_switch_property_binary_switch_property.set(state=True)
62 
63  def turn_off(self, **kwargs: Any) -> None:
64  """Switch off the device."""
65  self._binary_switch_property_binary_switch_property.set(state=False)
66 
67  def _sync(self, message: tuple) -> None:
68  """Update the binary switch state and consumption."""
69  if message[0].startswith("devolo.BinarySwitch"):
70  self._attr_is_on_attr_is_on = self._device_instance_device_instance.binary_switch_property[
71  message[0]
72  ].state
73  else:
74  self._generic_message_generic_message(message)
75  self.schedule_update_ha_stateschedule_update_ha_state()
None __init__(self, HomeControl homecontrol, Zwave device_instance, str element_uid)
Definition: switch.py:47
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None async_setup_entry(HomeAssistant hass, DevoloHomeControlConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:22