Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for SUPLA switch."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from pprint import pformat
7 from typing import Any
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 from . import DOMAIN, SUPLA_COORDINATORS, SUPLA_SERVERS
15 from .entity import SuplaEntity
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
21  hass: HomeAssistant,
22  config: ConfigType,
23  async_add_entities: AddEntitiesCallback,
24  discovery_info: DiscoveryInfoType | None = None,
25 ) -> None:
26  """Set up the SUPLA switches."""
27  if discovery_info is None:
28  return
29 
30  _LOGGER.debug("Discovery: %s", pformat(discovery_info))
31 
32  entities = []
33  for device in discovery_info.values():
34  server_name = device["server_name"]
35 
36  entities.append(
38  device,
39  hass.data[DOMAIN][SUPLA_SERVERS][server_name],
40  hass.data[DOMAIN][SUPLA_COORDINATORS][server_name],
41  )
42  )
43 
44  async_add_entities(entities)
45 
46 
48  """Representation of a SUPLA Switch."""
49 
50  async def async_turn_on(self, **kwargs: Any) -> None:
51  """Turn on the switch."""
52  await self.async_actionasync_action("TURN_ON")
53 
54  async def async_turn_off(self, **kwargs: Any) -> None:
55  """Turn off the switch."""
56  await self.async_actionasync_action("TURN_OFF")
57 
58  @property
59  def is_on(self):
60  """Return true if switch is on."""
61  if state := self.channel_datachannel_data.get("state"):
62  return state["on"]
63  return False
def async_action(self, action, **add_pars)
Definition: entity.py:48
None async_turn_off(self, **Any kwargs)
Definition: entity.py:1709
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:25