From 53051b377e18a6babd4819392b484cd18a725c14 Mon Sep 17 00:00:00 2001 From: rale Date: Tue, 27 Feb 2024 04:54:04 +0000 Subject: [PATCH 1/3] support for webos media player state --- homeassistant/components/webostv/media_player.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 647cf64ea8ece0..d011b17eb89393 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -241,6 +241,18 @@ def _update_states(self) -> None: name=self._device_name, ) + if ( + self._client.media_state is not None + and self._client.media_state.get("foregroundAppInfo") is not None + ): + for entry in self._client.media_state.get("foregroundAppInfo"): + if entry.get("playState") == "playing": + self._attr_state = MediaPlayerState.PLAYING + elif entry.get("playState") == "paused": + self._attr_state = MediaPlayerState.PAUSED + elif entry.get("playState") == "unloaded": + self._attr_state = MediaPlayerState.IDLE + if self._client.system_info is not None or self.state != MediaPlayerState.OFF: maj_v = self._client.software_info.get("major_ver") min_v = self._client.software_info.get("minor_ver") From 262c7a3e85af67ae65ba5a56f27fe9442fd5c776 Mon Sep 17 00:00:00 2001 From: rale Date: Tue, 26 Mar 2024 05:29:23 +0000 Subject: [PATCH 2/3] add test coverage and don't use assumed state if media player state is available --- .../components/webostv/media_player.py | 1 + tests/components/webostv/test_media_player.py | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index d011b17eb89393..6bbb17d4d3cf74 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -245,6 +245,7 @@ def _update_states(self) -> None: self._client.media_state is not None and self._client.media_state.get("foregroundAppInfo") is not None ): + self._attr_assumed_state = False for entry in self._client.media_state.get("foregroundAppInfo"): if entry.get("playState") == "playing": self._attr_state = MediaPlayerState.PLAYING diff --git a/tests/components/webostv/test_media_player.py b/tests/components/webostv/test_media_player.py index 2dff9477e50620..39c796b306e9fc 100644 --- a/tests/components/webostv/test_media_player.py +++ b/tests/components/webostv/test_media_player.py @@ -21,6 +21,7 @@ SERVICE_SELECT_SOURCE, MediaPlayerDeviceClass, MediaPlayerEntityFeature, + MediaPlayerState, MediaType, ) from homeassistant.components.webostv.const import ( @@ -811,3 +812,23 @@ async def test_reauth_reconnect(hass: HomeAssistant, client, monkeypatch) -> Non assert "context" in flow assert flow["context"].get("source") == SOURCE_REAUTH assert flow["context"].get("entry_id") == entry.entry_id + + +async def test_update_media_state(hass: HomeAssistant, client, monkeypatch) -> None: + """Test updating media state.""" + await setup_webostv(hass) + + data = {"foregroundAppInfo": [{"playState": "playing"}]} + monkeypatch.setattr(client, "media_state", data) + await client.mock_state_update() + assert hass.states.get(ENTITY_ID).state == MediaPlayerState.PLAYING + + data = {"foregroundAppInfo": [{"playState": "paused"}]} + monkeypatch.setattr(client, "media_state", data) + await client.mock_state_update() + assert hass.states.get(ENTITY_ID).state == MediaPlayerState.PAUSED + + data = {"foregroundAppInfo": [{"playState": "unloaded"}]} + monkeypatch.setattr(client, "media_state", data) + await client.mock_state_update() + assert hass.states.get(ENTITY_ID).state == MediaPlayerState.IDLE From ec8943725acd335747c1c89f1db80c17fb2a86d0 Mon Sep 17 00:00:00 2001 From: rale Date: Thu, 18 Apr 2024 22:33:25 -0500 Subject: [PATCH 3/3] fallback to assumed state if media state isn't available Co-authored-by: Shay Levy --- homeassistant/components/webostv/media_player.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 6bbb17d4d3cf74..34ff8aafca25e7 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -241,6 +241,7 @@ def _update_states(self) -> None: name=self._device_name, ) + self._attr_assumed_state = True if ( self._client.media_state is not None and self._client.media_state.get("foregroundAppInfo") is not None