This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

@@ -5,36 +5,48 @@ import logging
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.http.const import KEY_REAL_IP
from homeassistant.const import (
EVENT_HOMEASSISTANT_STOP, HTTP_BAD_REQUEST, HTTP_UNAUTHORIZED)
EVENT_HOMEASSISTANT_STOP,
HTTP_BAD_REQUEST,
HTTP_UNAUTHORIZED,
)
from . import (CONF_ALLOWED_CHAT_IDS, CONF_TRUSTED_NETWORKS, CONF_URL,
BaseTelegramBotEntity, initialize_bot)
from . import (
CONF_ALLOWED_CHAT_IDS,
CONF_TRUSTED_NETWORKS,
CONF_URL,
BaseTelegramBotEntity,
initialize_bot,
)
_LOGGER = logging.getLogger(__name__)
TELEGRAM_HANDLER_URL = '/api/telegram_webhooks'
REMOVE_HANDLER_URL = ''
TELEGRAM_HANDLER_URL = "/api/telegram_webhooks"
REMOVE_HANDLER_URL = ""
async def async_setup_platform(hass, config):
"""Set up the Telegram webhooks platform."""
import telegram
bot = initialize_bot(config)
current_status = await hass.async_add_job(bot.getWebhookInfo)
base_url = config.get(CONF_URL, hass.config.api.base_url)
# Some logging of Bot current status:
last_error_date = getattr(current_status, 'last_error_date', None)
last_error_date = getattr(current_status, "last_error_date", None)
if (last_error_date is not None) and (isinstance(last_error_date, int)):
last_error_date = dt.datetime.fromtimestamp(last_error_date)
_LOGGER.info("telegram webhook last_error_date: %s. Status: %s",
last_error_date, current_status)
_LOGGER.info(
"telegram webhook last_error_date: %s. Status: %s",
last_error_date,
current_status,
)
else:
_LOGGER.debug("telegram webhook Status: %s", current_status)
handler_url = "{0}{1}".format(base_url, TELEGRAM_HANDLER_URL)
if not handler_url.startswith('https'):
if not handler_url.startswith("https"):
_LOGGER.error("Invalid telegram webhook %s must be https", handler_url)
return False
@@ -45,10 +57,9 @@ async def async_setup_platform(hass, config):
return bot.setWebhook(handler_url, timeout=5)
except telegram.error.TimedOut:
retry_num += 1
_LOGGER.warning("Timeout trying to set webhook (retry #%d)",
retry_num)
_LOGGER.warning("Timeout trying to set webhook (retry #%d)", retry_num)
if current_status and current_status['url'] != handler_url:
if current_status and current_status["url"] != handler_url:
result = await hass.async_add_job(_try_to_set_webhook)
if result:
_LOGGER.info("Set new telegram webhook %s", handler_url)
@@ -57,10 +68,13 @@ async def async_setup_platform(hass, config):
return False
hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP,
lambda event: bot.setWebhook(REMOVE_HANDLER_URL))
hass.http.register_view(BotPushReceiver(
hass, config[CONF_ALLOWED_CHAT_IDS], config[CONF_TRUSTED_NETWORKS]))
EVENT_HOMEASSISTANT_STOP, lambda event: bot.setWebhook(REMOVE_HANDLER_URL)
)
hass.http.register_view(
BotPushReceiver(
hass, config[CONF_ALLOWED_CHAT_IDS], config[CONF_TRUSTED_NETWORKS]
)
)
return True
@@ -69,7 +83,7 @@ class BotPushReceiver(HomeAssistantView, BaseTelegramBotEntity):
requires_auth = False
url = TELEGRAM_HANDLER_URL
name = 'telegram_webhooks'
name = "telegram_webhooks"
def __init__(self, hass, allowed_chat_ids, trusted_networks):
"""Initialize the class."""
@@ -81,13 +95,13 @@ class BotPushReceiver(HomeAssistantView, BaseTelegramBotEntity):
real_ip = request[KEY_REAL_IP]
if not any(real_ip in net for net in self.trusted_networks):
_LOGGER.warning("Access denied from %s", real_ip)
return self.json_message('Access denied', HTTP_UNAUTHORIZED)
return self.json_message("Access denied", HTTP_UNAUTHORIZED)
try:
data = await request.json()
except ValueError:
return self.json_message('Invalid JSON', HTTP_BAD_REQUEST)
return self.json_message("Invalid JSON", HTTP_BAD_REQUEST)
if not self.process_message(data):
return self.json_message('Invalid message', HTTP_BAD_REQUEST)
return self.json_message("Invalid message", HTTP_BAD_REQUEST)
return None