Rewrite tests for Template Binary Sensor (#41098)

pull/41724/head
sycx2 2020-10-12 17:56:08 +02:00 committed by GitHub
parent 57996e1942
commit fc75927d85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 211 additions and 220 deletions

View File

@ -1,10 +1,9 @@
"""The tests for the Template Binary sensor platform."""
from datetime import timedelta
import logging
import unittest
from unittest import mock
from homeassistant import setup
from homeassistant.components import binary_sensor
from homeassistant.const import (
ATTR_DEVICE_CLASS,
EVENT_HOMEASSISTANT_START,
@ -15,207 +14,198 @@ from homeassistant.const import (
from homeassistant.core import CoreState
import homeassistant.util.dt as dt_util
from tests.common import (
assert_setup_component,
async_fire_time_changed,
get_test_home_assistant,
)
from tests.async_mock import patch
from tests.common import assert_setup_component, async_fire_time_changed
class TestBinarySensorTemplate(unittest.TestCase):
"""Test for Binary sensor template platform."""
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_setup(self):
"""Test the setup."""
config = {
"binary_sensor": {
"platform": "template",
"sensors": {
"test": {
"friendly_name": "virtual thingy",
"value_template": "{{ foo }}",
"device_class": "motion",
}
},
}
async def test_setup(hass):
"""Test the setup."""
config = {
"binary_sensor": {
"platform": "template",
"sensors": {
"test": {
"friendly_name": "virtual thingy",
"value_template": "{{ foo }}",
"device_class": "motion",
}
},
}
with assert_setup_component(1):
assert setup.setup_component(self.hass, "binary_sensor", config)
}
with assert_setup_component(1):
assert await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
def test_setup_no_sensors(self):
"""Test setup with no sensors."""
with assert_setup_component(0):
assert setup.setup_component(
self.hass, "binary_sensor", {"binary_sensor": {"platform": "template"}}
)
def test_setup_invalid_device(self):
"""Test the setup with invalid devices."""
with assert_setup_component(0):
assert setup.setup_component(
self.hass,
"binary_sensor",
{"binary_sensor": {"platform": "template", "sensors": {"foo bar": {}}}},
)
async def test_setup_no_sensors(hass):
"""Test setup with no sensors."""
with assert_setup_component(0):
assert await setup.async_setup_component(
hass, binary_sensor.DOMAIN, {"binary_sensor": {"platform": "template"}}
)
def test_setup_invalid_device_class(self):
"""Test setup with invalid sensor class."""
with assert_setup_component(0):
assert setup.setup_component(
self.hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test": {
"value_template": "{{ foo }}",
"device_class": "foobarnotreal",
}
},
}
},
)
def test_setup_invalid_missing_template(self):
"""Test setup with invalid and missing template."""
with assert_setup_component(0):
assert setup.setup_component(
self.hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "template",
"sensors": {"test": {"device_class": "motion"}},
}
},
)
async def test_setup_invalid_device(hass):
"""Test the setup with invalid devices."""
with assert_setup_component(0):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{"binary_sensor": {"platform": "template", "sensors": {"foo bar": {}}}},
)
def test_icon_template(self):
"""Test icon template."""
with assert_setup_component(1):
assert setup.setup_component(
self.hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test_template_sensor": {
"value_template": "{{ states.sensor.xyz.state }}",
"icon_template": "{% if "
"states.binary_sensor.test_state.state == "
"'Works' %}"
"mdi:check"
"{% endif %}",
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
async def test_setup_invalid_device_class(hass):
"""Test setup with invalid sensor class."""
with assert_setup_component(0):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test": {
"value_template": "{{ foo }}",
"device_class": "foobarnotreal",
}
},
}
},
)
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("icon") == ""
self.hass.states.set("binary_sensor.test_state", "Works")
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["icon"] == "mdi:check"
async def test_setup_invalid_missing_template(hass):
"""Test setup with invalid and missing template."""
with assert_setup_component(0):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
"sensors": {"test": {"device_class": "motion"}},
}
},
)
def test_entity_picture_template(self):
"""Test entity_picture template."""
with assert_setup_component(1):
assert setup.setup_component(
self.hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test_template_sensor": {
"value_template": "{{ states.sensor.xyz.state }}",
"entity_picture_template": "{% if "
"states.binary_sensor.test_state.state == "
"'Works' %}"
"/local/sensor.png"
"{% endif %}",
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
async def test_icon_template(hass):
"""Test icon template."""
with assert_setup_component(1):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test_template_sensor": {
"value_template": "{{ states.sensor.xyz.state }}",
"icon_template": "{% if "
"states.binary_sensor.test_state.state == "
"'Works' %}"
"mdi:check"
"{% endif %}",
}
},
}
},
)
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("entity_picture") == ""
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
self.hass.states.set("binary_sensor.test_state", "Works")
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["entity_picture"] == "/local/sensor.png"
state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("icon") == ""
def test_attribute_templates(self):
"""Test attribute_templates template."""
with assert_setup_component(1):
assert setup.setup_component(
self.hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test_template_sensor": {
"value_template": "{{ states.sensor.xyz.state }}",
"attribute_templates": {
"test_attribute": "It {{ states.sensor.test_state.state }}."
},
}
},
}
},
)
hass.states.async_set("binary_sensor.test_state", "Works")
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["icon"] == "mdi:check"
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("test_attribute") == "It ."
self.hass.states.set("sensor.test_state", "Works2")
self.hass.block_till_done()
self.hass.states.set("sensor.test_state", "Works")
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["test_attribute"] == "It Works."
async def test_entity_picture_template(hass):
"""Test entity_picture template."""
with assert_setup_component(1):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test_template_sensor": {
"value_template": "{{ states.sensor.xyz.state }}",
"entity_picture_template": "{% if "
"states.binary_sensor.test_state.state == "
"'Works' %}"
"/local/sensor.png"
"{% endif %}",
}
},
}
},
)
@mock.patch(
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("entity_picture") == ""
hass.states.async_set("binary_sensor.test_state", "Works")
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["entity_picture"] == "/local/sensor.png"
async def test_attribute_templates(hass):
"""Test attribute_templates template."""
with assert_setup_component(1):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test_template_sensor": {
"value_template": "{{ states.sensor.xyz.state }}",
"attribute_templates": {
"test_attribute": "It {{ states.sensor.test_state.state }}."
},
}
},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("test_attribute") == "It ."
hass.states.async_set("sensor.test_state", "Works2")
await hass.async_block_till_done()
hass.states.async_set("sensor.test_state", "Works")
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["test_attribute"] == "It Works."
async def test_match_all(hass):
"""Test template that is rerendered on any state lifecycle."""
with patch(
"homeassistant.components.template.binary_sensor."
"BinarySensorTemplate._update_state"
)
def test_match_all(self, _update_state):
"""Test template that is rerendered on any state lifecycle."""
) as _update_state:
with assert_setup_component(1):
assert setup.setup_component(
self.hass,
"binary_sensor",
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
@ -234,43 +224,44 @@ class TestBinarySensorTemplate(unittest.TestCase):
},
)
self.hass.start()
self.hass.block_till_done()
init_calls = len(_update_state.mock_calls)
await hass.async_start()
await hass.async_block_till_done()
init_calls = len(_update_state.mock_calls)
self.hass.states.set("sensor.any_state", "update")
self.hass.block_till_done()
assert len(_update_state.mock_calls) == init_calls
hass.states.async_set("sensor.any_state", "update")
await hass.async_block_till_done()
assert len(_update_state.mock_calls) == init_calls
def test_event(self):
"""Test the event."""
config = {
"binary_sensor": {
"platform": "template",
"sensors": {
"test": {
"friendly_name": "virtual thingy",
"value_template": "{{ states.sensor.test_state.state == 'on' }}",
"device_class": "motion",
}
},
}
async def test_event(hass):
"""Test the event."""
config = {
"binary_sensor": {
"platform": "template",
"sensors": {
"test": {
"friendly_name": "virtual thingy",
"value_template": "{{ states.sensor.test_state.state == 'on' }}",
"device_class": "motion",
}
},
}
with assert_setup_component(1):
assert setup.setup_component(self.hass, "binary_sensor", config)
}
with assert_setup_component(1):
assert await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test")
assert state.state == "off"
state = hass.states.get("binary_sensor.test")
assert state.state == "off"
self.hass.states.set("sensor.test_state", "on")
self.hass.block_till_done()
hass.states.async_set("sensor.test_state", "on")
await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test")
assert state.state == "on"
state = hass.states.get("binary_sensor.test")
assert state.state == "on"
async def test_template_delay_on(hass):
@ -288,7 +279,7 @@ async def test_template_delay_on(hass):
},
}
}
await setup.async_setup_component(hass, "binary_sensor", config)
await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
await hass.async_block_till_done()
await hass.async_start()
@ -348,7 +339,7 @@ async def test_template_delay_off(hass):
}
}
hass.states.async_set("sensor.test_state", "on")
await setup.async_setup_component(hass, "binary_sensor", config)
await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
await hass.async_block_till_done()
await hass.async_start()
@ -407,7 +398,7 @@ async def test_available_without_availability_template(hass):
},
}
}
await setup.async_setup_component(hass, "binary_sensor", config)
await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
@ -434,7 +425,7 @@ async def test_availability_template(hass):
},
}
}
await setup.async_setup_component(hass, "binary_sensor", config)
await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
@ -459,7 +450,7 @@ async def test_invalid_attribute_template(hass, caplog):
await setup.async_setup_component(
hass,
"binary_sensor",
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
@ -488,7 +479,7 @@ async def test_invalid_availability_template_keeps_component_available(hass, cap
await setup.async_setup_component(
hass,
"binary_sensor",
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
@ -517,7 +508,7 @@ async def test_no_update_template_match_all(hass, caplog):
await setup.async_setup_component(
hass,
"binary_sensor",
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
@ -583,7 +574,7 @@ async def test_unique_id(hass):
"""Test unique_id option only creates one binary sensor per id."""
await setup.async_setup_component(
hass,
"binary_sensor",
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
@ -625,7 +616,7 @@ async def test_template_validation_error(hass, caplog):
},
},
}
await setup.async_setup_component(hass, "binary_sensor", config)
await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()