mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 17:49:37 +01:00
Improve typing in telegram_bot (#162190)
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -288,7 +288,7 @@ class TelegramNotificationService:
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
app: BaseTelegramBot,
|
||||
app: BaseTelegramBot | None,
|
||||
bot: Bot,
|
||||
config: TelegramBotConfigEntry,
|
||||
parser: str,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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."""
|
||||
|
||||
Reference in New Issue
Block a user