core/homeassistant/components/gntp/notify.py

92 lines
2.5 KiB
Python
Raw Normal View History

"""GNTP (aka Growl) notification service."""
2016-03-26 01:39:08 +00:00
import logging
import os
import gntp.errors
import gntp.notifier
2016-09-06 22:16:21 +00:00
import voluptuous as vol
from homeassistant.components.notify import (
2019-07-31 19:25:30 +00:00
ATTR_TITLE,
ATTR_TITLE_DEFAULT,
PLATFORM_SCHEMA,
BaseNotificationService,
)
from homeassistant.const import CONF_PASSWORD, CONF_PORT
import homeassistant.helpers.config_validation as cv
2016-03-26 01:39:08 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONF_APP_NAME = "app_name"
CONF_APP_ICON = "app_icon"
CONF_HOSTNAME = "hostname"
2016-09-06 22:16:21 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_APP_NAME = "HomeAssistant"
DEFAULT_HOST = "localhost"
2016-09-06 22:16:21 +00:00
DEFAULT_PORT = 23053
2019-07-31 19:25:30 +00:00
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,
}
)
2016-09-06 22:16:21 +00:00
def get_service(hass, config, discovery_info=None):
2016-03-26 01:39:08 +00:00
"""Get the GNTP notification service."""
logging.getLogger("gntp").setLevel(logging.ERROR)
2016-09-06 22:16:21 +00:00
if config.get(CONF_APP_ICON) is None:
2019-07-31 19:25:30 +00:00
icon_file = os.path.join(
os.path.dirname(__file__),
"..",
"frontend",
"www_static",
"icons",
"favicon-192x192.png",
)
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
2019-07-31 19:25:30 +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
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,
2019-07-31 19:25:30 +00:00
port=port,
2016-03-26 01:39:08 +00:00
)
2016-05-17 23:51:32 +00:00
try:
self.gntp.register()
except gntp.errors.NetworkError:
_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."""
2019-07-31 19:25:30 +00:00
self.gntp.notify(
noteType="Notification",
title=kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT),
description=message,
)