mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 01:07:10 +00:00
Tweak template lock tests (#71734)
This commit is contained in:
parent
1cb00cbb79
commit
35e4f11e0b
@ -3,7 +3,48 @@ import pytest
|
||||
|
||||
from homeassistant import setup
|
||||
from homeassistant.components import lock
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
||||
from homeassistant.const import (
|
||||
ATTR_DOMAIN,
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_SERVICE_DATA,
|
||||
EVENT_CALL_SERVICE,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service_calls(hass):
|
||||
"""Track service call events for switch.test_state."""
|
||||
events = []
|
||||
entity_id = "switch.test_state"
|
||||
|
||||
@callback
|
||||
def capture_events(event):
|
||||
if event.data[ATTR_DOMAIN] != "switch":
|
||||
return
|
||||
if event.data[ATTR_SERVICE_DATA][ATTR_ENTITY_ID] != [entity_id]:
|
||||
return
|
||||
events.append(event)
|
||||
|
||||
hass.bus.async_listen(EVENT_CALL_SERVICE, capture_events)
|
||||
|
||||
return events
|
||||
|
||||
|
||||
OPTIMISTIC_LOCK_CONFIG = {
|
||||
"platform": "template",
|
||||
"lock": {
|
||||
"service": "switch.turn_on",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
|
||||
@ -12,17 +53,9 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVA
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"name": "Test template lock",
|
||||
"value_template": "{{ states.switch.test_state.state }}",
|
||||
"lock": {
|
||||
"service": "switch.turn_on",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
@ -48,16 +81,8 @@ async def test_template_state(hass, start_ha):
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"value_template": "{{ 1 == 1 }}",
|
||||
"lock": {
|
||||
"service": "switch.turn_on",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
@ -74,16 +99,8 @@ async def test_template_state_boolean_on(hass, start_ha):
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"value_template": "{{ 1 == 2 }}",
|
||||
"lock": {
|
||||
"service": "switch.turn_on",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
@ -155,16 +172,8 @@ async def test_template_syntax_error(hass, start_ha):
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"value_template": "{{ 1 + 1 }}",
|
||||
"lock": {
|
||||
"service": "switch.turn_on",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
@ -186,19 +195,15 @@ async def test_template_static(hass, start_ha):
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"value_template": "{{ states.switch.test_state.state }}",
|
||||
"lock": {"service": "test.automation"},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
async def test_lock_action(hass, start_ha, calls):
|
||||
async def test_lock_action(hass, start_ha, service_calls):
|
||||
"""Test lock action."""
|
||||
await setup.async_setup_component(hass, "switch", {})
|
||||
hass.states.async_set("switch.test_state", STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@ -210,7 +215,8 @@ async def test_lock_action(hass, start_ha, calls):
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[-1].data["service"] == "turn_on"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
|
||||
@ -219,19 +225,15 @@ async def test_lock_action(hass, start_ha, calls):
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"value_template": "{{ states.switch.test_state.state }}",
|
||||
"lock": {
|
||||
"service": "switch.turn_on",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
"unlock": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
)
|
||||
async def test_unlock_action(hass, start_ha, calls):
|
||||
async def test_unlock_action(hass, start_ha, service_calls):
|
||||
"""Test unlock action."""
|
||||
await setup.async_setup_component(hass, "switch", {})
|
||||
hass.states.async_set("switch.test_state", STATE_ON)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@ -243,7 +245,8 @@ async def test_unlock_action(hass, start_ha, calls):
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[-1].data["service"] == "turn_off"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("count,domain", [(1, lock.DOMAIN)])
|
||||
@ -252,10 +255,8 @@ async def test_unlock_action(hass, start_ha, calls):
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"value_template": "{{ states.input_select.test_state.state }}",
|
||||
"lock": {"service": "test.automation"},
|
||||
"unlock": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
],
|
||||
@ -264,7 +265,7 @@ async def test_unlock_action(hass, start_ha, calls):
|
||||
"test_state", [lock.STATE_UNLOCKING, lock.STATE_LOCKING, lock.STATE_JAMMED]
|
||||
)
|
||||
async def test_lock_state(hass, test_state, start_ha):
|
||||
"""Test unlocking."""
|
||||
"""Test value template."""
|
||||
hass.states.async_set("input_select.test_state", test_state)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@ -278,13 +279,8 @@ async def test_lock_state(hass, test_state, start_ha):
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"value_template": "{{ states('switch.test_state') }}",
|
||||
"lock": {"service": "switch.turn_on", "entity_id": "switch.test_state"},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
"availability_template": "{{ is_state('availability_state.state', 'on') }}",
|
||||
}
|
||||
},
|
||||
@ -313,14 +309,9 @@ async def test_available_template_with_entities(hass, start_ha):
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"value_template": "{{ 1 + 1 }}",
|
||||
"availability_template": "{{ x - 12 }}",
|
||||
"lock": {"service": "switch.turn_on", "entity_id": "switch.test_state"},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
@ -339,15 +330,10 @@ async def test_invalid_availability_template_keeps_component_available(
|
||||
[
|
||||
{
|
||||
lock.DOMAIN: {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"name": "test_template_lock_01",
|
||||
"unique_id": "not-so-unique-anymore",
|
||||
"value_template": "{{ true }}",
|
||||
"lock": {"service": "switch.turn_on", "entity_id": "switch.test_state"},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
}
|
||||
},
|
||||
],
|
||||
@ -359,15 +345,10 @@ async def test_unique_id(hass, start_ha):
|
||||
lock.DOMAIN,
|
||||
{
|
||||
"lock": {
|
||||
"platform": "template",
|
||||
**OPTIMISTIC_LOCK_CONFIG,
|
||||
"name": "test_template_lock_02",
|
||||
"unique_id": "not-so-unique-anymore",
|
||||
"value_template": "{{ false }}",
|
||||
"lock": {"service": "switch.turn_on", "entity_id": "switch.test_state"},
|
||||
"unlock": {
|
||||
"service": "switch.turn_off",
|
||||
"entity_id": "switch.test_state",
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user