Best effort state initialization of bayesian binary sensor (#30962)
* Best effort state initialization of bayesian binary sensor. Why: * https://github.com/home-assistant/home-assistant/issues/30119 This change addresses the need by: * Running the main update logic eagerly for each entity being observed on `async_added_to_hass`. * Test of the new behavior. * Refactor in order to reduce number of methods with side effects that mutate instance attributes. * Improve test coverage Why: * Because for some reason my commits decreased test coverage. This change addresses the need by: * Adding coverage for the case where a device returns `STATE_UNKNOWN` * Adding coverage for configurations with templates * rebase and ensure upstream tests passed * Delete commented code from addressing merge conflict.
This commit is contained in:
1 parent
f2f03cf552
commit
dd1608db0d
2 files changed
+240
-88
No files matched your search
@@ -2,6 +2,7 @@
|
||||
import unittest
|
||||
|
||||
from homeassistant.components.bayesian import binary_sensor as bayesian
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.setup import setup_component
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
@@ -18,6 +19,65 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_load_values_when_added_to_hass(self):
|
||||
"""Test that sensor initializes with observations of relevant entities."""
|
||||
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
"name": "Test_Binary",
|
||||
"platform": "bayesian",
|
||||
"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")
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
|
||||
state = self.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_false"] == 0.4
|
||||
|
||||
def test_unknown_state_does_not_influence_probability(self):
|
||||
"""Test that an unknown state does not change the output probability."""
|
||||
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
"name": "Test_Binary",
|
||||
"platform": "bayesian",
|
||||
"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", STATE_UNKNOWN)
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
|
||||
state = self.hass.states.get("binary_sensor.test_binary")
|
||||
assert state.attributes.get("observations") == [None]
|
||||
|
||||
def test_sensor_numeric_state(self):
|
||||
"""Test sensor on numeric state platform observations."""
|
||||
config = {
|
||||
@@ -52,7 +112,7 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
|
||||
state = self.hass.states.get("binary_sensor.test_binary")
|
||||
|
||||
assert [] == state.attributes.get("observations")
|
||||
assert [None, None] == state.attributes.get("observations")
|
||||
assert 0.2 == state.attributes.get("probability")
|
||||
|
||||
assert state.state == "off"
|
||||
@@ -66,10 +126,9 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get("binary_sensor.test_binary")
|
||||
assert [
|
||||
{"prob_false": 0.4, "prob_true": 0.6},
|
||||
{"prob_false": 0.1, "prob_true": 0.9},
|
||||
] == state.attributes.get("observations")
|
||||
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_false"] == 0.1
|
||||
assert round(abs(0.77 - state.attributes.get("probability")), 7) == 0
|
||||
|
||||
assert state.state == "on"
|
||||
@@ -118,7 +177,7 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
|
||||
state = self.hass.states.get("binary_sensor.test_binary")
|
||||
|
||||
assert [] == state.attributes.get("observations")
|
||||
assert [None] == state.attributes.get("observations")
|
||||
assert 0.2 == state.attributes.get("probability")
|
||||
|
||||
assert state.state == "off"
|
||||
@@ -131,9 +190,62 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get("binary_sensor.test_binary")
|
||||
assert [{"prob_true": 0.8, "prob_false": 0.4}] == state.attributes.get(
|
||||
"observations"
|
||||
)
|
||||
assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8
|
||||
assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4
|
||||
assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0
|
||||
|
||||
assert state.state == "on"
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "off")
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored", "on")
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get("binary_sensor.test_binary")
|
||||
assert round(abs(0.2 - state.attributes.get("probability")), 7) == 0
|
||||
|
||||
assert state.state == "off"
|
||||
|
||||
def test_sensor_value_template(self):
|
||||
"""Test sensor on template platform observations."""
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
"name": "Test_Binary",
|
||||
"platform": "bayesian",
|
||||
"observations": [
|
||||
{
|
||||
"platform": "template",
|
||||
"value_template": "{{states('sensor.test_monitored') == 'off'}}",
|
||||
"prob_given_true": 0.8,
|
||||
"prob_given_false": 0.4,
|
||||
}
|
||||
],
|
||||
"prior": 0.2,
|
||||
"probability_threshold": 0.32,
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "on")
|
||||
|
||||
state = self.hass.states.get("binary_sensor.test_binary")
|
||||
|
||||
assert [None] == state.attributes.get("observations")
|
||||
assert 0.2 == state.attributes.get("probability")
|
||||
|
||||
assert state.state == "off"
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "off")
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored", "on")
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored", "off")
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.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_false"] == 0.4
|
||||
assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0
|
||||
|
||||
assert state.state == "on"
|
||||
@@ -210,7 +322,7 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
|
||||
state = self.hass.states.get("binary_sensor.test_binary")
|
||||
|
||||
assert [] == state.attributes.get("observations")
|
||||
assert [None, None] == state.attributes.get("observations")
|
||||
assert 0.2 == state.attributes.get("probability")
|
||||
|
||||
assert state.state == "off"
|
||||
@@ -223,9 +335,9 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get("binary_sensor.test_binary")
|
||||
assert [{"prob_true": 0.8, "prob_false": 0.4}] == state.attributes.get(
|
||||
"observations"
|
||||
)
|
||||
|
||||
assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8
|
||||
assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4
|
||||
assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0
|
||||
|
||||
assert state.state == "on"
|
||||
@@ -242,20 +354,20 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
|
||||
def test_probability_updates(self):
|
||||
"""Test probability update function."""
|
||||
prob_true = [0.3, 0.6, 0.8]
|
||||
prob_false = [0.7, 0.4, 0.2]
|
||||
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_true, prob_false):
|
||||
for pt, pf in zip(prob_given_true, prob_given_false):
|
||||
prior = bayesian.update_probability(prior, pt, pf)
|
||||
|
||||
assert round(abs(0.720000 - prior), 7) == 0
|
||||
|
||||
prob_true = [0.8, 0.3, 0.9]
|
||||
prob_false = [0.6, 0.4, 0.2]
|
||||
prob_given_true = [0.8, 0.3, 0.9]
|
||||
prob_given_false = [0.6, 0.4, 0.2]
|
||||
prior = 0.7
|
||||
|
||||
for pt, pf in zip(prob_true, prob_false):
|
||||
for pt, pf in zip(prob_given_true, prob_given_false):
|
||||
prior = bayesian.update_probability(prior, pt, pf)
|
||||
|
||||
assert round(abs(0.9130434782608695 - prior), 7) == 0
|
||||
@@ -271,7 +383,7 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
"platform": "state",
|
||||
"entity_id": "sensor.test_monitored",
|
||||
"to_state": "off",
|
||||
"prob_given_true": 0.8,
|
||||
"prob_given_true": 0.9,
|
||||
"prob_given_false": 0.4,
|
||||
},
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user