Create KNX binary_sensor entities directly from config (#50708)

This commit is contained in:
Matthias Alphart 2021-05-17 15:33:09 +02:00 committed by GitHub
parent 9afa7df3c1
commit ac6d99d434
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 30 deletions

View File

@ -3,9 +3,11 @@ from __future__ import annotations
from typing import Any from typing import Any
from xknx import XKNX
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.const import CONF_DEVICE_CLASS, CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -13,6 +15,7 @@ from homeassistant.util import dt
from .const import ATTR_COUNTER, ATTR_LAST_KNX_UPDATE, ATTR_SOURCE, DOMAIN from .const import ATTR_COUNTER, ATTR_LAST_KNX_UPDATE, ATTR_SOURCE, DOMAIN
from .knx_entity import KnxEntity from .knx_entity import KnxEntity
from .schema import BinarySensorSchema
async def async_setup_platform( async def async_setup_platform(
@ -22,27 +25,47 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up binary sensor(s) for KNX platform.""" """Set up binary sensor(s) for KNX platform."""
if not discovery_info or not discovery_info["platform_config"]:
return
platform_config = discovery_info["platform_config"]
xknx: XKNX = hass.data[DOMAIN].xknx
entities = [] entities = []
for device in hass.data[DOMAIN].xknx.devices: for entity_config in platform_config:
if isinstance(device, XknxBinarySensor): entities.append(KNXBinarySensor(xknx, entity_config))
entities.append(KNXBinarySensor(device))
async_add_entities(entities) async_add_entities(entities)
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) -> None: def __init__(self, xknx: XKNX, config: ConfigType) -> None:
"""Initialize of KNX binary sensor.""" """Initialize of KNX binary sensor."""
self._device: XknxBinarySensor self._device: XknxBinarySensor
super().__init__(device) super().__init__(
device=XknxBinarySensor(
xknx,
name=config[CONF_NAME],
group_address_state=config[BinarySensorSchema.CONF_STATE_ADDRESS],
invert=config[BinarySensorSchema.CONF_INVERT],
sync_state=config[BinarySensorSchema.CONF_SYNC_STATE],
ignore_internal_state=config[
BinarySensorSchema.CONF_IGNORE_INTERNAL_STATE
],
context_timeout=config.get(BinarySensorSchema.CONF_CONTEXT_TIMEOUT),
reset_after=config.get(BinarySensorSchema.CONF_RESET_AFTER),
)
)
self._device_class: str | None = config.get(CONF_DEVICE_CLASS)
self._unique_id = f"{self._device.remote_value.group_address_state}" self._unique_id = f"{self._device.remote_value.group_address_state}"
@property @property
def device_class(self) -> str | None: 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_class in DEVICE_CLASSES:
return self._device.device_class return self._device_class
return None return None
@property @property

View File

@ -3,7 +3,6 @@ from __future__ import annotations
from xknx import XKNX from xknx import XKNX
from xknx.devices import ( from xknx.devices import (
BinarySensor as XknxBinarySensor,
Climate as XknxClimate, Climate as XknxClimate,
ClimateMode as XknxClimateMode, ClimateMode as XknxClimateMode,
Device as XknxDevice, Device as XknxDevice,
@ -11,11 +10,11 @@ from xknx.devices import (
Weather as XknxWeather, Weather as XknxWeather,
) )
from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME, CONF_TYPE from homeassistant.const import CONF_NAME, CONF_TYPE
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .const import SupportedPlatforms from .const import SupportedPlatforms
from .schema import BinarySensorSchema, ClimateSchema, SensorSchema, WeatherSchema from .schema import ClimateSchema, SensorSchema, WeatherSchema
def create_knx_device( def create_knx_device(
@ -30,9 +29,6 @@ def create_knx_device(
if platform is SupportedPlatforms.SENSOR: if platform is SupportedPlatforms.SENSOR:
return _create_sensor(knx_module, config) return _create_sensor(knx_module, config)
if platform is SupportedPlatforms.BINARY_SENSOR:
return _create_binary_sensor(knx_module, config)
if platform is SupportedPlatforms.WEATHER: if platform is SupportedPlatforms.WEATHER:
return _create_weather(knx_module, config) return _create_weather(knx_module, config)
@ -126,23 +122,6 @@ def _create_sensor(knx_module: XKNX, config: ConfigType) -> XknxSensor:
) )
def _create_binary_sensor(knx_module: XKNX, config: ConfigType) -> XknxBinarySensor:
"""Return a KNX binary sensor to be used within XKNX."""
device_name = config[CONF_NAME]
return XknxBinarySensor(
knx_module,
name=device_name,
group_address_state=config[BinarySensorSchema.CONF_STATE_ADDRESS],
invert=config[BinarySensorSchema.CONF_INVERT],
sync_state=config[BinarySensorSchema.CONF_SYNC_STATE],
device_class=config.get(CONF_DEVICE_CLASS),
ignore_internal_state=config[BinarySensorSchema.CONF_IGNORE_INTERNAL_STATE],
context_timeout=config.get(BinarySensorSchema.CONF_CONTEXT_TIMEOUT),
reset_after=config.get(BinarySensorSchema.CONF_RESET_AFTER),
)
def _create_weather(knx_module: XKNX, config: ConfigType) -> XknxWeather: def _create_weather(knx_module: XKNX, config: ConfigType) -> XknxWeather:
"""Return a KNX weather device to be used within XKNX.""" """Return a KNX weather device to be used within XKNX."""
return XknxWeather( return XknxWeather(