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-04-20 15:40:41 +00:00
|
|
|
from collections.abc import Iterable
|
|
|
|
from typing import Any, Callable
|
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
|
|
|
|
2020-08-26 16:03:03 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-04-15 07:47:43 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-03-19 09:22:18 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2021-03-19 09:22:18 +00:00
|
|
|
|
2021-04-15 07:47:43 +00:00
|
|
|
from .const import 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-03-19 09:22:18 +00:00
|
|
|
async def async_setup_platform(
|
2021-03-27 21:20:11 +00:00
|
|
|
hass: HomeAssistant,
|
2021-03-19 09:22:18 +00:00
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: Callable[[Iterable[Entity]], None],
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Set up switch(es) for KNX platform."""
|
2021-04-15 07:47:43 +00:00
|
|
|
if not discovery_info or not discovery_info["platform_config"]:
|
|
|
|
return
|
|
|
|
|
|
|
|
platform_config = discovery_info["platform_config"]
|
|
|
|
xknx: XKNX = hass.data[DOMAIN].xknx
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
entities = []
|
2021-04-15 07:47:43 +00:00
|
|
|
for entity_config in platform_config:
|
|
|
|
entities.append(KNXSwitch(xknx, entity_config))
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities)
|
2016-07-10 17:36:54 +00:00
|
|
|
|
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
class KNXSwitch(KnxEntity, SwitchEntity):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Representation of a KNX switch."""
|
|
|
|
|
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-03-19 09:22:18 +00:00
|
|
|
self._device: XknxSwitch
|
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),
|
|
|
|
invert=config[SwitchSchema.CONF_INVERT],
|
|
|
|
)
|
|
|
|
)
|
2021-04-28 13:50:01 +00:00
|
|
|
self._unique_id = f"{self._device.switch.group_address}"
|
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()
|