Example #1
0
    def __init__(self, hass, name, host, port, encryption=False, username=None,
                 password=None, turn_off_action=None):
        """Initialize the Kodi device."""
        import jsonrpc_async
        self.hass = hass
        self._name = name

        kwargs = {
            'timeout': DEFAULT_TIMEOUT,
            'session': async_get_clientsession(hass),
        }

        if username is not None:
            kwargs['auth'] = aiohttp.BasicAuth(username, password)
            image_auth_string = "{}:{}@".format(username, password)
        else:
            image_auth_string = ""

        protocol = 'https' if encryption else 'http'

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

        self._server = jsonrpc_async.Server(self._http_url, **kwargs)

        self._turn_off_action = turn_off_action
        self._players = list()
        self._properties = None
        self._item = None
        self._app_properties = None
Example #2
0
    def __init__(self, hass, name, host, port, tcp_port, encryption=False,
                 username=None, password=None, turn_off_action=None,
                 websocket=True):
        """Initialize the Kodi device."""
        import jsonrpc_async
        import jsonrpc_websocket
        self.hass = hass
        self._name = name

        kwargs = {
            'timeout': DEFAULT_TIMEOUT,
            'session': async_get_clientsession(hass),
        }

        if username is not None:
            kwargs['auth'] = aiohttp.BasicAuth(username, password)
            image_auth_string = "{}:{}@".format(username, password)
        else:
            image_auth_string = ""

        http_protocol = 'https' if encryption else 'http'
        ws_protocol = 'wss' if encryption else 'ws'

        self._http_url = '{}://{}:{}/jsonrpc'.format(http_protocol, host, port)
        self._image_url = '{}://{}{}:{}/image'.format(
            http_protocol, image_auth_string, host, port)
        self._ws_url = '{}://{}:{}/jsonrpc'.format(ws_protocol, host, tcp_port)

        self._http_server = jsonrpc_async.Server(self._http_url, **kwargs)
        if websocket:
            # Setup websocket connection
            self._ws_server = jsonrpc_websocket.Server(self._ws_url, **kwargs)

            # Register notification listeners
            self._ws_server.Player.OnPause = self.async_on_speed_event
            self._ws_server.Player.OnPlay = self.async_on_speed_event
            self._ws_server.Player.OnSpeedChanged = self.async_on_speed_event
            self._ws_server.Player.OnStop = self.async_on_stop
            self._ws_server.Application.OnVolumeChanged = \
                self.async_on_volume_changed
            self._ws_server.System.OnQuit = self.async_on_quit
            self._ws_server.System.OnRestart = self.async_on_quit
            self._ws_server.System.OnSleep = self.async_on_quit

            def on_hass_stop(event):
                """Close websocket connection when hass stops."""
                self.hass.async_add_job(self._ws_server.close())

            self.hass.bus.async_listen_once(
                EVENT_HOMEASSISTANT_STOP, on_hass_stop)
        else:
            self._ws_server = None

        self._turn_off_action = turn_off_action
        self._enable_websocket = websocket
        self._players = list()
        self._properties = {}
        self._item = {}
        self._app_properties = {}
Example #3
0
    def __init__(self, host, port, username, password, ssl, timeout, session):
        """Initialize the object."""
        super().__init__(host, port, username, password, ssl, timeout, session)

        http_protocol = "https" if ssl else "http"

        http_url = f"{http_protocol}://{host}:{port}/jsonrpc"

        self._http_server = jsonrpc_async.Server(http_url, **self._kwargs)
Example #4
0
    def __init__(self, hass, url, auth=None):
        """Initialize the service."""
        self._url = url

        kwargs = {
            "timeout": DEFAULT_TIMEOUT,
            "session": async_get_clientsession(hass)
        }

        if auth is not None:
            kwargs["auth"] = auth

        self._server = jsonrpc_async.Server(self._url, **kwargs)
Example #5
0
    def __init__(self, hass, url, auth=None):
        """Initialize the service."""
        import jsonrpc_async
        self._url = url

        kwargs = {
            'timeout': DEFAULT_TIMEOUT,
            'session': async_get_clientsession(hass),
        }

        if auth is not None:
            kwargs['auth'] = auth

        self._server = jsonrpc_async.Server(self._url, **kwargs)
    def __init__(
        self,
        hass,
        name,
        host,
        port,
        tcp_port,
        encryption=False,
        username=None,
        password=None,
        turn_on_action=None,
        turn_off_action=None,
        timeout=DEFAULT_TIMEOUT,
        websocket=True,
        unique_id=None,
    ):
        """Initialize the Kodi device."""
        self.hass = hass
        self._name = name
        self._unique_id = unique_id
        self._media_position_updated_at = None
        self._media_position = None

        kwargs = {"timeout": timeout, "session": async_get_clientsession(hass)}

        if username is not None:
            kwargs["auth"] = aiohttp.BasicAuth(username, password)
            image_auth_string = f"{username}:{password}@"
        else:
            image_auth_string = ""

        http_protocol = "https" if encryption else "http"
        ws_protocol = "wss" if encryption else "ws"

        self._http_url = f"{http_protocol}://{host}:{port}/jsonrpc"
        self._image_url = f"{http_protocol}://{image_auth_string}{host}:{port}/image"
        self._ws_url = f"{ws_protocol}://{host}:{tcp_port}/jsonrpc"

        self._http_server = jsonrpc_async.Server(self._http_url, **kwargs)
        if websocket:
            # Setup websocket connection
            self._ws_server = jsonrpc_websocket.Server(self._ws_url, **kwargs)

            # Register notification listeners
            self._ws_server.Player.OnPause = self.async_on_speed_event
            self._ws_server.Player.OnPlay = self.async_on_speed_event
            self._ws_server.Player.OnAVStart = self.async_on_speed_event
            self._ws_server.Player.OnAVChange = self.async_on_speed_event
            self._ws_server.Player.OnResume = self.async_on_speed_event
            self._ws_server.Player.OnSpeedChanged = self.async_on_speed_event
            self._ws_server.Player.OnSeek = self.async_on_speed_event
            self._ws_server.Player.OnStop = self.async_on_stop
            self._ws_server.Application.OnVolumeChanged = self.async_on_volume_changed
            self._ws_server.System.OnQuit = self.async_on_quit
            self._ws_server.System.OnRestart = self.async_on_quit
            self._ws_server.System.OnSleep = self.async_on_quit

            def on_hass_stop(event):
                """Close websocket connection when hass stops."""
                self.hass.async_create_task(self._ws_server.close())

            self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP,
                                            on_hass_stop)
        else:
            self._ws_server = None

        # Script creation for the turn on/off config options
        if turn_on_action is not None:
            turn_on_action = script.Script(
                self.hass,
                turn_on_action,
                f"{self.name} turn ON script",
                self.async_update_ha_state(True),
            )
        if turn_off_action is not None:
            turn_off_action = script.Script(
                self.hass,
                _check_deprecated_turn_off(hass, turn_off_action),
                f"{self.name} turn OFF script",
            )
        self._turn_on_action = turn_on_action
        self._turn_off_action = turn_off_action
        self._enable_websocket = websocket
        self._players = list()
        self._properties = {}
        self._item = {}
        self._app_properties = {}