Rewrite mold_indicator unittest tests to pytest style test functions (#41598)

This commit is contained in:
CurrentThread 2020-10-16 13:37:15 +02:00 committed by GitHub
parent 965bb2c8df
commit 75f27d6318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
"""The tests for the MoldIndicator sensor.""" """The tests for the MoldIndicator sensor."""
import unittest import pytest
from homeassistant.components.mold_indicator.sensor import ( from homeassistant.components.mold_indicator.sensor import (
ATTR_CRITICAL_TEMP, ATTR_CRITICAL_TEMP,
@ -12,284 +12,280 @@ from homeassistant.const import (
STATE_UNKNOWN, STATE_UNKNOWN,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant
class TestSensorMoldIndicator(unittest.TestCase): @pytest.fixture(autouse=True)
"""Test the MoldIndicator sensor.""" def init_sensors_fixture(hass):
"""Set up things to be run when tests are started."""
hass.states.async_set(
"test.indoortemp", "20", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
hass.states.async_set(
"test.outdoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
hass.states.async_set(
"test.indoorhumidity", "50", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE}
)
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.hass.states.set(
"test.indoortemp", "20", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
self.hass.states.set(
"test.outdoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
self.hass.states.set(
"test.indoorhumidity", "50", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE}
)
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self): async def test_setup(hass):
"""Stop down everything that was started.""" """Test the mold indicator sensor setup."""
self.hass.stop() assert await async_setup_component(
hass,
sensor.DOMAIN,
{
"sensor": {
"platform": "mold_indicator",
"indoor_temp_sensor": "test.indoortemp",
"outdoor_temp_sensor": "test.outdoortemp",
"indoor_humidity_sensor": "test.indoorhumidity",
"calibration_factor": 2.0,
}
},
)
await hass.async_block_till_done()
moldind = hass.states.get("sensor.mold_indicator")
assert moldind
assert PERCENTAGE == moldind.attributes.get("unit_of_measurement")
def test_setup(self):
"""Test the mold indicator sensor setup."""
assert setup_component(
self.hass,
sensor.DOMAIN,
{
"sensor": {
"platform": "mold_indicator",
"indoor_temp_sensor": "test.indoortemp",
"outdoor_temp_sensor": "test.outdoortemp",
"indoor_humidity_sensor": "test.indoorhumidity",
"calibration_factor": 2.0,
}
},
)
self.hass.block_till_done()
moldind = self.hass.states.get("sensor.mold_indicator")
assert moldind
assert PERCENTAGE == moldind.attributes.get("unit_of_measurement")
def test_invalidcalib(self): async def test_invalidcalib(hass):
"""Test invalid sensor values.""" """Test invalid sensor values."""
self.hass.states.set( hass.states.async_set(
"test.indoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} "test.indoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
) )
self.hass.states.set( hass.states.async_set(
"test.outdoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} "test.outdoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
) )
self.hass.states.set( hass.states.async_set(
"test.indoorhumidity", "0", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE} "test.indoorhumidity", "0", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE}
) )
assert setup_component( assert await async_setup_component(
self.hass, hass,
sensor.DOMAIN, sensor.DOMAIN,
{ {
"sensor": { "sensor": {
"platform": "mold_indicator", "platform": "mold_indicator",
"indoor_temp_sensor": "test.indoortemp", "indoor_temp_sensor": "test.indoortemp",
"outdoor_temp_sensor": "test.outdoortemp", "outdoor_temp_sensor": "test.outdoortemp",
"indoor_humidity_sensor": "test.indoorhumidity", "indoor_humidity_sensor": "test.indoorhumidity",
"calibration_factor": 0, "calibration_factor": 0,
} }
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
moldind = self.hass.states.get("sensor.mold_indicator") moldind = hass.states.get("sensor.mold_indicator")
assert moldind assert moldind
assert moldind.state == "unavailable" assert moldind.state == "unavailable"
assert moldind.attributes.get(ATTR_DEWPOINT) is None assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
def test_invalidhum(self):
"""Test invalid sensor values."""
self.hass.states.set(
"test.indoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
self.hass.states.set(
"test.outdoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
self.hass.states.set(
"test.indoorhumidity", "-1", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE}
)
assert setup_component( async def test_invalidhum(hass):
self.hass, """Test invalid sensor values."""
sensor.DOMAIN, hass.states.async_set(
{ "test.indoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
"sensor": { )
"platform": "mold_indicator", hass.states.async_set(
"indoor_temp_sensor": "test.indoortemp", "test.outdoortemp", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
"outdoor_temp_sensor": "test.outdoortemp", )
"indoor_humidity_sensor": "test.indoorhumidity", hass.states.async_set(
"calibration_factor": 2.0, "test.indoorhumidity", "-1", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE}
} )
},
)
self.hass.block_till_done() assert await async_setup_component(
self.hass.start() hass,
self.hass.block_till_done() sensor.DOMAIN,
moldind = self.hass.states.get("sensor.mold_indicator") {
assert moldind "sensor": {
assert moldind.state == "unavailable" "platform": "mold_indicator",
assert moldind.attributes.get(ATTR_DEWPOINT) is None "indoor_temp_sensor": "test.indoortemp",
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None "outdoor_temp_sensor": "test.outdoortemp",
"indoor_humidity_sensor": "test.indoorhumidity",
"calibration_factor": 2.0,
}
},
)
self.hass.states.set( await hass.async_block_till_done()
"test.indoorhumidity", "A", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE} await hass.async_start()
) await hass.async_block_till_done()
self.hass.block_till_done() moldind = hass.states.get("sensor.mold_indicator")
moldind = self.hass.states.get("sensor.mold_indicator") assert moldind
assert moldind assert moldind.state == "unavailable"
assert moldind.state == "unavailable" assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_DEWPOINT) is None assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
self.hass.states.set( hass.states.async_set(
"test.indoorhumidity", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} "test.indoorhumidity", "A", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE}
) )
self.hass.block_till_done() await hass.async_block_till_done()
moldind = self.hass.states.get("sensor.mold_indicator") moldind = hass.states.get("sensor.mold_indicator")
assert moldind assert moldind
assert moldind.state == "unavailable" assert moldind.state == "unavailable"
assert moldind.attributes.get(ATTR_DEWPOINT) is None assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
def test_calculation(self): hass.states.async_set(
"""Test the mold indicator internal calculations.""" "test.indoorhumidity", "10", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
assert setup_component( )
self.hass, await hass.async_block_till_done()
sensor.DOMAIN, moldind = hass.states.get("sensor.mold_indicator")
{ assert moldind
"sensor": { assert moldind.state == "unavailable"
"platform": "mold_indicator", assert moldind.attributes.get(ATTR_DEWPOINT) is None
"indoor_temp_sensor": "test.indoortemp", assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
"outdoor_temp_sensor": "test.outdoortemp",
"indoor_humidity_sensor": "test.indoorhumidity",
"calibration_factor": 2.0,
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
moldind = self.hass.states.get("sensor.mold_indicator")
assert moldind
# assert dewpoint
dewpoint = moldind.attributes.get(ATTR_DEWPOINT)
assert dewpoint
assert dewpoint > 9.25
assert dewpoint < 9.26
# assert temperature estimation async def test_calculation(hass):
esttemp = moldind.attributes.get(ATTR_CRITICAL_TEMP) """Test the mold indicator internal calculations."""
assert esttemp assert await async_setup_component(
assert esttemp > 14.9 hass,
assert esttemp < 15.1 sensor.DOMAIN,
{
"sensor": {
"platform": "mold_indicator",
"indoor_temp_sensor": "test.indoortemp",
"outdoor_temp_sensor": "test.outdoortemp",
"indoor_humidity_sensor": "test.indoorhumidity",
"calibration_factor": 2.0,
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
moldind = hass.states.get("sensor.mold_indicator")
assert moldind
# assert mold indicator value # assert dewpoint
state = moldind.state dewpoint = moldind.attributes.get(ATTR_DEWPOINT)
assert state assert dewpoint
assert state == "68" assert dewpoint > 9.25
assert dewpoint < 9.26
def test_unknown_sensor(self): # assert temperature estimation
"""Test the sensor_changed function.""" esttemp = moldind.attributes.get(ATTR_CRITICAL_TEMP)
assert setup_component( assert esttemp
self.hass, assert esttemp > 14.9
sensor.DOMAIN, assert esttemp < 15.1
{
"sensor": {
"platform": "mold_indicator",
"indoor_temp_sensor": "test.indoortemp",
"outdoor_temp_sensor": "test.outdoortemp",
"indoor_humidity_sensor": "test.indoorhumidity",
"calibration_factor": 2.0,
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.states.set( # assert mold indicator value
"test.indoortemp", STATE_UNKNOWN, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} state = moldind.state
) assert state
self.hass.block_till_done() assert state == "68"
moldind = self.hass.states.get("sensor.mold_indicator")
assert moldind
assert moldind.state == "unavailable"
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
self.hass.states.set(
"test.indoortemp", "30", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
self.hass.states.set(
"test.outdoortemp", STATE_UNKNOWN, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
self.hass.block_till_done()
moldind = self.hass.states.get("sensor.mold_indicator")
assert moldind
assert moldind.state == "unavailable"
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
self.hass.states.set( async def test_unknown_sensor(hass):
"test.outdoortemp", "25", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} """Test the sensor_changed function."""
) assert await async_setup_component(
self.hass.states.set( hass,
"test.indoorhumidity", sensor.DOMAIN,
STATE_UNKNOWN, {
{ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE}, "sensor": {
) "platform": "mold_indicator",
self.hass.block_till_done() "indoor_temp_sensor": "test.indoortemp",
moldind = self.hass.states.get("sensor.mold_indicator") "outdoor_temp_sensor": "test.outdoortemp",
assert moldind "indoor_humidity_sensor": "test.indoorhumidity",
assert moldind.state == "unavailable" "calibration_factor": 2.0,
assert moldind.attributes.get(ATTR_DEWPOINT) is None }
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None },
)
await hass.async_block_till_done()
await hass.async_start()
self.hass.states.set( hass.states.async_set(
"test.indoorhumidity", "20", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE} "test.indoortemp", STATE_UNKNOWN, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
) )
self.hass.block_till_done() await hass.async_block_till_done()
moldind = self.hass.states.get("sensor.mold_indicator") moldind = hass.states.get("sensor.mold_indicator")
assert moldind assert moldind
assert moldind.state == "23" assert moldind.state == "unavailable"
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
dewpoint = moldind.attributes.get(ATTR_DEWPOINT) hass.states.async_set(
assert dewpoint "test.indoortemp", "30", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
assert dewpoint > 4.58 )
assert dewpoint < 4.59 hass.states.async_set(
"test.outdoortemp", STATE_UNKNOWN, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
await hass.async_block_till_done()
moldind = hass.states.get("sensor.mold_indicator")
assert moldind
assert moldind.state == "unavailable"
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
esttemp = moldind.attributes.get(ATTR_CRITICAL_TEMP) hass.states.async_set(
assert esttemp "test.outdoortemp", "25", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
assert esttemp == 27.5 )
hass.states.async_set(
"test.indoorhumidity",
STATE_UNKNOWN,
{ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE},
)
await hass.async_block_till_done()
moldind = hass.states.get("sensor.mold_indicator")
assert moldind
assert moldind.state == "unavailable"
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
def test_sensor_changed(self): hass.states.async_set(
"""Test the sensor_changed function.""" "test.indoorhumidity", "20", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE}
assert setup_component( )
self.hass, await hass.async_block_till_done()
sensor.DOMAIN, moldind = hass.states.get("sensor.mold_indicator")
{ assert moldind
"sensor": { assert moldind.state == "23"
"platform": "mold_indicator",
"indoor_temp_sensor": "test.indoortemp",
"outdoor_temp_sensor": "test.outdoortemp",
"indoor_humidity_sensor": "test.indoorhumidity",
"calibration_factor": 2.0,
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.states.set( dewpoint = moldind.attributes.get(ATTR_DEWPOINT)
"test.indoortemp", "30", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} assert dewpoint
) assert dewpoint > 4.58
self.hass.block_till_done() assert dewpoint < 4.59
assert self.hass.states.get("sensor.mold_indicator").state == "90"
self.hass.states.set( esttemp = moldind.attributes.get(ATTR_CRITICAL_TEMP)
"test.outdoortemp", "25", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} assert esttemp
) assert esttemp == 27.5
self.hass.block_till_done()
assert self.hass.states.get("sensor.mold_indicator").state == "57"
self.hass.states.set(
"test.indoorhumidity", "20", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE} async def test_sensor_changed(hass):
) """Test the sensor_changed function."""
self.hass.block_till_done() assert await async_setup_component(
assert self.hass.states.get("sensor.mold_indicator").state == "23" hass,
sensor.DOMAIN,
{
"sensor": {
"platform": "mold_indicator",
"indoor_temp_sensor": "test.indoortemp",
"outdoor_temp_sensor": "test.outdoortemp",
"indoor_humidity_sensor": "test.indoorhumidity",
"calibration_factor": 2.0,
}
},
)
await hass.async_block_till_done()
await hass.async_start()
hass.states.async_set(
"test.indoortemp", "30", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
await hass.async_block_till_done()
assert hass.states.get("sensor.mold_indicator").state == "90"
hass.states.async_set(
"test.outdoortemp", "25", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}
)
await hass.async_block_till_done()
assert hass.states.get("sensor.mold_indicator").state == "57"
hass.states.async_set(
"test.indoorhumidity", "20", {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE}
)
await hass.async_block_till_done()
assert hass.states.get("sensor.mold_indicator").state == "23"