2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP switches."""
|
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
|
|
|
|
2021-04-15 07:47:43 +00:00
|
|
|
from xknx import XKNX
|
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
|
|
|
)
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 12:50:06 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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
|
|
|
|
|
2021-12-03 17:29:38 +00:00
|
|
|
from .const import CONF_RESPOND_TO_READ, DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS
|
2020-09-21 16:08:35 +00:00
|
|
|
from .knx_entity import KnxEntity
|
2021-04-15 07:47:43 +00:00
|
|
|
from .schema import SwitchSchema
|
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."""
|
2021-04-15 07:47:43 +00:00
|
|
|
xknx: XKNX = hass.data[DOMAIN].xknx
|
2021-12-03 17:29:38 +00:00
|
|
|
config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.SWITCH]
|
2021-04-15 07:47:43 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
async_add_entities(KNXSwitch(xknx, entity_config) for entity_config in config)
|
2016-07-10 17:36:54 +00:00
|
|
|
|
|
|
|
|
2021-06-15 11:08:19 +00:00
|
|
|
class KNXSwitch(KnxEntity, SwitchEntity, RestoreEntity):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Representation of a KNX switch."""
|
|
|
|
|
2021-06-26 12:30:36 +00:00
|
|
|
_device: XknxSwitch
|
|
|
|
|
2021-04-15 07:47:43 +00:00
|
|
|
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of KNX switch."""
|
2021-04-15 07:47:43 +00:00
|
|
|
super().__init__(
|
|
|
|
device=XknxSwitch(
|
|
|
|
xknx,
|
|
|
|
name=config[CONF_NAME],
|
|
|
|
group_address=config[KNX_ADDRESS],
|
|
|
|
group_address_state=config.get(SwitchSchema.CONF_STATE_ADDRESS),
|
2021-06-27 16:34:41 +00:00
|
|
|
respond_to_read=config[CONF_RESPOND_TO_READ],
|
2021-04-15 07:47:43 +00:00
|
|
|
invert=config[SwitchSchema.CONF_INVERT],
|
|
|
|
)
|
|
|
|
)
|
2021-10-25 03:12:26 +00:00
|
|
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
2022-10-26 19:04:11 +00:00
|
|
|
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
2021-06-26 12:30:36 +00:00
|
|
|
self._attr_unique_id = str(self._device.switch.group_address)
|
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()
|