Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Orvibo S20 Wifi Smart Switches."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from orvibo.s20 import S20, S20Exception, discover
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
13  SwitchEntity,
14 )
15 from homeassistant.const import (
16  CONF_DISCOVERY,
17  CONF_HOST,
18  CONF_MAC,
19  CONF_NAME,
20  CONF_SWITCHES,
21 )
22 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 DEFAULT_NAME = "Orvibo S20 Switch"
30 DEFAULT_DISCOVERY = True
31 
32 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
33  {
34  vol.Required(CONF_SWITCHES, default=[]): vol.All(
35  cv.ensure_list,
36  [
37  {
38  vol.Required(CONF_HOST): cv.string,
39  vol.Optional(CONF_MAC): cv.string,
40  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
41  }
42  ],
43  ),
44  vol.Optional(CONF_DISCOVERY, default=DEFAULT_DISCOVERY): cv.boolean,
45  }
46 )
47 
48 
50  hass: HomeAssistant,
51  config: ConfigType,
52  add_entities_callback: AddEntitiesCallback,
53  discovery_info: DiscoveryInfoType | None = None,
54 ) -> None:
55  """Set up S20 switches."""
56 
57  switch_data = {}
58  switches = []
59  switch_conf = config.get(CONF_SWITCHES, [config])
60 
61  if config.get(CONF_DISCOVERY):
62  _LOGGER.debug("Discovering S20 switches")
63  switch_data.update(discover())
64 
65  for switch in switch_conf:
66  switch_data[switch.get(CONF_HOST)] = switch
67 
68  for host, data in switch_data.items():
69  try:
70  switches.append(
71  S20Switch(data.get(CONF_NAME), S20(host, mac=data.get(CONF_MAC)))
72  )
73  _LOGGER.debug("Initialized S20 at %s", host)
74  except S20Exception:
75  _LOGGER.error("S20 at %s couldn't be initialized", host)
76 
77  add_entities_callback(switches)
78 
79 
81  """Representation of an S20 switch."""
82 
83  def __init__(self, name, s20):
84  """Initialize the S20 device."""
85 
86  self._name_name = name
87  self._s20_s20 = s20
88  self._state_state = False
89  self._exc_exc = S20Exception
90 
91  @property
92  def name(self):
93  """Return the name of the switch."""
94  return self._name_name
95 
96  @property
97  def is_on(self):
98  """Return true if device is on."""
99  return self._state_state
100 
101  def update(self) -> None:
102  """Update device state."""
103  try:
104  self._state_state = self._s20_s20.on
105  except self._exc_exc:
106  _LOGGER.exception("Error while fetching S20 state")
107 
108  def turn_on(self, **kwargs: Any) -> None:
109  """Turn the device on."""
110  try:
111  self._s20_s20.on = True
112  except self._exc_exc:
113  _LOGGER.exception("Error while turning on S20")
114 
115  def turn_off(self, **kwargs: Any) -> None:
116  """Turn the device off."""
117  try:
118  self._s20_s20.on = False
119  except self._exc_exc:
120  _LOGGER.exception("Error while turning off S20")
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities_callback, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:54
list[tuple[str, int]] discover(HomeAssistant hass)
Definition: config_flow.py:97