1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00

Rewrite tests for Template Light (#41163)

This commit is contained in:
sycx2 2021-03-17 11:30:44 +01:00 committed by GitHub
parent 6f7f4955a3
commit 009e44ed9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,17 +4,23 @@ import logging
import pytest import pytest
from homeassistant import setup from homeassistant import setup
import homeassistant.components.light as light
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP, ATTR_COLOR_TEMP,
ATTR_HS_COLOR, ATTR_HS_COLOR,
ATTR_WHITE_VALUE, ATTR_WHITE_VALUE,
) )
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE from homeassistant.const import (
from homeassistant.core import callback ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
)
from tests.common import assert_setup_component, get_test_home_assistant from tests.common import assert_setup_component, async_mock_service
from tests.components.light import common
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -22,35 +28,18 @@ _LOGGER = logging.getLogger(__name__)
_STATE_AVAILABILITY_BOOLEAN = "availability_boolean.state" _STATE_AVAILABILITY_BOOLEAN = "availability_boolean.state"
class TestTemplateLight: @pytest.fixture(name="calls")
"""Test the Template light.""" def fixture_calls(hass):
"""Track calls to a mock service."""
return async_mock_service(hass, "test", "automation")
hass = None
calls = None
# pylint: disable=invalid-name
def setup_method(self, method): async def test_template_state_invalid(hass):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.calls = []
@callback
def record_call(service):
"""Track function calls."""
self.calls.append(service)
self.hass.services.register("test", "automation", record_call)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_template_state_invalid(self):
"""Test template state with render error.""" """Test template state with render error."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -78,19 +67,20 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.state == STATE_OFF assert state.state == STATE_OFF
def test_template_state_text(self):
async def test_template_state_text(hass):
"""Test the state text of a template.""" """Test the state text of a template."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -118,32 +108,33 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.set("light.test_state", STATE_ON) state = hass.states.async_set("light.test_state", STATE_ON)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.state == STATE_ON assert state.state == STATE_ON
state = self.hass.states.set("light.test_state", STATE_OFF) state = hass.states.async_set("light.test_state", STATE_OFF)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.state == STATE_OFF assert state.state == STATE_OFF
@pytest.mark.parametrize(
@pytest.mark.parametrize(
"expected_state,template", "expected_state,template",
[(STATE_ON, "{{ 1 == 1 }}"), (STATE_OFF, "{{ 1 == 2 }}")], [(STATE_ON, "{{ 1 == 1 }}"), (STATE_OFF, "{{ 1 == 2 }}")],
) )
def test_template_state_boolean(self, expected_state, template): async def test_template_state_boolean(hass, expected_state, template):
"""Test the setting of the state with boolean on.""" """Test the setting of the state with boolean on."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -171,19 +162,20 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.state == expected_state assert state.state == expected_state
def test_template_syntax_error(self):
async def test_template_syntax_error(hass):
"""Test templating syntax error.""" """Test templating syntax error."""
with assert_setup_component(0, "light"): with assert_setup_component(0, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -211,18 +203,19 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
assert self.hass.states.all() == [] assert hass.states.async_all() == []
def test_invalid_name_does_not_create(self):
async def test_invalid_name_does_not_create(hass):
"""Test invalid name.""" """Test invalid name."""
with assert_setup_component(0, "light"): with assert_setup_component(0, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -250,18 +243,19 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
assert self.hass.states.all() == [] assert hass.states.async_all() == []
def test_invalid_light_does_not_create(self):
async def test_invalid_light_does_not_create(hass):
"""Test invalid light.""" """Test invalid light."""
with assert_setup_component(0, "light"): with assert_setup_component(0, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -270,31 +264,33 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
assert self.hass.states.all() == [] assert hass.states.async_all() == []
def test_no_lights_does_not_create(self):
async def test_no_lights_does_not_create(hass):
"""Test if there are no lights no creation.""" """Test if there are no lights no creation."""
with assert_setup_component(0, "light"): with assert_setup_component(0, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, "light", {"light": {"platform": "template"}} hass, "light", {"light": {"platform": "template"}}
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
assert self.hass.states.all() == [] assert hass.states.async_all() == []
@pytest.mark.parametrize(
@pytest.mark.parametrize(
"missing_key, count", [("value_template", 1), ("turn_on", 0), ("turn_off", 0)] "missing_key, count", [("value_template", 1), ("turn_on", 0), ("turn_off", 0)]
) )
def test_missing_key(self, missing_key, count): async def test_missing_key(hass, missing_key, count):
"""Test missing template.""" """Test missing template."""
light = { light_config = {
"light": { "light": {
"platform": "template", "platform": "template",
"lights": { "lights": {
@ -320,23 +316,24 @@ class TestTemplateLight:
} }
} }
del light["light"]["lights"]["light_one"][missing_key] del light_config["light"]["lights"]["light_one"][missing_key]
with assert_setup_component(count, "light"): with assert_setup_component(count, light.DOMAIN):
assert setup.setup_component(self.hass, "light", light) assert await setup.async_setup_component(hass, "light", light_config)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
if count: if count:
assert self.hass.states.all() != [] assert hass.states.async_all() != []
else: else:
assert self.hass.states.all() == [] assert hass.states.async_all() == []
def test_on_action(self):
async def test_on_action(hass, calls):
"""Test on action.""" """Test on action."""
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -361,26 +358,31 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("light.test_state", STATE_OFF) hass.states.async_set("light.test_state", STATE_OFF)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.state == STATE_OFF assert state.state == STATE_OFF
common.turn_on(self.hass, "light.test_template_light") await hass.services.async_call(
self.hass.block_till_done() light.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.test_template_light"},
blocking=True,
)
assert len(self.calls) == 1 assert len(calls) == 1
def test_on_action_optimistic(self):
async def test_on_action_optimistic(hass, calls):
"""Test on action with optimistic state.""" """Test on action with optimistic state."""
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -404,28 +406,33 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("light.test_state", STATE_OFF) hass.states.async_set("light.test_state", STATE_OFF)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.state == STATE_OFF assert state.state == STATE_OFF
common.turn_on(self.hass, "light.test_template_light") await hass.services.async_call(
self.hass.block_till_done() light.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.test_template_light"},
blocking=True,
)
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert len(self.calls) == 1 assert len(calls) == 1
assert state.state == STATE_ON assert state.state == STATE_ON
def test_off_action(self):
async def test_off_action(hass, calls):
"""Test off action.""" """Test off action."""
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -450,26 +457,31 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("light.test_state", STATE_ON) hass.states.async_set("light.test_state", STATE_ON)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.state == STATE_ON assert state.state == STATE_ON
common.turn_off(self.hass, "light.test_template_light") await hass.services.async_call(
self.hass.block_till_done() light.DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.test_template_light"},
blocking=True,
)
assert len(self.calls) == 1 assert len(calls) == 1
def test_off_action_optimistic(self):
async def test_off_action_optimistic(hass, calls):
"""Test off action with optimistic state.""" """Test off action with optimistic state."""
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -493,25 +505,30 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.state == STATE_OFF assert state.state == STATE_OFF
common.turn_off(self.hass, "light.test_template_light") await hass.services.async_call(
self.hass.block_till_done() light.DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.test_template_light"},
blocking=True,
)
assert len(self.calls) == 1 assert len(calls) == 1
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.state == STATE_OFF assert state.state == STATE_OFF
def test_white_value_action_no_template(self):
async def test_white_value_action_no_template(hass, calls):
"""Test setting white value with optimistic template.""" """Test setting white value with optimistic template."""
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -538,25 +555,29 @@ class TestTemplateLight:
} }
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.attributes.get("white_value") is None assert state.attributes.get("white_value") is None
common.turn_on( await hass.services.async_call(
self.hass, "light.test_template_light", **{ATTR_WHITE_VALUE: 124} light.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.test_template_light", ATTR_WHITE_VALUE: 124},
blocking=True,
) )
self.hass.block_till_done()
assert len(self.calls) == 1
assert self.calls[0].data["white_value"] == 124
state = self.hass.states.get("light.test_template_light") assert len(calls) == 1
assert calls[0].data["white_value"] == 124
state = hass.states.get("light.test_template_light")
assert state is not None assert state is not None
assert state.attributes.get("white_value") == 124 assert state.attributes.get("white_value") == 124
@pytest.mark.parametrize(
@pytest.mark.parametrize(
"expected_white_value,template", "expected_white_value,template",
[ [
(255, "{{255}}"), (255, "{{255}}"),
@ -565,13 +586,13 @@ class TestTemplateLight:
(None, "{{ none }}"), (None, "{{ none }}"),
(None, ""), (None, ""),
], ],
) )
def test_white_value_template(self, expected_white_value, template): async def test_white_value_template(hass, expected_white_value, template):
"""Test the template for the white value.""" """Test the template for the white value."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -600,19 +621,20 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state is not None assert state is not None
assert state.attributes.get("white_value") == expected_white_value assert state.attributes.get("white_value") == expected_white_value
def test_level_action_no_template(self):
async def test_level_action_no_template(hass, calls):
"""Test setting brightness with optimistic template.""" """Test setting brightness with optimistic template."""
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -639,24 +661,30 @@ class TestTemplateLight:
} }
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.attributes.get("brightness") is None assert state.attributes.get("brightness") is None
common.turn_on(self.hass, "light.test_template_light", **{ATTR_BRIGHTNESS: 124}) await hass.services.async_call(
self.hass.block_till_done() light.DOMAIN,
assert len(self.calls) == 1 SERVICE_TURN_ON,
assert self.calls[0].data["brightness"] == 124 {ATTR_ENTITY_ID: "light.test_template_light", ATTR_BRIGHTNESS: 124},
blocking=True,
)
state = self.hass.states.get("light.test_template_light") assert len(calls) == 1
assert calls[0].data["brightness"] == 124
state = hass.states.get("light.test_template_light")
_LOGGER.info(str(state.attributes)) _LOGGER.info(str(state.attributes))
assert state is not None assert state is not None
assert state.attributes.get("brightness") == 124 assert state.attributes.get("brightness") == 124
@pytest.mark.parametrize(
@pytest.mark.parametrize(
"expected_level,template", "expected_level,template",
[ [
(255, "{{255}}"), (255, "{{255}}"),
@ -665,13 +693,13 @@ class TestTemplateLight:
(None, "{{ none }}"), (None, "{{ none }}"),
(None, ""), (None, ""),
], ],
) )
def test_level_template(self, expected_level, template): async def test_level_template(hass, expected_level, template):
"""Test the template for the level.""" """Test the template for the level."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -700,15 +728,16 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state is not None assert state is not None
assert state.attributes.get("brightness") == expected_level assert state.attributes.get("brightness") == expected_level
@pytest.mark.parametrize(
@pytest.mark.parametrize(
"expected_temp,template", "expected_temp,template",
[ [
(500, "{{500}}"), (500, "{{500}}"),
@ -718,13 +747,13 @@ class TestTemplateLight:
(None, "{{ none }}"), (None, "{{ none }}"),
(None, ""), (None, ""),
], ],
) )
def test_temperature_template(self, expected_temp, template): async def test_temperature_template(hass, expected_temp, template):
"""Test the template for the temperature.""" """Test the template for the temperature."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -753,19 +782,20 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state is not None assert state is not None
assert state.attributes.get("color_temp") == expected_temp assert state.attributes.get("color_temp") == expected_temp
def test_temperature_action_no_template(self):
async def test_temperature_action_no_template(hass, calls):
"""Test setting temperature with optimistic template.""" """Test setting temperature with optimistic template."""
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -792,29 +822,35 @@ class TestTemplateLight:
} }
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.attributes.get("color_template") is None assert state.attributes.get("color_template") is None
common.turn_on(self.hass, "light.test_template_light", **{ATTR_COLOR_TEMP: 345}) await hass.services.async_call(
self.hass.block_till_done() light.DOMAIN,
assert len(self.calls) == 1 SERVICE_TURN_ON,
assert self.calls[0].data["color_temp"] == 345 {ATTR_ENTITY_ID: "light.test_template_light", ATTR_COLOR_TEMP: 345},
blocking=True,
)
state = self.hass.states.get("light.test_template_light") assert len(calls) == 1
assert calls[0].data["color_temp"] == 345
state = hass.states.get("light.test_template_light")
_LOGGER.info(str(state.attributes)) _LOGGER.info(str(state.attributes))
assert state is not None assert state is not None
assert state.attributes.get("color_temp") == 345 assert state.attributes.get("color_temp") == 345
def test_friendly_name(self):
async def test_friendly_name(hass):
"""Test the accessibility of the friendly_name attribute.""" """Test the accessibility of the friendly_name attribute."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -843,21 +879,22 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state is not None assert state is not None
assert state.attributes.get("friendly_name") == "Template light" assert state.attributes.get("friendly_name") == "Template light"
def test_icon_template(self):
async def test_icon_template(hass):
"""Test icon template.""" """Test icon template."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -889,26 +926,27 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.attributes.get("icon") == "" assert state.attributes.get("icon") == ""
state = self.hass.states.set("light.test_state", STATE_ON) state = hass.states.async_set("light.test_state", STATE_ON)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.attributes["icon"] == "mdi:check" assert state.attributes["icon"] == "mdi:check"
def test_entity_picture_template(self):
async def test_entity_picture_template(hass):
"""Test entity_picture template.""" """Test entity_picture template."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -940,25 +978,26 @@ class TestTemplateLight:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.attributes.get("entity_picture") == "" assert state.attributes.get("entity_picture") == ""
state = self.hass.states.set("light.test_state", STATE_ON) state = hass.states.async_set("light.test_state", STATE_ON)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.attributes["entity_picture"] == "/local/light.png" assert state.attributes["entity_picture"] == "/local/light.png"
def test_color_action_no_template(self):
async def test_color_action_no_template(hass, calls):
"""Test setting color with optimistic template.""" """Test setting color with optimistic template."""
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -996,32 +1035,36 @@ class TestTemplateLight:
} }
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state.attributes.get("hs_color") is None assert state.attributes.get("hs_color") is None
common.turn_on( await hass.services.async_call(
self.hass, "light.test_template_light", **{ATTR_HS_COLOR: (40, 50)} light.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.test_template_light", ATTR_HS_COLOR: (40, 50)},
blocking=True,
) )
self.hass.block_till_done()
assert len(self.calls) == 2
assert self.calls[0].data["h"] == 40
assert self.calls[0].data["s"] == 50
assert self.calls[1].data["h"] == 40
assert self.calls[1].data["s"] == 50
state = self.hass.states.get("light.test_template_light") assert len(calls) == 2
assert calls[0].data["h"] == 40
assert calls[0].data["s"] == 50
assert calls[1].data["h"] == 40
assert calls[1].data["s"] == 50
state = hass.states.get("light.test_template_light")
_LOGGER.info(str(state.attributes)) _LOGGER.info(str(state.attributes))
assert state is not None assert state is not None
assert self.calls[0].data["h"] == 40 assert calls[0].data["h"] == 40
assert self.calls[0].data["s"] == 50 assert calls[0].data["s"] == 50
assert self.calls[1].data["h"] == 40 assert calls[1].data["h"] == 40
assert self.calls[1].data["s"] == 50 assert calls[1].data["s"] == 50
@pytest.mark.parametrize(
@pytest.mark.parametrize(
"expected_hs,template", "expected_hs,template",
[ [
((360, 100), "{{(360, 100)}}"), ((360, 100), "{{(360, 100)}}"),
@ -1032,13 +1075,13 @@ class TestTemplateLight:
(None, ""), (None, ""),
(None, "{{ none }}"), (None, "{{ none }}"),
], ],
) )
def test_color_template(self, expected_hs, template): async def test_color_template(hass, expected_hs, template):
"""Test the template for the color.""" """Test the template for the color."""
with assert_setup_component(1, "light"): with assert_setup_component(1, light.DOMAIN):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -1068,10 +1111,10 @@ class TestTemplateLight:
} }
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("light.test_template_light") state = hass.states.get("light.test_template_light")
assert state is not None assert state is not None
assert state.attributes.get("hs_color") == expected_hs assert state.attributes.get("hs_color") == expected_hs
@ -1080,7 +1123,7 @@ async def test_available_template_with_entities(hass):
"""Test availability templates with values from other entities.""" """Test availability templates with values from other entities."""
await setup.async_setup_component( await setup.async_setup_component(
hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -1130,7 +1173,7 @@ async def test_invalid_availability_template_keeps_component_available(hass, cap
"""Test that an invalid availability keeps the device available.""" """Test that an invalid availability keeps the device available."""
await setup.async_setup_component( await setup.async_setup_component(
hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",
@ -1170,7 +1213,7 @@ async def test_unique_id(hass):
"""Test unique_id option only creates one light per id.""" """Test unique_id option only creates one light per id."""
await setup.async_setup_component( await setup.async_setup_component(
hass, hass,
"light", light.DOMAIN,
{ {
"light": { "light": {
"platform": "template", "platform": "template",