Improve notify type hints (#86685)

This commit is contained in:
epenet 2023-01-26 16:23:03 +01:00 committed by GitHub
parent 54fcf58449
commit 7af86fe130
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import homeassistant.components.persistent_notification as pn
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

View File

@ -9,8 +9,9 @@ from typing import Any, Protocol, cast
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 @@ def async_setup_legacy(
@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 @@ class BaseNotificationService:
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 @@ class BaseNotificationService:
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():

View File

@ -1967,6 +1967,10 @@ _INHERITANCE_MATCH: dict[str, list[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"},