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,28 +14,11 @@ 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):
async def test_setup(hass):
"""Test the setup."""
config = {
"binary_sensor": {
@ -51,30 +33,33 @@ class TestBinarySensorTemplate(unittest.TestCase):
}
}
with assert_setup_component(1):
assert setup.setup_component(self.hass, "binary_sensor", config)
assert await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
def test_setup_no_sensors(self):
async def test_setup_no_sensors(hass):
"""Test setup with no sensors."""
with assert_setup_component(0):
assert setup.setup_component(
self.hass, "binary_sensor", {"binary_sensor": {"platform": "template"}}
assert await setup.async_setup_component(
hass, binary_sensor.DOMAIN, {"binary_sensor": {"platform": "template"}}
)
def test_setup_invalid_device(self):
async def test_setup_invalid_device(hass):
"""Test the setup with invalid devices."""
with assert_setup_component(0):
assert setup.setup_component(
self.hass,
"binary_sensor",
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{"binary_sensor": {"platform": "template", "sensors": {"foo bar": {}}}},
)
def test_setup_invalid_device_class(self):
async def test_setup_invalid_device_class(hass):
"""Test setup with invalid sensor class."""
with assert_setup_component(0):
assert setup.setup_component(
self.hass,
"binary_sensor",
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
@ -88,12 +73,13 @@ class TestBinarySensorTemplate(unittest.TestCase):
},
)
def test_setup_invalid_missing_template(self):
async def test_setup_invalid_missing_template(hass):
"""Test setup with invalid and missing template."""
with assert_setup_component(0):
assert setup.setup_component(
self.hass,
"binary_sensor",
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
{
"binary_sensor": {
"platform": "template",
@ -102,12 +88,13 @@ class TestBinarySensorTemplate(unittest.TestCase):
},
)
def test_icon_template(self):
async def test_icon_template(hass):
"""Test icon template."""
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",
@ -125,24 +112,25 @@ class TestBinarySensorTemplate(unittest.TestCase):
},
)
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_template_sensor")
state = 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")
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"
def test_entity_picture_template(self):
async def test_entity_picture_template(hass):
"""Test entity_picture template."""
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",
@ -160,24 +148,25 @@ class TestBinarySensorTemplate(unittest.TestCase):
},
)
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_template_sensor")
state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("entity_picture") == ""
self.hass.states.set("binary_sensor.test_state", "Works")
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test_template_sensor")
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"
def test_attribute_templates(self):
async def test_attribute_templates(hass):
"""Test attribute_templates template."""
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",
@ -193,29 +182,30 @@ class TestBinarySensorTemplate(unittest.TestCase):
},
)
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_template_sensor")
state = 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")
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."
@mock.patch(
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,15 +224,16 @@ class TestBinarySensorTemplate(unittest.TestCase):
},
)
self.hass.start()
self.hass.block_till_done()
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()
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):
async def test_event(hass):
"""Test the event."""
config = {
"binary_sensor": {
@ -257,19 +248,19 @@ class TestBinarySensorTemplate(unittest.TestCase):
}
}
with assert_setup_component(1):
assert setup.setup_component(self.hass, "binary_sensor", config)
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")
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")
state = hass.states.get("binary_sensor.test")
assert state.state == "on"
@ -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()