From 87ab14d7584cd2a6d87eb71ab7de427137cc1baa Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 6 Sep 2022 20:18:52 +0200 Subject: [PATCH] Add protocol type for legacy notify platforms (#77894) --- homeassistant/components/notify/__init__.py | 2 +- homeassistant/components/notify/legacy.py | 27 ++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index 60d24578593..52864dd001d 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -57,7 +57,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: await asyncio.wait([asyncio.create_task(setup) for setup in platform_setups]) async def persistent_notification(service: ServiceCall) -> None: - """Send notification via the built-in persistsent_notify integration.""" + """Send notification via the built-in persistent_notify integration.""" message = service.data[ATTR_MESSAGE] message.hass = hass check_templates_warn(hass, message) diff --git a/homeassistant/components/notify/legacy.py b/homeassistant/components/notify/legacy.py index f9066b7dff9..3d6d1582848 100644 --- a/homeassistant/components/notify/legacy.py +++ b/homeassistant/components/notify/legacy.py @@ -4,7 +4,7 @@ from __future__ import annotations import asyncio from collections.abc import Coroutine from functools import partial -from typing import Any, cast +from typing import Any, Optional, Protocol, cast from homeassistant.const import CONF_DESCRIPTION, CONF_NAME from homeassistant.core import HomeAssistant, ServiceCall, callback @@ -33,6 +33,26 @@ NOTIFY_SERVICES = "notify_services" NOTIFY_DISCOVERY_DISPATCHER = "notify_discovery_dispatcher" +class LegacyNotifyPlatform(Protocol): + """Define the format of legacy notify platforms.""" + + async def async_get_service( + self, + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = ..., + ) -> BaseNotificationService: + """Set up notification service.""" + + def get_service( + self, + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = ..., + ) -> BaseNotificationService: + """Set up notification service.""" + + @callback def async_setup_legacy( hass: HomeAssistant, config: ConfigType @@ -50,8 +70,9 @@ def async_setup_legacy( if p_config is None: p_config = {} - platform = await async_prepare_setup_platform( - hass, config, DOMAIN, integration_name + platform = cast( + Optional[LegacyNotifyPlatform], + await async_prepare_setup_platform(hass, config, DOMAIN, integration_name), ) if platform is None: