Support buffering in media_player device triggers (#70864)

This commit is contained in:
Erik Montnemery 2022-04-27 11:47:04 +02:00 committed by GitHub
parent 71529f4476
commit e982e315ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 16 deletions

View file

@ -21,6 +21,7 @@ from homeassistant.const import (
CONF_FOR,
CONF_PLATFORM,
CONF_TYPE,
STATE_BUFFERING,
STATE_IDLE,
STATE_OFF,
STATE_ON,
@ -33,7 +34,7 @@ from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN
TRIGGER_TYPES = {"turned_on", "turned_off", "idle", "paused", "playing"}
TRIGGER_TYPES = {"turned_on", "turned_off", "buffering", "idle", "paused", "playing"}
MEDIA_PLAYER_TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
{
@ -101,15 +102,17 @@ async def async_attach_trigger(
"""Attach a trigger."""
if config[CONF_TYPE] not in TRIGGER_TYPES:
return await entity.async_attach_trigger(hass, config, action, automation_info)
if config[CONF_TYPE] == "turned_on":
to_state = STATE_ON
elif config[CONF_TYPE] == "turned_off":
to_state = STATE_OFF
if config[CONF_TYPE] == "buffering":
to_state = STATE_BUFFERING
elif config[CONF_TYPE] == "idle":
to_state = STATE_IDLE
elif config[CONF_TYPE] == "turned_off":
to_state = STATE_OFF
elif config[CONF_TYPE] == "turned_on":
to_state = STATE_ON
elif config[CONF_TYPE] == "paused":
to_state = STATE_PAUSED
else:
else: # "playing"
to_state = STATE_PLAYING
state_config = {

View file

@ -7,6 +7,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,
@ -61,12 +62,13 @@ async def test_get_triggers(hass, device_reg, entity_reg):
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
trigger_types = {
"turned_on",
"turned_off",
"buffering",
"changed_states",
"idle",
"paused",
"playing",
"changed_states",
"turned_off",
"turned_on",
}
expected_triggers = [
{
@ -126,12 +128,13 @@ async def test_get_triggers_hidden_auxiliary(
"metadata": {"secondary": True},
}
for trigger in [
"turned_on",
"turned_off",
"buffering",
"changed_states",
"idle",
"paused",
"playing",
"changed_states",
"turned_off",
"turned_on",
]
]
triggers = await async_get_device_automations(
@ -153,7 +156,7 @@ async def test_get_trigger_capabilities(hass, device_reg, entity_reg):
triggers = await async_get_device_automations(
hass, DeviceAutomationType.TRIGGER, device_entry.id
)
assert len(triggers) == 6
assert len(triggers) == 7
for trigger in triggers:
capabilities = await async_get_device_automation_capabilities(
hass, DeviceAutomationType.TRIGGER, trigger
@ -175,12 +178,13 @@ async def test_if_fires_on_state_change(hass, calls):
"{{{{ trigger.to_state.state}}}} - {{{{ trigger.for }}}}"
)
trigger_types = {
"turned_on",
"turned_off",
"buffering",
"changed_states",
"idle",
"paused",
"playing",
"changed_states",
"turned_off",
"turned_on",
}
assert await async_setup_component(
@ -251,6 +255,15 @@ async def test_if_fires_on_state_change(hass, calls):
"changed_states - device - media_player.entity - playing - paused - None",
}
# Fake that the entity is buffering.
hass.states.async_set("media_player.entity", STATE_BUFFERING)
await hass.async_block_till_done()
assert len(calls) == 12
assert {calls[10].data["some"], calls[11].data["some"]} == {
"buffering - device - media_player.entity - paused - buffering - None",
"changed_states - device - media_player.entity - paused - buffering - None",
}
async def test_if_fires_on_state_change_with_for(hass, calls):
"""Test for triggers firing with delay."""