Fix notify platform setup for KNX (#61842)
* Fix notify platform setup for KNX * Apply review suggestions * Store hass config in DATA_HASS_CONFIG * Readd guard clause
This commit is contained in:
parent
59e4b52065
commit
66b8f87e43
3 changed files with 35 additions and 19 deletions
|
@ -29,6 +29,7 @@ from homeassistant.const import (
|
|||
CONF_PORT,
|
||||
CONF_TYPE,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import Event, HomeAssistant, ServiceCall
|
||||
from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError
|
||||
|
@ -44,6 +45,7 @@ from .const import (
|
|||
CONF_KNX_INDIVIDUAL_ADDRESS,
|
||||
CONF_KNX_ROUTING,
|
||||
CONF_KNX_TUNNELING,
|
||||
DATA_HASS_CONFIG,
|
||||
DATA_KNX_CONFIG,
|
||||
DOMAIN,
|
||||
KNX_ADDRESS,
|
||||
|
@ -195,6 +197,7 @@ SERVICE_KNX_EXPOSURE_REGISTER_SCHEMA = vol.Any(
|
|||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Start the KNX integration."""
|
||||
hass.data[DATA_HASS_CONFIG] = config
|
||||
conf: ConfigType | None = config.get(DOMAIN)
|
||||
|
||||
if conf is None:
|
||||
|
@ -251,15 +254,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
)
|
||||
|
||||
hass.config_entries.async_setup_platforms(
|
||||
entry, [platform for platform in SUPPORTED_PLATFORMS if platform in config]
|
||||
entry,
|
||||
[
|
||||
platform
|
||||
for platform in SUPPORTED_PLATFORMS
|
||||
if platform in config and platform is not Platform.NOTIFY
|
||||
],
|
||||
)
|
||||
|
||||
# set up notify platform, no entry support for notify component yet,
|
||||
# have to use discovery to load platform.
|
||||
if NotifySchema.PLATFORM in conf:
|
||||
# set up notify platform, no entry support for notify component yet
|
||||
if NotifySchema.PLATFORM in config:
|
||||
hass.async_create_task(
|
||||
discovery.async_load_platform(
|
||||
hass, "notify", DOMAIN, conf[NotifySchema.PLATFORM], config
|
||||
hass, "notify", DOMAIN, {}, hass.data[DATA_HASS_CONFIG]
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -312,6 +319,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
platform
|
||||
for platform in SUPPORTED_PLATFORMS
|
||||
if platform in hass.data[DATA_KNX_CONFIG]
|
||||
and platform is not Platform.NOTIFY
|
||||
],
|
||||
)
|
||||
if unload_ok:
|
||||
|
|
|
@ -42,7 +42,10 @@ CONF_STATE_ADDRESS: Final = "state_address"
|
|||
CONF_SYNC_STATE: Final = "sync_state"
|
||||
CONF_KNX_INITIAL_CONNECTION_TYPES: Final = [CONF_KNX_TUNNELING, CONF_KNX_ROUTING]
|
||||
|
||||
# yaml config merged with config entry data
|
||||
DATA_KNX_CONFIG: Final = "knx_config"
|
||||
# original hass yaml config
|
||||
DATA_HASS_CONFIG: Final = "knx_hass_config"
|
||||
|
||||
ATTR_COUNTER: Final = "counter"
|
||||
ATTR_SOURCE: Final = "source"
|
||||
|
|
|
@ -11,7 +11,8 @@ from homeassistant.const import CONF_NAME
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import DOMAIN, KNX_ADDRESS
|
||||
from .const import DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS
|
||||
from .schema import NotifySchema
|
||||
|
||||
|
||||
async def async_get_service(
|
||||
|
@ -20,24 +21,28 @@ async def async_get_service(
|
|||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> KNXNotificationService | None:
|
||||
"""Get the KNX notification service."""
|
||||
if not discovery_info:
|
||||
if discovery_info is None:
|
||||
return None
|
||||
|
||||
platform_config: dict = discovery_info
|
||||
xknx: XKNX = hass.data[DOMAIN].xknx
|
||||
if platform_config := hass.data[DATA_KNX_CONFIG].get(NotifySchema.PLATFORM):
|
||||
xknx: XKNX = hass.data[DOMAIN].xknx
|
||||
|
||||
notification_devices = []
|
||||
for device_config in platform_config:
|
||||
notification_devices.append(
|
||||
XknxNotification(
|
||||
xknx,
|
||||
name=device_config[CONF_NAME],
|
||||
group_address=device_config[KNX_ADDRESS],
|
||||
notification_devices = []
|
||||
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
|
||||
)
|
||||
return (
|
||||
KNXNotificationService(notification_devices) if notification_devices else None
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
class KNXNotificationService(BaseNotificationService):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue