diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 647cf64ea8ece..34ff8aafca25e 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -241,6 +241,20 @@ 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 + ): + self._attr_assumed_state = False + 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") diff --git a/tests/components/webostv/test_media_player.py b/tests/components/webostv/test_media_player.py index 2dff9477e5062..39c796b306e9f 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