Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for VeSync switches."""
2 
3 import logging
4 from typing import Any
5 
6 from homeassistant.components.switch import SwitchEntity
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.core import HomeAssistant, callback
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_SWITCHES
13 from .entity import VeSyncDevice
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
19  hass: HomeAssistant,
20  config_entry: ConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Set up switches."""
24 
25  @callback
26  def discover(devices):
27  """Add new devices to platform."""
28  _setup_entities(devices, async_add_entities)
29 
30  config_entry.async_on_unload(
31  async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_SWITCHES), discover)
32  )
33 
34  _setup_entities(hass.data[DOMAIN][VS_SWITCHES], async_add_entities)
35 
36 
37 @callback
38 def _setup_entities(devices, async_add_entities):
39  """Check if device is online and add entity."""
40  entities = []
41  for dev in devices:
42  if DEV_TYPE_TO_HA.get(dev.device_type) == "outlet":
43  entities.append(VeSyncSwitchHA(dev))
44  elif DEV_TYPE_TO_HA.get(dev.device_type) == "switch":
45  entities.append(VeSyncLightSwitch(dev))
46  else:
47  _LOGGER.warning(
48  "%s - Unknown device type - %s", dev.device_name, dev.device_type
49  )
50  continue
51 
52  async_add_entities(entities, update_before_add=True)
53 
54 
56  """Base class for VeSync switch Device Representations."""
57 
58  _attr_name = None
59 
60  def turn_on(self, **kwargs: Any) -> None:
61  """Turn the device on."""
62  self.devicedevice.turn_on()
63 
64 
66  """Representation of a VeSync switch."""
67 
68  def __init__(self, plug):
69  """Initialize the VeSync switch device."""
70  super().__init__(plug)
71  self.smartplugsmartplug = plug
72 
73  def update(self) -> None:
74  """Update outlet details and energy usage."""
75  self.smartplugsmartplug.update()
76  self.smartplugsmartplug.update_energy()
77 
78 
80  """Handle representation of VeSync Light Switch."""
81 
82  def __init__(self, switch):
83  """Initialize Light Switch device class."""
84  super().__init__(switch)
85  self.switchswitch = switch
list[tuple[str, int]] discover(HomeAssistant hass)
Definition: config_flow.py:97
def _setup_entities(devices, async_add_entities)
Definition: switch.py:38
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:22
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103