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

+51 -41
View File
@@ -15,33 +15,35 @@ from homeassistant.util import slugify
_LOGGER = logging.getLogger(__name__)
# Platform specific data
ATTR_DATA = 'data'
ATTR_DATA = "data"
# Text to notify user of
ATTR_MESSAGE = 'message'
ATTR_MESSAGE = "message"
# Target of the notification (user, device, etc)
ATTR_TARGET = 'target'
ATTR_TARGET = "target"
# Title of notification
ATTR_TITLE = 'title'
ATTR_TITLE = "title"
ATTR_TITLE_DEFAULT = "Home Assistant"
DOMAIN = 'notify'
DOMAIN = "notify"
SERVICE_NOTIFY = 'notify'
SERVICE_NOTIFY = "notify"
PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): cv.string,
vol.Optional(CONF_NAME): cv.string,
}, extra=vol.ALLOW_EXTRA)
PLATFORM_SCHEMA = vol.Schema(
{vol.Required(CONF_PLATFORM): cv.string, vol.Optional(CONF_NAME): cv.string},
extra=vol.ALLOW_EXTRA,
)
NOTIFY_SERVICE_SCHEMA = vol.Schema({
vol.Required(ATTR_MESSAGE): cv.template,
vol.Optional(ATTR_TITLE): cv.template,
vol.Optional(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_DATA): dict,
})
NOTIFY_SERVICE_SCHEMA = vol.Schema(
{
vol.Required(ATTR_MESSAGE): cv.template,
vol.Optional(ATTR_TITLE): cv.template,
vol.Optional(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_DATA): dict,
}
)
async def async_setup(hass, config):
@@ -53,8 +55,7 @@ async def async_setup(hass, config):
if p_config is None:
p_config = {}
platform = await async_prepare_setup_platform(
hass, config, DOMAIN, p_type)
platform = await async_prepare_setup_platform(hass, config, DOMAIN, p_type)
if platform is None:
_LOGGER.error("Unknown notification service specified")
@@ -63,12 +64,14 @@ async def async_setup(hass, config):
_LOGGER.info("Setting up %s.%s", DOMAIN, p_type)
notify_service = None
try:
if hasattr(platform, 'async_get_service'):
notify_service = await \
platform.async_get_service(hass, p_config, discovery_info)
elif hasattr(platform, 'get_service'):
if hasattr(platform, "async_get_service"):
notify_service = await platform.async_get_service(
hass, p_config, discovery_info
)
elif hasattr(platform, "get_service"):
notify_service = await hass.async_add_job(
platform.get_service, hass, p_config, discovery_info)
platform.get_service, hass, p_config, discovery_info
)
else:
raise HomeAssistantError("Invalid notify platform.")
@@ -77,12 +80,12 @@ async def async_setup(hass, config):
# on discovery data.
if discovery_info is None:
_LOGGER.error(
"Failed to initialize notification service %s",
p_type)
"Failed to initialize notification service %s", p_type
)
return
except Exception: # pylint: disable=broad-except
_LOGGER.exception('Error setting up platform %s', p_type)
_LOGGER.exception("Error setting up platform %s", p_type)
return
notify_service.hass = hass
@@ -111,32 +114,40 @@ async def async_setup(hass, config):
await notify_service.async_send_message(**kwargs)
if hasattr(notify_service, 'targets'):
if hasattr(notify_service, "targets"):
platform_name = (
p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or
p_type)
p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or p_type
)
for name, target in notify_service.targets.items():
target_name = slugify('{}_{}'.format(platform_name, name))
target_name = slugify("{}_{}".format(platform_name, name))
targets[target_name] = target
hass.services.async_register(
DOMAIN, target_name, async_notify_message,
schema=NOTIFY_SERVICE_SCHEMA)
DOMAIN,
target_name,
async_notify_message,
schema=NOTIFY_SERVICE_SCHEMA,
)
platform_name = (
p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or
SERVICE_NOTIFY)
p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or SERVICE_NOTIFY
)
platform_name_slug = slugify(platform_name)
hass.services.async_register(
DOMAIN, platform_name_slug, async_notify_message,
schema=NOTIFY_SERVICE_SCHEMA)
DOMAIN,
platform_name_slug,
async_notify_message,
schema=NOTIFY_SERVICE_SCHEMA,
)
hass.config.components.add('{}.{}'.format(DOMAIN, p_type))
hass.config.components.add("{}.{}".format(DOMAIN, p_type))
return True
setup_tasks = [async_setup_platform(p_type, p_config) for p_type, p_config
in config_per_platform(config, DOMAIN)]
setup_tasks = [
async_setup_platform(p_type, p_config)
for p_type, p_config in config_per_platform(config, DOMAIN)
]
if setup_tasks:
await asyncio.wait(setup_tasks)
@@ -168,5 +179,4 @@ class BaseNotificationService:
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.async_add_job(
partial(self.send_message, message, **kwargs))
return self.hass.async_add_job(partial(self.send_message, message, **kwargs))