Rewrite threshold unittest tests to pytest style test functions (#41141)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Oren 2020-10-04 22:38:21 +03:00 committed by GitHub
parent f47d58d9c2
commit dd26ab6b4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,384 +1,385 @@
"""The test for the threshold sensor platform.""" """The test for the threshold sensor platform."""
import unittest
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, TEMP_CELSIUS from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, TEMP_CELSIUS
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant
class TestThresholdSensor(unittest.TestCase): async def test_sensor_upper(hass):
"""Test the threshold sensor.""" """Test if source is above threshold."""
config = {
def setup_method(self, method): "binary_sensor": {
"""Set up things to be run when tests are started.""" "platform": "threshold",
self.hass = get_test_home_assistant() "upper": "15",
"entity_id": "sensor.test_monitored",
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_sensor_upper(self):
"""Test if source is above threshold."""
config = {
"binary_sensor": {
"platform": "threshold",
"upper": "15",
"entity_id": "sensor.test_monitored",
}
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.states.set( hass.states.async_set(
"sensor.test_monitored", 16, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} "sensor.test_monitored", 16, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
) )
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "sensor.test_monitored" == state.attributes.get("entity_id") assert "sensor.test_monitored" == state.attributes.get("entity_id")
assert 16 == state.attributes.get("sensor_value") assert 16 == state.attributes.get("sensor_value")
assert "above" == state.attributes.get("position") assert "above" == state.attributes.get("position")
assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper") assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper")
assert 0.0 == state.attributes.get("hysteresis") assert 0.0 == state.attributes.get("hysteresis")
assert "upper" == state.attributes.get("type") assert "upper" == state.attributes.get("type")
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", 14) hass.states.async_set("sensor.test_monitored", 14)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
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.threshold") state = hass.states.get("binary_sensor.threshold")
assert state.state == "off" assert state.state == "off"
def test_sensor_lower(self):
"""Test if source is below threshold.""" async def test_sensor_lower(hass):
config = { """Test if source is below threshold."""
"binary_sensor": { config = {
"platform": "threshold", "binary_sensor": {
"lower": "15", "platform": "threshold",
"entity_id": "sensor.test_monitored", "lower": "15",
} "entity_id": "sensor.test_monitored",
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", 16) hass.states.async_set("sensor.test_monitored", 16)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "above" == state.attributes.get("position") assert "above" == state.attributes.get("position")
assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower") assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower")
assert 0.0 == state.attributes.get("hysteresis") assert 0.0 == state.attributes.get("hysteresis")
assert "lower" == state.attributes.get("type") assert "lower" == state.attributes.get("type")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 14) hass.states.async_set("sensor.test_monitored", 14)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert state.state == "on" assert state.state == "on"
def test_sensor_hysteresis(self):
"""Test if source is above threshold using hysteresis.""" async def test_sensor_hysteresis(hass):
config = { """Test if source is above threshold using hysteresis."""
"binary_sensor": { config = {
"platform": "threshold", "binary_sensor": {
"upper": "15", "platform": "threshold",
"hysteresis": "2.5", "upper": "15",
"entity_id": "sensor.test_monitored", "hysteresis": "2.5",
} "entity_id": "sensor.test_monitored",
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", 20) hass.states.async_set("sensor.test_monitored", 20)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "above" == state.attributes.get("position") assert "above" == state.attributes.get("position")
assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper") assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper")
assert 2.5 == state.attributes.get("hysteresis") assert 2.5 == state.attributes.get("hysteresis")
assert "upper" == state.attributes.get("type") assert "upper" == state.attributes.get("type")
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", 13) hass.states.async_set("sensor.test_monitored", 13)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", 12) hass.states.async_set("sensor.test_monitored", 12)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 17) hass.states.async_set("sensor.test_monitored", 17)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 18) hass.states.async_set("sensor.test_monitored", 18)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert state.state == "on" assert state.state == "on"
def test_sensor_in_range_no_hysteresis(self):
"""Test if source is within the range.""" async def test_sensor_in_range_no_hysteresis(hass):
config = { """Test if source is within the range."""
"binary_sensor": { config = {
"platform": "threshold", "binary_sensor": {
"lower": "10", "platform": "threshold",
"upper": "20", "lower": "10",
"entity_id": "sensor.test_monitored", "upper": "20",
} "entity_id": "sensor.test_monitored",
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.states.set( hass.states.async_set(
"sensor.test_monitored", 16, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} "sensor.test_monitored", 16, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
) )
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "sensor.test_monitored" == state.attributes.get("entity_id") assert state.attributes.get("entity_id") == "sensor.test_monitored"
assert 16 == state.attributes.get("sensor_value") assert 16 == state.attributes.get("sensor_value")
assert "in_range" == state.attributes.get("position") assert "in_range" == state.attributes.get("position")
assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower") assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower")
assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper") assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper")
assert 0.0 == state.attributes.get("hysteresis") assert 0.0 == state.attributes.get("hysteresis")
assert "range" == state.attributes.get("type") assert "range" == state.attributes.get("type")
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", 9) hass.states.async_set("sensor.test_monitored", 9)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "below" == state.attributes.get("position") assert "below" == state.attributes.get("position")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 21) hass.states.async_set("sensor.test_monitored", 21)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "above" == state.attributes.get("position") assert "above" == state.attributes.get("position")
assert state.state == "off" assert state.state == "off"
def test_sensor_in_range_with_hysteresis(self):
"""Test if source is within the range.""" async def test_sensor_in_range_with_hysteresis(hass):
config = { """Test if source is within the range."""
"binary_sensor": { config = {
"platform": "threshold", "binary_sensor": {
"lower": "10", "platform": "threshold",
"upper": "20", "lower": "10",
"hysteresis": "2", "upper": "20",
"entity_id": "sensor.test_monitored", "hysteresis": "2",
} "entity_id": "sensor.test_monitored",
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.states.set( hass.states.async_set(
"sensor.test_monitored", 16, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} "sensor.test_monitored", 16, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
) )
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "sensor.test_monitored" == state.attributes.get("entity_id") assert "sensor.test_monitored" == state.attributes.get("entity_id")
assert 16 == state.attributes.get("sensor_value") assert 16 == state.attributes.get("sensor_value")
assert "in_range" == state.attributes.get("position") assert "in_range" == state.attributes.get("position")
assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower") assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower")
assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper") assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper")
assert float(config["binary_sensor"]["hysteresis"]) == state.attributes.get( assert float(config["binary_sensor"]["hysteresis"]) == state.attributes.get(
"hysteresis" "hysteresis"
) )
assert "range" == state.attributes.get("type") assert "range" == state.attributes.get("type")
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", 8) hass.states.async_set("sensor.test_monitored", 8)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "in_range" == state.attributes.get("position") assert "in_range" == state.attributes.get("position")
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", 7) hass.states.async_set("sensor.test_monitored", 7)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "below" == state.attributes.get("position") assert "below" == state.attributes.get("position")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 12) hass.states.async_set("sensor.test_monitored", 12)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "below" == state.attributes.get("position") assert "below" == state.attributes.get("position")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 13) hass.states.async_set("sensor.test_monitored", 13)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "in_range" == state.attributes.get("position") assert "in_range" == state.attributes.get("position")
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", 22) hass.states.async_set("sensor.test_monitored", 22)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "in_range" == state.attributes.get("position") assert "in_range" == state.attributes.get("position")
assert state.state == "on" assert state.state == "on"
self.hass.states.set("sensor.test_monitored", 23) hass.states.async_set("sensor.test_monitored", 23)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "above" == state.attributes.get("position") assert "above" == state.attributes.get("position")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 18) hass.states.async_set("sensor.test_monitored", 18)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "above" == state.attributes.get("position") assert "above" == state.attributes.get("position")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 17) hass.states.async_set("sensor.test_monitored", 17)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "in_range" == state.attributes.get("position") assert "in_range" == state.attributes.get("position")
assert state.state == "on" assert state.state == "on"
def test_sensor_in_range_unknown_state(self):
"""Test if source is within the range.""" async def test_sensor_in_range_unknown_state(hass):
config = { """Test if source is within the range."""
"binary_sensor": { config = {
"platform": "threshold", "binary_sensor": {
"lower": "10", "platform": "threshold",
"upper": "20", "lower": "10",
"entity_id": "sensor.test_monitored", "upper": "20",
} "entity_id": "sensor.test_monitored",
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.states.set( hass.states.async_set(
"sensor.test_monitored", 16, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} "sensor.test_monitored", 16, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
) )
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "sensor.test_monitored" == state.attributes.get("entity_id") assert "sensor.test_monitored" == state.attributes.get("entity_id")
assert 16 == state.attributes.get("sensor_value") assert 16 == state.attributes.get("sensor_value")
assert "in_range" == state.attributes.get("position") assert "in_range" == state.attributes.get("position")
assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower") assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower")
assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper") assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper")
assert 0.0 == state.attributes.get("hysteresis") assert 0.0 == state.attributes.get("hysteresis")
assert "range" == state.attributes.get("type") assert "range" == state.attributes.get("type")
assert state.state == "on" assert state.state == "on"
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()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "unknown" == state.attributes.get("position") assert "unknown" == state.attributes.get("position")
assert state.state == "off" assert state.state == "off"
def test_sensor_lower_zero_threshold(self):
"""Test if a lower threshold of zero is set.""" async def test_sensor_lower_zero_threshold(hass):
config = { """Test if a lower threshold of zero is set."""
"binary_sensor": { config = {
"platform": "threshold", "binary_sensor": {
"lower": "0", "platform": "threshold",
"entity_id": "sensor.test_monitored", "lower": "0",
} "entity_id": "sensor.test_monitored",
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", 16) hass.states.async_set("sensor.test_monitored", 16)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "lower" == state.attributes.get("type") assert "lower" == state.attributes.get("type")
assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower") assert float(config["binary_sensor"]["lower"]) == state.attributes.get("lower")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", -3) hass.states.async_set("sensor.test_monitored", -3)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert state.state == "on" assert state.state == "on"
def test_sensor_upper_zero_threshold(self):
"""Test if an upper threshold of zero is set.""" async def test_sensor_upper_zero_threshold(hass):
config = { """Test if an upper threshold of zero is set."""
"binary_sensor": { config = {
"platform": "threshold", "binary_sensor": {
"upper": "0", "platform": "threshold",
"entity_id": "sensor.test_monitored", "upper": "0",
} "entity_id": "sensor.test_monitored",
} }
}
assert setup_component(self.hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.states.set("sensor.test_monitored", -10) hass.states.async_set("sensor.test_monitored", -10)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert "upper" == state.attributes.get("type") assert "upper" == state.attributes.get("type")
assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper") assert float(config["binary_sensor"]["upper"]) == state.attributes.get("upper")
assert state.state == "off" assert state.state == "off"
self.hass.states.set("sensor.test_monitored", 2) hass.states.async_set("sensor.test_monitored", 2)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.threshold") state = hass.states.get("binary_sensor.threshold")
assert state.state == "on" assert state.state == "on"