Remove deprecated template support in persistent notifications (#69021)
This commit is contained in:
parent
72c4c359a4
commit
5280bf2296
2 changed files with 3 additions and 51 deletions
|
@ -3,17 +3,15 @@ from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, cast
|
from typing import Any
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import websocket_api
|
from homeassistant.components import websocket_api
|
||||||
from homeassistant.const import ATTR_FRIENDLY_NAME
|
from homeassistant.const import ATTR_FRIENDLY_NAME
|
||||||
from homeassistant.core import Context, HomeAssistant, ServiceCall, callback
|
from homeassistant.core import Context, HomeAssistant, ServiceCall, callback
|
||||||
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, is_template_string
|
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
|
@ -82,36 +80,11 @@ def async_create(
|
||||||
)
|
)
|
||||||
notification_id = entity_id.split(".")[1]
|
notification_id = entity_id.split(".")[1]
|
||||||
|
|
||||||
warn = False
|
attr: dict[str, str] = {ATTR_MESSAGE: message}
|
||||||
|
|
||||||
attr: dict[str, str] = {}
|
|
||||||
if title is not None:
|
if title is not None:
|
||||||
if is_template_string(title):
|
|
||||||
warn = True
|
|
||||||
try:
|
|
||||||
title = cast(
|
|
||||||
str, Template(title, hass).async_render(parse_result=False) # type: ignore[no-untyped-call]
|
|
||||||
)
|
|
||||||
except TemplateError as ex:
|
|
||||||
_LOGGER.error("Error rendering title %s: %s", title, ex)
|
|
||||||
|
|
||||||
attr[ATTR_TITLE] = title
|
attr[ATTR_TITLE] = title
|
||||||
attr[ATTR_FRIENDLY_NAME] = title
|
attr[ATTR_FRIENDLY_NAME] = title
|
||||||
|
|
||||||
if is_template_string(message):
|
|
||||||
warn = True
|
|
||||||
try:
|
|
||||||
message = Template(message, hass).async_render(parse_result=False) # type: ignore[no-untyped-call]
|
|
||||||
except TemplateError as ex:
|
|
||||||
_LOGGER.error("Error rendering message %s: %s", message, ex)
|
|
||||||
|
|
||||||
attr[ATTR_MESSAGE] = message
|
|
||||||
|
|
||||||
if warn:
|
|
||||||
_LOGGER.warning(
|
|
||||||
"Passing a template string to persistent_notification.async_create function is deprecated"
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.states.async_set(entity_id, STATE, attr, context=context)
|
hass.states.async_set(entity_id, STATE, attr, context=context)
|
||||||
|
|
||||||
# Store notification and fire event
|
# Store notification and fire event
|
||||||
|
|
|
@ -20,7 +20,7 @@ async def test_create(hass):
|
||||||
assert len(hass.states.async_entity_ids(pn.DOMAIN)) == 0
|
assert len(hass.states.async_entity_ids(pn.DOMAIN)) == 0
|
||||||
assert len(notifications) == 0
|
assert len(notifications) == 0
|
||||||
|
|
||||||
pn.async_create(hass, "Hello World {{ 1 + 1 }}", title="{{ 1 + 1 }} beers")
|
pn.async_create(hass, "Hello World 2", title="2 beers")
|
||||||
|
|
||||||
entity_ids = hass.states.async_entity_ids(pn.DOMAIN)
|
entity_ids = hass.states.async_entity_ids(pn.DOMAIN)
|
||||||
assert len(entity_ids) == 1
|
assert len(entity_ids) == 1
|
||||||
|
@ -68,27 +68,6 @@ async def test_create_notification_id(hass):
|
||||||
assert notification["message"] == "test 2"
|
assert notification["message"] == "test 2"
|
||||||
|
|
||||||
|
|
||||||
async def test_create_template_error(hass):
|
|
||||||
"""Ensure we output templates if contain error."""
|
|
||||||
notifications = hass.data[pn.DOMAIN]
|
|
||||||
assert len(hass.states.async_entity_ids(pn.DOMAIN)) == 0
|
|
||||||
assert len(notifications) == 0
|
|
||||||
|
|
||||||
pn.async_create(hass, "{{ message + 1 }}", "{{ title + 1 }}")
|
|
||||||
|
|
||||||
entity_ids = hass.states.async_entity_ids(pn.DOMAIN)
|
|
||||||
assert len(entity_ids) == 1
|
|
||||||
assert len(notifications) == 1
|
|
||||||
|
|
||||||
state = hass.states.get(entity_ids[0])
|
|
||||||
assert state.attributes.get("message") == "{{ message + 1 }}"
|
|
||||||
assert state.attributes.get("title") == "{{ title + 1 }}"
|
|
||||||
|
|
||||||
notification = notifications.get(entity_ids[0])
|
|
||||||
assert notification["message"] == "{{ message + 1 }}"
|
|
||||||
assert notification["title"] == "{{ title + 1 }}"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_dismiss_notification(hass):
|
async def test_dismiss_notification(hass):
|
||||||
"""Ensure removal of specific notification."""
|
"""Ensure removal of specific notification."""
|
||||||
notifications = hass.data[pn.DOMAIN]
|
notifications = hass.data[pn.DOMAIN]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue