hass-core/homeassistant/components/apprise/notify.py
Chris Caron 3cb844f22c Add Apprise notification integration (#26868)
* Added apprise notification component

* flake-8 fixes; black formatting + import merged to 1 line

* pylint issues resolved

* added github name to manifest.json

* import moved to top as per code review request

* manifest formatting to avoid failing ci

* .coveragerc updated to include apprise

* removed block for written tests

* more test coverage

* formatting as per code review

* tests converted to async style as per code review

* increased coverage

* bumped version of apprise to 0.8.1

* test that mocked entries are called

* added tests for hass.service loading

* support tags for those who identify the TARGET option

* renamed variable as per code review

* 'assert not' used instead of 'is False'

* added period (in case linter isn't happy)
2019-10-15 00:53:59 +02:00

73 lines
2 KiB
Python

"""Apprise platform for notify component."""
import logging
import voluptuous as vol
import apprise
import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import (
ATTR_TARGET,
ATTR_TITLE,
ATTR_TITLE_DEFAULT,
PLATFORM_SCHEMA,
BaseNotificationService,
)
_LOGGER = logging.getLogger(__name__)
CONF_FILE = "config"
CONF_URL = "url"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_URL): vol.All(cv.ensure_list, [str]),
vol.Optional(CONF_FILE): cv.string,
}
)
def get_service(hass, config, discovery_info=None):
"""Get the Apprise notification service."""
# Create our object
a_obj = apprise.Apprise()
if config.get(CONF_FILE):
# Sourced from a Configuration File
a_config = apprise.AppriseConfig()
if not a_config.add(config[CONF_FILE]):
_LOGGER.error("Invalid Apprise config url provided")
return None
if not a_obj.add(a_config):
_LOGGER.error("Invalid Apprise config url provided")
return None
if config.get(CONF_URL):
# Ordered list of URLs
if not a_obj.add(config[CONF_URL]):
_LOGGER.error("Invalid Apprise URL(s) supplied")
return None
return AppriseNotificationService(a_obj)
class AppriseNotificationService(BaseNotificationService):
"""Implement the notification service for Apprise."""
def __init__(self, a_obj):
"""Initialize the service."""
self.apprise = a_obj
def send_message(self, message="", **kwargs):
"""Send a message to a specified target.
If no target/tags are specified, then services are notified as is
However, if any tags are specified, then they will be applied
to the notification causing filtering (if set up that way).
"""
targets = kwargs.get(ATTR_TARGET)
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
self.apprise.notify(body=message, title=title, tag=targets)