Improve tests for template binary sensor (#62103)

pull/62147/head
Erik Montnemery 2021-12-16 21:31:37 +01:00 committed by GitHub
parent 4de4cc7bd4
commit 4353b1e62c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 372 additions and 148 deletions

View File

@ -6,7 +6,7 @@ from unittest.mock import patch
import pytest
from homeassistant import setup
from homeassistant.components import binary_sensor
from homeassistant.components import binary_sensor, template
from homeassistant.const import (
ATTR_DEVICE_CLASS,
EVENT_HOMEASSISTANT_START,
@ -24,10 +24,55 @@ ON = "on"
OFF = "off"
@pytest.mark.parametrize("count,domain", [(1, binary_sensor.DOMAIN)])
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"config",
"config,domain,entity_id,name,attributes",
[
(
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test": {
"value_template": "{{ True }}",
}
},
},
},
binary_sensor.DOMAIN,
"binary_sensor.test",
"test",
{"friendly_name": "test"},
),
(
{
"template": {
"binary_sensor": {
"state": "{{ True }}",
}
},
},
template.DOMAIN,
"binary_sensor.unnamed_device",
"unnamed device",
{},
),
],
)
async def test_setup_minimal(hass, start_ha, entity_id, name, attributes):
"""Test the setup."""
state = hass.states.get(entity_id)
assert state is not None
assert state.name == name
assert state.state == ON
assert state.attributes == attributes
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"config,domain,entity_id",
[
(
{
"binary_sensor": {
"platform": "template",
@ -40,23 +85,54 @@ OFF = "off"
},
},
},
binary_sensor.DOMAIN,
"binary_sensor.test",
),
(
{
"template": {
"binary_sensor": {
"name": "virtual thingy",
"state": "{{ True }}",
"device_class": "motion",
}
},
},
template.DOMAIN,
"binary_sensor.virtual_thingy",
),
],
)
async def test_setup_legacy(hass, start_ha):
async def test_setup(hass, start_ha, entity_id):
"""Test the setup."""
state = hass.states.get("binary_sensor.test")
state = hass.states.get(entity_id)
assert state is not None
assert state.name == "virtual thingy"
assert state.state == ON
assert state.attributes["device_class"] == "motion"
@pytest.mark.parametrize("count,domain", [(0, binary_sensor.DOMAIN)])
@pytest.mark.parametrize("count", [0])
@pytest.mark.parametrize(
"config",
"config,domain",
[
# No legacy binary sensors
(
{"binary_sensor": {"platform": "template"}},
binary_sensor.DOMAIN,
),
# Legacy binary sensor missing mandatory config
(
{"binary_sensor": {"platform": "template", "sensors": {"foo bar": {}}}},
binary_sensor.DOMAIN,
),
# Binary sensor missing mandatory config
(
{"template": {"binary_sensor": {}}},
template.DOMAIN,
),
# Legacy binary sensor with invalid device class
(
{
"binary_sensor": {
"platform": "template",
@ -68,12 +144,30 @@ async def test_setup_legacy(hass, start_ha):
},
}
},
binary_sensor.DOMAIN,
),
# Binary sensor with invalid device class
(
{
"template": {
"binary_sensor": {
"state": "{{ foo }}",
"device_class": "foobarnotreal",
}
}
},
template.DOMAIN,
),
# Legacy binary sensor missing mandatory config
(
{
"binary_sensor": {
"platform": "template",
"sensors": {"test": {"device_class": "motion"}},
}
},
binary_sensor.DOMAIN,
),
],
)
async def test_setup_invalid_sensors(hass, count, start_ha):
@ -81,10 +175,11 @@ async def test_setup_invalid_sensors(hass, count, start_ha):
assert len(hass.states.async_entity_ids("binary_sensor")) == count
@pytest.mark.parametrize("count,domain", [(1, binary_sensor.DOMAIN)])
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"config",
"config,domain,entity_id",
[
(
{
"binary_sensor": {
"platform": "template",
@ -100,23 +195,43 @@ async def test_setup_invalid_sensors(hass, count, start_ha):
},
},
},
binary_sensor.DOMAIN,
"binary_sensor.test_template_sensor",
),
(
{
"template": {
"binary_sensor": {
"state": "{{ states.sensor.xyz.state }}",
"icon": "{% if "
"states.binary_sensor.test_state.state == "
"'Works' %}"
"mdi:check"
"{% endif %}",
},
},
},
template.DOMAIN,
"binary_sensor.unnamed_device",
),
],
)
async def test_icon_template(hass, start_ha):
async def test_icon_template(hass, start_ha, entity_id):
"""Test icon template."""
state = hass.states.get("binary_sensor.test_template_sensor")
state = hass.states.get(entity_id)
assert state.attributes.get("icon") == ""
hass.states.async_set("binary_sensor.test_state", "Works")
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test_template_sensor")
state = hass.states.get(entity_id)
assert state.attributes["icon"] == "mdi:check"
@pytest.mark.parametrize("count,domain", [(1, binary_sensor.DOMAIN)])
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"config",
"config,domain,entity_id",
[
(
{
"binary_sensor": {
"platform": "template",
@ -132,23 +247,43 @@ async def test_icon_template(hass, start_ha):
},
},
},
binary_sensor.DOMAIN,
"binary_sensor.test_template_sensor",
),
(
{
"template": {
"binary_sensor": {
"state": "{{ states.sensor.xyz.state }}",
"picture": "{% if "
"states.binary_sensor.test_state.state == "
"'Works' %}"
"/local/sensor.png"
"{% endif %}",
},
},
},
template.DOMAIN,
"binary_sensor.unnamed_device",
),
],
)
async def test_entity_picture_template(hass, start_ha):
async def test_entity_picture_template(hass, start_ha, entity_id):
"""Test entity_picture template."""
state = hass.states.get("binary_sensor.test_template_sensor")
state = hass.states.get(entity_id)
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")
state = hass.states.get(entity_id)
assert state.attributes["entity_picture"] == "/local/sensor.png"
@pytest.mark.parametrize("count,domain", [(1, binary_sensor.DOMAIN)])
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"config",
"config,domain,entity_id",
[
(
{
"binary_sensor": {
"platform": "template",
@ -162,17 +297,34 @@ async def test_entity_picture_template(hass, start_ha):
},
},
},
binary_sensor.DOMAIN,
"binary_sensor.test_template_sensor",
),
(
{
"template": {
"binary_sensor": {
"state": "{{ states.sensor.xyz.state }}",
"attributes": {
"test_attribute": "It {{ states.sensor.test_state.state }}."
},
},
},
},
template.DOMAIN,
"binary_sensor.unnamed_device",
),
],
)
async def test_attribute_templates(hass, start_ha):
async def test_attribute_templates(hass, start_ha, entity_id):
"""Test attribute_templates template."""
state = hass.states.get("binary_sensor.test_template_sensor")
state = hass.states.get(entity_id)
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")
state = hass.states.get(entity_id)
assert state.attributes["test_attribute"] == "It Works."
@ -247,10 +399,10 @@ async def test_event(hass, start_ha):
assert state.state == ON
@pytest.mark.parametrize("count,domain", [(1, binary_sensor.DOMAIN)])
@pytest.mark.parametrize(
"config",
"config,count,domain",
[
(
{
"binary_sensor": {
"platform": "template",
@ -270,6 +422,34 @@ async def test_event(hass, start_ha):
},
},
},
1,
binary_sensor.DOMAIN,
),
(
{
"template": [
{
"binary_sensor": {
"name": "test on",
"state": "{{ states.sensor.test_state.state == 'on' }}",
"device_class": "motion",
"delay_on": 5,
},
},
{
"binary_sensor": {
"name": "test off",
"state": "{{ states.sensor.test_state.state == 'on' }}",
"device_class": "motion",
"delay_off": 5,
},
},
]
},
2,
template.DOMAIN,
),
(
{
"binary_sensor": {
"platform": "template",
@ -289,6 +469,10 @@ async def test_event(hass, start_ha):
},
},
},
1,
binary_sensor.DOMAIN,
),
(
{
"binary_sensor": {
"platform": "template",
@ -308,6 +492,9 @@ async def test_event(hass, start_ha):
},
},
},
1,
binary_sensor.DOMAIN,
),
],
)
async def test_template_delay_on_off(hass, start_ha):
@ -349,10 +536,11 @@ async def test_template_delay_on_off(hass, start_ha):
assert hass.states.get("binary_sensor.test_off").state == OFF
@pytest.mark.parametrize("count,domain", [(1, binary_sensor.DOMAIN)])
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"config",
"config,domain,entity_id",
[
(
{
"binary_sensor": {
"platform": "template",
@ -366,20 +554,38 @@ async def test_template_delay_on_off(hass, start_ha):
},
},
},
binary_sensor.DOMAIN,
"binary_sensor.test",
),
(
{
"template": {
"binary_sensor": {
"name": "virtual thingy",
"state": "true",
"device_class": "motion",
"delay_off": 5,
},
},
},
template.DOMAIN,
"binary_sensor.virtual_thingy",
),
],
)
async def test_available_without_availability_template(hass, start_ha):
async def test_available_without_availability_template(hass, start_ha, entity_id):
"""Ensure availability is true without an availability_template."""
state = hass.states.get("binary_sensor.test")
state = hass.states.get(entity_id)
assert state.state != STATE_UNAVAILABLE
assert state.attributes[ATTR_DEVICE_CLASS] == "motion"
@pytest.mark.parametrize("count,domain", [(1, binary_sensor.DOMAIN)])
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"config",
"config,domain,entity_id",
[
(
{
"binary_sensor": {
"platform": "template",
@ -394,19 +600,37 @@ async def test_available_without_availability_template(hass, start_ha):
},
},
},
binary_sensor.DOMAIN,
"binary_sensor.test",
),
(
{
"template": {
"binary_sensor": {
"name": "virtual thingy",
"state": "true",
"device_class": "motion",
"delay_off": 5,
"availability": "{{ is_state('sensor.test_state','on') }}",
},
},
},
template.DOMAIN,
"binary_sensor.virtual_thingy",
),
],
)
async def test_availability_template(hass, start_ha):
async def test_availability_template(hass, start_ha, entity_id):
"""Test availability template."""
hass.states.async_set("sensor.test_state", STATE_OFF)
await hass.async_block_till_done()
assert hass.states.get("binary_sensor.test").state == STATE_UNAVAILABLE
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
hass.states.async_set("sensor.test_state", STATE_ON)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
state = hass.states.get(entity_id)
assert state.state != STATE_UNAVAILABLE
assert state.attributes[ATTR_DEVICE_CLASS] == "motion"