From c765e11f55282530275396f8bdc837cb96259920 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 6 Apr 2022 10:44:02 -0700 Subject: [PATCH] Fix telegram broadcast (#69452) --- .../components/telegram_bot/broadcast.py | 13 +----------- tests/components/telegram_bot/conftest.py | 10 ---------- .../components/telegram_bot/test_broadcast.py | 20 +++++++++++++++++++ .../telegram_bot/test_telegram_bot.py | 10 ++++++++++ 4 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 tests/components/telegram_bot/test_broadcast.py diff --git a/homeassistant/components/telegram_bot/broadcast.py b/homeassistant/components/telegram_bot/broadcast.py index dd0d4cd1f31..dff061da243 100644 --- a/homeassistant/components/telegram_bot/broadcast.py +++ b/homeassistant/components/telegram_bot/broadcast.py @@ -1,17 +1,6 @@ """Support for Telegram bot to send messages only.""" -import logging - -from . import initialize_bot - -_LOGGER = logging.getLogger(__name__) -async def async_setup_platform(hass, config): +async def async_setup_platform(hass, bot, config): """Set up the Telegram broadcast platform.""" - bot = initialize_bot(config) - - bot_config = await hass.async_add_executor_job(bot.getMe) - _LOGGER.debug( - "Telegram broadcast platform setup with bot %s", bot_config["username"] - ) return True diff --git a/tests/components/telegram_bot/conftest.py b/tests/components/telegram_bot/conftest.py index 61818e4c377..d8d445fbb86 100644 --- a/tests/components/telegram_bot/conftest.py +++ b/tests/components/telegram_bot/conftest.py @@ -2,7 +2,6 @@ from unittest.mock import patch import pytest -from telegram.ext.dispatcher import Dispatcher from homeassistant.components.telegram_bot import ( CONF_ALLOWED_CHAT_IDS, @@ -176,12 +175,3 @@ async def polling_platform(hass, config_polling): config_polling, ) await hass.async_block_till_done() - - -@pytest.fixture(autouse=True) -def clear_dispatcher(): - """Clear the singleton that telegram.ext.dispatcher.Dispatcher sets on itself.""" - yield - Dispatcher._set_singleton(None) - # This is how python-telegram-bot resets the dispatcher in their test suite - Dispatcher._Dispatcher__singleton_semaphore.release() diff --git a/tests/components/telegram_bot/test_broadcast.py b/tests/components/telegram_bot/test_broadcast.py new file mode 100644 index 00000000000..22e4b8c6065 --- /dev/null +++ b/tests/components/telegram_bot/test_broadcast.py @@ -0,0 +1,20 @@ +"""Test Telegram broadcast.""" +from homeassistant.setup import async_setup_component + + +async def test_setup(hass): + """Test setting up Telegram broadcast.""" + assert await async_setup_component( + hass, + "telegram_bot", + { + "telegram_bot": { + "platform": "broadcast", + "api_key": "1234567890:ABC", + "allowed_chat_ids": [1], + } + }, + ) + await hass.async_block_till_done() + + assert hass.services.has_service("telegram_bot", "send_message") is True diff --git a/tests/components/telegram_bot/test_telegram_bot.py b/tests/components/telegram_bot/test_telegram_bot.py index 9b099a180f7..94d069a6bc5 100644 --- a/tests/components/telegram_bot/test_telegram_bot.py +++ b/tests/components/telegram_bot/test_telegram_bot.py @@ -1,4 +1,5 @@ """Tests for the telegram_bot component.""" +import pytest from telegram import Update from telegram.ext.dispatcher import Dispatcher @@ -8,6 +9,15 @@ from homeassistant.components.telegram_bot.webhooks import TELEGRAM_WEBHOOK_URL from tests.common import async_capture_events +@pytest.fixture(autouse=True) +def clear_dispatcher(): + """Clear the singleton that telegram.ext.dispatcher.Dispatcher sets on itself.""" + yield + Dispatcher._set_singleton(None) + # This is how python-telegram-bot resets the dispatcher in their test suite + Dispatcher._Dispatcher__singleton_semaphore.release() + + async def test_webhook_platform_init(hass, webhook_platform): """Test initialization of the webhooks platform.""" assert hass.services.has_service(DOMAIN, SERVICE_SEND_MESSAGE) is True