Asyncio notify component migration (#5377)
* Async migrate notify/platform * convert group to async * fix unittest
This commit is contained in:
1 parent
f7ac644c11
commit
2a362fd1ff
6 files changed
+136
-88
No files matched your search
@@ -4,13 +4,15 @@ Provides functionality to notify people.
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/notify/
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
from functools import partial
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.bootstrap as bootstrap
|
||||
from homeassistant.bootstrap import async_prepare_setup_platform
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.config import load_yaml_config_file
|
||||
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
||||
@@ -64,91 +66,110 @@ def send_message(hass, message, title=None, data=None):
|
||||
hass.services.call(DOMAIN, SERVICE_NOTIFY, info)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
@asyncio.coroutine
|
||||
def async_setup(hass, config):
|
||||
"""Setup the notify services."""
|
||||
descriptions = load_yaml_config_file(
|
||||
descriptions = yield from hass.loop.run_in_executor(
|
||||
None, load_yaml_config_file,
|
||||
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
||||
|
||||
targets = {}
|
||||
|
||||
def setup_notify_platform(platform, p_config=None, discovery_info=None):
|
||||
@asyncio.coroutine
|
||||
def async_setup_platform(p_type, p_config=None, discovery_info=None):
|
||||
"""Set up a notify platform."""
|
||||
if p_config is None:
|
||||
p_config = {}
|
||||
if discovery_info is None:
|
||||
discovery_info = {}
|
||||
|
||||
notify_implementation = bootstrap.prepare_setup_platform(
|
||||
hass, config, DOMAIN, platform)
|
||||
platform = yield from async_prepare_setup_platform(
|
||||
hass, config, DOMAIN, p_type)
|
||||
|
||||
if notify_implementation is None:
|
||||
if platform is None:
|
||||
_LOGGER.error("Unknown notification service specified")
|
||||
return False
|
||||
return
|
||||
|
||||
notify_service = notify_implementation.get_service(
|
||||
hass, p_config, discovery_info)
|
||||
_LOGGER.info("Setting up %s.%s", DOMAIN, p_type)
|
||||
notify_service = None
|
||||
try:
|
||||
if hasattr(platform, 'async_get_service'):
|
||||
notify_service = yield from \
|
||||
platform.async_get_service(hass, p_config, discovery_info)
|
||||
elif hasattr(platform, 'get_service'):
|
||||
notify_service = yield from hass.loop.run_in_executor(
|
||||
None, platform.get_service, hass, p_config, discovery_info)
|
||||
else:
|
||||
raise HomeAssistantError("Invalid notify platform.")
|
||||
|
||||
if notify_service is None:
|
||||
_LOGGER.error("Failed to initialize notification service %s",
|
||||
platform)
|
||||
return False
|
||||
if notify_service is None:
|
||||
_LOGGER.error(
|
||||
"Failed to initialize notification service %s", p_type)
|
||||
return
|
||||
|
||||
def notify_message(notify_service, call):
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception('Error setting up platform %s', p_type)
|
||||
return
|
||||
|
||||
notify_service.hass = hass
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_notify_message(service):
|
||||
"""Handle sending notification message service calls."""
|
||||
kwargs = {}
|
||||
message = call.data[ATTR_MESSAGE]
|
||||
title = call.data.get(ATTR_TITLE)
|
||||
message = service.data[ATTR_MESSAGE]
|
||||
title = service.data.get(ATTR_TITLE)
|
||||
|
||||
if title:
|
||||
title.hass = hass
|
||||
kwargs[ATTR_TITLE] = title.render()
|
||||
kwargs[ATTR_TITLE] = title.async_render()
|
||||
|
||||
if targets.get(call.service) is not None:
|
||||
kwargs[ATTR_TARGET] = [targets[call.service]]
|
||||
elif call.data.get(ATTR_TARGET) is not None:
|
||||
kwargs[ATTR_TARGET] = call.data.get(ATTR_TARGET)
|
||||
if targets.get(service.service) is not None:
|
||||
kwargs[ATTR_TARGET] = [targets[service.service]]
|
||||
elif service.data.get(ATTR_TARGET) is not None:
|
||||
kwargs[ATTR_TARGET] = service.data.get(ATTR_TARGET)
|
||||
|
||||
message.hass = hass
|
||||
kwargs[ATTR_MESSAGE] = message.render()
|
||||
kwargs[ATTR_DATA] = call.data.get(ATTR_DATA)
|
||||
kwargs[ATTR_MESSAGE] = message.async_render()
|
||||
kwargs[ATTR_DATA] = service.data.get(ATTR_DATA)
|
||||
|
||||
notify_service.send_message(**kwargs)
|
||||
|
||||
service_call_handler = partial(notify_message, notify_service)
|
||||
yield from notify_service.async_send_message(**kwargs)
|
||||
|
||||
if hasattr(notify_service, 'targets'):
|
||||
platform_name = (
|
||||
p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or
|
||||
platform)
|
||||
p_type)
|
||||
for name, target in notify_service.targets.items():
|
||||
target_name = slugify('{}_{}'.format(platform_name, name))
|
||||
targets[target_name] = target
|
||||
hass.services.register(DOMAIN, target_name,
|
||||
service_call_handler,
|
||||
descriptions.get(SERVICE_NOTIFY),
|
||||
schema=NOTIFY_SERVICE_SCHEMA)
|
||||
hass.services.async_register(
|
||||
DOMAIN, target_name, async_notify_message,
|
||||
descriptions.get(SERVICE_NOTIFY),
|
||||
schema=NOTIFY_SERVICE_SCHEMA)
|
||||
|
||||
platform_name = (
|
||||
p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or
|
||||
SERVICE_NOTIFY)
|
||||
platform_name_slug = slugify(platform_name)
|
||||
|
||||
hass.services.register(
|
||||
DOMAIN, platform_name_slug, service_call_handler,
|
||||
hass.services.async_register(
|
||||
DOMAIN, platform_name_slug, async_notify_message,
|
||||
descriptions.get(SERVICE_NOTIFY), schema=NOTIFY_SERVICE_SCHEMA)
|
||||
|
||||
return True
|
||||
|
||||
for platform, p_config in config_per_platform(config, DOMAIN):
|
||||
if not setup_notify_platform(platform, p_config):
|
||||
_LOGGER.error("Failed to set up platform %s", platform)
|
||||
continue
|
||||
setup_tasks = [async_setup_platform(p_type, p_config) for p_type, p_config
|
||||
in config_per_platform(config, DOMAIN)]
|
||||
|
||||
def platform_discovered(platform, info):
|
||||
if setup_tasks:
|
||||
yield from asyncio.wait(setup_tasks, loop=hass.loop)
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_platform_discovered(platform, info):
|
||||
"""Callback to load a platform."""
|
||||
setup_notify_platform(platform, discovery_info=info)
|
||||
yield from async_setup_platform(platform, discovery_info=info)
|
||||
|
||||
discovery.listen_platform(hass, DOMAIN, platform_discovered)
|
||||
discovery.async_listen_platform(hass, DOMAIN, async_platform_discovered)
|
||||
|
||||
return True
|
||||
|
||||
@@ -156,9 +177,20 @@ def setup(hass, config):
|
||||
class BaseNotificationService(object):
|
||||
"""An abstract class for notification services."""
|
||||
|
||||
hass = None
|
||||
|
||||
def send_message(self, message, **kwargs):
|
||||
"""Send a message.
|
||||
|
||||
kwargs can contain ATTR_TITLE to specify a title.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
raise NotImplementedError()
|
||||
|
||||
def async_send_message(self, message, **kwargs):
|
||||
"""Send a message.
|
||||
|
||||
kwargs can contain ATTR_TITLE to specify a title.
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
return self.hass.loop.run_in_executor(
|
||||
None, partial(self.send_message, message, **kwargs))
|
||||
@@ -4,7 +4,7 @@ import asyncio
|
||||
import voluptuous as vol
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.notify import (
|
||||
PLATFORM_SCHEMA, BaseNotificationService)
|
||||
PLATFORM_SCHEMA, BaseNotificationService, ATTR_TARGET)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -32,20 +32,16 @@ class DiscordNotificationService(BaseNotificationService):
|
||||
self.hass = hass
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_send_message(self, message, target):
|
||||
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)
|
||||
|
||||
yield from discord_bot.login(self.token)
|
||||
|
||||
for channelid in target:
|
||||
for channelid in kwargs[ATTR_TARGET]:
|
||||
channel = discord.Object(id=channelid)
|
||||
yield from discord_bot.send_message(channel, message)
|
||||
|
||||
yield from discord_bot.logout()
|
||||
yield from discord_bot.close()
|
||||
|
||||
def send_message(self, message=None, target=None, **kwargs):
|
||||
"""Send a message using Discord."""
|
||||
self.hass.async_add_job(self.async_send_message(message, target))
|
||||
@@ -4,15 +4,15 @@ Group platform for notify component.
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/notify.group/
|
||||
"""
|
||||
import asyncio
|
||||
import collections
|
||||
from copy import deepcopy
|
||||
import logging
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import ATTR_SERVICE
|
||||
from homeassistant.components.notify import (DOMAIN, ATTR_MESSAGE, ATTR_DATA,
|
||||
PLATFORM_SCHEMA,
|
||||
BaseNotificationService)
|
||||
from homeassistant.components.notify import (
|
||||
DOMAIN, ATTR_MESSAGE, ATTR_DATA, PLATFORM_SCHEMA, BaseNotificationService)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -28,7 +28,10 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
|
||||
|
||||
def update(input_dict, update_source):
|
||||
"""Deep update a dictionary."""
|
||||
"""Deep update a dictionary.
|
||||
|
||||
Async friendly.
|
||||
"""
|
||||
for key, val in update_source.items():
|
||||
if isinstance(val, collections.Mapping):
|
||||
recurse = update(input_dict.get(key, {}), val)
|
||||
@@ -38,7 +41,8 @@ def update(input_dict, update_source):
|
||||
return input_dict
|
||||
|
||||
|
||||
def get_service(hass, config, discovery_info=None):
|
||||
@asyncio.coroutine
|
||||
def async_get_service(hass, config, discovery_info=None):
|
||||
"""Get the Group notification service."""
|
||||
return GroupNotifyPlatform(hass, config.get(CONF_SERVICES))
|
||||
|
||||
@@ -51,14 +55,19 @@ class GroupNotifyPlatform(BaseNotificationService):
|
||||
self.hass = hass
|
||||
self.entities = entities
|
||||
|
||||
def send_message(self, message="", **kwargs):
|
||||
@asyncio.coroutine
|
||||
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})
|
||||
|
||||
tasks = []
|
||||
for entity in self.entities:
|
||||
sending_payload = deepcopy(payload.copy())
|
||||
if entity.get(ATTR_DATA) is not None:
|
||||
update(sending_payload, entity.get(ATTR_DATA))
|
||||
self.hass.services.call(DOMAIN, entity.get(ATTR_SERVICE),
|
||||
sending_payload)
|
||||
tasks.append(self.hass.services.async_call(
|
||||
DOMAIN, entity.get(ATTR_SERVICE), sending_payload))
|
||||
|
||||
if tasks:
|
||||
yield from asyncio.wait(tasks, loop=self.hass.loop)
|
||||
Reference in new issue
Block a user