2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Telegram bot using polling."""
|
2017-04-12 06:10:56 +02:00
|
|
|
import logging
|
|
|
|
|
2019-10-18 02:19:34 +02:00
|
|
|
from telegram import Update
|
2019-12-09 14:41:48 +01:00
|
|
|
from telegram.error import NetworkError, RetryAfter, TelegramError, TimedOut
|
2022-04-03 05:39:14 +02:00
|
|
|
from telegram.ext import CallbackContext, TypeHandler, Updater
|
2019-10-18 02:19:34 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP
|
2017-04-12 06:10:56 +02:00
|
|
|
|
2022-04-03 05:39:14 +02:00
|
|
|
from . import BaseTelegramBotEntity
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2017-04-12 06:10:56 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-04-03 05:39:14 +02:00
|
|
|
async def async_setup_platform(hass, bot, config):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Set up the Telegram polling platform."""
|
2022-04-03 05:39:14 +02:00
|
|
|
pollbot = PollBot(hass, bot, config)
|
2017-04-12 06:10:56 +02:00
|
|
|
|
2022-04-03 05:39:14 +02:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, pollbot.start_polling)
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, pollbot.stop_polling)
|
2017-04-12 06:10:56 +02:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-07-09 19:52:26 +02:00
|
|
|
def process_error(update: Update, context: CallbackContext) -> None:
|
2018-09-26 12:59:37 +03:00
|
|
|
"""Telegram bot error handler."""
|
|
|
|
try:
|
2022-07-09 19:52:26 +02:00
|
|
|
if context.error:
|
|
|
|
raise context.error
|
2018-09-26 12:59:37 +03:00
|
|
|
except (TimedOut, NetworkError, RetryAfter):
|
|
|
|
# Long polling timeout or connection problem. Nothing serious.
|
|
|
|
pass
|
|
|
|
except TelegramError:
|
2020-12-17 21:09:58 +01:00
|
|
|
_LOGGER.error('Update "%s" caused error: "%s"', update, context.error)
|
2018-09-26 12:59:37 +03:00
|
|
|
|
|
|
|
|
2022-04-03 05:39:14 +02:00
|
|
|
class PollBot(BaseTelegramBotEntity):
|
|
|
|
"""
|
|
|
|
Controls the Updater object that holds the bot and a dispatcher.
|
2018-09-26 12:59:37 +03:00
|
|
|
|
2022-04-03 05:39:14 +02:00
|
|
|
The dispatcher is set up by the super class to pass telegram updates to `self.handle_update`
|
|
|
|
"""
|
2018-09-26 12:59:37 +03:00
|
|
|
|
2022-04-03 05:39:14 +02:00
|
|
|
def __init__(self, hass, bot, config):
|
|
|
|
"""Create Updater and Dispatcher before calling super()."""
|
|
|
|
self.bot = bot
|
2018-09-26 12:59:37 +03:00
|
|
|
self.updater = Updater(bot=bot, workers=4)
|
|
|
|
self.dispatcher = self.updater.dispatcher
|
2022-04-03 05:39:14 +02:00
|
|
|
self.dispatcher.add_handler(TypeHandler(Update, self.handle_update))
|
2018-09-26 12:59:37 +03:00
|
|
|
self.dispatcher.add_error_handler(process_error)
|
2022-04-03 05:39:14 +02:00
|
|
|
super().__init__(hass, config)
|
2017-04-12 06:10:56 +02:00
|
|
|
|
2022-04-03 05:39:14 +02:00
|
|
|
def start_polling(self, event=None):
|
2017-04-12 06:10:56 +02:00
|
|
|
"""Start the polling task."""
|
2022-04-03 05:39:14 +02:00
|
|
|
_LOGGER.debug("Starting polling")
|
2018-09-26 12:59:37 +03:00
|
|
|
self.updater.start_polling()
|
2017-04-12 06:10:56 +02:00
|
|
|
|
2022-04-03 05:39:14 +02:00
|
|
|
def stop_polling(self, event=None):
|
2017-04-12 06:10:56 +02:00
|
|
|
"""Stop the polling task."""
|
2022-04-03 05:39:14 +02:00
|
|
|
_LOGGER.debug("Stopping polling")
|
2018-09-26 12:59:37 +03:00
|
|
|
self.updater.stop()
|