Rewrite bayesian unittest tests to pytest style test functions. (#41740)

This commit is contained in:
thaohtp 2020-10-16 13:10:48 +02:00 committed by GitHub
parent 6f113e981b
commit aabf26bbc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,6 @@
"""The test for the bayesian sensor platform.""" """The test for the bayesian sensor platform."""
import json import json
from os import path from os import path
import unittest
from homeassistant import config as hass_config from homeassistant import config as hass_config
from homeassistant.components.bayesian import DOMAIN, binary_sensor as bayesian from homeassistant.components.bayesian import DOMAIN, binary_sensor as bayesian
@ -17,491 +16,488 @@ from homeassistant.const import (
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.core import Context, callback from homeassistant.core import Context, callback
from homeassistant.setup import async_setup_component, setup_component from homeassistant.setup import async_setup_component
from tests.async_mock import patch from tests.async_mock import patch
from tests.common import get_test_home_assistant
class TestBayesianBinarySensor(unittest.TestCase): async def test_load_values_when_added_to_hass(hass):
"""Test the threshold sensor.""" """Test that sensor initializes with observations of relevant entities."""
def setup_method(self, method): config = {
"""Set up things to be run when tests are started.""" "binary_sensor": {
self.hass = get_test_home_assistant() "name": "Test_Binary",
"platform": "bayesian",
def teardown_method(self, method): "observations": [
"""Stop everything that was started.""" {
self.hass.stop() "platform": "state",
"entity_id": "sensor.test_monitored",
def test_load_values_when_added_to_hass(self): "to_state": "off",
"""Test that sensor initializes with observations of relevant entities.""" "prob_given_true": 0.8,
"prob_given_false": 0.4,
config = { }
"binary_sensor": { ],
"name": "Test_Binary", "prior": 0.2,
"platform": "bayesian", "probability_threshold": 0.32,
"observations": [
{
"platform": "state",
"entity_id": "sensor.test_monitored",
"to_state": "off",
"prob_given_true": 0.8,
"prob_given_false": 0.4,
}
],
"prior": 0.2,
"probability_threshold": 0.32,
}
} }
}
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8
assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4 assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4
def test_unknown_state_does_not_influence_probability(self):
"""Test that an unknown state does not change the output probability."""
config = { async def test_unknown_state_does_not_influence_probability(hass):
"binary_sensor": { """Test that an unknown state does not change the output probability."""
"name": "Test_Binary",
"platform": "bayesian", config = {
"observations": [ "binary_sensor": {
{ "name": "Test_Binary",
"platform": "state", "platform": "bayesian",
"entity_id": "sensor.test_monitored", "observations": [
"to_state": "off", {
"prob_given_true": 0.8, "platform": "state",
"prob_given_false": 0.4, "entity_id": "sensor.test_monitored",
} "to_state": "off",
], "prob_given_true": 0.8,
"prior": 0.2, "prob_given_false": 0.4,
"probability_threshold": 0.32, }
} ],
"prior": 0.2,
"probability_threshold": 0.32,
} }
}
self.hass.states.set("sensor.test_monitored", STATE_UNKNOWN) hass.states.async_set("sensor.test_monitored", STATE_UNKNOWN)
self.hass.block_till_done() await hass.async_block_till_done()
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert state.attributes.get("observations") == [] assert state.attributes.get("observations") == []
def test_sensor_numeric_state(self):
"""Test sensor on numeric state platform observations.""" async def test_sensor_numeric_state(hass):
config = { """Test sensor on numeric state platform observations."""
"binary_sensor": { config = {
"platform": "bayesian", "binary_sensor": {
"name": "Test_Binary", "platform": "bayesian",
"observations": [ "name": "Test_Binary",
{ "observations": [
"platform": "numeric_state", {
"entity_id": "sensor.test_monitored", "platform": "numeric_state",
"below": 10, "entity_id": "sensor.test_monitored",
"above": 5, "below": 10,
"prob_given_true": 0.6, "above": 5,
}, "prob_given_true": 0.6,
{ },
"platform": "numeric_state", {
"entity_id": "sensor.test_monitored1", "platform": "numeric_state",
"below": 7, "entity_id": "sensor.test_monitored1",
"above": 5, "below": 7,
"prob_given_true": 0.9, "above": 5,
"prob_given_false": 0.1, "prob_given_true": 0.9,
}, "prob_given_false": 0.1,
], },
"prior": 0.2, ],
} "prior": 0.2,
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", 4) hass.states.async_set("sensor.test_monitored", 4)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert [] == state.attributes.get("observations") assert [] == state.attributes.get("observations")
assert 0.2 == state.attributes.get("probability") assert 0.2 == state.attributes.get("probability")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 6) hass.states.async_set("sensor.test_monitored", 6)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", 4) hass.states.async_set("sensor.test_monitored", 4)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", 6) hass.states.async_set("sensor.test_monitored", 6)
self.hass.states.set("sensor.test_monitored1", 6) hass.states.async_set("sensor.test_monitored1", 6)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert state.attributes.get("observations")[0]["prob_given_true"] == 0.6 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.6
assert state.attributes.get("observations")[1]["prob_given_true"] == 0.9 assert state.attributes.get("observations")[1]["prob_given_true"] == 0.9
assert state.attributes.get("observations")[1]["prob_given_false"] == 0.1 assert state.attributes.get("observations")[1]["prob_given_false"] == 0.1
assert round(abs(0.77 - state.attributes.get("probability")), 7) == 0 assert round(abs(0.77 - state.attributes.get("probability")), 7) == 0
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", 6) hass.states.async_set("sensor.test_monitored", 6)
self.hass.states.set("sensor.test_monitored1", 0) hass.states.async_set("sensor.test_monitored1", 0)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", 4) hass.states.async_set("sensor.test_monitored", 4)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert 0.2 == state.attributes.get("probability") assert 0.2 == state.attributes.get("probability")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 15) hass.states.async_set("sensor.test_monitored", 15)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert state.state == "off" assert state.state == "off"
def test_sensor_state(self):
"""Test sensor on state platform observations.""" async def test_sensor_state(hass):
config = { """Test sensor on state platform observations."""
"binary_sensor": { config = {
"name": "Test_Binary", "binary_sensor": {
"platform": "bayesian", "name": "Test_Binary",
"observations": [ "platform": "bayesian",
{ "observations": [
"platform": "state", {
"entity_id": "sensor.test_monitored", "platform": "state",
"to_state": "off", "entity_id": "sensor.test_monitored",
"prob_given_true": 0.8, "to_state": "off",
"prob_given_false": 0.4, "prob_given_true": 0.8,
} "prob_given_false": 0.4,
], }
"prior": 0.2, ],
"probability_threshold": 0.32, "prior": 0.2,
} "probability_threshold": 0.32,
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "on") hass.states.async_set("sensor.test_monitored", "on")
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert [] == state.attributes.get("observations") assert [] == state.attributes.get("observations")
assert 0.2 == state.attributes.get("probability") assert 0.2 == state.attributes.get("probability")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "on") hass.states.async_set("sensor.test_monitored", "on")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8
assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4 assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4
assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0 assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "on") hass.states.async_set("sensor.test_monitored", "on")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert round(abs(0.2 - state.attributes.get("probability")), 7) == 0 assert round(abs(0.2 - state.attributes.get("probability")), 7) == 0
assert state.state == "off" assert state.state == "off"
def test_sensor_value_template(self):
"""Test sensor on template platform observations.""" async def test_sensor_value_template(hass):
config = { """Test sensor on template platform observations."""
"binary_sensor": { config = {
"name": "Test_Binary", "binary_sensor": {
"platform": "bayesian", "name": "Test_Binary",
"observations": [ "platform": "bayesian",
{ "observations": [
"platform": "template", {
"value_template": "{{states('sensor.test_monitored') == 'off'}}", "platform": "template",
"prob_given_true": 0.8, "value_template": "{{states('sensor.test_monitored') == 'off'}}",
"prob_given_false": 0.4, "prob_given_true": 0.8,
} "prob_given_false": 0.4,
], }
"prior": 0.2, ],
"probability_threshold": 0.32, "prior": 0.2,
} "probability_threshold": 0.32,
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "on") hass.states.async_set("sensor.test_monitored", "on")
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert [] == state.attributes.get("observations") assert [] == state.attributes.get("observations")
assert 0.2 == state.attributes.get("probability") assert 0.2 == state.attributes.get("probability")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "on") hass.states.async_set("sensor.test_monitored", "on")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8
assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4 assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4
assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0 assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "on") hass.states.async_set("sensor.test_monitored", "on")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert round(abs(0.2 - state.attributes.get("probability")), 7) == 0 assert round(abs(0.2 - state.attributes.get("probability")), 7) == 0
assert state.state == "off" assert state.state == "off"
def test_threshold(self):
"""Test sensor on probability threshold limits.""" async def test_threshold(hass):
config = { """Test sensor on probability threshold limits."""
"binary_sensor": { config = {
"name": "Test_Binary", "binary_sensor": {
"platform": "bayesian", "name": "Test_Binary",
"observations": [ "platform": "bayesian",
{ "observations": [
"platform": "state", {
"entity_id": "sensor.test_monitored", "platform": "state",
"to_state": "on", "entity_id": "sensor.test_monitored",
"prob_given_true": 1.0, "to_state": "on",
} "prob_given_true": 1.0,
], }
"prior": 0.5, ],
"probability_threshold": 1.0, "prior": 0.5,
} "probability_threshold": 1.0,
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "on") hass.states.async_set("sensor.test_monitored", "on")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert round(abs(1.0 - state.attributes.get("probability")), 7) == 0 assert round(abs(1.0 - state.attributes.get("probability")), 7) == 0
assert state.state == "on" assert state.state == "on"
def test_multiple_observations(self):
"""Test sensor with multiple observations of same entity.""" async def test_multiple_observations(hass):
config = { """Test sensor with multiple observations of same entity."""
"binary_sensor": { config = {
"name": "Test_Binary", "binary_sensor": {
"platform": "bayesian", "name": "Test_Binary",
"observations": [ "platform": "bayesian",
{ "observations": [
"platform": "state", {
"entity_id": "sensor.test_monitored", "platform": "state",
"to_state": "blue", "entity_id": "sensor.test_monitored",
"prob_given_true": 0.8, "to_state": "blue",
"prob_given_false": 0.4, "prob_given_true": 0.8,
}, "prob_given_false": 0.4,
{ },
"platform": "state", {
"entity_id": "sensor.test_monitored", "platform": "state",
"to_state": "red", "entity_id": "sensor.test_monitored",
"prob_given_true": 0.2, "to_state": "red",
"prob_given_false": 0.4, "prob_given_true": 0.2,
}, "prob_given_false": 0.4,
], },
"prior": 0.2, ],
"probability_threshold": 0.32, "prior": 0.2,
} "probability_threshold": 0.32,
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
for key, attrs in state.attributes.items(): for key, attrs in state.attributes.items():
json.dumps(attrs) json.dumps(attrs)
assert [] == state.attributes.get("observations") assert [] == state.attributes.get("observations")
assert 0.2 == state.attributes.get("probability") assert 0.2 == state.attributes.get("probability")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", "blue") hass.states.async_set("sensor.test_monitored", "blue")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "blue") hass.states.async_set("sensor.test_monitored", "blue")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8
assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4 assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4
assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0 assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", "blue") hass.states.async_set("sensor.test_monitored", "blue")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "red") hass.states.async_set("sensor.test_monitored", "red")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert round(abs(0.11 - state.attributes.get("probability")), 7) == 0 assert round(abs(0.11 - state.attributes.get("probability")), 7) == 0
assert state.state == "off" assert state.state == "off"
def test_probability_updates(self):
"""Test probability update function."""
prob_given_true = [0.3, 0.6, 0.8]
prob_given_false = [0.7, 0.4, 0.2]
prior = 0.5
for pt, pf in zip(prob_given_true, prob_given_false): async def test_probability_updates(hass):
prior = bayesian.update_probability(prior, pt, pf) """Test probability update function."""
prob_given_true = [0.3, 0.6, 0.8]
prob_given_false = [0.7, 0.4, 0.2]
prior = 0.5
assert round(abs(0.720000 - prior), 7) == 0 for pt, pf in zip(prob_given_true, prob_given_false):
prior = bayesian.update_probability(prior, pt, pf)
prob_given_true = [0.8, 0.3, 0.9] assert round(abs(0.720000 - prior), 7) == 0
prob_given_false = [0.6, 0.4, 0.2]
prior = 0.7
for pt, pf in zip(prob_given_true, prob_given_false): prob_given_true = [0.8, 0.3, 0.9]
prior = bayesian.update_probability(prior, pt, pf) prob_given_false = [0.6, 0.4, 0.2]
prior = 0.7
assert round(abs(0.9130434782608695 - prior), 7) == 0 for pt, pf in zip(prob_given_true, prob_given_false):
prior = bayesian.update_probability(prior, pt, pf)
def test_observed_entities(self): assert round(abs(0.9130434782608695 - prior), 7) == 0
"""Test sensor on observed entities."""
config = {
"binary_sensor": { async def test_observed_entities(hass):
"name": "Test_Binary", """Test sensor on observed entities."""
"platform": "bayesian", config = {
"observations": [ "binary_sensor": {
{ "name": "Test_Binary",
"platform": "state", "platform": "bayesian",
"entity_id": "sensor.test_monitored", "observations": [
"to_state": "off", {
"prob_given_true": 0.9, "platform": "state",
"prob_given_false": 0.4, "entity_id": "sensor.test_monitored",
}, "to_state": "off",
{ "prob_given_true": 0.9,
"platform": "template", "prob_given_false": 0.4,
"value_template": "{{is_state('sensor.test_monitored1','on') and is_state('sensor.test_monitored','off')}}", },
"prob_given_true": 0.9, {
}, "platform": "template",
], "value_template": "{{is_state('sensor.test_monitored1','on') and is_state('sensor.test_monitored','off')}}",
"prior": 0.2, "prob_given_true": 0.9,
"probability_threshold": 0.32, },
} ],
"prior": 0.2,
"probability_threshold": 0.32,
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "on") hass.states.async_set("sensor.test_monitored", "on")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored1", "off") hass.states.async_set("sensor.test_monitored1", "off")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert [] == state.attributes.get("occurred_observation_entities") assert [] == state.attributes.get("occurred_observation_entities")
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert ["sensor.test_monitored"] == state.attributes.get( assert ["sensor.test_monitored"] == state.attributes.get(
"occurred_observation_entities" "occurred_observation_entities"
) )
self.hass.states.set("sensor.test_monitored1", "on") hass.states.async_set("sensor.test_monitored1", "on")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert ["sensor.test_monitored", "sensor.test_monitored1"] == sorted( assert ["sensor.test_monitored", "sensor.test_monitored1"] == sorted(
state.attributes.get("occurred_observation_entities") state.attributes.get("occurred_observation_entities")
) )
def test_state_attributes_are_serializable(self):
"""Test sensor on observed entities.""" async def test_state_attributes_are_serializable(hass):
config = { """Test sensor on observed entities."""
"binary_sensor": { config = {
"name": "Test_Binary", "binary_sensor": {
"platform": "bayesian", "name": "Test_Binary",
"observations": [ "platform": "bayesian",
{ "observations": [
"platform": "state", {
"entity_id": "sensor.test_monitored", "platform": "state",
"to_state": "off", "entity_id": "sensor.test_monitored",
"prob_given_true": 0.9, "to_state": "off",
"prob_given_false": 0.4, "prob_given_true": 0.9,
}, "prob_given_false": 0.4,
{ },
"platform": "template", {
"value_template": "{{is_state('sensor.test_monitored1','on') and is_state('sensor.test_monitored','off')}}", "platform": "template",
"prob_given_true": 0.9, "value_template": "{{is_state('sensor.test_monitored1','on') and is_state('sensor.test_monitored','off')}}",
}, "prob_given_true": 0.9,
], },
"prior": 0.2, ],
"probability_threshold": 0.32, "prior": 0.2,
} "probability_threshold": 0.32,
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", "on") hass.states.async_set("sensor.test_monitored", "on")
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored1", "off") hass.states.async_set("sensor.test_monitored1", "off")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert [] == state.attributes.get("occurred_observation_entities") assert [] == state.attributes.get("occurred_observation_entities")
self.hass.states.set("sensor.test_monitored", "off") hass.states.async_set("sensor.test_monitored", "off")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert ["sensor.test_monitored"] == state.attributes.get( assert ["sensor.test_monitored"] == state.attributes.get(
"occurred_observation_entities" "occurred_observation_entities"
) )
self.hass.states.set("sensor.test_monitored1", "on") hass.states.async_set("sensor.test_monitored1", "on")
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.test_binary") state = hass.states.get("binary_sensor.test_binary")
assert ["sensor.test_monitored", "sensor.test_monitored1"] == sorted( assert ["sensor.test_monitored", "sensor.test_monitored1"] == sorted(
state.attributes.get("occurred_observation_entities") state.attributes.get("occurred_observation_entities")
) )
for key, attrs in state.attributes.items(): for key, attrs in state.attributes.items():
json.dumps(attrs) json.dumps(attrs)
async def test_template_error(hass, caplog): async def test_template_error(hass, caplog):