Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for switches that can be controlled using the RaspyRFM rc module."""
2 
3 from __future__ import annotations
4 
5 from raspyrfm_client import RaspyRFMClient
6 from raspyrfm_client.device_implementations.controlunit.actions import Action
7 from raspyrfm_client.device_implementations.controlunit.controlunit_constants import (
8  ControlUnitModel,
9 )
10 from raspyrfm_client.device_implementations.gateway.manufacturer.gateway_constants import (
11  GatewayModel,
12 )
13 from raspyrfm_client.device_implementations.manufacturer_constants import Manufacturer
14 import voluptuous as vol
15 
17  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
18  SwitchEntity,
19 )
20 from homeassistant.const import (
21  CONF_HOST,
22  CONF_NAME,
23  CONF_PORT,
24  CONF_SWITCHES,
25  DEVICE_DEFAULT_NAME,
26 )
27 from homeassistant.core import HomeAssistant
29 from homeassistant.helpers.entity_platform import AddEntitiesCallback
30 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
31 
32 CONF_GATEWAY_MANUFACTURER = "gateway_manufacturer"
33 CONF_GATEWAY_MODEL = "gateway_model"
34 CONF_CONTROLUNIT_MANUFACTURER = "controlunit_manufacturer"
35 CONF_CONTROLUNIT_MODEL = "controlunit_model"
36 CONF_CHANNEL_CONFIG = "channel_config"
37 DEFAULT_HOST = "127.0.0.1"
38 
39 # define configuration parameters
40 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
41  {
42  vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
43  vol.Optional(CONF_PORT): cv.port,
44  vol.Optional(CONF_GATEWAY_MANUFACTURER): cv.string,
45  vol.Optional(CONF_GATEWAY_MODEL): cv.string,
46  vol.Required(CONF_SWITCHES): vol.Schema(
47  [
48  {
49  vol.Optional(CONF_NAME, default=DEVICE_DEFAULT_NAME): cv.string,
50  vol.Required(CONF_CONTROLUNIT_MANUFACTURER): cv.string,
51  vol.Required(CONF_CONTROLUNIT_MODEL): cv.string,
52  vol.Required(CONF_CHANNEL_CONFIG): {cv.string: cv.match_all},
53  }
54  ]
55  ),
56  },
57  extra=vol.ALLOW_EXTRA,
58 )
59 
60 
62  hass: HomeAssistant,
63  config: ConfigType,
64  add_entities: AddEntitiesCallback,
65  discovery_info: DiscoveryInfoType | None = None,
66 ) -> None:
67  """Set up the RaspyRFM switch."""
68 
69  gateway_manufacturer = config.get(
70  CONF_GATEWAY_MANUFACTURER, Manufacturer.SEEGEL_SYSTEME.value
71  )
72  gateway_model = config.get(CONF_GATEWAY_MODEL, GatewayModel.RASPYRFM.value)
73  host = config[CONF_HOST]
74  port = config.get(CONF_PORT)
75  switches = config[CONF_SWITCHES]
76 
77  raspyrfm_client = RaspyRFMClient()
78  gateway = raspyrfm_client.get_gateway(
79  Manufacturer(gateway_manufacturer), GatewayModel(gateway_model), host, port
80  )
81  switch_entities = []
82  for switch in switches:
83  name = switch[CONF_NAME]
84  controlunit_manufacturer = switch[CONF_CONTROLUNIT_MANUFACTURER]
85  controlunit_model = switch[CONF_CONTROLUNIT_MODEL]
86  channel_config = switch[CONF_CHANNEL_CONFIG]
87 
88  controlunit = raspyrfm_client.get_controlunit(
89  Manufacturer(controlunit_manufacturer), ControlUnitModel(controlunit_model)
90  )
91 
92  controlunit.set_channel_config(**channel_config)
93 
94  switch = RaspyRFMSwitch(raspyrfm_client, name, gateway, controlunit)
95  switch_entities.append(switch)
96 
97  add_entities(switch_entities)
98 
99 
101  """Representation of a RaspyRFM switch."""
102 
103  _attr_should_poll = False
104 
105  def __init__(self, raspyrfm_client, name: str, gateway, controlunit) -> None:
106  """Initialize the switch."""
107  self._raspyrfm_client_raspyrfm_client = raspyrfm_client
108 
109  self._name_name = name
110  self._gateway_gateway = gateway
111  self._controlunit_controlunit = controlunit
112 
113  self._state_state = None
114 
115  @property
116  def name(self):
117  """Return the name of the device if any."""
118  return self._name_name
119 
120  @property
121  def assumed_state(self):
122  """Return True when the current state cannot be queried."""
123  return True
124 
125  @property
126  def is_on(self):
127  """Return true if switch is on."""
128  return self._state_state
129 
130  def turn_on(self, **kwargs):
131  """Turn the switch on."""
132 
133  self._raspyrfm_client_raspyrfm_client.send(self._gateway_gateway, self._controlunit_controlunit, Action.ON)
134  self._state_state = True
135  self.schedule_update_ha_stateschedule_update_ha_state()
136 
137  def turn_off(self, **kwargs):
138  """Turn the switch off."""
139 
140  if Action.OFF in self._controlunit_controlunit.get_supported_actions():
141  self._raspyrfm_client_raspyrfm_client.send(self._gateway_gateway, self._controlunit_controlunit, Action.OFF)
142  else:
143  self._raspyrfm_client_raspyrfm_client.send(self._gateway_gateway, self._controlunit_controlunit, Action.ON)
144 
145  self._state_state = False
146  self.schedule_update_ha_stateschedule_update_ha_state()
None __init__(self, raspyrfm_client, str name, gateway, controlunit)
Definition: switch.py:105
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:66