Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for LiteJet switch."""
2 
3 from typing import Any
4 
5 from pylitejet import LiteJet, LiteJetError
6 
7 from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import HomeAssistantError
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .const import DOMAIN
15 
16 ATTR_NUMBER = "number"
17 
18 
20  hass: HomeAssistant,
21  config_entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up entry."""
25 
26  system: LiteJet = hass.data[DOMAIN]
27 
28  entities = []
29  for i in system.button_switches():
30  name = await system.get_switch_name(i)
31  entities.append(LiteJetSwitch(config_entry.entry_id, system, i, name))
32 
33  async_add_entities(entities, True)
34 
35 
37  """Representation of a single LiteJet switch."""
38 
39  _attr_should_poll = False
40  _attr_has_entity_name = True
41  _attr_entity_registry_enabled_default = False
42  _attr_device_class = SwitchDeviceClass.SWITCH
43 
44  def __init__(self, entry_id: str, system: LiteJet, i: int, name: str) -> None:
45  """Initialize a LiteJet switch."""
46  self._lj_lj = system
47  self._index_index = i
48  self._attr_is_on_attr_is_on = False
49  self._attr_unique_id_attr_unique_id = f"{entry_id}_{i}"
50  self._attr_name_attr_name = name
51 
52  # Keypad #1 has switches 1-6, #2 has 7-12, ...
53  keypad_number = system.get_switch_keypad_number(i)
54  self._attr_device_info_attr_device_info = DeviceInfo(
55  identifiers={(DOMAIN, f"{entry_id}_keypad_{keypad_number}")},
56  name=system.get_switch_keypad_name(i),
57  manufacturer="Centralite",
58  via_device=(DOMAIN, f"{entry_id}_mcp"),
59  )
60 
61  async def async_added_to_hass(self) -> None:
62  """Run when this Entity has been added to HA."""
63  self._lj_lj.on_switch_pressed(self._index_index, self._on_switch_pressed_on_switch_pressed)
64  self._lj_lj.on_switch_released(self._index_index, self._on_switch_released_on_switch_released)
65  self._lj_lj.on_connected_changed(self._on_connected_changed_on_connected_changed)
66 
67  async def async_will_remove_from_hass(self) -> None:
68  """Entity being removed from hass."""
69  self._lj_lj.unsubscribe(self._on_switch_pressed_on_switch_pressed)
70  self._lj_lj.unsubscribe(self._on_switch_released_on_switch_released)
71  self._lj_lj.unsubscribe(self._on_connected_changed_on_connected_changed)
72 
73  def _on_switch_pressed(self) -> None:
74  self._attr_is_on_attr_is_on = True
75  self.async_write_ha_stateasync_write_ha_state()
76 
77  def _on_switch_released(self) -> None:
78  self._attr_is_on_attr_is_on = False
79  self.async_write_ha_stateasync_write_ha_state()
80 
81  def _on_connected_changed(self, connected: bool, reason: str) -> None:
82  self._attr_available_attr_available = connected
83  self.async_write_ha_stateasync_write_ha_state()
84 
85  @property
86  def extra_state_attributes(self) -> dict[str, Any]:
87  """Return the device-specific state attributes."""
88  return {ATTR_NUMBER: self._index_index}
89 
90  async def async_turn_on(self, **kwargs: Any) -> None:
91  """Press the switch."""
92  try:
93  await self._lj_lj.press_switch(self._index_index)
94  except LiteJetError as exc:
95  raise HomeAssistantError from exc
96 
97  async def async_turn_off(self, **kwargs: Any) -> None:
98  """Release the switch."""
99  try:
100  await self._lj_lj.release_switch(self._index_index)
101  except LiteJetError as exc:
102  raise HomeAssistantError from exc
None _on_connected_changed(self, bool connected, str reason)
Definition: switch.py:81
None __init__(self, str entry_id, LiteJet system, int i, str name)
Definition: switch.py:44
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:23