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.""" """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,207 +14,198 @@ 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.""" """Test the setup."""
config = {
hass = None "binary_sensor": {
# pylint: disable=invalid-name "platform": "template",
"sensors": {
def setup_method(self, method): "test": {
"""Set up things to be run when tests are started.""" "friendly_name": "virtual thingy",
self.hass = get_test_home_assistant() "value_template": "{{ foo }}",
"device_class": "motion",
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",
}
},
}
} }
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): async def test_setup_no_sensors(hass):
"""Test the setup with invalid devices.""" """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, hass, binary_sensor.DOMAIN, {"binary_sensor": {"platform": "template"}}
"binary_sensor", )
{"binary_sensor": {"platform": "template", "sensors": {"foo bar": {}}}},
)
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): async def test_setup_invalid_device(hass):
"""Test setup with invalid and missing template.""" """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": {"test": {"device_class": "motion"}},
}
},
)
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() async def test_setup_invalid_device_class(hass):
self.hass.start() """Test setup with invalid sensor class."""
self.hass.block_till_done() 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") async def test_setup_invalid_missing_template(hass):
self.hass.block_till_done() """Test setup with invalid and missing template."""
state = self.hass.states.get("binary_sensor.test_template_sensor") with assert_setup_component(0):
assert state.attributes["icon"] == "mdi:check" 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() async def test_icon_template(hass):
self.hass.start() """Test icon template."""
self.hass.block_till_done() 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") await hass.async_block_till_done()
assert state.attributes.get("entity_picture") == "" await hass.async_start()
await hass.async_block_till_done()
self.hass.states.set("binary_sensor.test_state", "Works") state = hass.states.get("binary_sensor.test_template_sensor")
self.hass.block_till_done() assert state.attributes.get("icon") == ""
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["entity_picture"] == "/local/sensor.png"
def test_attribute_templates(self): hass.states.async_set("binary_sensor.test_state", "Works")
"""Test attribute_templates template.""" await hass.async_block_till_done()
with assert_setup_component(1): state = hass.states.get("binary_sensor.test_template_sensor")
assert setup.setup_component( assert state.attributes["icon"] == "mdi:check"
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 }}."
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test_template_sensor") async def test_entity_picture_template(hass):
assert state.attributes.get("test_attribute") == "It ." """Test entity_picture template."""
self.hass.states.set("sensor.test_state", "Works2") with assert_setup_component(1):
self.hass.block_till_done() assert await setup.async_setup_component(
self.hass.states.set("sensor.test_state", "Works") hass,
self.hass.block_till_done() binary_sensor.DOMAIN,
state = self.hass.states.get("binary_sensor.test_template_sensor") {
assert state.attributes["test_attribute"] == "It Works." "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." "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,43 +224,44 @@ 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):
"""Test the event.""" async def test_event(hass):
config = { """Test the event."""
"binary_sensor": { config = {
"platform": "template", "binary_sensor": {
"sensors": { "platform": "template",
"test": { "sensors": {
"friendly_name": "virtual thingy", "test": {
"value_template": "{{ states.sensor.test_state.state == 'on' }}", "friendly_name": "virtual thingy",
"device_class": "motion", "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() 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"
async def test_template_delay_on(hass): 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_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()