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

pull/41948/head
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
1 changed files with 251 additions and 255 deletions

View File

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