Allow combining value_template and position_template for template cover (#52383)
This commit is contained in:
parent
7f309b4e6e
commit
2097ab76f5
2 changed files with 137 additions and 61 deletions
|
@ -34,7 +34,7 @@ def calls_fixture(hass):
|
|||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
async def test_template_state_text(hass, calls):
|
||||
async def test_template_state_text(hass, calls, caplog):
|
||||
"""Test the state text of a template."""
|
||||
with assert_setup_component(1, "cover"):
|
||||
assert await setup.async_setup_component(
|
||||
|
@ -64,30 +64,147 @@ async def test_template_state_text(hass, calls):
|
|||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.async_set("cover.test_state", STATE_OPEN)
|
||||
hass.states.async_set("cover.test_state", STATE_OPEN)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPEN
|
||||
|
||||
state = hass.states.async_set("cover.test_state", STATE_CLOSED)
|
||||
hass.states.async_set("cover.test_state", STATE_CLOSED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_CLOSED
|
||||
|
||||
state = hass.states.async_set("cover.test_state", STATE_OPENING)
|
||||
hass.states.async_set("cover.test_state", STATE_OPENING)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPENING
|
||||
|
||||
state = hass.states.async_set("cover.test_state", STATE_CLOSING)
|
||||
hass.states.async_set("cover.test_state", STATE_CLOSING)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_CLOSING
|
||||
|
||||
# Unknown state sets position to None - "closing" takes precedence
|
||||
state = hass.states.async_set("cover.test_state", "dog")
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_CLOSING
|
||||
assert "Received invalid cover is_on state: dog" in caplog.text
|
||||
|
||||
# Set state to open
|
||||
hass.states.async_set("cover.test_state", STATE_OPEN)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPEN
|
||||
|
||||
# Unknown state sets position to None -> Open
|
||||
state = hass.states.async_set("cover.test_state", "cat")
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPEN
|
||||
assert "Received invalid cover is_on state: cat" in caplog.text
|
||||
|
||||
# Set state to closed
|
||||
hass.states.async_set("cover.test_state", STATE_CLOSED)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_CLOSED
|
||||
|
||||
# Unknown state sets position to None -> Open
|
||||
state = hass.states.async_set("cover.test_state", "bear")
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPEN
|
||||
assert "Received invalid cover is_on state: bear" in caplog.text
|
||||
|
||||
|
||||
async def test_template_state_text_combined(hass, calls, caplog):
|
||||
"""Test the state text of a template which combines position and value templates."""
|
||||
with assert_setup_component(1, "cover"):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
"cover",
|
||||
{
|
||||
"cover": {
|
||||
"platform": "template",
|
||||
"covers": {
|
||||
"test_template_cover": {
|
||||
"position_template": "{{ states.cover.test.attributes.position }}",
|
||||
"value_template": "{{ states.cover.test_state.state }}",
|
||||
"open_cover": {
|
||||
"service": "cover.open_cover",
|
||||
"entity_id": "cover.test_state",
|
||||
},
|
||||
"close_cover": {
|
||||
"service": "cover.close_cover",
|
||||
"entity_id": "cover.test_state",
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Test default state
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPEN
|
||||
|
||||
# Change to "open" should be ignored
|
||||
state = hass.states.async_set("cover.test_state", STATE_OPEN)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPEN
|
||||
|
||||
# Change to "closed" should be ignored
|
||||
state = hass.states.async_set("cover.test_state", STATE_CLOSED)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPEN
|
||||
|
||||
# Change to "opening" should be accepted
|
||||
state = hass.states.async_set("cover.test_state", STATE_OPENING)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPENING
|
||||
|
||||
# Change to "closing" should be accepted
|
||||
state = hass.states.async_set("cover.test_state", STATE_CLOSING)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_CLOSING
|
||||
|
||||
# Set position to 0=closed
|
||||
hass.states.async_set("cover.test", STATE_CLOSED, attributes={"position": 0})
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_CLOSING
|
||||
assert state.attributes["current_position"] == 0
|
||||
|
||||
# Clear "closing" state, STATE_OPEN will be ignored and state derived from position
|
||||
state = hass.states.async_set("cover.test_state", STATE_OPEN)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_CLOSED
|
||||
|
||||
# Set position to 10
|
||||
hass.states.async_set("cover.test", STATE_CLOSED, attributes={"position": 10})
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPEN
|
||||
assert state.attributes["current_position"] == 10
|
||||
|
||||
# Unknown state should be ignored
|
||||
state = hass.states.async_set("cover.test_state", "dog")
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("cover.test_template_cover")
|
||||
assert state.state == STATE_OPEN
|
||||
assert state.attributes["current_position"] == 10
|
||||
assert "Received invalid cover is_on state: dog" in caplog.text
|
||||
|
||||
|
||||
async def test_template_state_boolean(hass, calls):
|
||||
"""Test the value_template attribute."""
|
||||
|
@ -250,43 +367,6 @@ async def test_template_out_of_bounds(hass, calls):
|
|||
assert state.attributes.get("current_position") is None
|
||||
|
||||
|
||||
async def test_template_mutex(hass, calls):
|
||||
"""Test that only value or position template can be used."""
|
||||
with assert_setup_component(0, "cover"):
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
"cover",
|
||||
{
|
||||
"cover": {
|
||||
"platform": "template",
|
||||
"covers": {
|
||||
"test_template_cover": {
|
||||
"value_template": "{{ 1 == 1 }}",
|
||||
"position_template": "{{ 42 }}",
|
||||
"open_cover": {
|
||||
"service": "cover.open_cover",
|
||||
"entity_id": "cover.test_state",
|
||||
},
|
||||
"close_cover": {
|
||||
"service": "cover.close_cover",
|
||||
"entity_id": "cover.test_state",
|
||||
},
|
||||
"icon_template": "{% if states.cover.test_state.state %}"
|
||||
"mdi:check"
|
||||
"{% endif %}",
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.async_all() == []
|
||||
|
||||
|
||||
async def test_template_open_or_position(hass, caplog):
|
||||
"""Test that at least one of open_cover or set_position is used."""
|
||||
assert await setup.async_setup_component(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue