From 89b20b91339e80e80d878acb1e466e9c3e711832 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Fri, 11 Feb 2022 16:48:36 +0200 Subject: [PATCH] Fix webostv restored supported features turn on (#66318) * Fix webostv restored supported features turn on * Remove ternary operator expression --- .../components/webostv/media_player.py | 5 +- tests/components/webostv/test_media_player.py | 52 +++++++++++++++++-- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 8fa18ce3142..320ce092427 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -326,7 +326,10 @@ class LgWebOSMediaPlayerEntity(RestoreEntity, MediaPlayerEntity): def supported_features(self) -> int: """Flag media player features that are supported.""" if self.state == STATE_OFF and self._supported_features is not None: - return self._supported_features + if self._wrapper.turn_on: + return self._supported_features | SUPPORT_TURN_ON + + return self._supported_features & ~SUPPORT_TURN_ON supported = SUPPORT_WEBOSTV diff --git a/tests/components/webostv/test_media_player.py b/tests/components/webostv/test_media_player.py index 450d3d90377..c9cc4a78aee 100644 --- a/tests/components/webostv/test_media_player.py +++ b/tests/components/webostv/test_media_player.py @@ -59,6 +59,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, ) +from homeassistant.core import State from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry from homeassistant.setup import async_setup_component @@ -67,7 +68,7 @@ from homeassistant.util import dt from . import setup_webostv from .const import CHANNEL_2, ENTITY_ID, TV_NAME -from tests.common import async_fire_time_changed +from tests.common import async_fire_time_changed, mock_restore_cache @pytest.mark.parametrize( @@ -562,14 +563,27 @@ async def test_cached_supported_features(hass, client, monkeypatch): """Test test supported features.""" monkeypatch.setattr(client, "is_on", False) monkeypatch.setattr(client, "sound_output", None) + supported = SUPPORT_WEBOSTV | SUPPORT_WEBOSTV_VOLUME | SUPPORT_TURN_ON + mock_restore_cache( + hass, + [ + State( + ENTITY_ID, + STATE_OFF, + attributes={ + ATTR_SUPPORTED_FEATURES: supported, + }, + ) + ], + ) await setup_webostv(hass) await client.mock_state_update() - # TV off, support volume mute, step, set - supported = SUPPORT_WEBOSTV | SUPPORT_WEBOSTV_VOLUME | SUPPORT_VOLUME_SET + # TV off, restored state supports mute, step + # validate SUPPORT_TURN_ON is not cached attrs = hass.states.get(ENTITY_ID).attributes - assert attrs[ATTR_SUPPORTED_FEATURES] == supported + assert attrs[ATTR_SUPPORTED_FEATURES] == supported & ~SUPPORT_TURN_ON # TV on, support volume mute, step monkeypatch.setattr(client, "is_on", True) @@ -601,7 +615,7 @@ async def test_cached_supported_features(hass, client, monkeypatch): assert attrs[ATTR_SUPPORTED_FEATURES] == supported - # TV off, support volume mute, step, step, set + # TV off, support volume mute, step, set monkeypatch.setattr(client, "is_on", False) monkeypatch.setattr(client, "sound_output", None) await client.mock_state_update() @@ -610,3 +624,31 @@ async def test_cached_supported_features(hass, client, monkeypatch): attrs = hass.states.get(ENTITY_ID).attributes assert attrs[ATTR_SUPPORTED_FEATURES] == supported + + # Test support turn on is updated on cached state + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: [ + { + "trigger": { + "platform": "webostv.turn_on", + "entity_id": ENTITY_ID, + }, + "action": { + "service": "test.automation", + "data_template": { + "some": ENTITY_ID, + "id": "{{ trigger.id }}", + }, + }, + }, + ], + }, + ) + await client.mock_state_update() + + attrs = hass.states.get(ENTITY_ID).attributes + + assert attrs[ATTR_SUPPORTED_FEATURES] == supported | SUPPORT_TURN_ON