mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Fix for telegram polling. (added pausing when error occurs) (#10214)
* Fix for telegram polling. (added pausing when error occurs) * fix pylint error. invalid variable name ( Exception as _e)). Don't understand why as removing the underscore fails with my local pylint.. * fixing too short variable name. * moved logic to `check_incoming` * fix line too long error. * Simplify
This commit is contained in:
parent
8e1a73dd0f
commit
143d9492b2
@ -23,6 +23,13 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = TELEGRAM_PLATFORM_SCHEMA
|
PLATFORM_SCHEMA = TELEGRAM_PLATFORM_SCHEMA
|
||||||
|
RETRY_SLEEP = 10
|
||||||
|
|
||||||
|
|
||||||
|
class WrongHttpStatus(Exception):
|
||||||
|
"""Thrown when a wrong http status is received."""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@ -74,8 +81,6 @@ class TelegramPoll(BaseTelegramBotEntity):
|
|||||||
def get_updates(self, offset):
|
def get_updates(self, offset):
|
||||||
"""Bypass the default long polling method to enable asyncio."""
|
"""Bypass the default long polling method to enable asyncio."""
|
||||||
resp = None
|
resp = None
|
||||||
_json = {'result': [], 'ok': True} # Empty result.
|
|
||||||
|
|
||||||
if offset:
|
if offset:
|
||||||
self.post_data['offset'] = offset
|
self.post_data['offset'] = offset
|
||||||
try:
|
try:
|
||||||
@ -86,23 +91,32 @@ class TelegramPoll(BaseTelegramBotEntity):
|
|||||||
)
|
)
|
||||||
if resp.status == 200:
|
if resp.status == 200:
|
||||||
_json = yield from resp.json()
|
_json = yield from resp.json()
|
||||||
|
return _json
|
||||||
else:
|
else:
|
||||||
_LOGGER.error("Error %s on %s", resp.status, self.update_url)
|
raise WrongHttpStatus('wrong status %s', resp.status)
|
||||||
|
|
||||||
except ValueError:
|
|
||||||
_LOGGER.error("Error parsing Json message")
|
|
||||||
except (asyncio.TimeoutError, ClientError):
|
|
||||||
_LOGGER.error("Client connection error")
|
|
||||||
finally:
|
finally:
|
||||||
if resp is not None:
|
if resp is not None:
|
||||||
yield from resp.release()
|
yield from resp.release()
|
||||||
|
|
||||||
return _json
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def handle(self):
|
def check_incoming(self):
|
||||||
"""Receiving and processing incoming messages."""
|
"""Continuously check for incoming telegram messages."""
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
_updates = yield from self.get_updates(self.update_id)
|
_updates = yield from self.get_updates(self.update_id)
|
||||||
|
except (WrongHttpStatus, ClientError) as err:
|
||||||
|
# WrongHttpStatus: Non-200 status code.
|
||||||
|
# Occurs at times (mainly 502) and recovers
|
||||||
|
# automatically. Pause for a while before retrying.
|
||||||
|
_LOGGER.error(err)
|
||||||
|
yield from asyncio.sleep(RETRY_SLEEP)
|
||||||
|
except (asyncio.TimeoutError, ValueError):
|
||||||
|
# Long polling timeout. Nothing serious.
|
||||||
|
# Json error. Just retry for the next message.
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# no exception raised. update received data.
|
||||||
_updates = _updates.get('result')
|
_updates = _updates.get('result')
|
||||||
if _updates is None:
|
if _updates is None:
|
||||||
_LOGGER.error("Incorrect result received.")
|
_LOGGER.error("Incorrect result received.")
|
||||||
@ -110,16 +124,5 @@ class TelegramPoll(BaseTelegramBotEntity):
|
|||||||
for update in _updates:
|
for update in _updates:
|
||||||
self.update_id = update['update_id'] + 1
|
self.update_id = update['update_id'] + 1
|
||||||
self.process_message(update)
|
self.process_message(update)
|
||||||
|
|
||||||
@asyncio.coroutine
|
|
||||||
def check_incoming(self):
|
|
||||||
"""Loop which continuously checks for incoming telegram messages."""
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
# Each handle call sends a long polling post request
|
|
||||||
# to the telegram server. If no incoming message it will return
|
|
||||||
# an empty list. Calling self.handle() without any delay or
|
|
||||||
# timeout will for this reason not really stress the processor.
|
|
||||||
yield from self.handle()
|
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
_LOGGER.debug("Stopping Telegram polling bot")
|
_LOGGER.debug("Stopping Telegram polling bot")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user