Remove explicit templating of telegram_bot service data (#128906)

This commit is contained in:
Erik Montnemery 2024-10-21 20:20:29 +02:00 committed by GitHub
parent f34ba9bf96
commit 8edac51401
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,7 +37,6 @@ from homeassistant.const import (
HTTP_DIGEST_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION,
) )
from homeassistant.core import Context, HomeAssistant, ServiceCall from homeassistant.core import Context, HomeAssistant, ServiceCall
from homeassistant.exceptions import TemplateError
from homeassistant.helpers import config_validation as cv, issue_registry as ir from homeassistant.helpers import config_validation as cv, issue_registry as ir
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import async_get_loaded_integration from homeassistant.loader import async_get_loaded_integration
@ -175,14 +174,14 @@ BASE_SERVICE_SCHEMA = vol.Schema(
) )
SERVICE_SCHEMA_SEND_MESSAGE = BASE_SERVICE_SCHEMA.extend( SERVICE_SCHEMA_SEND_MESSAGE = BASE_SERVICE_SCHEMA.extend(
{vol.Required(ATTR_MESSAGE): cv.template, vol.Optional(ATTR_TITLE): cv.template} {vol.Required(ATTR_MESSAGE): cv.string, vol.Optional(ATTR_TITLE): cv.string}
) )
SERVICE_SCHEMA_SEND_FILE = BASE_SERVICE_SCHEMA.extend( SERVICE_SCHEMA_SEND_FILE = BASE_SERVICE_SCHEMA.extend(
{ {
vol.Optional(ATTR_URL): cv.template, vol.Optional(ATTR_URL): cv.string,
vol.Optional(ATTR_FILE): cv.template, vol.Optional(ATTR_FILE): cv.string,
vol.Optional(ATTR_CAPTION): cv.template, vol.Optional(ATTR_CAPTION): cv.string,
vol.Optional(ATTR_USERNAME): cv.string, vol.Optional(ATTR_USERNAME): cv.string,
vol.Optional(ATTR_PASSWORD): cv.string, vol.Optional(ATTR_PASSWORD): cv.string,
vol.Optional(ATTR_AUTHENTICATION): cv.string, vol.Optional(ATTR_AUTHENTICATION): cv.string,
@ -196,8 +195,8 @@ SERVICE_SCHEMA_SEND_STICKER = SERVICE_SCHEMA_SEND_FILE.extend(
SERVICE_SCHEMA_SEND_LOCATION = BASE_SERVICE_SCHEMA.extend( SERVICE_SCHEMA_SEND_LOCATION = BASE_SERVICE_SCHEMA.extend(
{ {
vol.Required(ATTR_LONGITUDE): cv.template, vol.Required(ATTR_LONGITUDE): cv.string,
vol.Required(ATTR_LATITUDE): cv.template, vol.Required(ATTR_LATITUDE): cv.string,
} }
) )
@ -229,7 +228,7 @@ SERVICE_SCHEMA_EDIT_CAPTION = vol.Schema(
cv.positive_int, vol.All(cv.string, "last") cv.positive_int, vol.All(cv.string, "last")
), ),
vol.Required(ATTR_CHAT_ID): vol.Coerce(int), vol.Required(ATTR_CHAT_ID): vol.Coerce(int),
vol.Required(ATTR_CAPTION): cv.template, vol.Required(ATTR_CAPTION): cv.string,
vol.Optional(ATTR_KEYBOARD_INLINE): cv.ensure_list, vol.Optional(ATTR_KEYBOARD_INLINE): cv.ensure_list,
}, },
extra=vol.ALLOW_EXTRA, extra=vol.ALLOW_EXTRA,
@ -248,7 +247,7 @@ SERVICE_SCHEMA_EDIT_REPLYMARKUP = vol.Schema(
SERVICE_SCHEMA_ANSWER_CALLBACK_QUERY = vol.Schema( SERVICE_SCHEMA_ANSWER_CALLBACK_QUERY = vol.Schema(
{ {
vol.Required(ATTR_MESSAGE): cv.template, vol.Required(ATTR_MESSAGE): cv.string,
vol.Required(ATTR_CALLBACK_QUERY_ID): vol.Coerce(int), vol.Required(ATTR_CALLBACK_QUERY_ID): vol.Coerce(int),
vol.Optional(ATTR_SHOW_ALERT): cv.boolean, vol.Optional(ATTR_SHOW_ALERT): cv.boolean,
}, },
@ -402,38 +401,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def async_send_telegram_message(service: ServiceCall) -> None: async def async_send_telegram_message(service: ServiceCall) -> None:
"""Handle sending Telegram Bot message service calls.""" """Handle sending Telegram Bot message service calls."""
def _render_template_attr(data, attribute):
if attribute_templ := data.get(attribute):
if any(
isinstance(attribute_templ, vtype) for vtype in (float, int, str)
):
data[attribute] = attribute_templ
else:
try:
data[attribute] = attribute_templ.async_render(
parse_result=False
)
except TemplateError as exc:
_LOGGER.error(
"TemplateError in %s: %s -> %s",
attribute,
attribute_templ.template,
exc,
)
data[attribute] = attribute_templ.template
msgtype = service.service msgtype = service.service
kwargs = dict(service.data) kwargs = dict(service.data)
for attribute in (
ATTR_MESSAGE,
ATTR_TITLE,
ATTR_URL,
ATTR_FILE,
ATTR_CAPTION,
ATTR_LONGITUDE,
ATTR_LATITUDE,
):
_render_template_attr(kwargs, attribute)
_LOGGER.debug("New telegram message %s: %s", msgtype, kwargs) _LOGGER.debug("New telegram message %s: %s", msgtype, kwargs)
if msgtype == SERVICE_SEND_MESSAGE: if msgtype == SERVICE_SEND_MESSAGE: