Skip to content

Commit

Permalink
Improve notify type hints (home-assistant#86685)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jan 26, 2023
1 parent 54fcf58 commit 7af86fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
4 changes: 3 additions & 1 deletion homeassistant/components/notify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions homeassistant/components/notify/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand Down
4 changes: 4 additions & 0 deletions pylint/plugins/hass_enforce_type_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit 7af86fe

Please sign in to comment.