Add availability_template to Template Lock platform (#26517)

* Added availability_template to Template Lock platform

* Added to test for invalid values in availability_template

* Black and Lint fix

* black formatting

* Updated AVAILABILITY_TEMPLATE Rendering error

* Moved const to package Const.py

* Fix import order (pylint)

* Moved availability_template rendering to common loop

* Brought contant into line

* 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:
Gil Peeters
2019-10-01 08:15:15 -04:00
committed by Charles Garwood
parent 2e3bc5964d
commit f4a1f2809b
2 changed files with 104 additions and 6 deletions
+67 -3
View File
@@ -5,7 +5,7 @@ from homeassistant.core import callback
from homeassistant import setup
from homeassistant.components import lock
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE
from tests.common import get_test_home_assistant, assert_setup_component
@@ -254,9 +254,9 @@ class TestTemplateLock:
assert state.state == lock.STATE_UNLOCKED
assert (
"Template lock Template Lock has no entity ids configured "
"Template lock 'Template Lock' has no entity ids configured "
"to track nor were we able to extract the entities to track "
"from the value_template template. This entity will only "
"from the 'value_template' template. This entity will only "
"be able to be updated manually."
) in caplog.text
@@ -332,3 +332,67 @@ class TestTemplateLock:
self.hass.block_till_done()
assert len(self.calls) == 1
async def test_available_template_with_entities(hass):
"""Test availability templates with values from other entities."""
await setup.async_setup_component(
hass,
"lock",
{
"lock": {
"platform": "template",
"value_template": "{{ 'on' }}",
"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') }}",
}
},
)
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("lock.template_lock").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("lock.template_lock").state == STATE_UNAVAILABLE
async def test_invalid_availability_template_keeps_component_available(hass, caplog):
"""Test that an invalid availability keeps the device available."""
await setup.async_setup_component(
hass,
"lock",
{
"lock": {
"platform": "template",
"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",
},
}
},
)
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.get("lock.template_lock").state != STATE_UNAVAILABLE
assert ("UndefinedError: 'x' is undefined") in caplog.text