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
|
|
|
|
|
|
|
|
from typing import Any, Callable, Iterable
|
|
|
|
|
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-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
|
|
|
|
|
|
|
from .const import DOMAIN
|
2020-09-21 16:08:35 +00:00
|
|
|
from .knx_entity import KnxEntity
|
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."""
|
|
|
|
entities = []
|
2020-09-21 16:08:35 +00:00
|
|
|
for device in hass.data[DOMAIN].xknx.devices:
|
2020-08-30 18:13:47 +00:00
|
|
|
if isinstance(device, XknxSwitch):
|
|
|
|
entities.append(KNXSwitch(device))
|
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-03-19 09:22:18 +00:00
|
|
|
def __init__(self, device: XknxSwitch) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of KNX switch."""
|
2021-03-19 09:22:18 +00:00
|
|
|
self._device: XknxSwitch
|
2020-09-21 16:08:35 +00:00
|
|
|
super().__init__(device)
|
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()
|