Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for KNX/IP switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from xknx.devices import Switch as XknxSwitch
8 
9 from homeassistant import config_entries
10 from homeassistant.components.switch import SwitchEntity
11 from homeassistant.const import (
12  CONF_DEVICE_CLASS,
13  CONF_ENTITY_CATEGORY,
14  CONF_NAME,
15  STATE_ON,
16  STATE_UNAVAILABLE,
17  STATE_UNKNOWN,
18  Platform,
19 )
20 from homeassistant.core import HomeAssistant
22  AddEntitiesCallback,
23  async_get_current_platform,
24 )
25 from homeassistant.helpers.restore_state import RestoreEntity
26 from homeassistant.helpers.typing import ConfigType
27 
28 from . import KNXModule
29 from .const import (
30  CONF_INVERT,
31  CONF_RESPOND_TO_READ,
32  CONF_SYNC_STATE,
33  DOMAIN,
34  KNX_ADDRESS,
35  KNX_MODULE_KEY,
36 )
37 from .entity import KnxUiEntity, KnxUiEntityPlatformController, KnxYamlEntity
38 from .schema import SwitchSchema
39 from .storage.const import (
40  CONF_ENTITY,
41  CONF_GA_PASSIVE,
42  CONF_GA_STATE,
43  CONF_GA_SWITCH,
44  CONF_GA_WRITE,
45 )
46 
47 
49  hass: HomeAssistant,
50  config_entry: config_entries.ConfigEntry,
51  async_add_entities: AddEntitiesCallback,
52 ) -> None:
53  """Set up switch(es) for KNX platform."""
54  knx_module = hass.data[KNX_MODULE_KEY]
55  platform = async_get_current_platform()
56  knx_module.config_store.add_platform(
57  platform=Platform.SWITCH,
59  knx_module=knx_module,
60  entity_platform=platform,
61  entity_class=KnxUiSwitch,
62  ),
63  )
64 
65  entities: list[KnxYamlEntity | KnxUiEntity] = []
66  if yaml_platform_config := knx_module.config_yaml.get(Platform.SWITCH):
67  entities.extend(
68  KnxYamlSwitch(knx_module, entity_config)
69  for entity_config in yaml_platform_config
70  )
71  if ui_config := knx_module.config_store.data["entities"].get(Platform.SWITCH):
72  entities.extend(
73  KnxUiSwitch(knx_module, unique_id, config)
74  for unique_id, config in ui_config.items()
75  )
76  if entities:
77  async_add_entities(entities)
78 
79 
81  """Base class for a KNX switch."""
82 
83  _device: XknxSwitch
84 
85  async def async_added_to_hass(self) -> None:
86  """Restore last state."""
87  await super().async_added_to_hass()
88  if not self._device.switch.readable and (
89  last_state := await self.async_get_last_stateasync_get_last_state()
90  ):
91  if last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
92  self._device.switch.value = last_state.state == STATE_ON
93 
94  @property
95  def is_on(self) -> bool:
96  """Return true if device is on."""
97  return bool(self._device.state)
98 
99  async def async_turn_on(self, **kwargs: Any) -> None:
100  """Turn the device on."""
101  await self._device.set_on()
102 
103  async def async_turn_off(self, **kwargs: Any) -> None:
104  """Turn the device off."""
105  await self._device.set_off()
106 
107 
109  """Representation of a KNX switch configured from YAML."""
110 
111  _device: XknxSwitch
112 
113  def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
114  """Initialize of KNX switch."""
115  super().__init__(
116  knx_module=knx_module,
117  device=XknxSwitch(
118  xknx=knx_module.xknx,
119  name=config[CONF_NAME],
120  group_address=config[KNX_ADDRESS],
121  group_address_state=config.get(SwitchSchema.CONF_STATE_ADDRESS),
122  respond_to_read=config[CONF_RESPOND_TO_READ],
123  invert=config[SwitchSchema.CONF_INVERT],
124  ),
125  )
126  self._attr_entity_category_attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
127  self._attr_device_class_attr_device_class = config.get(CONF_DEVICE_CLASS)
128  self._attr_unique_id_attr_unique_id = str(self._device_device.switch.group_address)
129 
130 
132  """Representation of a KNX switch configured from UI."""
133 
134  _device: XknxSwitch
135 
136  def __init__(
137  self, knx_module: KNXModule, unique_id: str, config: dict[str, Any]
138  ) -> None:
139  """Initialize KNX switch."""
140  super().__init__(
141  knx_module=knx_module,
142  unique_id=unique_id,
143  entity_config=config[CONF_ENTITY],
144  )
145  self._device_device = XknxSwitch(
146  knx_module.xknx,
147  name=config[CONF_ENTITY][CONF_NAME],
148  group_address=config[DOMAIN][CONF_GA_SWITCH][CONF_GA_WRITE],
149  group_address_state=[
150  config[DOMAIN][CONF_GA_SWITCH][CONF_GA_STATE],
151  *config[DOMAIN][CONF_GA_SWITCH][CONF_GA_PASSIVE],
152  ],
153  respond_to_read=config[DOMAIN][CONF_RESPOND_TO_READ],
154  sync_state=config[DOMAIN][CONF_SYNC_STATE],
155  invert=config[DOMAIN][CONF_INVERT],
156  )
None __init__(self, KNXModule knx_module, str unique_id, dict[str, Any] config)
Definition: switch.py:138
None __init__(self, KNXModule knx_module, ConfigType config)
Definition: switch.py:113
None async_turn_off(self, **Any kwargs)
Definition: entity.py:1709
None async_turn_on(self, **Any kwargs)
Definition: entity.py:1701
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:52