1 """Support for KNX/IP switches."""
3 from __future__
import annotations
7 from xknx.devices
import Switch
as XknxSwitch
9 from homeassistant
import config_entries
23 async_get_current_platform,
28 from .
import KNXModule
37 from .entity
import KnxUiEntity, KnxUiEntityPlatformController, KnxYamlEntity
38 from .schema
import SwitchSchema
39 from .storage.const
import (
51 async_add_entities: AddEntitiesCallback,
53 """Set up switch(es) for KNX platform."""
54 knx_module = hass.data[KNX_MODULE_KEY]
56 knx_module.config_store.add_platform(
57 platform=Platform.SWITCH,
59 knx_module=knx_module,
60 entity_platform=platform,
61 entity_class=KnxUiSwitch,
65 entities: list[KnxYamlEntity | KnxUiEntity] = []
66 if yaml_platform_config := knx_module.config_yaml.get(Platform.SWITCH):
69 for entity_config
in yaml_platform_config
71 if ui_config := knx_module.config_store.data[
"entities"].
get(Platform.SWITCH):
74 for unique_id, config
in ui_config.items()
81 """Base class for a KNX switch."""
86 """Restore last state."""
88 if not self._device.switch.readable
and (
91 if last_state.state
not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
92 self._device.switch.value = last_state.state == STATE_ON
95 def is_on(self) -> bool:
96 """Return true if device is on."""
97 return bool(self._device.state)
100 """Turn the device on."""
101 await self._device.set_on()
104 """Turn the device off."""
105 await self._device.set_off()
109 """Representation of a KNX switch configured from YAML."""
113 def __init__(self, knx_module: KNXModule, config: ConfigType) ->
None:
114 """Initialize of KNX switch."""
116 knx_module=knx_module,
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],
132 """Representation of a KNX switch configured from UI."""
137 self, knx_module: KNXModule, unique_id: str, config: dict[str, Any]
139 """Initialize KNX switch."""
141 knx_module=knx_module,
143 entity_config=config[CONF_ENTITY],
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],
153 respond_to_read=config[DOMAIN][CONF_RESPOND_TO_READ],
154 sync_state=config[DOMAIN][CONF_SYNC_STATE],
155 invert=config[DOMAIN][CONF_INVERT],
None __init__(self, KNXModule knx_module, str unique_id, dict[str, Any] config)
None __init__(self, KNXModule knx_module, ConfigType config)
None async_added_to_hass(self)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
State|None async_get_last_state(self)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)