From c530bc823b948a2f745c27bcff941210441a395c Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 27 Apr 2022 15:40:07 +0200 Subject: [PATCH] Support buffering in media_player device conditions (#70863) Co-authored-by: Franck Nijhof --- .../media_player/device_condition.py | 24 ++++++--- .../components/media_player/strings.json | 2 + .../media_player/translations/en.json | 2 + .../media_player/test_device_condition.py | 53 ++++++++++++++++++- 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/media_player/device_condition.py b/homeassistant/components/media_player/device_condition.py index 2d8c04dc181..5f57bcea48e 100644 --- a/homeassistant/components/media_player/device_condition.py +++ b/homeassistant/components/media_player/device_condition.py @@ -10,6 +10,7 @@ from homeassistant.const import ( CONF_DOMAIN, CONF_ENTITY_ID, CONF_TYPE, + STATE_BUFFERING, STATE_IDLE, STATE_OFF, STATE_ON, @@ -23,7 +24,14 @@ from homeassistant.helpers.typing import ConfigType, TemplateVarsType from .const import DOMAIN -CONDITION_TYPES = {"is_on", "is_off", "is_idle", "is_paused", "is_playing"} +CONDITION_TYPES = { + "is_on", + "is_off", + "is_buffering", + "is_idle", + "is_paused", + "is_playing", +} CONDITION_SCHEMA = DEVICE_CONDITION_BASE_SCHEMA.extend( { @@ -63,16 +71,18 @@ def async_condition_from_config( hass: HomeAssistant, config: ConfigType ) -> condition.ConditionCheckerType: """Create a function to test a device condition.""" - if config[CONF_TYPE] == "is_playing": - state = STATE_PLAYING + if config[CONF_TYPE] == "is_buffering": + state = STATE_BUFFERING elif config[CONF_TYPE] == "is_idle": state = STATE_IDLE - elif config[CONF_TYPE] == "is_paused": - state = STATE_PAUSED + elif config[CONF_TYPE] == "is_off": + state = STATE_OFF elif config[CONF_TYPE] == "is_on": state = STATE_ON - else: - state = STATE_OFF + elif config[CONF_TYPE] == "is_paused": + state = STATE_PAUSED + else: # is_playing + state = STATE_PLAYING def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool: """Test if an entity is a certain state.""" diff --git a/homeassistant/components/media_player/strings.json b/homeassistant/components/media_player/strings.json index 7831731f4cb..bb6c7d16f5a 100644 --- a/homeassistant/components/media_player/strings.json +++ b/homeassistant/components/media_player/strings.json @@ -2,6 +2,7 @@ "title": "Media player", "device_automation": { "condition_type": { + "is_buffering": "{entity_name} is buffering", "is_on": "{entity_name} is on", "is_off": "{entity_name} is off", "is_idle": "{entity_name} is idle", @@ -9,6 +10,7 @@ "is_playing": "{entity_name} is playing" }, "trigger_type": { + "buffering": "{entity_name} starts buffering", "turned_on": "{entity_name} turned on", "turned_off": "{entity_name} turned off", "idle": "{entity_name} becomes idle", diff --git a/homeassistant/components/media_player/translations/en.json b/homeassistant/components/media_player/translations/en.json index d1eb6e7c106..119c057167e 100644 --- a/homeassistant/components/media_player/translations/en.json +++ b/homeassistant/components/media_player/translations/en.json @@ -1,6 +1,7 @@ { "device_automation": { "condition_type": { + "is_buffering": "{entity_name} is buffering", "is_idle": "{entity_name} is idle", "is_off": "{entity_name} is off", "is_on": "{entity_name} is on", @@ -8,6 +9,7 @@ "is_playing": "{entity_name} is playing" }, "trigger_type": { + "buffering": "{entity_name} starts buffering", "changed_states": "{entity_name} changed states", "idle": "{entity_name} becomes idle", "paused": "{entity_name} is paused", diff --git a/tests/components/media_player/test_device_condition.py b/tests/components/media_player/test_device_condition.py index 111e1048d9c..7556c2476da 100644 --- a/tests/components/media_player/test_device_condition.py +++ b/tests/components/media_player/test_device_condition.py @@ -5,6 +5,7 @@ import homeassistant.components.automation as automation from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.media_player import DOMAIN from homeassistant.const import ( + STATE_BUFFERING, STATE_IDLE, STATE_OFF, STATE_ON, @@ -63,7 +64,14 @@ async def test_get_conditions(hass, device_reg, entity_reg): "entity_id": f"{DOMAIN}.test_5678", "metadata": {"secondary": False}, } - for condition in ["is_off", "is_on", "is_idle", "is_paused", "is_playing"] + for condition in [ + "is_buffering", + "is_off", + "is_on", + "is_idle", + "is_paused", + "is_playing", + ] ] conditions = await async_get_device_automations( hass, DeviceAutomationType.CONDITION, device_entry.id @@ -111,7 +119,14 @@ async def test_get_conditions_hidden_auxiliary( "entity_id": f"{DOMAIN}.test_5678", "metadata": {"secondary": True}, } - for condition in ["is_off", "is_on", "is_idle", "is_paused", "is_playing"] + for condition in [ + "is_buffering", + "is_off", + "is_on", + "is_idle", + "is_paused", + "is_playing", + ] ] conditions = await async_get_device_automations( hass, DeviceAutomationType.CONDITION, device_entry.id @@ -218,6 +233,24 @@ async def test_if_state(hass, calls): }, }, }, + { + "trigger": {"platform": "event", "event_type": "test_event6"}, + "condition": [ + { + "condition": "device", + "domain": DOMAIN, + "device_id": "", + "entity_id": "media_player.entity", + "type": "is_buffering", + } + ], + "action": { + "service": "test.automation", + "data_template": { + "some": "is_buffering - {{ trigger.platform }} - {{ trigger.event.event_type }}" + }, + }, + }, ] }, ) @@ -226,6 +259,7 @@ async def test_if_state(hass, calls): hass.bus.async_fire("test_event3") hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event5") + hass.bus.async_fire("test_event6") await hass.async_block_till_done() assert len(calls) == 1 assert calls[0].data["some"] == "is_on - event - test_event1" @@ -236,6 +270,7 @@ async def test_if_state(hass, calls): hass.bus.async_fire("test_event3") hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event5") + hass.bus.async_fire("test_event6") await hass.async_block_till_done() assert len(calls) == 2 assert calls[1].data["some"] == "is_off - event - test_event2" @@ -246,6 +281,7 @@ async def test_if_state(hass, calls): hass.bus.async_fire("test_event3") hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event5") + hass.bus.async_fire("test_event6") await hass.async_block_till_done() assert len(calls) == 3 assert calls[2].data["some"] == "is_idle - event - test_event3" @@ -256,6 +292,7 @@ async def test_if_state(hass, calls): hass.bus.async_fire("test_event3") hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event5") + hass.bus.async_fire("test_event6") await hass.async_block_till_done() assert len(calls) == 4 assert calls[3].data["some"] == "is_paused - event - test_event4" @@ -266,6 +303,18 @@ async def test_if_state(hass, calls): hass.bus.async_fire("test_event3") hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event5") + hass.bus.async_fire("test_event6") await hass.async_block_till_done() assert len(calls) == 5 assert calls[4].data["some"] == "is_playing - event - test_event5" + + hass.states.async_set("media_player.entity", STATE_BUFFERING) + hass.bus.async_fire("test_event1") + hass.bus.async_fire("test_event2") + hass.bus.async_fire("test_event3") + hass.bus.async_fire("test_event4") + hass.bus.async_fire("test_event5") + hass.bus.async_fire("test_event6") + await hass.async_block_till_done() + assert len(calls) == 6 + assert calls[5].data["some"] == "is_buffering - event - test_event6"