Update Kodi notifier to async (#6497)

* Update Kodi notifier to async

* Change Kodi CONF_SSL to CONF_PROXY_SSL
This commit is contained in:
Adam Mills 2017-03-11 13:41:05 -05:00 committed by Paulus Schoutsen
parent 9a86ccaaea
commit 157ab77232
4 changed files with 54 additions and 30 deletions

View file

@ -19,11 +19,12 @@ from homeassistant.components.media_player import (
PLATFORM_SCHEMA)
from homeassistant.const import (
STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING, CONF_HOST, CONF_NAME,
CONF_PORT, CONF_SSL, CONF_USERNAME, CONF_PASSWORD,
CONF_PORT, CONF_SSL, CONF_PROXY_SSL, CONF_USERNAME, CONF_PASSWORD,
EVENT_HOMEASSISTANT_STOP)
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.deprecation import get_deprecated
REQUIREMENTS = ['jsonrpc-async==0.4', 'jsonrpc-websocket==0.2']
@ -37,7 +38,7 @@ DEFAULT_NAME = 'Kodi'
DEFAULT_PORT = 8080
DEFAULT_TCP_PORT = 9090
DEFAULT_TIMEOUT = 5
DEFAULT_SSL = False
DEFAULT_PROXY_SSL = False
DEFAULT_ENABLE_WEBSOCKET = True
TURN_OFF_ACTION = [None, 'quit', 'hibernate', 'suspend', 'reboot', 'shutdown']
@ -51,7 +52,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_TCP_PORT, default=DEFAULT_TCP_PORT): cv.port,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
vol.Optional(CONF_PROXY_SSL, default=DEFAULT_PROXY_SSL): cv.boolean,
vol.Optional(CONF_TURN_OFF_ACTION, default=None): vol.In(TURN_OFF_ACTION),
vol.Inclusive(CONF_USERNAME, 'auth'): cv.string,
vol.Inclusive(CONF_PASSWORD, 'auth'): cv.string,
@ -66,7 +67,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
tcp_port = config.get(CONF_TCP_PORT)
encryption = config.get(CONF_SSL)
encryption = get_deprecated(config, CONF_PROXY_SSL, CONF_SSL)
websocket = config.get(CONF_ENABLE_WEBSOCKET)
if host.startswith('http://') or host.startswith('https://'):

View file

@ -4,24 +4,33 @@ Kodi notification service.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.kodi/
"""
import asyncio
import logging
import aiohttp
import voluptuous as vol
from homeassistant.const import (ATTR_ICON, CONF_HOST, CONF_PORT,
CONF_USERNAME, CONF_PASSWORD)
from homeassistant.components.notify import (ATTR_TITLE, ATTR_TITLE_DEFAULT,
ATTR_DATA, PLATFORM_SCHEMA,
from homeassistant.const import (
ATTR_ICON, CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD,
CONF_PROXY_SSL)
from homeassistant.components.notify import (
ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_DATA, PLATFORM_SCHEMA,
BaseNotificationService)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['jsonrpc-async==0.4']
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['jsonrpc-requests==0.3']
DEFAULT_PORT = 8080
DEFAULT_PROXY_SSL = False
DEFAULT_TIMEOUT = 5
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,
})
@ -29,51 +38,66 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
ATTR_DISPLAYTIME = 'displaytime'
def get_service(hass, config, discovery_info=None):
@asyncio.coroutine
def async_get_service(hass, config, discovery_info=None):
"""Return the notify service."""
url = '{}:{}'.format(config.get(CONF_HOST), config.get(CONF_PORT))
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
encryption = config.get(CONF_PROXY_SSL)
if host.startswith('http://') or host.startswith('https://'):
host = host.lstrip('http://').lstrip('https://')
_LOGGER.warning(
"Kodi host name should no longer conatin http:// See updated "
"definitions here: "
"https://home-assistant.io/components/media_player.kodi/")
http_protocol = 'https' if encryption else 'http'
url = '{}://{}:{}/jsonrpc'.format(http_protocol, host, port)
if username is not None:
auth = (username, password)
auth = aiohttp.BasicAuth(username, password)
else:
auth = None
return KODINotificationService(
url,
auth
)
return KodiNotificationService(hass, url, auth)
class KODINotificationService(BaseNotificationService):
class KodiNotificationService(BaseNotificationService):
"""Implement the notification service for Kodi."""
def __init__(self, url, auth=None):
def __init__(self, hass, url, auth=None):
"""Initialize the service."""
import jsonrpc_requests
import jsonrpc_async
self._url = url
kwargs = {'timeout': 5}
kwargs = {
'timeout': DEFAULT_TIMEOUT,
'session': async_get_clientsession(hass),
}
if auth is not None:
kwargs['auth'] = auth
self._server = jsonrpc_requests.Server(
'{}/jsonrpc'.format(self._url), **kwargs)
self._server = jsonrpc_async.Server(self._url, **kwargs)
def send_message(self, message="", **kwargs):
@asyncio.coroutine
def async_send_message(self, message="", **kwargs):
"""Send a message to Kodi."""
import jsonrpc_requests
import jsonrpc_async
try:
data = kwargs.get(ATTR_DATA) or {}
displaytime = data.get(ATTR_DISPLAYTIME, 10000)
icon = data.get(ATTR_ICON, "info")
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
self._server.GUI.ShowNotification(title, message, icon,
displaytime)
yield from self._server.GUI.ShowNotification(
title, message, icon, displaytime)
except jsonrpc_requests.jsonrpc.TransportError:
except jsonrpc_async.TransportError:
_LOGGER.warning('Unable to fetch Kodi data, Is Kodi online?')

View file

@ -127,6 +127,7 @@ CONF_PLATFORM = 'platform'
CONF_PORT = 'port'
CONF_PREFIX = 'prefix'
CONF_PROTOCOL = 'protocol'
CONF_PROXY_SSL = 'proxy_ssl'
CONF_QUOTE = 'quote'
CONF_RECIPIENT = 'recipient'
CONF_RESOURCE = 'resource'

View file

@ -316,10 +316,8 @@ insteonlocal==0.48
insteonplm==0.7.4
# homeassistant.components.media_player.kodi
jsonrpc-async==0.4
# homeassistant.components.notify.kodi
jsonrpc-requests==0.3
jsonrpc-async==0.4
# homeassistant.components.media_player.kodi
jsonrpc-websocket==0.2