Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for switches using GC100."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
10  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
11  SwitchEntity,
12 )
13 from homeassistant.const import DEVICE_DEFAULT_NAME
14 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
18 
19 from . import CONF_PORTS, DATA_GC100
20 
21 _SWITCH_SCHEMA = vol.Schema({cv.string: cv.string})
22 
23 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
24  {vol.Required(CONF_PORTS): vol.All(cv.ensure_list, [_SWITCH_SCHEMA])}
25 )
26 
27 
29  hass: HomeAssistant,
30  config: ConfigType,
31  add_entities: AddEntitiesCallback,
32  discovery_info: DiscoveryInfoType | None = None,
33 ) -> None:
34  """Set up the GC100 devices."""
35  switches = []
36  ports = config[CONF_PORTS]
37  for port in ports:
38  for port_addr, port_name in port.items():
39  switches.append(GC100Switch(port_name, port_addr, hass.data[DATA_GC100]))
40  add_entities(switches, True)
41 
42 
44  """Represent a switch/relay from GC100."""
45 
46  def __init__(self, name, port_addr, gc100):
47  """Initialize the GC100 switch."""
48  self._name_name = name or DEVICE_DEFAULT_NAME
49  self._port_addr_port_addr = port_addr
50  self._gc100_gc100 = gc100
51  self._state_state = None
52 
53  @property
54  def name(self):
55  """Return the name of the switch."""
56  return self._name_name
57 
58  @property
59  def is_on(self):
60  """Return the state of the entity."""
61  return self._state_state
62 
63  def turn_on(self, **kwargs: Any) -> None:
64  """Turn the device on."""
65  self._gc100_gc100.write_switch(self._port_addr_port_addr, 1, self.set_stateset_state)
66 
67  def turn_off(self, **kwargs: Any) -> None:
68  """Turn the device off."""
69  self._gc100_gc100.write_switch(self._port_addr_port_addr, 0, self.set_stateset_state)
70 
71  def update(self) -> None:
72  """Update the sensor state."""
73  self._gc100_gc100.read_sensor(self._port_addr_port_addr, self.set_stateset_state)
74 
75  def set_state(self, state):
76  """Set the current state."""
77  self._state_state = state == 1
78  self.schedule_update_ha_stateschedule_update_ha_state()
def __init__(self, name, port_addr, gc100)
Definition: switch.py:46
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
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:33