Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for EufyHome switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import lakeside
8 
9 from homeassistant.components.switch import SwitchEntity
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
13 
14 
16  hass: HomeAssistant,
17  config: ConfigType,
18  add_entities: AddEntitiesCallback,
19  discovery_info: DiscoveryInfoType | None = None,
20 ) -> None:
21  """Set up EufyHome switches."""
22  if discovery_info is None:
23  return
24  add_entities([EufyHomeSwitch(discovery_info)], True)
25 
26 
28  """Representation of a EufyHome switch."""
29 
30  def __init__(self, device):
31  """Initialize the light."""
32 
33  self._state_state = None
34  self._name_name = device["name"]
35  self._address_address = device["address"]
36  self._code_code = device["code"]
37  self._type_type = device["type"]
38  self._switch_switch = lakeside.switch(self._address_address, self._code_code, self._type_type)
39  self._switch_switch.connect()
40 
41  def update(self) -> None:
42  """Synchronise state from the switch."""
43  self._switch_switch.update()
44  self._state_state = self._switch_switch.power
45 
46  @property
47  def unique_id(self):
48  """Return the ID of this light."""
49  return self._address_address
50 
51  @property
52  def name(self):
53  """Return the name of the device if any."""
54  return self._name_name
55 
56  @property
57  def is_on(self):
58  """Return true if device is on."""
59  return self._state_state
60 
61  def turn_on(self, **kwargs: Any) -> None:
62  """Turn the specified switch on."""
63  try:
64  self._switch_switch.set_state(True)
65  except BrokenPipeError:
66  self._switch_switch.connect()
67  self._switch_switch.set_state(power=True)
68 
69  def turn_off(self, **kwargs: Any) -> None:
70  """Turn the specified switch off."""
71  try:
72  self._switch_switch.set_state(False)
73  except BrokenPipeError:
74  self._switch_switch.connect()
75  self._switch_switch.set_state(False)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:20