Rewrite tests for Template Binary Sensor (#41098)

This commit is contained in:
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

View File

@ -1,10 +1,9 @@
"""The tests for the Template Binary sensor platform.""" """The tests for the Template Binary sensor platform."""
from datetime import timedelta from datetime import timedelta
import logging import logging
import unittest
from unittest import mock
from homeassistant import setup from homeassistant import setup
from homeassistant.components import binary_sensor
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS, ATTR_DEVICE_CLASS,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_START,
@ -15,28 +14,11 @@ from homeassistant.const import (
from homeassistant.core import CoreState from homeassistant.core import CoreState
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 TestBinarySensorTemplate(unittest.TestCase): async def test_setup(hass):
"""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.""" """Test the setup."""
config = { config = {
"binary_sensor": { "binary_sensor": {
@ -51,30 +33,33 @@ class TestBinarySensorTemplate(unittest.TestCase):
} }
} }
with assert_setup_component(1): 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.""" """Test setup with no sensors."""
with assert_setup_component(0): with assert_setup_component(0):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, "binary_sensor", {"binary_sensor": {"platform": "template"}} 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.""" """Test the setup with invalid devices."""
with assert_setup_component(0): with assert_setup_component(0):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{"binary_sensor": {"platform": "template", "sensors": {"foo bar": {}}}}, {"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.""" """Test setup with invalid sensor class."""
with assert_setup_component(0): with assert_setup_component(0):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "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.""" """Test setup with invalid and missing template."""
with assert_setup_component(0): with assert_setup_component(0):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "platform": "template",
@ -102,12 +88,13 @@ class TestBinarySensorTemplate(unittest.TestCase):
}, },
) )
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):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "platform": "template",
@ -125,24 +112,25 @@ class TestBinarySensorTemplate(unittest.TestCase):
}, },
) )
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("binary_sensor.test_template_sensor") state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("icon") == "" assert state.attributes.get("icon") == ""
self.hass.states.set("binary_sensor.test_state", "Works") hass.states.async_set("binary_sensor.test_state", "Works")
self.hass.block_till_done() 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["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):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "platform": "template",
@ -160,24 +148,25 @@ class TestBinarySensorTemplate(unittest.TestCase):
}, },
) )
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("binary_sensor.test_template_sensor") state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("entity_picture") == "" assert state.attributes.get("entity_picture") == ""
self.hass.states.set("binary_sensor.test_state", "Works") hass.states.async_set("binary_sensor.test_state", "Works")
self.hass.block_till_done() 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["entity_picture"] == "/local/sensor.png" assert state.attributes["entity_picture"] == "/local/sensor.png"
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):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "platform": "template",
@ -193,29 +182,30 @@ class TestBinarySensorTemplate(unittest.TestCase):
}, },
) )
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("binary_sensor.test_template_sensor") state = hass.states.get("binary_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", "Works2") hass.states.async_set("sensor.test_state", "Works2")
self.hass.block_till_done() await hass.async_block_till_done()
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("binary_sensor.test_template_sensor") state = hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["test_attribute"] == "It Works." 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." "homeassistant.components.template.binary_sensor."
"BinarySensorTemplate._update_state" "BinarySensorTemplate._update_state"
) ) as _update_state:
def test_match_all(self, _update_state):
"""Test template that is rerendered on any state lifecycle."""
with assert_setup_component(1): with assert_setup_component(1):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "platform": "template",
@ -234,15 +224,16 @@ class TestBinarySensorTemplate(unittest.TestCase):
}, },
) )
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
init_calls = len(_update_state.mock_calls) init_calls = len(_update_state.mock_calls)
self.hass.states.set("sensor.any_state", "update") hass.states.async_set("sensor.any_state", "update")
self.hass.block_till_done() await hass.async_block_till_done()
assert len(_update_state.mock_calls) == init_calls assert len(_update_state.mock_calls) == init_calls
def test_event(self):
async def test_event(hass):
"""Test the event.""" """Test the event."""
config = { config = {
"binary_sensor": { "binary_sensor": {
@ -257,19 +248,19 @@ class TestBinarySensorTemplate(unittest.TestCase):
} }
} }
with assert_setup_component(1): 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() 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("binary_sensor.test") state = hass.states.get("binary_sensor.test")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_state", "on") hass.states.async_set("sensor.test_state", "on")
self.hass.block_till_done() 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" 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_block_till_done()
await hass.async_start() await hass.async_start()
@ -348,7 +339,7 @@ async def test_template_delay_off(hass):
} }
} }
hass.states.async_set("sensor.test_state", "on") 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_block_till_done()
await hass.async_start() 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_block_till_done()
await hass.async_start() await hass.async_start()
await hass.async_block_till_done() 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_block_till_done()
await hass.async_start() await hass.async_start()
await hass.async_block_till_done() await hass.async_block_till_done()
@ -459,7 +450,7 @@ async def test_invalid_attribute_template(hass, caplog):
await setup.async_setup_component( await setup.async_setup_component(
hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "platform": "template",
@ -488,7 +479,7 @@ async def test_invalid_availability_template_keeps_component_available(hass, cap
await setup.async_setup_component( await setup.async_setup_component(
hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "platform": "template",
@ -517,7 +508,7 @@ async def test_no_update_template_match_all(hass, caplog):
await setup.async_setup_component( await setup.async_setup_component(
hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "platform": "template",
@ -583,7 +574,7 @@ async def test_unique_id(hass):
"""Test unique_id option only creates one binary sensor per id.""" """Test unique_id option only creates one binary sensor per id."""
await setup.async_setup_component( await setup.async_setup_component(
hass, hass,
"binary_sensor", binary_sensor.DOMAIN,
{ {
"binary_sensor": { "binary_sensor": {
"platform": "template", "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_block_till_done()
await hass.async_start() await hass.async_start()
await hass.async_block_till_done() await hass.async_block_till_done()