Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 62 additions & 81 deletions tests/components/template/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,48 @@

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)])
Expand All @@ -12,17 +53,9 @@
[
{
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",
},
}
},
],
Expand All @@ -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",
},
}
},
],
Expand All @@ -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",
},
}
},
],
Expand Down Expand Up @@ -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",
},
}
},
],
Expand All @@ -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()

Expand All @@ -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)])
Expand All @@ -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()

Expand All @@ -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)])
Expand All @@ -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"},
}
},
],
Expand All @@ -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()

Expand All @@ -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') }}",
}
},
Expand Down Expand Up @@ -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",
},
}
},
],
Expand All @@ -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",
},
}
},
],
Expand All @@ -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",
},
},
},
)
Expand Down