Add availability_template to Template Vacuum platform (#26514)
* Added availability_template to Template Vacuum platform * Added to test for invalid values in availability_template * Updated AVAILABILITY_TEMPLATE Rendering error * Moved const to package Const.py * Removed 'Magic' string * Cleaned up const and compare lowercase result to 'true' * reverted _available back to boolean * Fixed tests (async, magic values and state checks)
This commit is contained in:
1 parent
ed82ec5d8e
commit
11c9bab078
2 files changed
+90
-1
No files matched your search
@@ -3,7 +3,7 @@ import logging
|
||||
import pytest
|
||||
|
||||
from homeassistant import setup
|
||||
from homeassistant.const import STATE_ON, STATE_UNKNOWN
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNKNOWN, STATE_UNAVAILABLE
|
||||
from homeassistant.components.vacuum import (
|
||||
ATTR_BATTERY_LEVEL,
|
||||
STATE_CLEANING,
|
||||
@@ -210,6 +210,68 @@ async def test_invalid_templates(hass, calls):
|
||||
_verify(hass, STATE_UNKNOWN, None)
|
||||
|
||||
|
||||
async def test_available_template_with_entities(hass, calls):
|
||||
"""Test availability templates with values from other entities."""
|
||||
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
"vacuum",
|
||||
{
|
||||
"vacuum": {
|
||||
"platform": "template",
|
||||
"vacuums": {
|
||||
"test_template_vacuum": {
|
||||
"availability_template": "{{ is_state('availability_state.state', 'on') }}",
|
||||
"start": {"service": "script.vacuum_start"},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# When template returns true..
|
||||
hass.states.async_set("availability_state.state", STATE_ON)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Device State should not be unavailable
|
||||
assert hass.states.get("vacuum.test_template_vacuum").state != STATE_UNAVAILABLE
|
||||
|
||||
# When Availability template returns false
|
||||
hass.states.async_set("availability_state.state", STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# device state should be unavailable
|
||||
assert hass.states.get("vacuum.test_template_vacuum").state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def test_invalid_availability_template_keeps_component_available(hass, caplog):
|
||||
"""Test that an invalid availability keeps the device available."""
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
"vacuum",
|
||||
{
|
||||
"vacuum": {
|
||||
"platform": "template",
|
||||
"vacuums": {
|
||||
"test_template_vacuum": {
|
||||
"availability_template": "{{ x - 12 }}",
|
||||
"start": {"service": "script.vacuum_start"},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("vacuum.test_template_vacuum") != STATE_UNAVAILABLE
|
||||
assert ("UndefinedError: 'x' is undefined") in caplog.text
|
||||
|
||||
|
||||
# End of template tests #
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user