mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add 'for' to media_player device triggers (#48505)
* Add 'for' to media_player device triggers * Update tests/components/media_player/test_device_trigger.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update test_device_trigger.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
225d79c216
commit
04a09d9215
@ -10,6 +10,7 @@ from homeassistant.const import (
|
|||||||
CONF_DEVICE_ID,
|
CONF_DEVICE_ID,
|
||||||
CONF_DOMAIN,
|
CONF_DOMAIN,
|
||||||
CONF_ENTITY_ID,
|
CONF_ENTITY_ID,
|
||||||
|
CONF_FOR,
|
||||||
CONF_PLATFORM,
|
CONF_PLATFORM,
|
||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
STATE_IDLE,
|
STATE_IDLE,
|
||||||
@ -30,6 +31,7 @@ TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend(
|
|||||||
{
|
{
|
||||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||||
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
|
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
|
||||||
|
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -59,6 +61,15 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> list[dict]:
|
|||||||
return triggers
|
return triggers
|
||||||
|
|
||||||
|
|
||||||
|
async def async_get_trigger_capabilities(hass: HomeAssistant, config: dict) -> dict:
|
||||||
|
"""List trigger capabilities."""
|
||||||
|
return {
|
||||||
|
"extra_fields": vol.Schema(
|
||||||
|
{vol.Optional(CONF_FOR): cv.positive_time_period_dict}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def async_attach_trigger(
|
async def async_attach_trigger(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config: ConfigType,
|
config: ConfigType,
|
||||||
@ -82,6 +93,8 @@ async def async_attach_trigger(
|
|||||||
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
||||||
state_trigger.CONF_TO: to_state,
|
state_trigger.CONF_TO: to_state,
|
||||||
}
|
}
|
||||||
|
if CONF_FOR in config:
|
||||||
|
state_config[CONF_FOR] = config[CONF_FOR]
|
||||||
state_config = state_trigger.TRIGGER_SCHEMA(state_config)
|
state_config = state_trigger.TRIGGER_SCHEMA(state_config)
|
||||||
return await state_trigger.async_attach_trigger(
|
return await state_trigger.async_attach_trigger(
|
||||||
hass, state_config, action, automation_info, platform_type="device"
|
hass, state_config, action, automation_info, platform_type="device"
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
"""The tests for Media player device triggers."""
|
"""The tests for Media player device triggers."""
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import homeassistant.components.automation as automation
|
import homeassistant.components.automation as automation
|
||||||
@ -12,10 +14,13 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.helpers import device_registry
|
from homeassistant.helpers import device_registry
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
assert_lists_same,
|
assert_lists_same,
|
||||||
|
async_fire_time_changed,
|
||||||
|
async_get_device_automation_capabilities,
|
||||||
async_get_device_automations,
|
async_get_device_automations,
|
||||||
async_mock_service,
|
async_mock_service,
|
||||||
mock_device_registry,
|
mock_device_registry,
|
||||||
@ -67,6 +72,29 @@ async def test_get_triggers(hass, device_reg, entity_reg):
|
|||||||
assert_lists_same(triggers, expected_triggers)
|
assert_lists_same(triggers, expected_triggers)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_trigger_capabilities(hass, device_reg, entity_reg):
|
||||||
|
"""Test we get the expected capabilities from a media player."""
|
||||||
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
device_entry = device_reg.async_get_or_create(
|
||||||
|
config_entry_id=config_entry.entry_id,
|
||||||
|
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||||
|
)
|
||||||
|
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
|
||||||
|
|
||||||
|
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||||
|
assert len(triggers) == 5
|
||||||
|
for trigger in triggers:
|
||||||
|
capabilities = await async_get_device_automation_capabilities(
|
||||||
|
hass, "trigger", trigger
|
||||||
|
)
|
||||||
|
assert capabilities == {
|
||||||
|
"extra_fields": [
|
||||||
|
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_if_fires_on_state_change(hass, calls):
|
async def test_if_fires_on_state_change(hass, calls):
|
||||||
"""Test triggers firing."""
|
"""Test triggers firing."""
|
||||||
hass.states.async_set("media_player.entity", STATE_OFF)
|
hass.states.async_set("media_player.entity", STATE_OFF)
|
||||||
@ -145,3 +173,57 @@ async def test_if_fires_on_state_change(hass, calls):
|
|||||||
calls[4].data["some"]
|
calls[4].data["some"]
|
||||||
== "paused - device - media_player.entity - playing - paused - None"
|
== "paused - device - media_player.entity - playing - paused - None"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_state_change_with_for(hass, calls):
|
||||||
|
"""Test for triggers firing with delay."""
|
||||||
|
entity_id = f"{DOMAIN}.entity"
|
||||||
|
hass.states.async_set(entity_id, STATE_OFF)
|
||||||
|
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: [
|
||||||
|
{
|
||||||
|
"trigger": {
|
||||||
|
"platform": "device",
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"device_id": "",
|
||||||
|
"entity_id": entity_id,
|
||||||
|
"type": "turned_on",
|
||||||
|
"for": {"seconds": 5},
|
||||||
|
},
|
||||||
|
"action": {
|
||||||
|
"service": "test.automation",
|
||||||
|
"data_template": {
|
||||||
|
"some": "turn_off {{ trigger.%s }}"
|
||||||
|
% "}} - {{ trigger.".join(
|
||||||
|
(
|
||||||
|
"platform",
|
||||||
|
"entity_id",
|
||||||
|
"from_state.state",
|
||||||
|
"to_state.state",
|
||||||
|
"for",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get(entity_id).state == STATE_OFF
|
||||||
|
assert len(calls) == 0
|
||||||
|
|
||||||
|
hass.states.async_set(entity_id, STATE_ON)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 0
|
||||||
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 1
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert (
|
||||||
|
calls[0].data["some"] == f"turn_off device - {entity_id} - off - on - 0:00:05"
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user