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) PLATFORM_SCHEMA)
from homeassistant.const import ( from homeassistant.const import (
STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING, CONF_HOST, CONF_NAME, 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) EVENT_HOMEASSISTANT_STOP)
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.deprecation import get_deprecated
REQUIREMENTS = ['jsonrpc-async==0.4', 'jsonrpc-websocket==0.2'] REQUIREMENTS = ['jsonrpc-async==0.4', 'jsonrpc-websocket==0.2']
@ -37,7 +38,7 @@ DEFAULT_NAME = 'Kodi'
DEFAULT_PORT = 8080 DEFAULT_PORT = 8080
DEFAULT_TCP_PORT = 9090 DEFAULT_TCP_PORT = 9090
DEFAULT_TIMEOUT = 5 DEFAULT_TIMEOUT = 5
DEFAULT_SSL = False DEFAULT_PROXY_SSL = False
DEFAULT_ENABLE_WEBSOCKET = True DEFAULT_ENABLE_WEBSOCKET = True
TURN_OFF_ACTION = [None, 'quit', 'hibernate', 'suspend', 'reboot', 'shutdown'] 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_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_TCP_PORT, default=DEFAULT_TCP_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.Optional(CONF_TURN_OFF_ACTION, default=None): vol.In(TURN_OFF_ACTION),
vol.Inclusive(CONF_USERNAME, 'auth'): cv.string, vol.Inclusive(CONF_USERNAME, 'auth'): cv.string,
vol.Inclusive(CONF_PASSWORD, '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) host = config.get(CONF_HOST)
port = config.get(CONF_PORT) port = config.get(CONF_PORT)
tcp_port = config.get(CONF_TCP_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) websocket = config.get(CONF_ENABLE_WEBSOCKET)
if host.startswith('http://') or host.startswith('https://'): 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 For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.kodi/ https://home-assistant.io/components/notify.kodi/
""" """
import asyncio
import logging import logging
import aiohttp
import voluptuous as vol import voluptuous as vol
from homeassistant.const import (ATTR_ICON, CONF_HOST, CONF_PORT, from homeassistant.const import (
CONF_USERNAME, CONF_PASSWORD) ATTR_ICON, CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD,
from homeassistant.components.notify import (ATTR_TITLE, ATTR_TITLE_DEFAULT, CONF_PROXY_SSL)
ATTR_DATA, PLATFORM_SCHEMA, from homeassistant.components.notify import (
ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_DATA, PLATFORM_SCHEMA,
BaseNotificationService) BaseNotificationService)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['jsonrpc-async==0.4']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['jsonrpc-requests==0.3']
DEFAULT_PORT = 8080 DEFAULT_PORT = 8080
DEFAULT_PROXY_SSL = False
DEFAULT_TIMEOUT = 5
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string, vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, 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_USERNAME, 'auth'): cv.string,
vol.Inclusive(CONF_PASSWORD, 'auth'): cv.string, vol.Inclusive(CONF_PASSWORD, 'auth'): cv.string,
}) })
@ -29,51 +38,66 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
ATTR_DISPLAYTIME = 'displaytime' 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.""" """Return the notify service."""
url = '{}:{}'.format(config.get(CONF_HOST), config.get(CONF_PORT)) url = '{}:{}'.format(config.get(CONF_HOST), config.get(CONF_PORT))
username = config.get(CONF_USERNAME) username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD) 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: if username is not None:
auth = (username, password) auth = aiohttp.BasicAuth(username, password)
else: else:
auth = None auth = None
return KODINotificationService( return KodiNotificationService(hass, url, auth)
url,
auth
)
class KODINotificationService(BaseNotificationService): class KodiNotificationService(BaseNotificationService):
"""Implement the notification service for Kodi.""" """Implement the notification service for Kodi."""
def __init__(self, url, auth=None): def __init__(self, hass, url, auth=None):
"""Initialize the service.""" """Initialize the service."""
import jsonrpc_requests import jsonrpc_async
self._url = url self._url = url
kwargs = {'timeout': 5} kwargs = {
'timeout': DEFAULT_TIMEOUT,
'session': async_get_clientsession(hass),
}
if auth is not None: if auth is not None:
kwargs['auth'] = auth kwargs['auth'] = auth
self._server = jsonrpc_requests.Server( self._server = jsonrpc_async.Server(self._url, **kwargs)
'{}/jsonrpc'.format(self._url), **kwargs)
def send_message(self, message="", **kwargs): @asyncio.coroutine
def async_send_message(self, message="", **kwargs):
"""Send a message to Kodi.""" """Send a message to Kodi."""
import jsonrpc_requests import jsonrpc_async
try: try:
data = kwargs.get(ATTR_DATA) or {} data = kwargs.get(ATTR_DATA) or {}
displaytime = data.get(ATTR_DISPLAYTIME, 10000) displaytime = data.get(ATTR_DISPLAYTIME, 10000)
icon = data.get(ATTR_ICON, "info") icon = data.get(ATTR_ICON, "info")
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
self._server.GUI.ShowNotification(title, message, icon, yield from self._server.GUI.ShowNotification(
displaytime) title, message, icon, displaytime)
except jsonrpc_requests.jsonrpc.TransportError: except jsonrpc_async.TransportError:
_LOGGER.warning('Unable to fetch Kodi data, Is Kodi online?') _LOGGER.warning('Unable to fetch Kodi data, Is Kodi online?')

View file

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

View file

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