mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Support buffering in media_player device triggers (#70864)
This commit is contained in:
parent
71529f4476
commit
e982e315ee
@ -21,6 +21,7 @@ from homeassistant.const import (
|
|||||||
CONF_FOR,
|
CONF_FOR,
|
||||||
CONF_PLATFORM,
|
CONF_PLATFORM,
|
||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
|
STATE_BUFFERING,
|
||||||
STATE_IDLE,
|
STATE_IDLE,
|
||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
@ -33,7 +34,7 @@ from homeassistant.helpers.typing import ConfigType
|
|||||||
|
|
||||||
from .const import DOMAIN
|
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(
|
MEDIA_PLAYER_TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
@ -101,15 +102,17 @@ async def async_attach_trigger(
|
|||||||
"""Attach a trigger."""
|
"""Attach a trigger."""
|
||||||
if config[CONF_TYPE] not in TRIGGER_TYPES:
|
if config[CONF_TYPE] not in TRIGGER_TYPES:
|
||||||
return await entity.async_attach_trigger(hass, config, action, automation_info)
|
return await entity.async_attach_trigger(hass, config, action, automation_info)
|
||||||
if config[CONF_TYPE] == "turned_on":
|
if config[CONF_TYPE] == "buffering":
|
||||||
to_state = STATE_ON
|
to_state = STATE_BUFFERING
|
||||||
elif config[CONF_TYPE] == "turned_off":
|
|
||||||
to_state = STATE_OFF
|
|
||||||
elif config[CONF_TYPE] == "idle":
|
elif config[CONF_TYPE] == "idle":
|
||||||
to_state = STATE_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":
|
elif config[CONF_TYPE] == "paused":
|
||||||
to_state = STATE_PAUSED
|
to_state = STATE_PAUSED
|
||||||
else:
|
else: # "playing"
|
||||||
to_state = STATE_PLAYING
|
to_state = STATE_PLAYING
|
||||||
|
|
||||||
state_config = {
|
state_config = {
|
||||||
|
@ -7,6 +7,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,
|
||||||
@ -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)
|
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
|
||||||
|
|
||||||
trigger_types = {
|
trigger_types = {
|
||||||
"turned_on",
|
"buffering",
|
||||||
"turned_off",
|
"changed_states",
|
||||||
"idle",
|
"idle",
|
||||||
"paused",
|
"paused",
|
||||||
"playing",
|
"playing",
|
||||||
"changed_states",
|
"turned_off",
|
||||||
|
"turned_on",
|
||||||
}
|
}
|
||||||
expected_triggers = [
|
expected_triggers = [
|
||||||
{
|
{
|
||||||
@ -126,12 +128,13 @@ async def test_get_triggers_hidden_auxiliary(
|
|||||||
"metadata": {"secondary": True},
|
"metadata": {"secondary": True},
|
||||||
}
|
}
|
||||||
for trigger in [
|
for trigger in [
|
||||||
"turned_on",
|
"buffering",
|
||||||
"turned_off",
|
"changed_states",
|
||||||
"idle",
|
"idle",
|
||||||
"paused",
|
"paused",
|
||||||
"playing",
|
"playing",
|
||||||
"changed_states",
|
"turned_off",
|
||||||
|
"turned_on",
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
triggers = await async_get_device_automations(
|
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(
|
triggers = await async_get_device_automations(
|
||||||
hass, DeviceAutomationType.TRIGGER, device_entry.id
|
hass, DeviceAutomationType.TRIGGER, device_entry.id
|
||||||
)
|
)
|
||||||
assert len(triggers) == 6
|
assert len(triggers) == 7
|
||||||
for trigger in triggers:
|
for trigger in triggers:
|
||||||
capabilities = await async_get_device_automation_capabilities(
|
capabilities = await async_get_device_automation_capabilities(
|
||||||
hass, DeviceAutomationType.TRIGGER, trigger
|
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.to_state.state}}}} - {{{{ trigger.for }}}}"
|
||||||
)
|
)
|
||||||
trigger_types = {
|
trigger_types = {
|
||||||
"turned_on",
|
"buffering",
|
||||||
"turned_off",
|
"changed_states",
|
||||||
"idle",
|
"idle",
|
||||||
"paused",
|
"paused",
|
||||||
"playing",
|
"playing",
|
||||||
"changed_states",
|
"turned_off",
|
||||||
|
"turned_on",
|
||||||
}
|
}
|
||||||
|
|
||||||
assert await async_setup_component(
|
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",
|
"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):
|
async def test_if_fires_on_state_change_with_for(hass, calls):
|
||||||
"""Test for triggers firing with delay."""
|
"""Test for triggers firing with delay."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user