From 8461cf271702cf2e935fa1f83ed9d04b39d053cd Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 3 Jun 2017 02:50:37 -0700 Subject: [PATCH] Fix telegram_bot (#7877) --- .../components/telegram_bot/__init__.py | 159 +++++++++--------- .../components/telegram_bot/polling.py | 2 +- .../components/telegram_bot/webhooks.py | 2 +- 3 files changed, 80 insertions(+), 83 deletions(-) diff --git a/homeassistant/components/telegram_bot/__init__.py b/homeassistant/components/telegram_bot/__init__.py index 5693f51c1f4..2fa77dcec02 100644 --- a/homeassistant/components/telegram_bot/__init__.py +++ b/homeassistant/components/telegram_bot/__init__.py @@ -194,95 +194,92 @@ def load_data(url=None, filepath=None, username=None, password=None, @asyncio.coroutine def async_setup(hass, config): """Set up the Telegram bot component.""" - conf = config[DOMAIN] + if not config[DOMAIN]: + return False + + p_config = config[DOMAIN][0] descriptions = yield from hass.async_add_job( load_yaml_config_file, os.path.join(os.path.dirname(__file__), 'services.yaml')) + p_type = p_config.get(CONF_PLATFORM) + + platform = yield from async_prepare_setup_platform( + hass, config, DOMAIN, p_type) + + if platform is None: + return + + _LOGGER.info("Setting up %s.%s", DOMAIN, p_type) + try: + receiver_service = yield from \ + platform.async_setup_platform(hass, p_config) + if receiver_service is None: + _LOGGER.error( + "Failed to initialize Telegram bot %s", p_type) + return False + + except Exception: # pylint: disable=broad-except + _LOGGER.exception("Error setting up platform %s", p_type) + return False + + notify_service = TelegramNotificationService( + hass, + p_config.get(CONF_API_KEY), + p_config.get(CONF_ALLOWED_CHAT_IDS), + p_config.get(ATTR_PARSER) + ) + @asyncio.coroutine - def async_setup_platform(p_type, p_config=None, discovery_info=None): - """Set up a Telegram bot platform.""" - platform = yield from async_prepare_setup_platform( - hass, config, DOMAIN, p_type) + def async_send_telegram_message(service): + """Handle sending Telegram Bot message service calls.""" + def _render_template_attr(data, attribute): + attribute_templ = data.get(attribute) + if attribute_templ: + if any([isinstance(attribute_templ, vtype) + for vtype in [float, int, str]]): + data[attribute] = attribute_templ + else: + attribute_templ.hass = hass + try: + data[attribute] = attribute_templ.async_render() + except TemplateError as exc: + _LOGGER.error( + "TemplateError in %s: %s -> %s", + attribute, attribute_templ.template, exc) + data[attribute] = attribute_templ.template - if platform is None: - _LOGGER.error("Unknown notification service specified") - return + 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) - _LOGGER.info("Setting up %s.%s", DOMAIN, p_type) - try: - receiver_service = yield from \ - platform.async_setup_platform(hass, p_config, discovery_info) - if receiver_service is None: - _LOGGER.error( - "Failed to initialize Telegram bot %s", p_type) - return + if msgtype == SERVICE_SEND_MESSAGE: + yield from hass.async_add_job( + partial(notify_service.send_message, **kwargs)) + elif msgtype == SERVICE_SEND_PHOTO: + yield from hass.async_add_job( + partial(notify_service.send_file, True, **kwargs)) + elif msgtype == SERVICE_SEND_DOCUMENT: + yield from hass.async_add_job( + partial(notify_service.send_file, False, **kwargs)) + elif msgtype == SERVICE_SEND_LOCATION: + yield from hass.async_add_job( + partial(notify_service.send_location, **kwargs)) + elif msgtype == SERVICE_ANSWER_CALLBACK_QUERY: + yield from hass.async_add_job( + partial(notify_service.answer_callback_query, **kwargs)) + else: + yield from hass.async_add_job( + partial(notify_service.edit_message, msgtype, **kwargs)) - except Exception: # pylint: disable=broad-except - _LOGGER.exception("Error setting up platform %s", p_type) - return - - notify_service = TelegramNotificationService( - hass, - p_config.get(CONF_API_KEY), - p_config.get(CONF_ALLOWED_CHAT_IDS), - p_config.get(ATTR_PARSER) - ) - - @asyncio.coroutine - def async_send_telegram_message(service): - """Handle sending Telegram Bot message service calls.""" - def _render_template_attr(data, attribute): - attribute_templ = data.get(attribute) - if attribute_templ: - if any([isinstance(attribute_templ, vtype) - for vtype in [float, int, str]]): - data[attribute] = attribute_templ - else: - attribute_templ.hass = hass - try: - data[attribute] = attribute_templ.async_render() - 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: - yield from hass.async_add_job( - partial(notify_service.send_message, **kwargs)) - elif msgtype == SERVICE_SEND_PHOTO: - yield from hass.async_add_job( - partial(notify_service.send_file, True, **kwargs)) - elif msgtype == SERVICE_SEND_DOCUMENT: - yield from hass.async_add_job( - partial(notify_service.send_file, False, **kwargs)) - elif msgtype == SERVICE_SEND_LOCATION: - yield from hass.async_add_job( - partial(notify_service.send_location, **kwargs)) - elif msgtype == SERVICE_ANSWER_CALLBACK_QUERY: - yield from hass.async_add_job( - partial(notify_service.answer_callback_query, **kwargs)) - else: - yield from hass.async_add_job( - partial(notify_service.edit_message, msgtype, **kwargs)) - - # Register notification services - for service_notif, schema in SERVICE_MAP.items(): - hass.services.async_register( - DOMAIN, service_notif, async_send_telegram_message, - descriptions.get(service_notif), schema=schema) - - return True - - yield from async_setup_platform(conf.get(CONF_PLATFORM), conf) + # Register notification services + for service_notif, schema in SERVICE_MAP.items(): + hass.services.async_register( + DOMAIN, service_notif, async_send_telegram_message, + descriptions.get(service_notif), schema=schema) return True diff --git a/homeassistant/components/telegram_bot/polling.py b/homeassistant/components/telegram_bot/polling.py index ff89806b292..e24ada194ef 100644 --- a/homeassistant/components/telegram_bot/polling.py +++ b/homeassistant/components/telegram_bot/polling.py @@ -25,7 +25,7 @@ PLATFORM_SCHEMA = TELEGRAM_PLATFORM_SCHEMA @asyncio.coroutine -def async_setup_platform(hass, config, async_add_devices, discovery_info=None): +def async_setup_platform(hass, config): """Set up the Telegram polling platform.""" import telegram bot = telegram.Bot(config[CONF_API_KEY]) diff --git a/homeassistant/components/telegram_bot/webhooks.py b/homeassistant/components/telegram_bot/webhooks.py index b5378a01991..f57c0620644 100644 --- a/homeassistant/components/telegram_bot/webhooks.py +++ b/homeassistant/components/telegram_bot/webhooks.py @@ -47,7 +47,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ @asyncio.coroutine -def async_setup_platform(hass, config, async_add_devices, discovery_info=None): +def async_setup_platform(hass, config): """Set up the Telegram webhooks platform.""" import telegram bot = telegram.Bot(config[CONF_API_KEY])