Create KNX cover entities directly from config (#50707)

This commit is contained in:
Matthias Alphart 2021-05-17 12:17:19 +02:00 committed by GitHub
parent 9ee3b77135
commit 1c7242a37a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 47 deletions

View file

@ -4,6 +4,7 @@ from __future__ import annotations
from datetime import datetime from datetime import datetime
from typing import Any, Callable from typing import Any, Callable
from xknx import XKNX
from xknx.devices import Cover as XknxCover, Device as XknxDevice from xknx.devices import Cover as XknxCover, Device as XknxDevice
from xknx.telegram.address import parse_device_group_address from xknx.telegram.address import parse_device_group_address
@ -22,6 +23,7 @@ from homeassistant.components.cover import (
SUPPORT_STOP_TILT, SUPPORT_STOP_TILT,
CoverEntity, CoverEntity,
) )
from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -40,24 +42,26 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up cover(s) for KNX platform.""" """Set up cover(s) for KNX platform."""
_async_migrate_unique_id(hass, discovery_info) if not discovery_info or not discovery_info["platform_config"]:
return
platform_config = discovery_info["platform_config"]
_async_migrate_unique_id(hass, 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, XknxCover): entities.append(KNXCover(xknx, entity_config))
entities.append(KNXCover(device))
async_add_entities(entities) async_add_entities(entities)
@callback @callback
def _async_migrate_unique_id( def _async_migrate_unique_id(
hass: HomeAssistant, discovery_info: DiscoveryInfoType | None hass: HomeAssistant, platform_config: list[ConfigType]
) -> None: ) -> None:
"""Change unique_ids used in 2021.4 to include position_target GA.""" """Change unique_ids used in 2021.4 to include position_target GA."""
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
if not discovery_info or not discovery_info["platform_config"]:
return
platform_config = discovery_info["platform_config"]
for entity_config in platform_config: for entity_config in platform_config:
# normalize group address strings - ga_updown was the old uid but is optional # normalize group address strings - ga_updown was the old uid but is optional
updown_addresses = entity_config.get(CoverSchema.CONF_MOVE_LONG_ADDRESS) updown_addresses = entity_config.get(CoverSchema.CONF_MOVE_LONG_ADDRESS)
@ -82,12 +86,34 @@ def _async_migrate_unique_id(
class KNXCover(KnxEntity, CoverEntity): class KNXCover(KnxEntity, CoverEntity):
"""Representation of a KNX cover.""" """Representation of a KNX cover."""
def __init__(self, device: XknxCover): def __init__(self, xknx: XKNX, config: ConfigType):
"""Initialize the cover.""" """Initialize the cover."""
self._device: XknxCover self._device: XknxCover
super().__init__(device) super().__init__(
device=XknxCover(
xknx,
name=config[CONF_NAME],
group_address_long=config.get(CoverSchema.CONF_MOVE_LONG_ADDRESS),
group_address_short=config.get(CoverSchema.CONF_MOVE_SHORT_ADDRESS),
group_address_stop=config.get(CoverSchema.CONF_STOP_ADDRESS),
group_address_position_state=config.get(
CoverSchema.CONF_POSITION_STATE_ADDRESS
),
group_address_angle=config.get(CoverSchema.CONF_ANGLE_ADDRESS),
group_address_angle_state=config.get(
CoverSchema.CONF_ANGLE_STATE_ADDRESS
),
group_address_position=config.get(CoverSchema.CONF_POSITION_ADDRESS),
travel_time_down=config[CoverSchema.CONF_TRAVELLING_TIME_DOWN],
travel_time_up=config[CoverSchema.CONF_TRAVELLING_TIME_UP],
invert_position=config[CoverSchema.CONF_INVERT_POSITION],
invert_angle=config[CoverSchema.CONF_INVERT_ANGLE],
)
)
self._device_class: str | None = config.get(CONF_DEVICE_CLASS)
self._unique_id = ( self._unique_id = (
f"{device.updown.group_address}_{device.position_target.group_address}" f"{self._device.updown.group_address}_"
f"{self._device.position_target.group_address}"
) )
self._unsubscribe_auto_updater: Callable[[], None] | None = None self._unsubscribe_auto_updater: Callable[[], None] | None = None
@ -101,8 +127,8 @@ class KNXCover(KnxEntity, CoverEntity):
@property @property
def device_class(self) -> str | None: def device_class(self) -> str | None:
"""Return the class of this device, from component DEVICE_CLASSES.""" """Return the class of this device, from component DEVICE_CLASSES."""
if self._device.device_class in DEVICE_CLASSES: if self._device_class in DEVICE_CLASSES:
return self._device.device_class return self._device_class
if self._device.supports_angle: if self._device.supports_angle:
return DEVICE_CLASS_BLIND return DEVICE_CLASS_BLIND
return None return None

View file

@ -6,7 +6,6 @@ from xknx.devices import (
BinarySensor as XknxBinarySensor, BinarySensor as XknxBinarySensor,
Climate as XknxClimate, Climate as XknxClimate,
ClimateMode as XknxClimateMode, ClimateMode as XknxClimateMode,
Cover as XknxCover,
Device as XknxDevice, Device as XknxDevice,
Sensor as XknxSensor, Sensor as XknxSensor,
Weather as XknxWeather, Weather as XknxWeather,
@ -16,13 +15,7 @@ from homeassistant.const import CONF_DEVICE_CLASS, 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 ( from .schema import BinarySensorSchema, ClimateSchema, SensorSchema, WeatherSchema
BinarySensorSchema,
ClimateSchema,
CoverSchema,
SensorSchema,
WeatherSchema,
)
def create_knx_device( def create_knx_device(
@ -31,9 +24,6 @@ def create_knx_device(
config: ConfigType, config: ConfigType,
) -> XknxDevice | None: ) -> XknxDevice | None:
"""Return the requested XKNX device.""" """Return the requested XKNX device."""
if platform is SupportedPlatforms.COVER:
return _create_cover(knx_module, config)
if platform is SupportedPlatforms.CLIMATE: if platform is SupportedPlatforms.CLIMATE:
return _create_climate(knx_module, config) return _create_climate(knx_module, config)
@ -49,28 +39,6 @@ def create_knx_device(
return None return None
def _create_cover(knx_module: XKNX, config: ConfigType) -> XknxCover:
"""Return a KNX Cover device to be used within XKNX."""
return XknxCover(
knx_module,
name=config[CONF_NAME],
group_address_long=config.get(CoverSchema.CONF_MOVE_LONG_ADDRESS),
group_address_short=config.get(CoverSchema.CONF_MOVE_SHORT_ADDRESS),
group_address_stop=config.get(CoverSchema.CONF_STOP_ADDRESS),
group_address_position_state=config.get(
CoverSchema.CONF_POSITION_STATE_ADDRESS
),
group_address_angle=config.get(CoverSchema.CONF_ANGLE_ADDRESS),
group_address_angle_state=config.get(CoverSchema.CONF_ANGLE_STATE_ADDRESS),
group_address_position=config.get(CoverSchema.CONF_POSITION_ADDRESS),
travel_time_down=config[CoverSchema.CONF_TRAVELLING_TIME_DOWN],
travel_time_up=config[CoverSchema.CONF_TRAVELLING_TIME_UP],
invert_position=config[CoverSchema.CONF_INVERT_POSITION],
invert_angle=config[CoverSchema.CONF_INVERT_ANGLE],
device_class=config.get(CONF_DEVICE_CLASS),
)
def _create_climate(knx_module: XKNX, config: ConfigType) -> XknxClimate: def _create_climate(knx_module: XKNX, config: ConfigType) -> XknxClimate:
"""Return a KNX Climate device to be used within XKNX.""" """Return a KNX Climate device to be used within XKNX."""
climate_mode = XknxClimateMode( climate_mode = XknxClimateMode(