Skip to content

Commit

Permalink
Kodi cleanup and config update (#5098)
Browse files Browse the repository at this point in the history
  • Loading branch information
emlove authored and balloob committed Jan 3, 2017
1 parent 254eb4e commit 018d786
Showing 1 changed file with 30 additions and 41 deletions.
71 changes: 30 additions & 41 deletions homeassistant/components/media_player/kodi.py
Expand Up @@ -43,53 +43,50 @@
})


def setup_platform(hass, config, add_devices, discovery_info=None):
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Setup the Kodi platform."""
url = '{}:{}'.format(config.get(CONF_HOST), config.get(CONF_PORT))
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)

jsonrpc_url = config.get('url') # deprecated
if jsonrpc_url:
url = jsonrpc_url.rstrip('/jsonrpc')
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/")

username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)

if username is not None:
auth = (username, password)
else:
auth = None

add_devices([
add_entities([
KodiDevice(
config.get(CONF_NAME),
url,
auth=auth,
host=host, port=port,
username=config.get(CONF_USERNAME),
password=config.get(CONF_PASSWORD),
turn_off_action=config.get(CONF_TURN_OFF_ACTION)),
])


class KodiDevice(MediaPlayerDevice):
"""Representation of a XBMC/Kodi device."""

def __init__(self, name, url, auth=None, turn_off_action=None):
def __init__(self, name, host, port, username=None, password=None,
turn_off_action=None):
"""Initialize the Kodi device."""
import jsonrpc_requests
self._name = name
self._url = url
self._basic_auth_url = None

kwargs = {'timeout': 5}

if auth is not None:
kwargs['auth'] = auth
scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
self._basic_auth_url = \
urllib.parse.urlunsplit((scheme, '{}:{}@{}'.format
(auth[0], auth[1], netloc),
path, query, fragment))
if username is not None:
kwargs['auth'] = (username, password)
image_auth_string = "{}:{}@".format(username, password)
else:
image_auth_string = ""

self._http_url = 'http://{}:{}/jsonrpc'.format(host, port)
self._image_url = 'http://{}{}:{}/image'.format(
image_auth_string, host, port)

self._server = jsonrpc_requests.Server(
'{}/jsonrpc'.format(self._url), **kwargs)
self._server = jsonrpc_requests.Server(self._http_url, **kwargs)

self._turn_off_action = turn_off_action
self._players = list()
Expand All @@ -110,7 +107,7 @@ def _get_players(self):
return self._server.Player.GetActivePlayers()
except jsonrpc_requests.jsonrpc.TransportError:
if self._players is not None:
_LOGGER.warning('Unable to fetch kodi data')
_LOGGER.info('Unable to fetch kodi data')
_LOGGER.debug('Unable to fetch kodi data', exc_info=True)
return None

Expand Down Expand Up @@ -193,21 +190,13 @@ def media_duration(self):
@property
def media_image_url(self):
"""Image url of current playing media."""
if self._item is not None:
return self._get_image_url()
if self._item is None:
return None

def _get_image_url(self):
"""Helper function that parses the thumbnail URLs used by Kodi."""
url_components = urllib.parse.urlparse(self._item['thumbnail'])

if url_components.scheme == 'image':
if self._basic_auth_url is not None:
return '{}/image/{}'.format(
self._basic_auth_url,
urllib.parse.quote_plus(self._item['thumbnail']))

return '{}/image/{}'.format(
self._url,
return '{}/{}'.format(
self._image_url,
urllib.parse.quote_plus(self._item['thumbnail']))

@property
Expand Down

0 comments on commit 018d786

Please sign in to comment.