<commit_before>"""
homeassistant.components.media_player.kodi
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Provides an interface to the XBMC/Kodi JSON-RPC API

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/media_player.kodi/
"""
import urllib
import logging

from homeassistant.components.media_player import (
    MediaPlayerDevice, SUPPORT_PAUSE, SUPPORT_SEEK, SUPPORT_VOLUME_SET,
    SUPPORT_VOLUME_MUTE, SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK)
from homeassistant.const import (
    STATE_IDLE, STATE_PLAYING, STATE_PAUSED, STATE_OFF)

_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['jsonrpc-requests==0.1']

SUPPORT_KODI = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
    SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_SEEK


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
    """ Sets up the kodi platform. """

    add_devices([
        KodiDevice(
            config.get('name', 'Kodi'),
            config.get('url'),
            auth=(
                config.get('user', ''),
                config.get('password', ''))),
    ])


def _get_image_url(kodi_url):
    """ Helper function that parses the thumbnail URLs used by Kodi. """
    url_components = urllib.parse.urlparse(kodi_url)

    if url_components.scheme == 'image':
        return urllib.parse.unquote(url_components.netloc)


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

    # pylint: disable=too-many-public-methods

    def __init__(self, name, url, auth=None):
        import jsonrpc_requests
        self._name = name
        self._url = url
        self._server = jsonrpc_requests.Server(url, auth=auth)
        self._players = None
        self._properties = None
        self._item = None
        self._app_properties = None

        self.update()

    @property
    def name(self):
        """ Returns the name of the device. """
        return self._name

    def _get_players(self):
        """ Returns the active player objects or None """
        import jsonrpc_requests
        try:
            return self._server.Player.GetActivePlayers()
        except jsonrpc_requests.jsonrpc.TransportError:
            _LOGGER.exception('Unable to fetch kodi data')
            return None

    @property<commit_msg>kodi: remove traceback on state check<commit_after>"""
homeassistant.components.media_player.kodi
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Provides an interface to the XBMC/Kodi JSON-RPC API

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/media_player.kodi/
"""
import urllib
import logging

from homeassistant.components.media_player import (
    MediaPlayerDevice, SUPPORT_PAUSE, SUPPORT_SEEK, SUPPORT_VOLUME_SET,
    SUPPORT_VOLUME_MUTE, SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK)
from homeassistant.const import (
    STATE_IDLE, STATE_PLAYING, STATE_PAUSED, STATE_OFF)

_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['jsonrpc-requests==0.1']

SUPPORT_KODI = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
    SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_SEEK


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
    """ Sets up the kodi platform. """

    add_devices([
        KodiDevice(
            config.get('name', 'Kodi'),
            config.get('url'),
            auth=(
                config.get('user', ''),
                config.get('password', ''))),
    ])


def _get_image_url(kodi_url):
    """ Helper function that parses the thumbnail URLs used by Kodi. """
    url_components = urllib.parse.urlparse(kodi_url)

    if url_components.scheme == 'image':
        return urllib.parse.unquote(url_components.netloc)


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

    # pylint: disable=too-many-public-methods

    def __init__(self, name, url, auth=None):
        import jsonrpc_requests
        self._name = name
        self._url = url
        self._server = jsonrpc_requests.Server(url, auth=auth)
        self._players = None
        self._properties = None
        self._item = None
        self._app_properties = None

        self.update()

    @property
    def name(self):
        """ Returns the name of the device. """
        return self._name

    def _get_players(self):
        """ Returns the active player objects or None """
        import jsonrpc_requests
        try:
            return self._server.Player.GetActivePlayers()
        except jsonrpc_requests.jsonrpc.TransportError:
            _LOGGER.warning('Unable to fetch kodi data')
            _LOGGER.debug('Unable to fetch kodi data', exc_info=True)
            return None

    @property
{'fraction_non_alphanumeric': 0.11289658014009064, 'fraction_numerical': 0.0008240626287597857, 'mean_word_length': 3.1178965224766753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '665682', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1452, 'n_words': 330}