mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Fix Kodi refresh going on after a stop or quit notification (#192353)
This commit is contained in:
@@ -147,6 +147,7 @@ class KodiEntity(MediaPlayerEntity):
|
||||
self._kodi = kodi
|
||||
self._attr_unique_id = uid
|
||||
self._device_id = None
|
||||
self._state_generation = 0
|
||||
self._players = None
|
||||
self._properties = {}
|
||||
self._item = {}
|
||||
@@ -162,6 +163,7 @@ class KodiEntity(MediaPlayerEntity):
|
||||
)
|
||||
|
||||
def _reset_state(self, players=None):
|
||||
self._state_generation += 1
|
||||
self._players = players
|
||||
self._properties = {}
|
||||
self._item = {}
|
||||
@@ -345,50 +347,65 @@ class KodiEntity(MediaPlayerEntity):
|
||||
self._reset_state()
|
||||
return
|
||||
|
||||
# A notification or the watchdog can reset the state while a request
|
||||
# is pending.
|
||||
# The replies of a refresh that started before the reset are dropped.
|
||||
generation = self._state_generation
|
||||
|
||||
try:
|
||||
self._players = await self._kodi.get_players()
|
||||
players = await self._kodi.get_players()
|
||||
except TransportError, ProtocolError:
|
||||
if not self._connection.can_subscribe:
|
||||
self._reset_state()
|
||||
return
|
||||
raise
|
||||
|
||||
if self._kodi_is_off:
|
||||
self._reset_state()
|
||||
if generation != self._state_generation:
|
||||
return
|
||||
|
||||
if self._players:
|
||||
self._app_properties = await self._kodi.get_application_properties(
|
||||
["volume", "muted"]
|
||||
)
|
||||
if not players:
|
||||
self._reset_state(players)
|
||||
return
|
||||
|
||||
self._properties = await self._kodi.get_player_properties(
|
||||
self._players[0], ["time", "totaltime", "speed", "live"]
|
||||
)
|
||||
self._players = players
|
||||
|
||||
position = self._properties["time"]
|
||||
if self._media_position != position:
|
||||
self._media_position_updated_at = dt_util.utcnow()
|
||||
self._media_position = position
|
||||
app_properties = await self._kodi.get_application_properties(
|
||||
["volume", "muted"]
|
||||
)
|
||||
if generation != self._state_generation:
|
||||
return
|
||||
self._app_properties = app_properties
|
||||
|
||||
self._item = await self._kodi.get_playing_item_properties(
|
||||
self._players[0],
|
||||
[
|
||||
"title",
|
||||
"file",
|
||||
"uniqueid",
|
||||
"thumbnail",
|
||||
"artist",
|
||||
"albumartist",
|
||||
"showtitle",
|
||||
"album",
|
||||
"season",
|
||||
"episode",
|
||||
"streamdetails",
|
||||
],
|
||||
)
|
||||
else:
|
||||
self._reset_state([])
|
||||
properties = await self._kodi.get_player_properties(
|
||||
players[0], ["time", "totaltime", "speed", "live"]
|
||||
)
|
||||
if generation != self._state_generation:
|
||||
return
|
||||
self._properties = properties
|
||||
|
||||
position = properties["time"]
|
||||
if self._media_position != position:
|
||||
self._media_position_updated_at = dt_util.utcnow()
|
||||
self._media_position = position
|
||||
|
||||
item = await self._kodi.get_playing_item_properties(
|
||||
players[0],
|
||||
[
|
||||
"title",
|
||||
"file",
|
||||
"uniqueid",
|
||||
"thumbnail",
|
||||
"artist",
|
||||
"albumartist",
|
||||
"showtitle",
|
||||
"album",
|
||||
"season",
|
||||
"episode",
|
||||
"streamdetails",
|
||||
],
|
||||
)
|
||||
if generation == self._state_generation:
|
||||
self._item = item
|
||||
|
||||
@property
|
||||
@override
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import asyncio
|
||||
from collections.abc import Awaitable, Callable
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
@@ -557,6 +558,78 @@ async def test_websocket_refresh_failure(
|
||||
assert hass.states.get(ENTITY_ID).state == MediaPlayerState.PAUSED
|
||||
|
||||
|
||||
async def stop_playback(connection: MagicMock) -> None:
|
||||
"""Let Kodi report that the playback stopped."""
|
||||
connection.server.Player.OnStop(
|
||||
"xbmc", {"end": False, "item": {"id": 1, "type": "movie"}}
|
||||
)
|
||||
|
||||
|
||||
async def quit_kodi(connection: MagicMock) -> None:
|
||||
"""Let Kodi report that it quits."""
|
||||
await connection.server.System.OnQuit("xbmc", None)
|
||||
|
||||
|
||||
REFRESH_REQUESTS = [
|
||||
"get_players",
|
||||
"get_application_properties",
|
||||
"get_player_properties",
|
||||
"get_playing_item_properties",
|
||||
]
|
||||
|
||||
|
||||
@websocket
|
||||
@pytest.mark.parametrize("request_name", REFRESH_REQUESTS)
|
||||
@pytest.mark.parametrize(
|
||||
("notification", "expected_state"),
|
||||
[
|
||||
pytest.param(stop_playback, MediaPlayerState.IDLE, id="stop"),
|
||||
pytest.param(quit_kodi, MediaPlayerState.OFF, id="quit"),
|
||||
],
|
||||
)
|
||||
async def test_websocket_reset_during_refresh(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_connection: MagicMock,
|
||||
mock_kodi: MagicMock,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
request_name: str,
|
||||
notification: Callable[[MagicMock], Awaitable[None]],
|
||||
expected_state: MediaPlayerState,
|
||||
) -> None:
|
||||
"""Test a stop or quit ends a refresh that waits for a reply."""
|
||||
await set_playing(hass, mock_kodi)
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
assert hass.states.get(ENTITY_ID).state == MediaPlayerState.PLAYING
|
||||
request = getattr(mock_kodi, request_name)
|
||||
reply = request.side_effect
|
||||
requested = asyncio.Event()
|
||||
replied = asyncio.Event()
|
||||
|
||||
async def pending_request(*args: Any) -> Any:
|
||||
result = reply(*args) if reply else request.return_value
|
||||
requested.set()
|
||||
await replied.wait()
|
||||
return result
|
||||
|
||||
request.side_effect = pending_request
|
||||
mock_kodi.reset_mock()
|
||||
|
||||
async with asyncio.timeout(5):
|
||||
mock_connection.server.Player.OnPause("xbmc", player_notification(0))
|
||||
await requested.wait()
|
||||
await notification(mock_connection)
|
||||
replied.set()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == expected_state
|
||||
assert ATTR_MEDIA_TITLE not in state.attributes
|
||||
assert not [record for record in caplog.records if record.levelno >= logging.ERROR]
|
||||
for later_request in REFRESH_REQUESTS[REFRESH_REQUESTS.index(request_name) + 1 :]:
|
||||
getattr(mock_kodi, later_request).assert_not_awaited()
|
||||
|
||||
|
||||
@websocket
|
||||
async def test_websocket_stop(
|
||||
hass: HomeAssistant,
|
||||
|
||||
Reference in New Issue
Block a user