From 3200b0150af6c275de229cf22ce3bf9ab9e36be5 Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Sun, 16 May 2021 12:07:44 +0200 Subject: [PATCH] Create KNX notify entities directly from config (#50709) --- homeassistant/components/knx/factory.py | 13 ------------- homeassistant/components/knx/notify.py | 21 +++++++++++++++++---- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/knx/factory.py b/homeassistant/components/knx/factory.py index c5c95f50150..f89371ee1dc 100644 --- a/homeassistant/components/knx/factory.py +++ b/homeassistant/components/knx/factory.py @@ -9,7 +9,6 @@ from xknx.devices import ( Cover as XknxCover, Device as XknxDevice, Light as XknxLight, - Notification as XknxNotification, Sensor as XknxSensor, Weather as XknxWeather, ) @@ -46,9 +45,6 @@ def create_knx_device( if platform is SupportedPlatforms.SENSOR: return _create_sensor(knx_module, config) - if platform is SupportedPlatforms.NOTIFY: - return _create_notify(knx_module, config) - if platform is SupportedPlatforms.BINARY_SENSOR: 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: """Return a KNX binary sensor to be used within XKNX.""" device_name = config[CONF_NAME] diff --git a/homeassistant/components/knx/notify.py b/homeassistant/components/knx/notify.py index 62ba1109526..6f549d9cfac 100644 --- a/homeassistant/components/knx/notify.py +++ b/homeassistant/components/knx/notify.py @@ -3,13 +3,15 @@ from __future__ import annotations from typing import Any +from xknx import XKNX from xknx.devices import Notification as XknxNotification from homeassistant.components.notify import BaseNotificationService +from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from .const import DOMAIN +from .const import DOMAIN, KNX_ADDRESS async def async_get_service( @@ -18,10 +20,21 @@ async def async_get_service( discovery_info: DiscoveryInfoType | None = None, ) -> KNXNotificationService | None: """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 = [] - for device in hass.data[DOMAIN].xknx.devices: - if isinstance(device, XknxNotification): - notification_devices.append(device) + for device_config in platform_config: + notification_devices.append( + XknxNotification( + xknx, + name=device_config[CONF_NAME], + group_address=device_config[KNX_ADDRESS], + ) + ) return ( KNXNotificationService(notification_devices) if notification_devices else None )