From 4b7693c662a1c1319bc04332dddae87afa393b97 Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 12 May 2022 14:37:49 +0200 Subject: [PATCH 1/2] Tweak template lock tests --- tests/components/template/test_lock.py | 144 +++++++++++-------------- 1 file changed, 63 insertions(+), 81 deletions(-) diff --git a/tests/components/template/test_lock.py b/tests/components/template/test_lock.py index 80c83e0885a76..09fdbf84ab3e8 100644 --- a/tests/components/template/test_lock.py +++ b/tests/components/template/test_lock.py @@ -3,7 +3,49 @@ 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): + print(event.data) + 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 +54,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", - }, } }, ], @@ -48,16 +82,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 +100,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 +173,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 +196,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 +216,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 +226,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 +246,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 +256,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 +266,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 +280,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 +310,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 +331,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 +346,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", - }, }, }, ) From c3f4e16eb6b698de3dea864be647fea567427a74 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 12 May 2022 16:14:24 +0200 Subject: [PATCH 2/2] Update test_lock.py --- tests/components/template/test_lock.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/components/template/test_lock.py b/tests/components/template/test_lock.py index 09fdbf84ab3e8..e53f6660162c9 100644 --- a/tests/components/template/test_lock.py +++ b/tests/components/template/test_lock.py @@ -23,7 +23,6 @@ def service_calls(hass): @callback def capture_events(event): - print(event.data) if event.data[ATTR_DOMAIN] != "switch": return if event.data[ATTR_SERVICE_DATA][ATTR_ENTITY_ID] != [entity_id]: