mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 14:47:38 +00:00
Rewrite bayesian unittest tests to pytest style test functions. (#41740)
This commit is contained in:
parent
6f113e981b
commit
aabf26bbc8
@ -1,7 +1,6 @@
|
||||
"""The test for the bayesian sensor platform."""
|
||||
import json
|
||||
from os import path
|
||||
import unittest
|
||||
|
||||
from homeassistant import config as hass_config
|
||||
from homeassistant.components.bayesian import DOMAIN, binary_sensor as bayesian
|
||||
@ -17,24 +16,12 @@ from homeassistant.const import (
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
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.common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestBayesianBinarySensor(unittest.TestCase):
|
||||
"""Test the threshold sensor."""
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_load_values_when_added_to_hass(self):
|
||||
async def test_load_values_when_added_to_hass(hass):
|
||||
"""Test that sensor initializes with observations of relevant entities."""
|
||||
|
||||
config = {
|
||||
@ -55,17 +42,18 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "off")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, "binary_sensor", config)
|
||||
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_false"] == 0.4
|
||||
|
||||
def test_unknown_state_does_not_influence_probability(self):
|
||||
|
||||
async def test_unknown_state_does_not_influence_probability(hass):
|
||||
"""Test that an unknown state does not change the output probability."""
|
||||
|
||||
config = {
|
||||
@ -86,16 +74,17 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", STATE_UNKNOWN)
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", STATE_UNKNOWN)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, "binary_sensor", config)
|
||||
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") == []
|
||||
|
||||
def test_sensor_numeric_state(self):
|
||||
|
||||
async def test_sensor_numeric_state(hass):
|
||||
"""Test sensor on numeric state platform observations."""
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
@ -122,28 +111,28 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, "binary_sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", 4)
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", 4)
|
||||
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 0.2 == state.attributes.get("probability")
|
||||
|
||||
assert state.state == "off"
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", 6)
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored", 4)
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored", 6)
|
||||
self.hass.states.set("sensor.test_monitored1", 6)
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", 6)
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", 4)
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", 6)
|
||||
hass.states.async_set("sensor.test_monitored1", 6)
|
||||
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")[1]["prob_given_true"] == 0.9
|
||||
assert state.attributes.get("observations")[1]["prob_given_false"] == 0.1
|
||||
@ -151,25 +140,26 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
|
||||
assert state.state == "on"
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", 6)
|
||||
self.hass.states.set("sensor.test_monitored1", 0)
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored", 4)
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", 6)
|
||||
hass.states.async_set("sensor.test_monitored1", 0)
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", 4)
|
||||
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 state.state == "off"
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", 15)
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", 15)
|
||||
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"
|
||||
|
||||
def test_sensor_state(self):
|
||||
|
||||
async def test_sensor_state(hass):
|
||||
"""Test sensor on state platform observations."""
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
@ -189,43 +179,44 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, "binary_sensor", config)
|
||||
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 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()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "on")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
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_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()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "on")
|
||||
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 state.state == "off"
|
||||
|
||||
def test_sensor_value_template(self):
|
||||
|
||||
async def test_sensor_value_template(hass):
|
||||
"""Test sensor on template platform observations."""
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
@ -244,43 +235,44 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, "binary_sensor", config)
|
||||
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 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()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "on")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
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_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()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "on")
|
||||
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 state.state == "off"
|
||||
|
||||
def test_threshold(self):
|
||||
|
||||
async def test_threshold(hass):
|
||||
"""Test sensor on probability threshold limits."""
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
@ -299,18 +291,19 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, "binary_sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "on")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "on")
|
||||
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 state.state == "on"
|
||||
|
||||
def test_multiple_observations(self):
|
||||
|
||||
async def test_multiple_observations(hass):
|
||||
"""Test sensor with multiple observations of same entity."""
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
@ -337,12 +330,12 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, "binary_sensor", config)
|
||||
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():
|
||||
json.dumps(attrs)
|
||||
@ -351,14 +344,14 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
|
||||
assert state.state == "off"
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "blue")
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored", "off")
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored", "blue")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "blue")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "blue")
|
||||
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_false"] == 0.4
|
||||
@ -366,17 +359,18 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
|
||||
assert state.state == "on"
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "blue")
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored", "red")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "blue")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "red")
|
||||
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 state.state == "off"
|
||||
|
||||
def test_probability_updates(self):
|
||||
|
||||
async def test_probability_updates(hass):
|
||||
"""Test probability update function."""
|
||||
prob_given_true = [0.3, 0.6, 0.8]
|
||||
prob_given_false = [0.7, 0.4, 0.2]
|
||||
@ -396,7 +390,8 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
|
||||
assert round(abs(0.9130434782608695 - prior), 7) == 0
|
||||
|
||||
def test_observed_entities(self):
|
||||
|
||||
async def test_observed_entities(hass):
|
||||
"""Test sensor on observed entities."""
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
@ -421,34 +416,35 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, "binary_sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "on")
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored1", "off")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "on")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored1", "off")
|
||||
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")
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "off")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
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(
|
||||
"occurred_observation_entities"
|
||||
)
|
||||
|
||||
self.hass.states.set("sensor.test_monitored1", "on")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored1", "on")
|
||||
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(
|
||||
state.attributes.get("occurred_observation_entities")
|
||||
)
|
||||
|
||||
def test_state_attributes_are_serializable(self):
|
||||
|
||||
async def test_state_attributes_are_serializable(hass):
|
||||
"""Test sensor on observed entities."""
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
@ -473,29 +469,29 @@ class TestBayesianBinarySensor(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, "binary_sensor", config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, "binary_sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "on")
|
||||
self.hass.block_till_done()
|
||||
self.hass.states.set("sensor.test_monitored1", "off")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "on")
|
||||
await hass.async_block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored1", "off")
|
||||
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")
|
||||
|
||||
self.hass.states.set("sensor.test_monitored", "off")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored", "off")
|
||||
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(
|
||||
"occurred_observation_entities"
|
||||
)
|
||||
|
||||
self.hass.states.set("sensor.test_monitored1", "on")
|
||||
self.hass.block_till_done()
|
||||
hass.states.async_set("sensor.test_monitored1", "on")
|
||||
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(
|
||||
state.attributes.get("occurred_observation_entities")
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user