2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP switches."""
|
2024-03-08 14:01:29 +00:00
|
|
|
|
2021-03-19 09:22:18 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-04 12:50:06 +00:00
|
|
|
from typing import Any
|
2021-03-19 09:22:18 +00:00
|
|
|
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx.devices import Switch as XknxSwitch
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
from homeassistant import config_entries
|
2020-08-26 16:03:03 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-10-25 03:12:26 +00:00
|
|
|
from homeassistant.const import (
|
2022-10-26 19:04:11 +00:00
|
|
|
CONF_DEVICE_CLASS,
|
2021-10-25 03:12:26 +00:00
|
|
|
CONF_ENTITY_CATEGORY,
|
|
|
|
CONF_NAME,
|
|
|
|
STATE_ON,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
STATE_UNKNOWN,
|
2021-12-03 17:29:38 +00:00
|
|
|
Platform,
|
2021-10-25 03:12:26 +00:00
|
|
|
)
|
2024-08-05 10:34:48 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-07-21 18:01:48 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2024-08-05 10:34:48 +00:00
|
|
|
from homeassistant.helpers.entity_platform import (
|
|
|
|
AddEntitiesCallback,
|
|
|
|
async_get_current_platform,
|
|
|
|
)
|
2021-06-15 11:08:19 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2021-11-20 10:30:41 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
2024-07-21 18:01:48 +00:00
|
|
|
from . import KNXModule
|
|
|
|
from .const import (
|
|
|
|
CONF_INVERT,
|
|
|
|
CONF_RESPOND_TO_READ,
|
2024-07-22 07:35:29 +00:00
|
|
|
CONF_SYNC_STATE,
|
2024-07-21 18:01:48 +00:00
|
|
|
DOMAIN,
|
|
|
|
KNX_ADDRESS,
|
2024-09-09 07:01:21 +00:00
|
|
|
KNX_MODULE_KEY,
|
2024-07-21 18:01:48 +00:00
|
|
|
)
|
2024-08-05 10:34:48 +00:00
|
|
|
from .knx_entity import KnxUiEntity, KnxUiEntityPlatformController, KnxYamlEntity
|
2021-04-15 07:47:43 +00:00
|
|
|
from .schema import SwitchSchema
|
2024-07-21 18:01:48 +00:00
|
|
|
from .storage.const import (
|
|
|
|
CONF_DEVICE_INFO,
|
|
|
|
CONF_ENTITY,
|
|
|
|
CONF_GA_PASSIVE,
|
|
|
|
CONF_GA_STATE,
|
|
|
|
CONF_GA_SWITCH,
|
|
|
|
CONF_GA_WRITE,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-09-14 06:03:30 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
async def async_setup_entry(
|
2021-03-27 21:20:11 +00:00
|
|
|
hass: HomeAssistant,
|
2021-11-20 10:30:41 +00:00
|
|
|
config_entry: config_entries.ConfigEntry,
|
2021-05-04 12:50:06 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-03-19 09:22:18 +00:00
|
|
|
) -> None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Set up switch(es) for KNX platform."""
|
2024-09-09 07:01:21 +00:00
|
|
|
knx_module = hass.data[KNX_MODULE_KEY]
|
2024-08-05 10:34:48 +00:00
|
|
|
platform = async_get_current_platform()
|
|
|
|
knx_module.config_store.add_platform(
|
|
|
|
platform=Platform.SWITCH,
|
|
|
|
controller=KnxUiEntityPlatformController(
|
|
|
|
knx_module=knx_module,
|
|
|
|
entity_platform=platform,
|
|
|
|
entity_class=KnxUiSwitch,
|
|
|
|
),
|
|
|
|
)
|
2021-04-15 07:47:43 +00:00
|
|
|
|
2024-08-05 10:34:48 +00:00
|
|
|
entities: list[KnxYamlEntity | KnxUiEntity] = []
|
2024-09-09 07:01:21 +00:00
|
|
|
if yaml_platform_config := knx_module.config_yaml.get(Platform.SWITCH):
|
2024-07-21 18:01:48 +00:00
|
|
|
entities.extend(
|
2024-08-02 10:53:39 +00:00
|
|
|
KnxYamlSwitch(knx_module, entity_config)
|
|
|
|
for entity_config in yaml_platform_config
|
2024-07-21 18:01:48 +00:00
|
|
|
)
|
|
|
|
if ui_config := knx_module.config_store.data["entities"].get(Platform.SWITCH):
|
|
|
|
entities.extend(
|
|
|
|
KnxUiSwitch(knx_module, unique_id, config)
|
|
|
|
for unique_id, config in ui_config.items()
|
|
|
|
)
|
|
|
|
if entities:
|
|
|
|
async_add_entities(entities)
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2021-06-26 12:30:36 +00:00
|
|
|
|
2024-08-02 10:53:39 +00:00
|
|
|
class _KnxSwitch(SwitchEntity, RestoreEntity):
|
2024-07-21 18:01:48 +00:00
|
|
|
"""Base class for a KNX switch."""
|
|
|
|
|
|
|
|
_device: XknxSwitch
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-06-15 11:08:19 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Restore last state."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
if not self._device.switch.readable and (
|
|
|
|
last_state := await self.async_get_last_state()
|
|
|
|
):
|
|
|
|
if last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
|
|
|
|
self._device.switch.value = last_state.state == STATE_ON
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
2021-03-19 09:22:18 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return true if device is on."""
|
2021-03-19 09:22:18 +00:00
|
|
|
return bool(self._device.state)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-03-19 09:22:18 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Turn the device on."""
|
2020-09-21 16:08:35 +00:00
|
|
|
await self._device.set_on()
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-03-19 09:22:18 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Turn the device off."""
|
2020-09-21 16:08:35 +00:00
|
|
|
await self._device.set_off()
|
2024-07-21 18:01:48 +00:00
|
|
|
|
|
|
|
|
2024-08-05 10:34:48 +00:00
|
|
|
class KnxYamlSwitch(_KnxSwitch, KnxYamlEntity):
|
2024-07-21 18:01:48 +00:00
|
|
|
"""Representation of a KNX switch configured from YAML."""
|
|
|
|
|
2024-08-02 10:53:39 +00:00
|
|
|
_device: XknxSwitch
|
|
|
|
|
|
|
|
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
|
2024-07-21 18:01:48 +00:00
|
|
|
"""Initialize of KNX switch."""
|
|
|
|
super().__init__(
|
2024-08-02 10:53:39 +00:00
|
|
|
knx_module=knx_module,
|
2024-07-21 18:01:48 +00:00
|
|
|
device=XknxSwitch(
|
2024-08-02 10:53:39 +00:00
|
|
|
xknx=knx_module.xknx,
|
2024-07-21 18:01:48 +00:00
|
|
|
name=config[CONF_NAME],
|
|
|
|
group_address=config[KNX_ADDRESS],
|
|
|
|
group_address_state=config.get(SwitchSchema.CONF_STATE_ADDRESS),
|
|
|
|
respond_to_read=config[CONF_RESPOND_TO_READ],
|
|
|
|
invert=config[SwitchSchema.CONF_INVERT],
|
2024-08-02 10:53:39 +00:00
|
|
|
),
|
2024-07-21 18:01:48 +00:00
|
|
|
)
|
|
|
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
|
|
|
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
|
|
|
self._attr_unique_id = str(self._device.switch.group_address)
|
|
|
|
|
|
|
|
|
2024-08-05 10:34:48 +00:00
|
|
|
class KnxUiSwitch(_KnxSwitch, KnxUiEntity):
|
2024-07-21 18:01:48 +00:00
|
|
|
"""Representation of a KNX switch configured from UI."""
|
|
|
|
|
|
|
|
_attr_has_entity_name = True
|
2024-08-02 10:53:39 +00:00
|
|
|
_device: XknxSwitch
|
2024-07-21 18:01:48 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, knx_module: KNXModule, unique_id: str, config: dict[str, Any]
|
|
|
|
) -> None:
|
2024-08-05 10:34:48 +00:00
|
|
|
"""Initialize KNX switch."""
|
|
|
|
self._knx_module = knx_module
|
|
|
|
self._device = XknxSwitch(
|
|
|
|
knx_module.xknx,
|
|
|
|
name=config[CONF_ENTITY][CONF_NAME],
|
|
|
|
group_address=config[DOMAIN][CONF_GA_SWITCH][CONF_GA_WRITE],
|
|
|
|
group_address_state=[
|
|
|
|
config[DOMAIN][CONF_GA_SWITCH][CONF_GA_STATE],
|
|
|
|
*config[DOMAIN][CONF_GA_SWITCH][CONF_GA_PASSIVE],
|
|
|
|
],
|
|
|
|
respond_to_read=config[DOMAIN][CONF_RESPOND_TO_READ],
|
|
|
|
sync_state=config[DOMAIN][CONF_SYNC_STATE],
|
|
|
|
invert=config[DOMAIN][CONF_INVERT],
|
2024-07-21 18:01:48 +00:00
|
|
|
)
|
|
|
|
self._attr_entity_category = config[CONF_ENTITY][CONF_ENTITY_CATEGORY]
|
|
|
|
self._attr_unique_id = unique_id
|
|
|
|
if device_info := config[CONF_ENTITY].get(CONF_DEVICE_INFO):
|
|
|
|
self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, device_info)})
|