Improve template test sensor (#41199)

This commit is contained in:
sycx2 2020-10-05 14:52:31 +02:00 committed by GitHub
parent 2763ace2bc
commit e813d3ebf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,9 +1,9 @@
"""The test for the Template sensor platform.""" """The test for the Template sensor platform."""
from asyncio import Event from asyncio import Event
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch
from homeassistant.bootstrap import async_from_config_dict from homeassistant.bootstrap import async_from_config_dict
from homeassistant.components import sensor
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_PICTURE, ATTR_ENTITY_PICTURE,
ATTR_ICON, ATTR_ICON,
@ -15,36 +15,19 @@ from homeassistant.const import (
) )
from homeassistant.core import CoreState, callback from homeassistant.core import CoreState, callback
from homeassistant.helpers.template import Template from homeassistant.helpers.template import Template
from homeassistant.setup import ATTR_COMPONENT, async_setup_component, setup_component from homeassistant.setup import ATTR_COMPONENT, async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from tests.common import ( from tests.async_mock import patch
assert_setup_component, from tests.common import assert_setup_component, async_fire_time_changed
async_fire_time_changed,
get_test_home_assistant,
)
class TestTemplateSensor: async def test_template(hass):
"""Test the Template sensor."""
hass = None
# pylint: disable=invalid-name
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_template(self):
"""Test template.""" """Test template."""
with assert_setup_component(1): with assert_setup_component(1, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -57,24 +40,25 @@ class TestTemplateSensor:
}, },
) )
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("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.state == "It ." assert state.state == "It ."
self.hass.states.set("sensor.test_state", "Works") hass.states.async_set("sensor.test_state", "Works")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.state == "It Works." assert state.state == "It Works."
def test_icon_template(self):
async def test_icon_template(hass):
"""Test icon template.""" """Test icon template."""
with assert_setup_component(1): with assert_setup_component(1, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -91,24 +75,25 @@ class TestTemplateSensor:
}, },
) )
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("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.attributes.get("icon") == "" assert state.attributes.get("icon") == ""
self.hass.states.set("sensor.test_state", "Works") hass.states.async_set("sensor.test_state", "Works")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
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): with assert_setup_component(1, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -125,24 +110,25 @@ class TestTemplateSensor:
}, },
) )
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("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.attributes.get("entity_picture") == "" assert state.attributes.get("entity_picture") == ""
self.hass.states.set("sensor.test_state", "Works") hass.states.async_set("sensor.test_state", "Works")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.attributes["entity_picture"] == "/local/sensor.png" assert state.attributes["entity_picture"] == "/local/sensor.png"
def test_friendly_name_template(self):
async def test_friendly_name_template(hass):
"""Test friendly_name template.""" """Test friendly_name template."""
with assert_setup_component(1): with assert_setup_component(1, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -156,24 +142,25 @@ class TestTemplateSensor:
}, },
) )
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("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.attributes.get("friendly_name") == "It ." assert state.attributes.get("friendly_name") == "It ."
self.hass.states.set("sensor.test_state", "Works") hass.states.async_set("sensor.test_state", "Works")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.attributes["friendly_name"] == "It Works." assert state.attributes["friendly_name"] == "It Works."
def test_friendly_name_template_with_unknown_state(self):
async def test_friendly_name_template_with_unknown_state(hass):
"""Test friendly_name template with an unknown value_template.""" """Test friendly_name template with an unknown value_template."""
with assert_setup_component(1): with assert_setup_component(1, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -187,24 +174,25 @@ class TestTemplateSensor:
}, },
) )
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("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.attributes["friendly_name"] == "It ." assert state.attributes["friendly_name"] == "It ."
self.hass.states.set("sensor.test_state", "Works") hass.states.async_set("sensor.test_state", "Works")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.attributes["friendly_name"] == "It Works." assert state.attributes["friendly_name"] == "It Works."
def test_attribute_templates(self):
async def test_attribute_templates(hass):
"""Test attribute_templates template.""" """Test attribute_templates template."""
with assert_setup_component(1): with assert_setup_component(1, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -220,47 +208,47 @@ class TestTemplateSensor:
}, },
) )
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("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.attributes.get("test_attribute") == "It ." assert state.attributes.get("test_attribute") == "It ."
self.hass.states.set("sensor.test_state", "Works") hass.states.async_set("sensor.test_state", "Works")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.attributes["test_attribute"] == "It Works." assert state.attributes["test_attribute"] == "It Works."
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): with assert_setup_component(0, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
"sensors": { "sensors": {
"test_template_sensor": { "test_template_sensor": {"value_template": "{% if rubbish %}"}
"value_template": "{% if rubbish %}"
}
}, },
} }
}, },
) )
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_template_attribute_missing(self):
async def test_template_attribute_missing(hass):
"""Test missing attribute template.""" """Test missing attribute template."""
with assert_setup_component(1): with assert_setup_component(1, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -274,19 +262,20 @@ class TestTemplateSensor:
}, },
) )
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("sensor.test_template_sensor") state = hass.states.get("sensor.test_template_sensor")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
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): with assert_setup_component(0, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -299,18 +288,19 @@ class TestTemplateSensor:
}, },
) )
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_sensor_does_not_create(self):
async def test_invalid_sensor_does_not_create(hass):
"""Test invalid sensor.""" """Test invalid sensor."""
with assert_setup_component(0): with assert_setup_component(0, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -319,30 +309,32 @@ class TestTemplateSensor:
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
assert self.hass.states.all() == [] assert hass.states.async_all() == []
def test_no_sensors_does_not_create(self):
async def test_no_sensors_does_not_create(hass):
"""Test no sensors.""" """Test no sensors."""
with assert_setup_component(0): with assert_setup_component(0, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, "sensor", {"sensor": {"platform": "template"}} hass, sensor.DOMAIN, {"sensor": {"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() == []
def test_missing_template_does_not_create(self):
async def test_missing_template_does_not_create(hass):
"""Test missing template.""" """Test missing template."""
with assert_setup_component(0): with assert_setup_component(0, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -355,18 +347,19 @@ class TestTemplateSensor:
}, },
) )
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_setup_invalid_device_class(self):
async def test_setup_invalid_device_class(hass):
"""Test setup with invalid device_class.""" """Test setup with invalid device_class."""
with assert_setup_component(0): with assert_setup_component(0, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -380,12 +373,13 @@ class TestTemplateSensor:
}, },
) )
def test_setup_valid_device_class(self):
async def test_setup_valid_device_class(hass):
"""Test setup with valid device_class.""" """Test setup with valid device_class."""
with assert_setup_component(1): with assert_setup_component(1, sensor.DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -401,57 +395,13 @@ class TestTemplateSensor:
} }
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.test1") state = hass.states.get("sensor.test1")
assert state.attributes["device_class"] == "temperature" assert state.attributes["device_class"] == "temperature"
state = self.hass.states.get("sensor.test2") state = hass.states.get("sensor.test2")
assert "device_class" not in state.attributes assert "device_class" not in state.attributes
def test_available_template_with_entities(self):
"""Test availability tempalates with values from other entities."""
with assert_setup_component(1):
assert setup_component(
self.hass,
"sensor",
{
"sensor": {
"platform": "template",
"sensors": {
"test_template_sensor": {
"value_template": "{{ states.sensor.test_state.state }}",
"availability_template": "{{ is_state('availability_boolean.state', 'on') }}",
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
# When template returns true..
self.hass.states.set("availability_boolean.state", STATE_ON)
self.hass.block_till_done()
# Device State should not be unavailable
assert (
self.hass.states.get("sensor.test_template_sensor").state
!= STATE_UNAVAILABLE
)
# When Availability template returns false
self.hass.states.set("availability_boolean.state", STATE_OFF)
self.hass.block_till_done()
# device state should be unavailable
assert (
self.hass.states.get("sensor.test_template_sensor").state
== STATE_UNAVAILABLE
)
async def test_creating_sensor_loads_group(hass): async def test_creating_sensor_loads_group(hass):
"""Test setting up template sensor loads group component first.""" """Test setting up template sensor loads group component first."""
@ -496,10 +446,10 @@ async def test_creating_sensor_loads_group(hass):
async def test_available_template_with_entities(hass): async def test_available_template_with_entities(hass):
"""Test availability tempalates with values from other entities.""" """Test availability tempalates with values from other entities."""
hass.states.async_set("sensor.availability_sensor", STATE_OFF) hass.states.async_set("sensor.availability_sensor", STATE_OFF)
with assert_setup_component(1, "sensor"): with assert_setup_component(1, sensor.DOMAIN):
assert await async_setup_component( assert await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -538,7 +488,7 @@ async def test_invalid_attribute_template(hass, caplog):
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -569,7 +519,7 @@ async def test_invalid_availability_template_keeps_component_available(hass, cap
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -599,7 +549,7 @@ async def test_no_template_match_all(hass, caplog):
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -681,7 +631,7 @@ async def test_unique_id(hass):
"""Test unique_id option only creates one sensor per id.""" """Test unique_id option only creates one sensor per id."""
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -716,7 +666,7 @@ async def test_sun_renders_once_per_sensor(hass):
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -772,7 +722,7 @@ async def test_self_referencing_sensor_loop(hass, caplog):
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -807,7 +757,7 @@ async def test_self_referencing_sensor_with_icon_loop(hass, caplog):
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -845,7 +795,7 @@ async def test_self_referencing_sensor_with_icon_and_picture_entity_loop(hass, c
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -885,7 +835,7 @@ async def test_self_referencing_entity_picture_loop(hass, caplog):
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",
@ -961,7 +911,7 @@ async def test_self_referencing_icon_with_no_loop(hass, caplog):
await async_setup_component( await async_setup_component(
hass, hass,
"sensor", sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "template", "platform": "template",