mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Template binary sensor to not track all state changes (#18573)
This commit is contained in:
committed by
Paulus Schoutsen
parent
97c493448b
commit
84fd66c8a1
@@ -1,10 +1,9 @@
|
||||
"""The tests for the Template Binary sensor platform."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from homeassistant.const import MATCH_ALL
|
||||
from homeassistant.const import MATCH_ALL, EVENT_HOMEASSISTANT_START
|
||||
from homeassistant import setup
|
||||
from homeassistant.components.binary_sensor import template
|
||||
from homeassistant.exceptions import TemplateError
|
||||
@@ -182,7 +181,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
||||
|
||||
self.hass.states.set('sensor.any_state', 'update')
|
||||
self.hass.block_till_done()
|
||||
assert len(_async_render.mock_calls) > init_calls
|
||||
assert len(_async_render.mock_calls) == init_calls
|
||||
|
||||
def test_attributes(self):
|
||||
"""Test the attributes."""
|
||||
@@ -252,8 +251,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
||||
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_template_delay_on(hass):
|
||||
async def test_template_delay_on(hass):
|
||||
"""Test binary sensor template delay on."""
|
||||
config = {
|
||||
'binary_sensor': {
|
||||
@@ -269,51 +267,50 @@ def test_template_delay_on(hass):
|
||||
},
|
||||
},
|
||||
}
|
||||
yield from setup.async_setup_component(hass, 'binary_sensor', config)
|
||||
yield from hass.async_start()
|
||||
await setup.async_setup_component(hass, 'binary_sensor', config)
|
||||
await hass.async_start()
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
async_fire_time_changed(hass, future)
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
# check with time changes
|
||||
hass.states.async_set('sensor.test_state', 'off')
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'off')
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
async_fire_time_changed(hass, future)
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_template_delay_off(hass):
|
||||
async def test_template_delay_off(hass):
|
||||
"""Test binary sensor template delay off."""
|
||||
config = {
|
||||
'binary_sensor': {
|
||||
@@ -330,44 +327,110 @@ def test_template_delay_off(hass):
|
||||
},
|
||||
}
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from setup.async_setup_component(hass, 'binary_sensor', config)
|
||||
yield from hass.async_start()
|
||||
await setup.async_setup_component(hass, 'binary_sensor', config)
|
||||
await hass.async_start()
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'off')
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
async_fire_time_changed(hass, future)
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
# check with time changes
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'off')
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
async_fire_time_changed(hass, future)
|
||||
yield from hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
|
||||
async def test_no_update_template_match_all(hass, caplog):
|
||||
"""Test that we do not update sensors that match on all."""
|
||||
hass.states.async_set('binary_sensor.test_sensor', 'true')
|
||||
|
||||
await setup.async_setup_component(hass, 'binary_sensor', {
|
||||
'binary_sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'all_state': {
|
||||
'value_template': '{{ "true" }}',
|
||||
},
|
||||
'all_icon': {
|
||||
'value_template':
|
||||
'{{ states.binary_sensor.test_sensor.state }}',
|
||||
'icon_template': '{{ 1 + 1 }}',
|
||||
},
|
||||
'all_entity_picture': {
|
||||
'value_template':
|
||||
'{{ states.binary_sensor.test_sensor.state }}',
|
||||
'entity_picture_template': '{{ 1 + 1 }}',
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all()) == 4
|
||||
assert ('Template binary sensor all_state has no entity ids '
|
||||
'configured to track nor were we able to extract the entities to '
|
||||
'track from the value template') in caplog.text
|
||||
assert ('Template binary sensor all_icon has no entity ids '
|
||||
'configured to track nor were we able to extract the entities to '
|
||||
'track from the icon template') in caplog.text
|
||||
assert ('Template binary sensor all_entity_picture has no entity ids '
|
||||
'configured to track nor were we able to extract the entities to '
|
||||
'track from the entity_picture template') in caplog.text
|
||||
|
||||
assert hass.states.get('binary_sensor.all_state').state == 'off'
|
||||
assert hass.states.get('binary_sensor.all_icon').state == 'off'
|
||||
assert hass.states.get('binary_sensor.all_entity_picture').state == 'off'
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get('binary_sensor.all_state').state == 'on'
|
||||
assert hass.states.get('binary_sensor.all_icon').state == 'on'
|
||||
assert hass.states.get('binary_sensor.all_entity_picture').state == 'on'
|
||||
|
||||
hass.states.async_set('binary_sensor.test_sensor', 'false')
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get('binary_sensor.all_state').state == 'on'
|
||||
assert hass.states.get('binary_sensor.all_icon').state == 'on'
|
||||
assert hass.states.get('binary_sensor.all_entity_picture').state == 'on'
|
||||
|
||||
await hass.helpers.entity_component.async_update_entity(
|
||||
'binary_sensor.all_state')
|
||||
await hass.helpers.entity_component.async_update_entity(
|
||||
'binary_sensor.all_icon')
|
||||
await hass.helpers.entity_component.async_update_entity(
|
||||
'binary_sensor.all_entity_picture')
|
||||
|
||||
assert hass.states.get('binary_sensor.all_state').state == 'on'
|
||||
assert hass.states.get('binary_sensor.all_icon').state == 'off'
|
||||
assert hass.states.get('binary_sensor.all_entity_picture').state == 'off'
|
||||
|
||||
Reference in New Issue
Block a user