Fix duplicate template handling in Persistent Notifications (#47217)

This commit is contained in:
Franck Nijhof 2021-03-02 01:27:26 +01:00 committed by GitHub
parent cc6293623f
commit 4f9f870e7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.template import Template
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
from homeassistant.util import slugify from homeassistant.util import slugify
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -35,8 +36,8 @@ SERVICE_MARK_READ = "mark_read"
SCHEMA_SERVICE_CREATE = vol.Schema( SCHEMA_SERVICE_CREATE = vol.Schema(
{ {
vol.Required(ATTR_MESSAGE): cv.template, vol.Required(ATTR_MESSAGE): vol.Any(cv.dynamic_template, cv.string),
vol.Optional(ATTR_TITLE): cv.template, vol.Optional(ATTR_TITLE): vol.Any(cv.dynamic_template, cv.string),
vol.Optional(ATTR_NOTIFICATION_ID): cv.string, vol.Optional(ATTR_NOTIFICATION_ID): cv.string,
} }
) )
@ -118,22 +119,24 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool:
attr = {} attr = {}
if title is not None: if title is not None:
try: if isinstance(title, Template):
title.hass = hass try:
title = title.async_render(parse_result=False) title.hass = hass
except TemplateError as ex: title = title.async_render(parse_result=False)
_LOGGER.error("Error rendering title %s: %s", title, ex) except TemplateError as ex:
title = title.template _LOGGER.error("Error rendering title %s: %s", title, ex)
title = title.template
attr[ATTR_TITLE] = title attr[ATTR_TITLE] = title
attr[ATTR_FRIENDLY_NAME] = title attr[ATTR_FRIENDLY_NAME] = title
try: if isinstance(message, Template):
message.hass = hass try:
message = message.async_render(parse_result=False) message.hass = hass
except TemplateError as ex: message = message.async_render(parse_result=False)
_LOGGER.error("Error rendering message %s: %s", message, ex) except TemplateError as ex:
message = message.template _LOGGER.error("Error rendering message %s: %s", message, ex)
message = message.template
attr[ATTR_MESSAGE] = message attr[ATTR_MESSAGE] = message