Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Allows to configure a switch using RPi GPIO."""
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 CONF_HOST, 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_INVERT_LOGIC, DEFAULT_INVERT_LOGIC, setup_output, write_output
20 
21 CONF_PORTS = "ports"
22 
23 _SENSORS_SCHEMA = vol.Schema({cv.positive_int: cv.string})
24 
25 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
26  {
27  vol.Required(CONF_HOST): cv.string,
28  vol.Required(CONF_PORTS): _SENSORS_SCHEMA,
29  vol.Optional(CONF_INVERT_LOGIC, default=DEFAULT_INVERT_LOGIC): cv.boolean,
30  }
31 )
32 
33 
35  hass: HomeAssistant,
36  config: ConfigType,
37  add_entities: AddEntitiesCallback,
38  discovery_info: DiscoveryInfoType | None = None,
39 ) -> None:
40  """Set up the Remote Raspberry PI GPIO devices."""
41  address = config[CONF_HOST]
42  invert_logic = config[CONF_INVERT_LOGIC]
43  ports = config[CONF_PORTS]
44 
45  devices = []
46  for port, name in ports.items():
47  try:
48  led = setup_output(address, port, invert_logic)
49  except (ValueError, IndexError, KeyError, OSError):
50  return
51  new_switch = RemoteRPiGPIOSwitch(name, led)
52  devices.append(new_switch)
53 
54  add_entities(devices)
55 
56 
58  """Representation of a Remote Raspberry Pi GPIO."""
59 
60  _attr_should_poll = False
61 
62  def __init__(self, name, led):
63  """Initialize the pin."""
64  self._name_name = name or DEVICE_DEFAULT_NAME
65  self._state_state = False
66  self._switch_switch = led
67 
68  @property
69  def name(self):
70  """Return the name of the switch."""
71  return self._name_name
72 
73  @property
74  def assumed_state(self):
75  """If unable to access real state of the entity."""
76  return True
77 
78  @property
79  def is_on(self):
80  """Return true if device is on."""
81  return self._state_state
82 
83  def turn_on(self, **kwargs: Any) -> None:
84  """Turn the device on."""
85  write_output(self._switch_switch, 1)
86  self._state_state = True
87  self.schedule_update_ha_stateschedule_update_ha_state()
88 
89  def turn_off(self, **kwargs: Any) -> None:
90  """Turn the device off."""
91  write_output(self._switch_switch, 0)
92  self._state_state = False
93  self.schedule_update_ha_stateschedule_update_ha_state()
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:39
def setup_output(address, port, invert_logic)
Definition: __init__.py:17