Type check KNX integration binary_sensor, sensor and switch (#48050)

This commit is contained in:
Matthias Alphart 2021-03-19 10:22:18 +01:00 committed by GitHub
parent fb1e76db8c
commit e522b311ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 17 deletions

View File

@ -1,17 +1,28 @@
"""Support for KNX/IP binary sensors.""" """Support for KNX/IP binary sensors."""
from __future__ import annotations from __future__ import annotations
from typing import Any from typing import Any, Callable, Iterable
from xknx.devices import BinarySensor as XknxBinarySensor from xknx.devices import BinarySensor as XknxBinarySensor
from homeassistant.components.binary_sensor import DEVICE_CLASSES, BinarySensorEntity from homeassistant.components.binary_sensor import DEVICE_CLASSES, BinarySensorEntity
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import (
ConfigType,
DiscoveryInfoType,
HomeAssistantType,
)
from .const import ATTR_COUNTER, DOMAIN from .const import ATTR_COUNTER, DOMAIN
from .knx_entity import KnxEntity from .knx_entity import KnxEntity
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(
hass: HomeAssistantType,
config: ConfigType,
async_add_entities: Callable[[Iterable[Entity]], None],
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up binary sensor(s) for KNX platform.""" """Set up binary sensor(s) for KNX platform."""
entities = [] entities = []
for device in hass.data[DOMAIN].xknx.devices: for device in hass.data[DOMAIN].xknx.devices:
@ -23,19 +34,20 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class KNXBinarySensor(KnxEntity, BinarySensorEntity): class KNXBinarySensor(KnxEntity, BinarySensorEntity):
"""Representation of a KNX binary sensor.""" """Representation of a KNX binary sensor."""
def __init__(self, device: XknxBinarySensor): def __init__(self, device: XknxBinarySensor) -> None:
"""Initialize of KNX binary sensor.""" """Initialize of KNX binary sensor."""
self._device: XknxBinarySensor
super().__init__(device) super().__init__(device)
@property @property
def device_class(self): def device_class(self) -> str | None:
"""Return the class of this sensor.""" """Return the class of this sensor."""
if self._device.device_class in DEVICE_CLASSES: if self._device.device_class in DEVICE_CLASSES:
return self._device.device_class return self._device.device_class
return None return None
@property @property
def is_on(self): def is_on(self) -> bool:
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
return self._device.is_on() return self._device.is_on()

View File

@ -1,14 +1,29 @@
"""Support for KNX/IP sensors.""" """Support for KNX/IP sensors."""
from __future__ import annotations
from typing import Callable, Iterable
from xknx.devices import Sensor as XknxSensor from xknx.devices import Sensor as XknxSensor
from homeassistant.components.sensor import DEVICE_CLASSES from homeassistant.components.sensor import DEVICE_CLASSES
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import (
ConfigType,
DiscoveryInfoType,
HomeAssistantType,
StateType,
)
from .const import DOMAIN from .const import DOMAIN
from .knx_entity import KnxEntity from .knx_entity import KnxEntity
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(
hass: HomeAssistantType,
config: ConfigType,
async_add_entities: Callable[[Iterable[Entity]], None],
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up sensor(s) for KNX platform.""" """Set up sensor(s) for KNX platform."""
entities = [] entities = []
for device in hass.data[DOMAIN].xknx.devices: for device in hass.data[DOMAIN].xknx.devices:
@ -20,22 +35,23 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class KNXSensor(KnxEntity, Entity): class KNXSensor(KnxEntity, Entity):
"""Representation of a KNX sensor.""" """Representation of a KNX sensor."""
def __init__(self, device: XknxSensor): def __init__(self, device: XknxSensor) -> None:
"""Initialize of a KNX sensor.""" """Initialize of a KNX sensor."""
self._device: XknxSensor
super().__init__(device) super().__init__(device)
@property @property
def state(self): def state(self) -> StateType:
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._device.resolve_state() return self._device.resolve_state()
@property @property
def unit_of_measurement(self) -> str: def unit_of_measurement(self) -> str | None:
"""Return the unit this state is expressed in.""" """Return the unit this state is expressed in."""
return self._device.unit_of_measurement() return self._device.unit_of_measurement()
@property @property
def device_class(self): def device_class(self) -> str | None:
"""Return the device class of the sensor.""" """Return the device class of the sensor."""
device_class = self._device.ha_device_class() device_class = self._device.ha_device_class()
if device_class in DEVICE_CLASSES: if device_class in DEVICE_CLASSES:

View File

@ -1,13 +1,28 @@
"""Support for KNX/IP switches.""" """Support for KNX/IP switches."""
from __future__ import annotations
from typing import Any, Callable, Iterable
from xknx.devices import Switch as XknxSwitch from xknx.devices import Switch as XknxSwitch
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import (
ConfigType,
DiscoveryInfoType,
HomeAssistantType,
)
from . import DOMAIN from .const import DOMAIN
from .knx_entity import KnxEntity from .knx_entity import KnxEntity
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(
hass: HomeAssistantType,
config: ConfigType,
async_add_entities: Callable[[Iterable[Entity]], None],
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up switch(es) for KNX platform.""" """Set up switch(es) for KNX platform."""
entities = [] entities = []
for device in hass.data[DOMAIN].xknx.devices: for device in hass.data[DOMAIN].xknx.devices:
@ -19,19 +34,20 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class KNXSwitch(KnxEntity, SwitchEntity): class KNXSwitch(KnxEntity, SwitchEntity):
"""Representation of a KNX switch.""" """Representation of a KNX switch."""
def __init__(self, device: XknxSwitch): def __init__(self, device: XknxSwitch) -> None:
"""Initialize of KNX switch.""" """Initialize of KNX switch."""
self._device: XknxSwitch
super().__init__(device) super().__init__(device)
@property @property
def is_on(self): def is_on(self) -> bool:
"""Return true if device is on.""" """Return true if device is on."""
return self._device.state return bool(self._device.state)
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the device on.""" """Turn the device on."""
await self._device.set_on() await self._device.set_on()
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off.""" """Turn the device off."""
await self._device.set_off() await self._device.set_off()