Tweak template switch tests (#71738)

This commit is contained in:
Erik Montnemery 2022-05-12 17:08:21 +02:00 committed by GitHub
parent 35e4f11e0b
commit 11cc1feb85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,28 +5,51 @@ import pytest
from homeassistant import setup from homeassistant import setup
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import ( from homeassistant.const import (
ATTR_DOMAIN,
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
ATTR_SERVICE_DATA,
EVENT_CALL_SERVICE,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
) )
from homeassistant.core import CoreState, State from homeassistant.core import CoreState, State, callback
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import ( from tests.common import assert_setup_component, mock_component, mock_restore_cache
assert_setup_component,
async_mock_service,
mock_component,
mock_restore_cache,
)
@pytest.fixture @pytest.fixture
def calls(hass): def service_calls(hass):
"""Track calls to a mock service.""" """Track service call events for switch.test_state."""
return async_mock_service(hass, "test", "automation") 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_SWITCH_CONFIG = {
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
}
async def test_template_state_text(hass): async def test_template_state_text(hass):
@ -40,15 +63,8 @@ async def test_template_state_text(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ states.switch.test_state.state }}", "value_template": "{{ states.switch.test_state.state }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
} }
}, },
} }
@ -83,15 +99,8 @@ async def test_template_state_boolean_on(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ 1 == 1 }}", "value_template": "{{ 1 == 1 }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
} }
}, },
} }
@ -117,15 +126,8 @@ async def test_template_state_boolean_off(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ 1 == 2 }}", "value_template": "{{ 1 == 2 }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
} }
}, },
} }
@ -151,15 +153,8 @@ async def test_icon_template(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ states.switch.test_state.state }}", "value_template": "{{ states.switch.test_state.state }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
"icon_template": "{% if states.switch.test_state.state %}" "icon_template": "{% if states.switch.test_state.state %}"
"mdi:check" "mdi:check"
"{% endif %}", "{% endif %}",
@ -194,15 +189,8 @@ async def test_entity_picture_template(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ states.switch.test_state.state }}", "value_template": "{{ states.switch.test_state.state }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
"entity_picture_template": "{% if states.switch.test_state.state %}" "entity_picture_template": "{% if states.switch.test_state.state %}"
"/local/switch.png" "/local/switch.png"
"{% endif %}", "{% endif %}",
@ -237,15 +225,8 @@ async def test_template_syntax_error(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{% if rubbish %}", "value_template": "{% if rubbish %}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
} }
}, },
} }
@ -270,15 +251,8 @@ async def test_invalid_name_does_not_create(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test INVALID switch": { "test INVALID switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ rubbish }", "value_template": "{{ rubbish }",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
} }
}, },
} }
@ -393,7 +367,7 @@ async def test_missing_off_does_not_create(hass):
assert hass.states.async_all("switch") == [] assert hass.states.async_all("switch") == []
async def test_on_action(hass, calls): async def test_on_action(hass, service_calls):
"""Test on action.""" """Test on action."""
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -403,12 +377,8 @@ async def test_on_action(hass, calls):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ states.switch.test_state.state }}", "value_template": "{{ states.switch.test_state.state }}",
"turn_on": {"service": "test.automation"},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
} }
}, },
} }
@ -432,10 +402,11 @@ async def test_on_action(hass, calls):
blocking=True, blocking=True,
) )
assert len(calls) == 1 assert len(service_calls) == 1
assert service_calls[-1].data["service"] == "turn_on"
async def test_on_action_optimistic(hass, calls): async def test_on_action_optimistic(hass, service_calls):
"""Test on action in optimistic mode.""" """Test on action in optimistic mode."""
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -445,11 +416,7 @@ async def test_on_action_optimistic(hass, calls):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
"turn_on": {"service": "test.automation"}, **OPTIMISTIC_SWITCH_CONFIG,
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
} }
}, },
} }
@ -473,11 +440,13 @@ async def test_on_action_optimistic(hass, calls):
) )
state = hass.states.get("switch.test_template_switch") state = hass.states.get("switch.test_template_switch")
assert len(calls) == 1
assert state.state == STATE_ON assert state.state == STATE_ON
assert len(service_calls) == 1
assert service_calls[-1].data["service"] == "turn_on"
async def test_off_action(hass, calls):
async def test_off_action(hass, service_calls):
"""Test off action.""" """Test off action."""
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -487,12 +456,8 @@ async def test_off_action(hass, calls):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ states.switch.test_state.state }}", "value_template": "{{ states.switch.test_state.state }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {"service": "test.automation"},
} }
}, },
} }
@ -516,10 +481,11 @@ async def test_off_action(hass, calls):
blocking=True, blocking=True,
) )
assert len(calls) == 1 assert len(service_calls) == 1
assert service_calls[-1].data["service"] == "turn_off"
async def test_off_action_optimistic(hass, calls): async def test_off_action_optimistic(hass, service_calls):
"""Test off action in optimistic mode.""" """Test off action in optimistic mode."""
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -529,11 +495,7 @@ async def test_off_action_optimistic(hass, calls):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
"turn_off": {"service": "test.automation"}, **OPTIMISTIC_SWITCH_CONFIG,
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
} }
}, },
} }
@ -557,9 +519,11 @@ async def test_off_action_optimistic(hass, calls):
) )
state = hass.states.get("switch.test_template_switch") state = hass.states.get("switch.test_template_switch")
assert len(calls) == 1
assert state.state == STATE_OFF assert state.state == STATE_OFF
assert len(service_calls) == 1
assert service_calls[-1].data["service"] == "turn_off"
async def test_restore_state(hass): async def test_restore_state(hass):
"""Test state restoration.""" """Test state restoration."""
@ -582,12 +546,10 @@ async def test_restore_state(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"s1": { "s1": {
"turn_on": {"service": "test.automation"}, **OPTIMISTIC_SWITCH_CONFIG,
"turn_off": {"service": "test.automation"},
}, },
"s2": { "s2": {
"turn_on": {"service": "test.automation"}, **OPTIMISTIC_SWITCH_CONFIG,
"turn_off": {"service": "test.automation"},
}, },
}, },
} }
@ -614,15 +576,8 @@ async def test_available_template_with_entities(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ 1 == 1 }}", "value_template": "{{ 1 == 1 }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
"availability_template": "{{ is_state('availability_state.state', 'on') }}", "availability_template": "{{ is_state('availability_state.state', 'on') }}",
} }
}, },
@ -655,15 +610,8 @@ async def test_invalid_availability_template_keeps_component_available(hass, cap
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch": { "test_template_switch": {
**OPTIMISTIC_SWITCH_CONFIG,
"value_template": "{{ true }}", "value_template": "{{ true }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
"availability_template": "{{ x - 12 }}", "availability_template": "{{ x - 12 }}",
} }
}, },
@ -689,28 +637,14 @@ async def test_unique_id(hass):
"platform": "template", "platform": "template",
"switches": { "switches": {
"test_template_switch_01": { "test_template_switch_01": {
**OPTIMISTIC_SWITCH_CONFIG,
"unique_id": "not-so-unique-anymore", "unique_id": "not-so-unique-anymore",
"value_template": "{{ true }}", "value_template": "{{ true }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
}, },
"test_template_switch_02": { "test_template_switch_02": {
**OPTIMISTIC_SWITCH_CONFIG,
"unique_id": "not-so-unique-anymore", "unique_id": "not-so-unique-anymore",
"value_template": "{{ false }}", "value_template": "{{ false }}",
"turn_on": {
"service": "switch.turn_on",
"entity_id": "switch.test_state",
},
"turn_off": {
"service": "switch.turn_off",
"entity_id": "switch.test_state",
},
}, },
}, },
} }