Add 'for' to cover device triggers (#48324)

This commit is contained in:
Erik Montnemery 2021-03-29 22:57:30 +02:00 committed by GitHub
parent 52475c108f
commit c459789c09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 5 deletions

View file

@ -1,4 +1,6 @@
"""The tests for Cover device triggers."""
from datetime import timedelta
import pytest
import homeassistant.components.automation as automation
@ -12,10 +14,12 @@ from homeassistant.const import (
)
from homeassistant.helpers import device_registry
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from tests.common import (
MockConfigEntry,
assert_lists_same,
async_fire_time_changed,
async_get_device_automation_capabilities,
async_get_device_automations,
async_mock_service,
@ -234,7 +238,11 @@ async def test_get_trigger_capabilities(hass, device_reg, entity_reg):
capabilities = await async_get_device_automation_capabilities(
hass, "trigger", trigger
)
assert capabilities == {"extra_fields": []}
assert capabilities == {
"extra_fields": [
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
]
}
async def test_get_trigger_capabilities_set_pos(hass, device_reg, entity_reg):
@ -284,7 +292,15 @@ async def test_get_trigger_capabilities_set_pos(hass, device_reg, entity_reg):
if trigger["type"] == "position":
assert capabilities == expected_capabilities
else:
assert capabilities == {"extra_fields": []}
assert capabilities == {
"extra_fields": [
{
"name": "for",
"optional": True,
"type": "positive_time_period_dict",
}
]
}
async def test_get_trigger_capabilities_set_tilt_pos(hass, device_reg, entity_reg):
@ -334,7 +350,15 @@ async def test_get_trigger_capabilities_set_tilt_pos(hass, device_reg, entity_re
if trigger["type"] == "tilt_position":
assert capabilities == expected_capabilities
else:
assert capabilities == {"extra_fields": []}
assert capabilities == {
"extra_fields": [
{
"name": "for",
"optional": True,
"type": "positive_time_period_dict",
}
]
}
async def test_if_fires_on_state_change(hass, calls):
@ -459,6 +483,61 @@ async def test_if_fires_on_state_change(hass, calls):
] == "closing - device - {} - opening - closing - None".format("cover.entity")
async def test_if_fires_on_state_change_with_for(hass, calls):
"""Test for triggers firing with delay."""
entity_id = "cover.entity"
hass.states.async_set(entity_id, STATE_CLOSED)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": entity_id,
"type": "opened",
"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_CLOSED
assert len(calls) == 0
hass.states.async_set(entity_id, STATE_OPEN)
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} - closed - open - 0:00:05"
)
async def test_if_fires_on_position(hass, calls):
"""Test for position triggers."""
platform = getattr(hass.components, f"test.{DOMAIN}")