From 9eeae8eac68433260e3dcdbc764240d758809c5f Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 4 Feb 2026 13:15:23 +0100 Subject: [PATCH] Improve typing in telegram_bot (#162190) --- .../components/telegram_bot/__init__.py | 23 +++++++++++++++---- homeassistant/components/telegram_bot/bot.py | 2 +- .../components/telegram_bot/broadcast.py | 4 ++-- .../components/telegram_bot/polling.py | 2 +- .../components/telegram_bot/webhooks.py | 2 +- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/telegram_bot/__init__.py b/homeassistant/components/telegram_bot/__init__.py index 834f4e04941..95f3909e62e 100644 --- a/homeassistant/components/telegram_bot/__init__.py +++ b/homeassistant/components/telegram_bot/__init__.py @@ -3,7 +3,7 @@ from __future__ import annotations import logging -from types import ModuleType +from typing import Protocol, cast from telegram import Bot from telegram.constants import InputMediaType @@ -45,7 +45,12 @@ from homeassistant.helpers.typing import ConfigType, VolSchemaType from homeassistant.util.json import JsonValueType from . import broadcast, polling, webhooks -from .bot import TelegramBotConfigEntry, TelegramNotificationService, initialize_bot +from .bot import ( + BaseTelegramBot, + TelegramBotConfigEntry, + TelegramNotificationService, + initialize_bot, +) from .const import ( ATTR_ALLOWS_MULTIPLE_ANSWERS, ATTR_AUTHENTICATION, @@ -399,7 +404,16 @@ SERVICE_MAP: dict[str, VolSchemaType] = { } -MODULES: dict[str, ModuleType] = { +class BotPlatformModule(Protocol): + """Define the module protocol for telegram bot modules.""" + + async def async_setup_bot_platform( + self, hass: HomeAssistant, bot: Bot, config: TelegramBotConfigEntry + ) -> BaseTelegramBot | None: + """Set up the Telegram bot platform.""" + + +MODULES = { PLATFORM_BROADCAST: broadcast, PLATFORM_POLLING: polling, PLATFORM_WEBHOOKS: webhooks, @@ -826,8 +840,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: TelegramBotConfigEntry) p_type: str = entry.data[CONF_PLATFORM] _LOGGER.debug("Setting up %s.%s", DOMAIN, p_type) + module = cast(BotPlatformModule, MODULES[p_type]) try: - receiver_service = await MODULES[p_type].async_setup_platform(hass, bot, entry) + receiver_service = await module.async_setup_bot_platform(hass, bot, entry) except Exception: _LOGGER.exception("Error setting up Telegram bot %s", p_type) await bot.shutdown() diff --git a/homeassistant/components/telegram_bot/bot.py b/homeassistant/components/telegram_bot/bot.py index 2956ee9396e..91215b9a13b 100644 --- a/homeassistant/components/telegram_bot/bot.py +++ b/homeassistant/components/telegram_bot/bot.py @@ -288,7 +288,7 @@ class TelegramNotificationService: def __init__( self, hass: HomeAssistant, - app: BaseTelegramBot, + app: BaseTelegramBot | None, bot: Bot, config: TelegramBotConfigEntry, parser: str, diff --git a/homeassistant/components/telegram_bot/broadcast.py b/homeassistant/components/telegram_bot/broadcast.py index 147423c4ce0..43dbb1f8c34 100644 --- a/homeassistant/components/telegram_bot/broadcast.py +++ b/homeassistant/components/telegram_bot/broadcast.py @@ -7,8 +7,8 @@ from homeassistant.core import HomeAssistant from .bot import BaseTelegramBot, TelegramBotConfigEntry -async def async_setup_platform( +async def async_setup_bot_platform( hass: HomeAssistant, bot: Bot, config: TelegramBotConfigEntry -) -> type[BaseTelegramBot] | None: +) -> BaseTelegramBot | None: """Set up the Telegram broadcast platform.""" return None diff --git a/homeassistant/components/telegram_bot/polling.py b/homeassistant/components/telegram_bot/polling.py index 59231853e59..52a136fa4c9 100644 --- a/homeassistant/components/telegram_bot/polling.py +++ b/homeassistant/components/telegram_bot/polling.py @@ -14,7 +14,7 @@ from .helpers import get_base_url _LOGGER = logging.getLogger(__name__) -async def async_setup_platform( +async def async_setup_bot_platform( hass: HomeAssistant, bot: Bot, config: TelegramBotConfigEntry ) -> BaseTelegramBot | None: """Set up the Telegram polling platform.""" diff --git a/homeassistant/components/telegram_bot/webhooks.py b/homeassistant/components/telegram_bot/webhooks.py index 7b8a1749631..980ecd19333 100644 --- a/homeassistant/components/telegram_bot/webhooks.py +++ b/homeassistant/components/telegram_bot/webhooks.py @@ -27,7 +27,7 @@ TELEGRAM_WEBHOOK_URL = "/api/telegram_webhooks" SECRET_TOKEN_LENGTH = 32 -async def async_setup_platform( +async def async_setup_bot_platform( hass: HomeAssistant, bot: Bot, config: TelegramBotConfigEntry ) -> BaseTelegramBot | None: """Set up the Telegram webhooks platform."""