Fix telegram broadcast (#69452)

This commit is contained in:
Paulus Schoutsen 2022-04-06 10:44:02 -07:00 committed by GitHub
parent bc194cd209
commit c765e11f55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 22 deletions

View file

@ -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

View file

@ -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()

View file

@ -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

View file

@ -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