From 8edac5140114a74fc146623b15f8e2bb112f7bdf Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 21 Oct 2024 20:20:29 +0200 Subject: [PATCH] Remove explicit templating of telegram_bot service data (#128906) --- .../components/telegram_bot/__init__.py | 47 ++++--------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/telegram_bot/__init__.py b/homeassistant/components/telegram_bot/__init__.py index 64e2517a40b..b9a032d7f28 100644 --- a/homeassistant/components/telegram_bot/__init__.py +++ b/homeassistant/components/telegram_bot/__init__.py @@ -37,7 +37,6 @@ from homeassistant.const import ( HTTP_DIGEST_AUTHENTICATION, ) 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.typing import ConfigType 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( - {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( { - vol.Optional(ATTR_URL): cv.template, - vol.Optional(ATTR_FILE): cv.template, - vol.Optional(ATTR_CAPTION): cv.template, + vol.Optional(ATTR_URL): cv.string, + vol.Optional(ATTR_FILE): cv.string, + vol.Optional(ATTR_CAPTION): cv.string, vol.Optional(ATTR_USERNAME): cv.string, vol.Optional(ATTR_PASSWORD): 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( { - vol.Required(ATTR_LONGITUDE): cv.template, - vol.Required(ATTR_LATITUDE): cv.template, + vol.Required(ATTR_LONGITUDE): cv.string, + vol.Required(ATTR_LATITUDE): cv.string, } ) @@ -229,7 +228,7 @@ SERVICE_SCHEMA_EDIT_CAPTION = vol.Schema( cv.positive_int, vol.All(cv.string, "last") ), 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, }, extra=vol.ALLOW_EXTRA, @@ -248,7 +247,7 @@ SERVICE_SCHEMA_EDIT_REPLYMARKUP = 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.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: """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 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) if msgtype == SERVICE_SEND_MESSAGE: