Create KNX notify entities directly from config (#50709)

This commit is contained in:
Matthias Alphart 2021-05-16 12:07:44 +02:00 committed by GitHub
parent c2e2b046d9
commit 3200b0150a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 17 deletions

View File

@ -9,7 +9,6 @@ from xknx.devices import (
Cover as XknxCover, Cover as XknxCover,
Device as XknxDevice, Device as XknxDevice,
Light as XknxLight, Light as XknxLight,
Notification as XknxNotification,
Sensor as XknxSensor, Sensor as XknxSensor,
Weather as XknxWeather, Weather as XknxWeather,
) )
@ -46,9 +45,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.NOTIFY:
return _create_notify(knx_module, config)
if platform is SupportedPlatforms.BINARY_SENSOR: if platform is SupportedPlatforms.BINARY_SENSOR:
return _create_binary_sensor(knx_module, config) return _create_binary_sensor(knx_module, config)
@ -269,15 +265,6 @@ def _create_sensor(knx_module: XKNX, config: ConfigType) -> XknxSensor:
) )
def _create_notify(knx_module: XKNX, config: ConfigType) -> XknxNotification:
"""Return a KNX notification to be used within XKNX."""
return XknxNotification(
knx_module,
name=config[CONF_NAME],
group_address=config[KNX_ADDRESS],
)
def _create_binary_sensor(knx_module: XKNX, config: ConfigType) -> XknxBinarySensor: def _create_binary_sensor(knx_module: XKNX, config: ConfigType) -> XknxBinarySensor:
"""Return a KNX binary sensor to be used within XKNX.""" """Return a KNX binary sensor to be used within XKNX."""
device_name = config[CONF_NAME] device_name = config[CONF_NAME]

View File

@ -3,13 +3,15 @@ from __future__ import annotations
from typing import Any from typing import Any
from xknx import XKNX
from xknx.devices import Notification as XknxNotification from xknx.devices import Notification as XknxNotification
from homeassistant.components.notify import BaseNotificationService from homeassistant.components.notify import BaseNotificationService
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import DOMAIN from .const import DOMAIN, KNX_ADDRESS
async def async_get_service( async def async_get_service(
@ -18,10 +20,21 @@ async def async_get_service(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> KNXNotificationService | None: ) -> KNXNotificationService | None:
"""Get the KNX notification service.""" """Get the KNX notification service."""
if not discovery_info or not discovery_info["platform_config"]:
return None
platform_config = discovery_info["platform_config"]
xknx: XKNX = hass.data[DOMAIN].xknx
notification_devices = [] notification_devices = []
for device in hass.data[DOMAIN].xknx.devices: for device_config in platform_config:
if isinstance(device, XknxNotification): notification_devices.append(
notification_devices.append(device) XknxNotification(
xknx,
name=device_config[CONF_NAME],
group_address=device_config[KNX_ADDRESS],
)
)
return ( return (
KNXNotificationService(notification_devices) if notification_devices else None KNXNotificationService(notification_devices) if notification_devices else None
) )