Async syntax 5, light & lock & remote & scene & telegram & helpers (#17019)

This commit is contained in:
cdce8p authored and Paulus Schoutsen committed 2018-10-01 08:56:50 +02:00
1 parent 9e4c8f45d6
commit 3b5e5cbcd6
28 files changed
+112 -195

No files matched your search

@@ -4,7 +4,6 @@ Component to send and receive Telegram messages.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/telegram_bot/
"""
import asyncio
import io
from functools import partial
import logging
@@ -210,8 +209,7 @@ def load_data(hass, url=None, filepath=None, username=None, password=None,
return None
@asyncio.coroutine
def async_setup(hass, config):
async def async_setup(hass, config):
"""Set up the Telegram bot component."""
if not config[DOMAIN]:
return False
@@ -220,7 +218,7 @@ def async_setup(hass, config):
p_type = p_config.get(CONF_PLATFORM)
platform = yield from async_prepare_setup_platform(
platform = await async_prepare_setup_platform(
hass, config, DOMAIN, p_type)
if platform is None:
@@ -228,7 +226,7 @@ def async_setup(hass, config):
_LOGGER.info("Setting up %s.%s", DOMAIN, p_type)
try:
receiver_service = yield from \
receiver_service = await \
platform.async_setup_platform(hass, p_config)
if receiver_service is False:
_LOGGER.error(
@@ -247,8 +245,7 @@ def async_setup(hass, config):
p_config.get(ATTR_PARSER)
)
@asyncio.coroutine
def async_send_telegram_message(service):
async def async_send_telegram_message(service):
"""Handle sending Telegram Bot message service calls."""
def _render_template_attr(data, attribute):
attribute_templ = data.get(attribute)
@@ -274,23 +271,23 @@ def async_setup(hass, config):
_LOGGER.debug("New telegram message %s: %s", msgtype, kwargs)
if msgtype == SERVICE_SEND_MESSAGE:
yield from hass.async_add_job(
await hass.async_add_job(
partial(notify_service.send_message, **kwargs))
elif msgtype in [SERVICE_SEND_PHOTO, SERVICE_SEND_STICKER,
SERVICE_SEND_VIDEO, SERVICE_SEND_DOCUMENT]:
yield from hass.async_add_job(
await hass.async_add_job(
partial(notify_service.send_file, msgtype, **kwargs))
elif msgtype == SERVICE_SEND_LOCATION:
yield from hass.async_add_job(
await hass.async_add_job(
partial(notify_service.send_location, **kwargs))
elif msgtype == SERVICE_ANSWER_CALLBACK_QUERY:
yield from hass.async_add_job(
await hass.async_add_job(
partial(notify_service.answer_callback_query, **kwargs))
elif msgtype == SERVICE_DELETE_MESSAGE:
yield from hass.async_add_job(
await hass.async_add_job(
partial(notify_service.delete_message, **kwargs))
else:
yield from hass.async_add_job(
await hass.async_add_job(
partial(notify_service.edit_message, msgtype, **kwargs))
# Register notification services
@@ -4,7 +4,6 @@ Telegram bot implementation to send messages only.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/telegram_bot.broadcast/
"""
import asyncio
import logging
from homeassistant.components.telegram_bot import (
@@ -16,14 +15,13 @@ _LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = TELEGRAM_PLATFORM_SCHEMA
@asyncio.coroutine
def async_setup_platform(hass, config):
async def async_setup_platform(hass, config):
"""Set up the Telegram broadcast platform."""
# Check the API key works
bot = initialize_bot(config)
bot_config = yield from hass.async_add_job(bot.getMe)
bot_config = await hass.async_add_job(bot.getMe)
_LOGGER.debug("Telegram broadcast platform setup with bot %s",
bot_config['username'])
return True
@@ -4,7 +4,6 @@ Telegram bot polling implementation.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/telegram_bot.polling/
"""
import asyncio
import logging
from homeassistant.components.telegram_bot import (
@@ -20,8 +19,7 @@ _LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = TELEGRAM_PLATFORM_SCHEMA
@asyncio.coroutine
def async_setup_platform(hass, config):
async def async_setup_platform(hass, config):
"""Set up the Telegram polling platform."""
bot = initialize_bot(config)
pol = TelegramPoll(bot, hass, config[CONF_ALLOWED_CHAT_IDS])
@@ -4,7 +4,6 @@ Allows utilizing telegram webhooks.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/telegram_bot.webhooks/
"""
import asyncio
import datetime as dt
from ipaddress import ip_network
import logging
@@ -47,13 +46,12 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
})
@asyncio.coroutine
def async_setup_platform(hass, config):
async def async_setup_platform(hass, config):
"""Set up the Telegram webhooks platform."""
import telegram
bot = initialize_bot(config)
current_status = yield from hass.async_add_job(bot.getWebhookInfo)
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:
@@ -81,7 +79,7 @@ def async_setup_platform(hass, config):
retry_num)
if current_status and current_status['url'] != handler_url:
result = yield from hass.async_add_job(_try_to_set_webhook)
result = await hass.async_add_job(_try_to_set_webhook)
if result:
_LOGGER.info("Set new telegram webhook %s", handler_url)
else:
@@ -108,8 +106,7 @@ class BotPushReceiver(HomeAssistantView, BaseTelegramBotEntity):
BaseTelegramBotEntity.__init__(self, hass, allowed_chat_ids)
self.trusted_networks = trusted_networks
@asyncio.coroutine
def post(self, request):
async def post(self, request):
"""Accept the POST from telegram."""
real_ip = request[KEY_REAL_IP]
if not any(real_ip in net for net in self.trusted_networks):
@@ -117,7 +114,7 @@ class BotPushReceiver(HomeAssistantView, BaseTelegramBotEntity):
return self.json_message('Access denied', HTTP_UNAUTHORIZED)
try:
data = yield from request.json()
data = await request.json()
except ValueError:
return self.json_message('Invalid JSON', HTTP_BAD_REQUEST)