2017-05-24 18:32:22 +00:00
|
|
|
"""The tests for the Template light platform."""
|
|
|
|
import logging
|
|
|
|
|
2020-01-07 16:22:42 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-05-24 18:32:22 +00:00
|
|
|
from homeassistant import setup
|
|
|
|
from homeassistant.components.light import ATTR_BRIGHTNESS
|
2019-12-08 20:05:08 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
|
|
|
from homeassistant.core import callback
|
2017-05-24 18:32:22 +00:00
|
|
|
|
2019-12-08 20:05:08 +00:00
|
|
|
from tests.common import assert_setup_component, get_test_home_assistant
|
2018-09-27 21:13:11 +00:00
|
|
|
from tests.components.light import common
|
|
|
|
|
2017-05-24 18:32:22 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-09-28 12:01:18 +00:00
|
|
|
# Represent for light's availability
|
|
|
|
_STATE_AVAILABILITY_BOOLEAN = "availability_boolean.state"
|
|
|
|
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
class TestTemplateLight:
|
|
|
|
"""Test the Template light."""
|
|
|
|
|
|
|
|
hass = None
|
|
|
|
calls = None
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
|
|
|
|
def setup_method(self, method):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
self.calls = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_call(service):
|
|
|
|
"""Track function calls.."""
|
|
|
|
self.calls.append(service)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.services.register("test", "automation", record_call)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
2020-01-07 16:22:42 +00:00
|
|
|
def test_template_state_invalid(self):
|
|
|
|
"""Test template state with render error."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(1, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
2020-01-07 16:22:42 +00:00
|
|
|
"value_template": "{{states.test['big.fat...']}}",
|
2019-07-31 19:25:30 +00:00
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
2020-01-07 16:22:42 +00:00
|
|
|
def test_template_state_text(self):
|
|
|
|
"""Test the state text of a template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(1, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
2020-01-07 16:22:42 +00:00
|
|
|
"value_template": "{{ states.light.test_state.state }}",
|
2019-07-31 19:25:30 +00:00
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2020-01-07 16:22:42 +00:00
|
|
|
state = self.hass.states.set("light.test_state", STATE_ON)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
assert state.state == STATE_ON
|
|
|
|
|
2020-01-07 16:22:42 +00:00
|
|
|
state = self.hass.states.set("light.test_state", STATE_OFF)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get("light.test_template_light")
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"expected_state,template",
|
|
|
|
[(STATE_ON, "{{ 1 == 1 }}"), (STATE_OFF, "{{ 1 == 2 }}")],
|
|
|
|
)
|
|
|
|
def test_template_state_boolean(self, expected_state, template):
|
|
|
|
"""Test the setting of the state with boolean on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(1, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
2020-01-07 16:22:42 +00:00
|
|
|
"value_template": template,
|
2019-07-31 19:25:30 +00:00
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2020-01-07 16:22:42 +00:00
|
|
|
assert state.state == expected_state
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
def test_template_syntax_error(self):
|
|
|
|
"""Test templating syntax error."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(0, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"value_template": "{%- if false -%}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
|
|
|
def test_invalid_name_does_not_create(self):
|
|
|
|
"""Test invalid name."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(0, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"bad name here": {
|
|
|
|
"value_template": "{{ 1== 1}}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
|
|
|
def test_invalid_light_does_not_create(self):
|
|
|
|
"""Test invalid light."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(0, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"switches": {"test_template_light": "Invalid"},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
|
|
|
def test_no_lights_does_not_create(self):
|
|
|
|
"""Test if there are no lights no creation."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(0, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass, "light", {"light": {"platform": "template"}}
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert self.hass.states.all() == []
|
|
|
|
|
2020-01-07 16:22:42 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"missing_key, count", [("value_template", 1), ("turn_on", 0), ("turn_off", 0)]
|
|
|
|
)
|
|
|
|
def test_missing_key(self, missing_key, count):
|
2017-05-24 18:32:22 +00:00
|
|
|
"""Test missing template."""
|
2020-01-07 16:22:42 +00:00
|
|
|
light = {
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"light_one": {
|
|
|
|
"value_template": "{{ 1== 1}}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2020-01-07 16:22:42 +00:00
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2020-01-07 16:22:42 +00:00
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2020-01-07 16:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-24 18:32:22 +00:00
|
|
|
|
2020-01-07 16:22:42 +00:00
|
|
|
del light["light"]["lights"]["light_one"][missing_key]
|
|
|
|
with assert_setup_component(count, "light"):
|
|
|
|
assert setup.setup_component(self.hass, "light", light)
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2020-01-07 16:22:42 +00:00
|
|
|
if count:
|
|
|
|
assert self.hass.states.all() != []
|
|
|
|
else:
|
|
|
|
assert self.hass.states.all() == []
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
def test_on_action(self):
|
|
|
|
"""Test on action."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"value_template": "{{states.light.test_state.state}}",
|
|
|
|
"turn_on": {"service": "test.automation"},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.states.set("light.test_state", STATE_OFF)
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_on(self.hass, "light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert len(self.calls) == 1
|
|
|
|
|
|
|
|
def test_on_action_optimistic(self):
|
|
|
|
"""Test on action with optimistic state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"turn_on": {"service": "test.automation"},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.states.set("light.test_state", STATE_OFF)
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_on(self.hass, "light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
assert len(self.calls) == 1
|
|
|
|
assert state.state == STATE_ON
|
|
|
|
|
|
|
|
def test_off_action(self):
|
|
|
|
"""Test off action."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"value_template": "{{states.light.test_state.state}}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {"service": "test.automation"},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.states.set("light.test_state", STATE_ON)
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
assert state.state == STATE_ON
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_off(self.hass, "light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert len(self.calls) == 1
|
|
|
|
|
|
|
|
def test_off_action_optimistic(self):
|
|
|
|
"""Test off action with optimistic state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {"service": "test.automation"},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_off(self.hass, "light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert len(self.calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
assert state.state == STATE_OFF
|
|
|
|
|
|
|
|
def test_level_action_no_template(self):
|
|
|
|
"""Test setting brightness with optimistic template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"value_template": "{{1 == 1}}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "test.automation",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "test.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
|
|
|
assert state.attributes.get("brightness") is None
|
2017-05-24 18:32:22 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_on(self.hass, "light.test_template_light", **{ATTR_BRIGHTNESS: 124})
|
2017-05-24 18:32:22 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
assert len(self.calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert self.calls[0].data["brightness"] == "124"
|
2017-05-24 18:32:22 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
_LOGGER.info(str(state.attributes))
|
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("brightness") == 124
|
2017-05-24 18:32:22 +00:00
|
|
|
|
2020-01-07 16:22:42 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"expected_level,template",
|
|
|
|
[(255, "{{255}}"), (None, "{{256}}"), (None, "{{x - 12}}")],
|
|
|
|
)
|
|
|
|
def test_level_template(self, expected_level, template):
|
2017-05-24 18:32:22 +00:00
|
|
|
"""Test the template for the level."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(1, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"value_template": "{{ 1 == 1 }}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2020-01-07 16:22:42 +00:00
|
|
|
"level_template": template,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
},
|
2017-05-24 18:32:22 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-05-24 18:32:22 +00:00
|
|
|
assert state is not None
|
2020-01-07 16:22:42 +00:00
|
|
|
assert state.attributes.get("brightness") == expected_level
|
2017-05-24 18:32:22 +00:00
|
|
|
|
2017-09-14 08:13:01 +00:00
|
|
|
def test_friendly_name(self):
|
|
|
|
"""Test the accessibility of the friendly_name attribute."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(1, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"friendly_name": "Template light",
|
|
|
|
"value_template": "{{ 1 == 1 }}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
2017-09-14 08:13:01 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2017-09-14 08:13:01 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-09-14 08:13:01 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-09-14 08:13:01 +00:00
|
|
|
assert state is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("friendly_name") == "Template light"
|
2017-10-30 16:28:37 +00:00
|
|
|
|
|
|
|
def test_icon_template(self):
|
|
|
|
"""Test icon template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(1, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"friendly_name": "Template light",
|
|
|
|
"value_template": "{{ 1 == 1 }}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"icon_template": "{% if states.light.test_state.state %}"
|
2017-10-30 16:28:37 +00:00
|
|
|
"mdi:check"
|
2019-07-31 19:25:30 +00:00
|
|
|
"{% endif %}",
|
|
|
|
}
|
|
|
|
},
|
2017-10-30 16:28:37 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-10-30 16:28:37 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
|
|
|
assert state.attributes.get("icon") == ""
|
2017-10-30 16:28:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.set("light.test_state", STATE_ON)
|
2017-10-30 16:28:37 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-10-30 16:28:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes["icon"] == "mdi:check"
|
2017-10-30 16:28:37 +00:00
|
|
|
|
|
|
|
def test_entity_picture_template(self):
|
|
|
|
"""Test entity_picture template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with assert_setup_component(1, "light"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"friendly_name": "Template light",
|
|
|
|
"value_template": "{{ 1 == 1 }}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"entity_picture_template": "{% if states.light.test_state.state %}"
|
2017-10-30 16:28:37 +00:00
|
|
|
"/local/light.png"
|
2019-07-31 19:25:30 +00:00
|
|
|
"{% endif %}",
|
|
|
|
}
|
|
|
|
},
|
2017-10-30 16:28:37 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2017-10-30 16:28:37 +00:00
|
|
|
|
|
|
|
self.hass.start()
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
|
|
|
assert state.attributes.get("entity_picture") == ""
|
2017-10-30 16:28:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.set("light.test_state", STATE_ON)
|
2017-10-30 16:28:37 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("light.test_template_light")
|
2017-10-30 16:28:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes["entity_picture"] == "/local/light.png"
|
2019-09-28 12:01:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_available_template_with_entities(hass):
|
|
|
|
"""Test availability templates with values from other entities."""
|
|
|
|
|
|
|
|
await setup.async_setup_component(
|
|
|
|
hass,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"availability_template": "{{ is_state('availability_boolean.state', 'on') }}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# When template returns true..
|
|
|
|
hass.states.async_set(_STATE_AVAILABILITY_BOOLEAN, STATE_ON)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Device State should not be unavailable
|
|
|
|
assert hass.states.get("light.test_template_light").state != STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
# When Availability template returns false
|
|
|
|
hass.states.async_set(_STATE_AVAILABILITY_BOOLEAN, STATE_OFF)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# device state should be unavailable
|
|
|
|
assert hass.states.get("light.test_template_light").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,
|
|
|
|
"light",
|
|
|
|
{
|
|
|
|
"light": {
|
|
|
|
"platform": "template",
|
|
|
|
"lights": {
|
|
|
|
"test_template_light": {
|
|
|
|
"availability_template": "{{ x - 12 }}",
|
|
|
|
"turn_on": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"turn_off": {
|
|
|
|
"service": "light.turn_off",
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
},
|
|
|
|
"set_level": {
|
|
|
|
"service": "light.turn_on",
|
|
|
|
"data_template": {
|
|
|
|
"entity_id": "light.test_state",
|
|
|
|
"brightness": "{{brightness}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("light.test_template_light").state != STATE_UNAVAILABLE
|
|
|
|
assert ("UndefinedError: 'x' is undefined") in caplog.text
|