Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for wired switches attached to a Konnected device."""
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.const import (
9  ATTR_STATE,
10  CONF_DEVICES,
11  CONF_NAME,
12  CONF_REPEAT,
13  CONF_SWITCHES,
14  CONF_ZONE,
15 )
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.device_registry import DeviceInfo
18 from homeassistant.helpers.dispatcher import async_dispatcher_connect
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import (
22  CONF_ACTIVATION,
23  CONF_MOMENTARY,
24  CONF_PAUSE,
25  DOMAIN as KONNECTED_DOMAIN,
26  STATE_HIGH,
27  STATE_LOW,
28 )
29 
30 _LOGGER = logging.getLogger(__name__)
31 
32 
34  hass: HomeAssistant,
35  config_entry: ConfigEntry,
36  async_add_entities: AddEntitiesCallback,
37 ) -> None:
38  """Set up switches attached to a Konnected device from a config entry."""
39  data = hass.data[KONNECTED_DOMAIN]
40  device_id = config_entry.data["id"]
41  switches = [
42  KonnectedSwitch(device_id, zone_data.get(CONF_ZONE), zone_data)
43  for zone_data in data[CONF_DEVICES][device_id][CONF_SWITCHES]
44  ]
45  async_add_entities(switches)
46 
47 
49  """Representation of a Konnected switch."""
50 
51  def __init__(self, device_id, zone_num, data):
52  """Initialize the Konnected switch."""
53  self._data_data = data
54  self._device_id_device_id = device_id
55  self._zone_num_zone_num = zone_num
56  self._activation_activation = self._data_data.get(CONF_ACTIVATION, STATE_HIGH)
57  self._momentary_momentary = self._data_data.get(CONF_MOMENTARY)
58  self._pause_pause = self._data_data.get(CONF_PAUSE)
59  self._repeat_repeat = self._data_data.get(CONF_REPEAT)
60  self._attr_is_on_attr_is_on = self._boolean_state_boolean_state(self._data_data.get(ATTR_STATE))
61  self._attr_name_attr_name = self._data_data.get(CONF_NAME)
62  self._attr_unique_id_attr_unique_id = (
63  f"{device_id}-{self._zone_num}-{self._momentary}-"
64  f"{self._pause}-{self._repeat}"
65  )
66  self._attr_device_info_attr_device_info = DeviceInfo(identifiers={(KONNECTED_DOMAIN, device_id)})
67 
68  @property
69  def panel(self):
70  """Return the Konnected HTTP client."""
71  device_data = self.hasshass.data[KONNECTED_DOMAIN][CONF_DEVICES][self._device_id_device_id]
72  return device_data.get("panel")
73 
74  @property
75  def available(self) -> bool:
76  """Return whether the panel is available."""
77  return self.panelpanel.available
78 
79  async def async_turn_on(self, **kwargs: Any) -> None:
80  """Send a command to turn on the switch."""
81  resp = await self.panelpanel.update_switch(
82  self._zone_num_zone_num,
83  int(self._activation_activation == STATE_HIGH),
84  self._momentary_momentary,
85  self._repeat_repeat,
86  self._pause_pause,
87  )
88 
89  if resp.get(ATTR_STATE) is not None:
90  self._set_state_set_state(True)
91 
92  if self._momentary_momentary and resp.get(ATTR_STATE) != -1:
93  # Immediately set the state back off for momentary switches
94  self._set_state_set_state(False)
95 
96  async def async_turn_off(self, **kwargs: Any) -> None:
97  """Send a command to turn off the switch."""
98  resp = await self.panelpanel.update_switch(
99  self._zone_num_zone_num, int(self._activation_activation == STATE_LOW)
100  )
101 
102  if resp.get(ATTR_STATE) is not None:
103  self._set_state_set_state(self._boolean_state_boolean_state(resp.get(ATTR_STATE)))
104 
105  def _boolean_state(self, int_state: int | None) -> bool | None:
106  if int_state == 0:
107  return self._activation_activation == STATE_LOW
108  if int_state == 1:
109  return self._activation_activation == STATE_HIGH
110  return None
111 
112  def _set_state(self, state):
113  self._attr_is_on_attr_is_on = state
114  self.async_write_ha_stateasync_write_ha_state()
115  _LOGGER.debug(
116  "Setting status of %s actuator zone %s to %s",
117  self._device_id_device_id,
118  self.namename,
119  state,
120  )
121 
122  @callback
123  def async_set_state(self, state):
124  """Update the switch state."""
125  self._set_state_set_state(state)
126 
127  async def async_added_to_hass(self) -> None:
128  """Store entity_id and register state change callback."""
129  self._data_data["entity_id"] = self.entity_identity_id
130  self.async_on_removeasync_on_remove(
132  self.hasshass, f"konnected.{self.entity_id}.update", self.async_set_stateasync_set_state
133  )
134  )
bool|None _boolean_state(self, int|None int_state)
Definition: switch.py:105
def __init__(self, device_id, zone_num, data)
Definition: switch.py:51
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
str|UndefinedType|None name(self)
Definition: entity.py:738
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:37
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103