mirror of
https://github.com/home-assistant/core
synced 2024-08-02 23:40:32 +02:00
Add 'for' to lock device triggers (#48504)
* Add 'for' to lock device triggers * Update tests/components/lock/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
04a09d9215
commit
9d8521169b
@ -10,6 +10,7 @@ from homeassistant.const import (
|
||||
CONF_DEVICE_ID,
|
||||
CONF_DOMAIN,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_FOR,
|
||||
CONF_PLATFORM,
|
||||
CONF_TYPE,
|
||||
STATE_LOCKED,
|
||||
@ -27,6 +28,7 @@ TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
|
||||
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
|
||||
}
|
||||
)
|
||||
|
||||
@ -56,6 +58,15 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> list[dict]:
|
||||
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(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
@ -73,6 +84,8 @@ async def async_attach_trigger(
|
||||
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
||||
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)
|
||||
return await state_trigger.async_attach_trigger(
|
||||
hass, state_config, action, automation_info, platform_type="device"
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""The tests for Lock device triggers."""
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.automation as automation
|
||||
@ -6,10 +8,13 @@ from homeassistant.components.lock import DOMAIN
|
||||
from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED
|
||||
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,
|
||||
mock_device_registry,
|
||||
@ -65,6 +70,29 @@ async def test_get_triggers(hass, device_reg, entity_reg):
|
||||
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 lock."""
|
||||
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) == 2
|
||||
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):
|
||||
"""Test for turn_on and turn_off triggers firing."""
|
||||
hass.states.async_set("lock.entity", STATE_UNLOCKED)
|
||||
@ -131,3 +159,58 @@ async def test_if_fires_on_state_change(hass, calls):
|
||||
assert calls[1].data[
|
||||
"some"
|
||||
] == "unlocked - device - {} - locked - unlocked - None".format("lock.entity")
|
||||
|
||||
|
||||
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_UNLOCKED)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": entity_id,
|
||||
"type": "locked",
|
||||
"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_UNLOCKED
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set(entity_id, STATE_LOCKED)
|
||||
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} - unlocked - locked - 0:00:05"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user