Deprecate passing template to notify (#56069)

pull/56481/head
Paulus Schoutsen 2021-09-20 21:49:02 -07:00 committed by GitHub
parent aabc8cd2d5
commit d4864f5750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View File

@ -10,9 +10,9 @@ import voluptuous as vol
import homeassistant.components.persistent_notification as pn
from homeassistant.const import CONF_DESCRIPTION, CONF_NAME, CONF_PLATFORM
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_per_platform, discovery
from homeassistant.helpers import config_per_platform, discovery, template
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.service import async_set_service_schema
from homeassistant.loader import async_get_integration, bind_hass
@ -68,6 +68,19 @@ PERSISTENT_NOTIFICATION_SERVICE_SCHEMA = vol.Schema(
)
@callback
def _check_templates_warn(hass: HomeAssistant, tpl: template.Template) -> None:
"""Warn user that passing templates to notify service is deprecated."""
if tpl.is_static or hass.data.get("notify_template_warned"):
return
hass.data["notify_template_warned"] = True
_LOGGER.warning(
"Passing templates to notify service is deprecated and will be removed in 2021.12. "
"Automations and scripts handle templates automatically"
)
@bind_hass
async def async_reload(hass: HomeAssistant, integration_name: str) -> None:
"""Register notify services for an integration."""
@ -144,6 +157,7 @@ class BaseNotificationService:
title = service.data.get(ATTR_TITLE)
if title:
_check_templates_warn(self.hass, title)
title.hass = self.hass
kwargs[ATTR_TITLE] = title.async_render(parse_result=False)
@ -152,6 +166,7 @@ class BaseNotificationService:
elif service.data.get(ATTR_TARGET) is not None:
kwargs[ATTR_TARGET] = service.data.get(ATTR_TARGET)
_check_templates_warn(self.hass, message)
message.hass = self.hass
kwargs[ATTR_MESSAGE] = message.async_render(parse_result=False)
kwargs[ATTR_DATA] = service.data.get(ATTR_DATA)
@ -261,10 +276,12 @@ async def async_setup(hass, config):
payload = {}
message = service.data[ATTR_MESSAGE]
message.hass = hass
_check_templates_warn(hass, message)
payload[ATTR_MESSAGE] = message.async_render(parse_result=False)
title = service.data.get(ATTR_TITLE)
if title:
_check_templates_warn(hass, title)
title.hass = hass
payload[ATTR_TITLE] = title.async_render(parse_result=False)

View File

@ -1,6 +1,7 @@
"""The tests for notify services that change targets."""
from homeassistant.components import notify
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
async def test_same_targets(hass: HomeAssistant):
@ -81,3 +82,19 @@ class NotificationService(notify.BaseNotificationService):
def targets(self):
"""Return a dictionary of devices."""
return self.target_list
async def test_warn_template(hass, caplog):
"""Test warning when template used."""
assert await async_setup_component(hass, "notify", {})
assert await async_setup_component(hass, "persistent_notification", {})
await hass.services.async_call(
"notify",
"persistent_notification",
{"message": "{{ 1 + 1 }}", "title": "Test notif {{ 1 + 1 }}"},
blocking=True,
)
# We should only log it once
assert caplog.text.count("Passing templates to notify service is deprecated") == 1
assert hass.states.get("persistent_notification.notification") is not None