Prevent kodi from blocking startup (#38257)

* Prevent kodi from blocking startup

* Update homeassistant/components/kodi/media_player.py

* isort

* ignore args

* adjustments per review

* asyncio
This commit is contained in:
J. Nick Koston 2020-07-27 19:43:42 -10:00 committed by GitHub
parent 5fef9653a8
commit f06ae1fa95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,7 @@
"""Support for interfacing with the XBMC/Kodi JSON-RPC API.""" """Support for interfacing with the XBMC/Kodi JSON-RPC API."""
import asyncio
from collections import OrderedDict from collections import OrderedDict
from datetime import timedelta
from functools import wraps from functools import wraps
import logging import logging
import re import re
@ -53,6 +55,7 @@ from homeassistant.const import (
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv, script from homeassistant.helpers import config_validation as cv, script
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.template import Template from homeassistant.helpers.template import Template
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.util.yaml import dump from homeassistant.util.yaml import dump
@ -82,6 +85,8 @@ DEPRECATED_TURN_OFF_ACTIONS = {
"shutdown": "System.Shutdown", "shutdown": "System.Shutdown",
} }
WEBSOCKET_WATCHDOG_INTERVAL = timedelta(minutes=3)
# https://github.com/xbmc/xbmc/blob/master/xbmc/media/MediaType.h # https://github.com/xbmc/xbmc/blob/master/xbmc/media/MediaType.h
MEDIA_TYPES = { MEDIA_TYPES = {
"music": MEDIA_TYPE_MUSIC, "music": MEDIA_TYPE_MUSIC,
@ -435,6 +440,26 @@ class KodiDevice(MediaPlayerEntity):
# run until the websocket connection is closed. # run until the websocket connection is closed.
self.hass.loop.create_task(ws_loop_wrapper()) self.hass.loop.create_task(ws_loop_wrapper())
async def async_added_to_hass(self):
"""Connect the websocket if needed."""
if not self._enable_websocket:
return
asyncio.create_task(self.async_ws_connect())
self.async_on_remove(
async_track_time_interval(
self.hass,
self._async_connect_websocket_if_disconnected,
WEBSOCKET_WATCHDOG_INTERVAL,
)
)
async def _async_connect_websocket_if_disconnected(self, *_):
"""Reconnect the websocket if it fails."""
if not self._ws_server.connected:
await self.async_ws_connect()
async def async_update(self): async def async_update(self):
"""Retrieve latest state.""" """Retrieve latest state."""
self._players = await self._get_players() self._players = await self._get_players()
@ -445,9 +470,6 @@ class KodiDevice(MediaPlayerEntity):
self._app_properties = {} self._app_properties = {}
return return
if self._enable_websocket and not self._ws_server.connected:
self.hass.async_create_task(self.async_ws_connect())
self._app_properties = await self.server.Application.GetProperties( self._app_properties = await self.server.Application.GetProperties(
["volume", "muted"] ["volume", "muted"]
) )