From 7af86fe130b0c7a09d445405751521cbc9ba14d1 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 26 Jan 2023 16:23:03 +0100 Subject: [PATCH] Improve notify type hints (#86685) --- homeassistant/components/notify/__init__.py | 4 +++- homeassistant/components/notify/legacy.py | 18 ++++++++++++------ pylint/plugins/hass_enforce_type_hints.py | 4 ++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index 52864dd001d65e..eae47b55179875 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -9,6 +9,7 @@ from homeassistant.const import CONF_NAME, CONF_PLATFORM from homeassistant.core import HomeAssistant, ServiceCall import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.template import Template from homeassistant.helpers.typing import ConfigType from .const import ( # noqa: F401 @@ -58,11 +59,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def persistent_notification(service: ServiceCall) -> None: """Send notification via the built-in persistent_notify integration.""" - message = service.data[ATTR_MESSAGE] + message: Template = service.data[ATTR_MESSAGE] message.hass = hass check_templates_warn(hass, message) title = None + title_tpl: Template | None if title_tpl := service.data.get(ATTR_TITLE): check_templates_warn(hass, title_tpl) title_tpl.hass = hass diff --git a/homeassistant/components/notify/legacy.py b/homeassistant/components/notify/legacy.py index 6046039aab11be..2d91e1c065a072 100644 --- a/homeassistant/components/notify/legacy.py +++ b/homeassistant/components/notify/legacy.py @@ -9,8 +9,9 @@ from homeassistant.const import CONF_DESCRIPTION, CONF_NAME from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import config_per_platform, discovery, template +from homeassistant.helpers import config_per_platform, discovery from homeassistant.helpers.service import async_set_service_schema +from homeassistant.helpers.template import Template from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.loader import async_get_integration, bind_hass from homeassistant.setup import async_prepare_setup_platform, async_start_setup @@ -144,7 +145,7 @@ async def async_platform_discovered( @callback -def check_templates_warn(hass: HomeAssistant, tpl: template.Template) -> None: +def check_templates_warn(hass: HomeAssistant, tpl: Template) -> None: """Warn user that passing templates to notify service is deprecated.""" if tpl.is_static or hass.data.get("notify_template_warned"): return @@ -217,7 +218,12 @@ class BaseNotificationService: hass: HomeAssistant = None # type: ignore[assignment] # Name => target - registered_targets: dict[str, str] + registered_targets: dict[str, Any] + + @property + def targets(self) -> dict[str, Any] | None: + """Return a dictionary of registered targets.""" + return None def send_message(self, message: str, **kwargs: Any) -> None: """Send a message. @@ -238,8 +244,8 @@ async def async_send_message(self, message: str, **kwargs: Any) -> None: async def _async_notify_message_service(self, service: ServiceCall) -> None: """Handle sending notification message service calls.""" kwargs = {} - message = service.data[ATTR_MESSAGE] - + message: Template = service.data[ATTR_MESSAGE] + title: Template | None if title := service.data.get(ATTR_TITLE): check_templates_warn(self.hass, title) title.hass = self.hass @@ -279,7 +285,7 @@ async def async_setup( async def async_register_services(self) -> None: """Create or update the notify services.""" - if hasattr(self, "targets"): + if self.targets is not None: stale_targets = set(self.registered_targets) for name, target in self.targets.items(): diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index 0ecf9f58398487..d3c9e090a23564 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -1967,6 +1967,10 @@ class ClassTypeHintMatch: ClassTypeHintMatch( base_class="BaseNotificationService", matches=[ + TypeHintMatch( + function_name="targets", + return_type=["dict[str, Any]", None], + ), TypeHintMatch( function_name="send_message", arg_types={1: "str"},