mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Improve notify type hints (#86685)
This commit is contained in:
parent
54fcf58449
commit
7af86fe130
@ -9,6 +9,7 @@ import homeassistant.components.persistent_notification as pn
|
|||||||
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.template import Template
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from .const import ( # noqa: F401
|
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:
|
async def persistent_notification(service: ServiceCall) -> None:
|
||||||
"""Send notification via the built-in persistent_notify integration."""
|
"""Send notification via the built-in persistent_notify integration."""
|
||||||
message = service.data[ATTR_MESSAGE]
|
message: Template = service.data[ATTR_MESSAGE]
|
||||||
message.hass = hass
|
message.hass = hass
|
||||||
check_templates_warn(hass, message)
|
check_templates_warn(hass, message)
|
||||||
|
|
||||||
title = None
|
title = None
|
||||||
|
title_tpl: Template | None
|
||||||
if title_tpl := service.data.get(ATTR_TITLE):
|
if title_tpl := service.data.get(ATTR_TITLE):
|
||||||
check_templates_warn(hass, title_tpl)
|
check_templates_warn(hass, title_tpl)
|
||||||
title_tpl.hass = hass
|
title_tpl.hass = hass
|
||||||
|
@ -9,8 +9,9 @@ from typing import Any, Protocol, cast
|
|||||||
from homeassistant.const import CONF_DESCRIPTION, CONF_NAME
|
from homeassistant.const import CONF_DESCRIPTION, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
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.service import async_set_service_schema
|
||||||
|
from homeassistant.helpers.template import Template
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
from homeassistant.loader import async_get_integration, bind_hass
|
from homeassistant.loader import async_get_integration, bind_hass
|
||||||
from homeassistant.setup import async_prepare_setup_platform, async_start_setup
|
from homeassistant.setup import async_prepare_setup_platform, async_start_setup
|
||||||
@ -144,7 +145,7 @@ def async_setup_legacy(
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@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."""
|
"""Warn user that passing templates to notify service is deprecated."""
|
||||||
if tpl.is_static or hass.data.get("notify_template_warned"):
|
if tpl.is_static or hass.data.get("notify_template_warned"):
|
||||||
return
|
return
|
||||||
@ -217,7 +218,12 @@ class BaseNotificationService:
|
|||||||
hass: HomeAssistant = None # type: ignore[assignment]
|
hass: HomeAssistant = None # type: ignore[assignment]
|
||||||
|
|
||||||
# Name => target
|
# 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:
|
def send_message(self, message: str, **kwargs: Any) -> None:
|
||||||
"""Send a message.
|
"""Send a message.
|
||||||
@ -238,8 +244,8 @@ class BaseNotificationService:
|
|||||||
async def _async_notify_message_service(self, service: ServiceCall) -> None:
|
async def _async_notify_message_service(self, service: ServiceCall) -> None:
|
||||||
"""Handle sending notification message service calls."""
|
"""Handle sending notification message service calls."""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
message = service.data[ATTR_MESSAGE]
|
message: Template = service.data[ATTR_MESSAGE]
|
||||||
|
title: Template | None
|
||||||
if title := service.data.get(ATTR_TITLE):
|
if title := service.data.get(ATTR_TITLE):
|
||||||
check_templates_warn(self.hass, title)
|
check_templates_warn(self.hass, title)
|
||||||
title.hass = self.hass
|
title.hass = self.hass
|
||||||
@ -279,7 +285,7 @@ class BaseNotificationService:
|
|||||||
|
|
||||||
async def async_register_services(self) -> None:
|
async def async_register_services(self) -> None:
|
||||||
"""Create or update the notify services."""
|
"""Create or update the notify services."""
|
||||||
if hasattr(self, "targets"):
|
if self.targets is not None:
|
||||||
stale_targets = set(self.registered_targets)
|
stale_targets = set(self.registered_targets)
|
||||||
|
|
||||||
for name, target in self.targets.items():
|
for name, target in self.targets.items():
|
||||||
|
@ -1967,6 +1967,10 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = {
|
|||||||
ClassTypeHintMatch(
|
ClassTypeHintMatch(
|
||||||
base_class="BaseNotificationService",
|
base_class="BaseNotificationService",
|
||||||
matches=[
|
matches=[
|
||||||
|
TypeHintMatch(
|
||||||
|
function_name="targets",
|
||||||
|
return_type=["dict[str, Any]", None],
|
||||||
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="send_message",
|
function_name="send_message",
|
||||||
arg_types={1: "str"},
|
arg_types={1: "str"},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user