2019-04-03 15:40:03 +00:00
|
|
|
"""GNTP (aka Growl) notification service."""
|
2016-03-26 01:39:08 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2016-09-06 22:16:21 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_PORT
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-03-26 01:39:08 +00:00
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (
|
2019-03-21 05:56:46 +00:00
|
|
|
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
|
|
|
|
|
2016-03-26 01:39:08 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
_GNTP_LOGGER = logging.getLogger('gntp')
|
|
|
|
_GNTP_LOGGER.setLevel(logging.ERROR)
|
|
|
|
|
|
|
|
|
2016-09-06 22:16:21 +00:00
|
|
|
CONF_APP_NAME = 'app_name'
|
|
|
|
CONF_APP_ICON = 'app_icon'
|
|
|
|
CONF_HOSTNAME = 'hostname'
|
|
|
|
|
|
|
|
DEFAULT_APP_NAME = 'HomeAssistant'
|
|
|
|
DEFAULT_HOST = 'localhost'
|
|
|
|
DEFAULT_PORT = 23053
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Optional(CONF_APP_NAME, default=DEFAULT_APP_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_APP_ICON): vol.Url,
|
|
|
|
vol.Optional(CONF_HOSTNAME, default=DEFAULT_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-03-26 01:39:08 +00:00
|
|
|
"""Get the GNTP notification service."""
|
2016-09-06 22:16:21 +00:00
|
|
|
if config.get(CONF_APP_ICON) is None:
|
2016-03-26 01:39:08 +00:00
|
|
|
icon_file = os.path.join(os.path.dirname(__file__), "..", "frontend",
|
2016-07-20 21:46:16 +00:00
|
|
|
"www_static", "icons", "favicon-192x192.png")
|
2018-01-31 10:30:48 +00:00
|
|
|
with open(icon_file, 'rb') as file:
|
|
|
|
app_icon = file.read()
|
2016-03-26 01:39:08 +00:00
|
|
|
else:
|
2016-09-06 22:16:21 +00:00
|
|
|
app_icon = config.get(CONF_APP_ICON)
|
2016-03-26 01:39:08 +00:00
|
|
|
|
2016-09-06 22:16:21 +00:00
|
|
|
return GNTPNotificationService(config.get(CONF_APP_NAME),
|
|
|
|
app_icon,
|
|
|
|
config.get(CONF_HOSTNAME),
|
|
|
|
config.get(CONF_PASSWORD),
|
|
|
|
config.get(CONF_PORT))
|
2016-03-26 01:39:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GNTPNotificationService(BaseNotificationService):
|
|
|
|
"""Implement the notification service for GNTP."""
|
|
|
|
|
|
|
|
def __init__(self, app_name, app_icon, hostname, password, port):
|
|
|
|
"""Initialize the service."""
|
2016-05-17 23:51:32 +00:00
|
|
|
import gntp.notifier
|
|
|
|
import gntp.errors
|
|
|
|
self.gntp = gntp.notifier.GrowlNotifier(
|
2016-03-26 01:39:08 +00:00
|
|
|
applicationName=app_name,
|
|
|
|
notifications=["Notification"],
|
|
|
|
applicationIcon=app_icon,
|
|
|
|
hostname=hostname,
|
|
|
|
password=password,
|
|
|
|
port=port
|
|
|
|
)
|
2016-05-17 23:51:32 +00:00
|
|
|
try:
|
|
|
|
self.gntp.register()
|
|
|
|
except gntp.errors.NetworkError:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.error("Unable to register with the GNTP host")
|
2016-05-17 23:51:32 +00:00
|
|
|
return
|
2016-03-26 01:39:08 +00:00
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
|
|
|
"""Send a message to a user."""
|
2016-09-01 13:35:46 +00:00
|
|
|
self.gntp.notify(noteType="Notification",
|
|
|
|
title=kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT),
|
2016-03-26 01:39:08 +00:00
|
|
|
description=message)
|