Async syntax 4/8 (#17018)

* Async syntax 4, media_player & notify

* Pylint fixes
This commit is contained in:
cdce8p authored and Paulus Schoutsen committed 2018-10-01 08:58:21 +02:00
1 parent 3b5e5cbcd6
commit 134eeecd65
17 files changed
+212 -314

No files matched your search

+10 -14
View File
@@ -49,18 +49,16 @@ NOTIFY_SERVICE_SCHEMA = vol.Schema({
})
@asyncio.coroutine
def async_setup(hass, config):
async def async_setup(hass, config):
"""Set up the notify services."""
targets = {}
@asyncio.coroutine
def async_setup_platform(p_type, p_config=None, discovery_info=None):
async def async_setup_platform(p_type, p_config=None, discovery_info=None):
"""Set up a notify platform."""
if p_config is None:
p_config = {}
platform = yield from async_prepare_setup_platform(
platform = await async_prepare_setup_platform(
hass, config, DOMAIN, p_type)
if platform is None:
@@ -71,10 +69,10 @@ def async_setup(hass, config):
notify_service = None
try:
if hasattr(platform, 'async_get_service'):
notify_service = yield from \
notify_service = await \
platform.async_get_service(hass, p_config, discovery_info)
elif hasattr(platform, 'get_service'):
notify_service = yield from hass.async_add_job(
notify_service = await hass.async_add_job(
platform.get_service, hass, p_config, discovery_info)
else:
raise HomeAssistantError("Invalid notify platform.")
@@ -97,8 +95,7 @@ def async_setup(hass, config):
if discovery_info is None:
discovery_info = {}
@asyncio.coroutine
def async_notify_message(service):
async def async_notify_message(service):
"""Handle sending notification message service calls."""
kwargs = {}
message = service.data[ATTR_MESSAGE]
@@ -117,7 +114,7 @@ def async_setup(hass, config):
kwargs[ATTR_MESSAGE] = message.async_render()
kwargs[ATTR_DATA] = service.data.get(ATTR_DATA)
yield from notify_service.async_send_message(**kwargs)
await notify_service.async_send_message(**kwargs)
if hasattr(notify_service, 'targets'):
platform_name = (
@@ -147,12 +144,11 @@ def async_setup(hass, config):
in config_per_platform(config, DOMAIN)]
if setup_tasks:
yield from asyncio.wait(setup_tasks, loop=hass.loop)
await asyncio.wait(setup_tasks, loop=hass.loop)
@asyncio.coroutine
def async_platform_discovered(platform, info):
async def async_platform_discovered(platform, info):
"""Handle for discovered platform."""
yield from async_setup_platform(platform, discovery_info=info)
await async_setup_platform(platform, discovery_info=info)
discovery.async_listen_platform(hass, DOMAIN, async_platform_discovered)
+7 -10
View File
@@ -4,7 +4,6 @@ Discord platform for notify component.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.discord/
"""
import asyncio
import logging
import voluptuous as vol
@@ -39,8 +38,7 @@ class DiscordNotificationService(BaseNotificationService):
self.token = token
self.hass = hass
@asyncio.coroutine
def async_send_message(self, message, **kwargs):
async def async_send_message(self, message, **kwargs):
"""Login to Discord, send message to channel(s) and log out."""
import discord
discord_bot = discord.Client(loop=self.hass.loop)
@@ -51,8 +49,7 @@ class DiscordNotificationService(BaseNotificationService):
# pylint: disable=unused-variable
@discord_bot.event
@asyncio.coroutine
def on_ready():
async def on_ready():
"""Send the messages when the bot is ready."""
try:
data = kwargs.get(ATTR_DATA)
@@ -60,14 +57,14 @@ class DiscordNotificationService(BaseNotificationService):
images = data.get(ATTR_IMAGES)
for channelid in kwargs[ATTR_TARGET]:
channel = discord.Object(id=channelid)
yield from discord_bot.send_message(channel, message)
await discord_bot.send_message(channel, message)
if images:
for anum, f_name in enumerate(images):
yield from discord_bot.send_file(channel, f_name)
await discord_bot.send_file(channel, f_name)
except (discord.errors.HTTPException,
discord.errors.NotFound) as error:
_LOGGER.warning("Communication error: %s", error)
yield from discord_bot.logout()
yield from discord_bot.close()
await discord_bot.logout()
await discord_bot.close()
yield from discord_bot.start(self.token)
await discord_bot.start(self.token)
+3 -5
View File
@@ -41,8 +41,7 @@ def update(input_dict, update_source):
return input_dict
@asyncio.coroutine
def async_get_service(hass, config, discovery_info=None):
async def async_get_service(hass, config, discovery_info=None):
"""Get the Group notification service."""
return GroupNotifyPlatform(hass, config.get(CONF_SERVICES))
@@ -55,8 +54,7 @@ class GroupNotifyPlatform(BaseNotificationService):
self.hass = hass
self.entities = entities
@asyncio.coroutine
def async_send_message(self, message="", **kwargs):
async def async_send_message(self, message="", **kwargs):
"""Send message to all entities in the group."""
payload = {ATTR_MESSAGE: message}
payload.update({key: val for key, val in kwargs.items() if val})
@@ -70,4 +68,4 @@ class GroupNotifyPlatform(BaseNotificationService):
DOMAIN, entity.get(ATTR_SERVICE), sending_payload))
if tasks:
yield from asyncio.wait(tasks, loop=self.hass.loop)
await asyncio.wait(tasks, loop=self.hass.loop)
+3 -6
View File
@@ -4,7 +4,6 @@ Kodi notification service.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.kodi/
"""
import asyncio
import logging
import aiohttp
@@ -38,8 +37,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
ATTR_DISPLAYTIME = 'displaytime'
@asyncio.coroutine
def async_get_service(hass, config, discovery_info=None):
async def async_get_service(hass, config, discovery_info=None):
"""Return the notify service."""
url = '{}:{}'.format(config.get(CONF_HOST), config.get(CONF_PORT))
@@ -86,8 +84,7 @@ class KodiNotificationService(BaseNotificationService):
self._server = jsonrpc_async.Server(self._url, **kwargs)
@asyncio.coroutine
def async_send_message(self, message="", **kwargs):
async def async_send_message(self, message="", **kwargs):
"""Send a message to Kodi."""
import jsonrpc_async
try:
@@ -96,7 +93,7 @@ class KodiNotificationService(BaseNotificationService):
displaytime = data.get(ATTR_DISPLAYTIME, 10000)
icon = data.get(ATTR_ICON, "info")
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
yield from self._server.GUI.ShowNotification(
await self._server.GUI.ShowNotification(
title, message, icon, displaytime)
except jsonrpc_async.TransportError:
+4 -6
View File
@@ -25,8 +25,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
})
@asyncio.coroutine
def async_get_service(hass, config, discovery_info=None):
async def async_get_service(hass, config, discovery_info=None):
"""Get the Prowl notification service."""
return ProwlNotificationService(hass, config[CONF_API_KEY])
@@ -39,8 +38,7 @@ class ProwlNotificationService(BaseNotificationService):
self._hass = hass
self._api_key = api_key
@asyncio.coroutine
def async_send_message(self, message, **kwargs):
async def async_send_message(self, message, **kwargs):
"""Send the message to the user."""
response = None
session = None
@@ -59,8 +57,8 @@ class ProwlNotificationService(BaseNotificationService):
try:
with async_timeout.timeout(10, loop=self._hass.loop):
response = yield from session.post(url, data=payload)
result = yield from response.text()
response = await session.post(url, data=payload)
result = await response.text()
if response.status != 200 or 'error' in result:
_LOGGER.error("Prowl service returned http "