Handle Kodi shutdown (#39856)

* Handle Kodi shutdown

* Core review comments

* Make async_on_quit a coroutine
This commit is contained in:
On Freund 2020-09-12 15:22:14 +03:00 committed by GitHub
parent aaa8083d49
commit cf6b84790f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 9 deletions

View File

@ -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"
],

View File

@ -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):

View File

@ -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

View File

@ -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