diff --git a/homeassistant/components/kodi/manifest.json b/homeassistant/components/kodi/manifest.json index b3794d5dfa2..da4daf85ada 100644 --- a/homeassistant/components/kodi/manifest.json +++ b/homeassistant/components/kodi/manifest.json @@ -2,7 +2,7 @@ "domain": "kodi", "name": "Kodi", "documentation": "https://www.home-assistant.io/integrations/kodi", - "requirements": ["pykodi==0.1.2"], + "requirements": ["pykodi==0.2.0"], "codeowners": [ "@OnFreund" ], diff --git a/homeassistant/components/kodi/media_player.py b/homeassistant/components/kodi/media_player.py index ce05f3fc732..f13d5301625 100644 --- a/homeassistant/components/kodi/media_player.py +++ b/homeassistant/components/kodi/media_player.py @@ -5,6 +5,7 @@ import logging import re import jsonrpc_base +from pykodi import CannotConnectError import voluptuous as vol from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity @@ -324,11 +325,15 @@ class KodiEntity(MediaPlayerEntity): self._app_properties["muted"] = data["muted"] self.async_write_ha_state() - @callback - def async_on_quit(self, sender, data): + async def async_on_quit(self, sender, data): """Reset the player state on quit action.""" + await self._clear_connection() + + async def _clear_connection(self, close=True): self._reset_state() - self.hass.async_create_task(self._connection.close()) + self.async_write_ha_state() + if close: + await self._connection.close() @property def unique_id(self): @@ -386,14 +391,23 @@ class KodiEntity(MediaPlayerEntity): try: await self._connection.connect() self._on_ws_connected() - except jsonrpc_base.jsonrpc.TransportError: - _LOGGER.info("Unable to connect to Kodi via websocket") + except (jsonrpc_base.jsonrpc.TransportError, CannotConnectError): _LOGGER.debug("Unable to connect to Kodi via websocket", exc_info=True) + await self._clear_connection(False) + + async def _ping(self): + try: + await self._kodi.ping() + except (jsonrpc_base.jsonrpc.TransportError, CannotConnectError): + _LOGGER.debug("Unable to ping Kodi via websocket", exc_info=True) + await self._clear_connection() async def _async_connect_websocket_if_disconnected(self, *_): """Reconnect the websocket if it fails.""" if not self._connection.connected: await self._async_ws_connect() + else: + await self._ping() @callback def _register_ws_callbacks(self): @@ -464,7 +478,7 @@ class KodiEntity(MediaPlayerEntity): @property def should_poll(self): """Return True if entity has to be polled for state.""" - return (not self._connection.can_subscribe) or (not self._connection.connected) + return not self._connection.can_subscribe @property def volume_level(self): diff --git a/requirements_all.txt b/requirements_all.txt index a2256a6ea82..bc80165d46b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1431,7 +1431,7 @@ pyitachip2ir==0.0.7 pykira==0.1.1 # homeassistant.components.kodi -pykodi==0.1.2 +pykodi==0.2.0 # homeassistant.components.kwb pykwb==0.0.8 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index f4acc7ae79c..0b34d1f3333 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -692,7 +692,7 @@ pyisy==2.0.2 pykira==0.1.1 # homeassistant.components.kodi -pykodi==0.1.2 +pykodi==0.2.0 # homeassistant.components.lastfm pylast==3.3.0