2019-04-03 15:40:03 +00:00
|
|
|
"""Kodi notification service."""
|
2016-09-27 03:26:32 +00:00
|
|
|
import logging
|
2017-03-11 18:41:05 +00:00
|
|
|
|
|
|
|
import aiohttp
|
2016-09-27 03:26:32 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-03-11 18:41:05 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ICON,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_PROXY_SSL,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2017-03-11 18:41:05 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2016-09-27 03:26:32 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DATA,
|
|
|
|
ATTR_TITLE,
|
|
|
|
ATTR_TITLE_DEFAULT,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-09-27 03:26:32 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_PORT = 8080
|
2017-03-11 18:41:05 +00:00
|
|
|
DEFAULT_PROXY_SSL = False
|
|
|
|
DEFAULT_TIMEOUT = 5
|
2016-09-27 03:26:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
vol.Optional(CONF_PROXY_SSL, default=DEFAULT_PROXY_SSL): cv.boolean,
|
|
|
|
vol.Inclusive(CONF_USERNAME, "auth"): cv.string,
|
|
|
|
vol.Inclusive(CONF_PASSWORD, "auth"): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-09-27 03:26:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DISPLAYTIME = "displaytime"
|
2016-09-27 03:26:32 +00:00
|
|
|
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_get_service(hass, config, discovery_info=None):
|
2016-09-27 03:26:32 +00:00
|
|
|
"""Return the notify service."""
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "{}:{}".format(config.get(CONF_HOST), config.get(CONF_PORT))
|
2016-09-27 03:26:32 +00:00
|
|
|
|
2016-12-06 15:43:11 +00:00
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
|
2017-03-11 18:41:05 +00:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
port = config.get(CONF_PORT)
|
|
|
|
encryption = config.get(CONF_PROXY_SSL)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if host.startswith("http://") or host.startswith("https://"):
|
|
|
|
host = host[host.index("://") + 3 :]
|
2017-03-11 18:41:05 +00:00
|
|
|
_LOGGER.warning(
|
2017-09-23 15:15:46 +00:00
|
|
|
"Kodi host name should no longer contain http:// See updated "
|
2017-03-11 18:41:05 +00:00
|
|
|
"definitions here: "
|
2019-07-31 19:25:30 +00:00
|
|
|
"https://home-assistant.io/components/media_player.kodi/"
|
|
|
|
)
|
2017-03-11 18:41:05 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
http_protocol = "https" if encryption else "http"
|
|
|
|
url = "{}://{}:{}/jsonrpc".format(http_protocol, host, port)
|
2017-03-11 18:41:05 +00:00
|
|
|
|
2016-12-06 15:43:11 +00:00
|
|
|
if username is not None:
|
2017-03-11 18:41:05 +00:00
|
|
|
auth = aiohttp.BasicAuth(username, password)
|
2016-12-06 15:43:11 +00:00
|
|
|
else:
|
|
|
|
auth = None
|
2016-09-27 03:26:32 +00:00
|
|
|
|
2017-03-11 18:41:05 +00:00
|
|
|
return KodiNotificationService(hass, url, auth)
|
2016-09-27 03:26:32 +00:00
|
|
|
|
|
|
|
|
2017-03-11 18:41:05 +00:00
|
|
|
class KodiNotificationService(BaseNotificationService):
|
2016-09-27 03:26:32 +00:00
|
|
|
"""Implement the notification service for Kodi."""
|
|
|
|
|
2017-03-11 18:41:05 +00:00
|
|
|
def __init__(self, hass, url, auth=None):
|
2016-09-27 03:26:32 +00:00
|
|
|
"""Initialize the service."""
|
2017-03-11 18:41:05 +00:00
|
|
|
import jsonrpc_async
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-09-27 03:26:32 +00:00
|
|
|
self._url = url
|
2016-12-06 15:43:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
kwargs = {"timeout": DEFAULT_TIMEOUT, "session": async_get_clientsession(hass)}
|
2016-12-06 15:43:11 +00:00
|
|
|
|
|
|
|
if auth is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
kwargs["auth"] = auth
|
2016-12-06 15:43:11 +00:00
|
|
|
|
2017-03-11 18:41:05 +00:00
|
|
|
self._server = jsonrpc_async.Server(self._url, **kwargs)
|
2016-09-27 03:26:32 +00:00
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_send_message(self, message="", **kwargs):
|
2016-09-27 03:26:32 +00:00
|
|
|
"""Send a message to Kodi."""
|
2017-03-11 18:41:05 +00:00
|
|
|
import jsonrpc_async
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-09-27 03:26:32 +00:00
|
|
|
try:
|
|
|
|
data = kwargs.get(ATTR_DATA) or {}
|
|
|
|
|
2019-03-04 14:25:28 +00:00
|
|
|
displaytime = int(data.get(ATTR_DISPLAYTIME, 10000))
|
2016-09-27 03:26:32 +00:00
|
|
|
icon = data.get(ATTR_ICON, "info")
|
|
|
|
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
2019-07-31 19:25:30 +00:00
|
|
|
await self._server.GUI.ShowNotification(title, message, icon, displaytime)
|
2016-09-27 03:26:32 +00:00
|
|
|
|
2017-03-11 18:41:05 +00:00
|
|
|
except jsonrpc_async.TransportError:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.warning("Unable to fetch Kodi data. Is Kodi online?")
|