Support buffering in media_player device conditions (#70863)

Co-authored-by: Franck Nijhof <git@frenck.dev>
pull/70880/head^2
Erik Montnemery 2022-04-27 15:40:07 +02:00 committed by GitHub
parent 9daf3d8e72
commit c530bc823b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 9 deletions

View File

@ -10,6 +10,7 @@ from homeassistant.const import (
CONF_DOMAIN, CONF_DOMAIN,
CONF_ENTITY_ID, CONF_ENTITY_ID,
CONF_TYPE, CONF_TYPE,
STATE_BUFFERING,
STATE_IDLE, STATE_IDLE,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
@ -23,7 +24,14 @@ from homeassistant.helpers.typing import ConfigType, TemplateVarsType
from .const import DOMAIN 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( CONDITION_SCHEMA = DEVICE_CONDITION_BASE_SCHEMA.extend(
{ {
@ -63,16 +71,18 @@ def async_condition_from_config(
hass: HomeAssistant, config: ConfigType hass: HomeAssistant, config: ConfigType
) -> condition.ConditionCheckerType: ) -> condition.ConditionCheckerType:
"""Create a function to test a device condition.""" """Create a function to test a device condition."""
if config[CONF_TYPE] == "is_playing": if config[CONF_TYPE] == "is_buffering":
state = STATE_PLAYING state = STATE_BUFFERING
elif config[CONF_TYPE] == "is_idle": elif config[CONF_TYPE] == "is_idle":
state = STATE_IDLE state = STATE_IDLE
elif config[CONF_TYPE] == "is_paused": elif config[CONF_TYPE] == "is_off":
state = STATE_PAUSED state = STATE_OFF
elif config[CONF_TYPE] == "is_on": elif config[CONF_TYPE] == "is_on":
state = STATE_ON state = STATE_ON
else: elif config[CONF_TYPE] == "is_paused":
state = STATE_OFF state = STATE_PAUSED
else: # is_playing
state = STATE_PLAYING
def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool: def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:
"""Test if an entity is a certain state.""" """Test if an entity is a certain state."""

View File

@ -2,6 +2,7 @@
"title": "Media player", "title": "Media player",
"device_automation": { "device_automation": {
"condition_type": { "condition_type": {
"is_buffering": "{entity_name} is buffering",
"is_on": "{entity_name} is on", "is_on": "{entity_name} is on",
"is_off": "{entity_name} is off", "is_off": "{entity_name} is off",
"is_idle": "{entity_name} is idle", "is_idle": "{entity_name} is idle",
@ -9,6 +10,7 @@
"is_playing": "{entity_name} is playing" "is_playing": "{entity_name} is playing"
}, },
"trigger_type": { "trigger_type": {
"buffering": "{entity_name} starts buffering",
"turned_on": "{entity_name} turned on", "turned_on": "{entity_name} turned on",
"turned_off": "{entity_name} turned off", "turned_off": "{entity_name} turned off",
"idle": "{entity_name} becomes idle", "idle": "{entity_name} becomes idle",

View File

@ -1,6 +1,7 @@
{ {
"device_automation": { "device_automation": {
"condition_type": { "condition_type": {
"is_buffering": "{entity_name} is buffering",
"is_idle": "{entity_name} is idle", "is_idle": "{entity_name} is idle",
"is_off": "{entity_name} is off", "is_off": "{entity_name} is off",
"is_on": "{entity_name} is on", "is_on": "{entity_name} is on",
@ -8,6 +9,7 @@
"is_playing": "{entity_name} is playing" "is_playing": "{entity_name} is playing"
}, },
"trigger_type": { "trigger_type": {
"buffering": "{entity_name} starts buffering",
"changed_states": "{entity_name} changed states", "changed_states": "{entity_name} changed states",
"idle": "{entity_name} becomes idle", "idle": "{entity_name} becomes idle",
"paused": "{entity_name} is paused", "paused": "{entity_name} is paused",

View File

@ -5,6 +5,7 @@ import homeassistant.components.automation as automation
from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.components.media_player import DOMAIN from homeassistant.components.media_player import DOMAIN
from homeassistant.const import ( from homeassistant.const import (
STATE_BUFFERING,
STATE_IDLE, STATE_IDLE,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
@ -63,7 +64,14 @@ async def test_get_conditions(hass, device_reg, entity_reg):
"entity_id": f"{DOMAIN}.test_5678", "entity_id": f"{DOMAIN}.test_5678",
"metadata": {"secondary": False}, "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( conditions = await async_get_device_automations(
hass, DeviceAutomationType.CONDITION, device_entry.id hass, DeviceAutomationType.CONDITION, device_entry.id
@ -111,7 +119,14 @@ async def test_get_conditions_hidden_auxiliary(
"entity_id": f"{DOMAIN}.test_5678", "entity_id": f"{DOMAIN}.test_5678",
"metadata": {"secondary": True}, "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( conditions = await async_get_device_automations(
hass, DeviceAutomationType.CONDITION, device_entry.id 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_event3")
hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event4")
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
assert calls[0].data["some"] == "is_on - event - test_event1" 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_event3")
hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event4")
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(calls) == 2
assert calls[1].data["some"] == "is_off - event - test_event2" 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_event3")
hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event4")
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 3 assert len(calls) == 3
assert calls[2].data["some"] == "is_idle - event - test_event3" 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_event3")
hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event4")
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 4 assert len(calls) == 4
assert calls[3].data["some"] == "is_paused - event - test_event4" 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_event3")
hass.bus.async_fire("test_event4") hass.bus.async_fire("test_event4")
hass.bus.async_fire("test_event5") hass.bus.async_fire("test_event5")
hass.bus.async_fire("test_event6")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 5 assert len(calls) == 5
assert calls[4].data["some"] == "is_playing - event - test_event5" 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"