2017-02-27 19:19:11 +00:00
|
|
|
"""Tests for MH-Z19 sensor."""
|
|
|
|
import unittest
|
2019-12-09 13:20:40 +00:00
|
|
|
from unittest.mock import DEFAULT, Mock, patch
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-03-19 06:07:39 +00:00
|
|
|
import homeassistant.components.mhz19.sensor as mhz19
|
2019-12-09 13:20:40 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN
|
2020-02-25 01:52:14 +00:00
|
|
|
from homeassistant.const import CONCENTRATION_PARTS_PER_MILLION, TEMP_FAHRENHEIT
|
2019-12-09 13:20:40 +00:00
|
|
|
from homeassistant.setup import setup_component
|
|
|
|
|
|
|
|
from tests.common import assert_setup_component, get_test_home_assistant
|
2017-02-27 19:19:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestMHZ19Sensor(unittest.TestCase):
|
|
|
|
"""Test the MH-Z19 sensor."""
|
|
|
|
|
|
|
|
hass = None
|
|
|
|
|
|
|
|
def setup_method(self, method):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2017-02-27 19:19:11 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_setup_missing_config(self):
|
|
|
|
"""Test setup with configuration missing required entries."""
|
|
|
|
with assert_setup_component(0):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(self.hass, DOMAIN, {"sensor": {"platform": "mhz19"}})
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("pmsensor.co2sensor.read_mh_z19", side_effect=OSError("test error"))
|
2017-02-27 19:19:11 +00:00
|
|
|
def test_setup_failed_connect(self, mock_co2):
|
|
|
|
"""Test setup when connection error occurs."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not mhz19.setup_platform(
|
|
|
|
self.hass,
|
|
|
|
{"platform": "mhz19", mhz19.CONF_SERIAL_DEVICE: "test.serial"},
|
|
|
|
None,
|
|
|
|
)
|
2017-02-27 19:19:11 +00:00
|
|
|
|
|
|
|
def test_setup_connected(self):
|
|
|
|
"""Test setup when connection succeeds."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.multiple(
|
|
|
|
"pmsensor.co2sensor",
|
|
|
|
read_mh_z19=DEFAULT,
|
|
|
|
read_mh_z19_with_temperature=DEFAULT,
|
|
|
|
):
|
2017-02-27 19:19:11 +00:00
|
|
|
from pmsensor.co2sensor import read_mh_z19_with_temperature
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-02-27 19:19:11 +00:00
|
|
|
read_mh_z19_with_temperature.return_value = None
|
|
|
|
mock_add = Mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mhz19.setup_platform(
|
|
|
|
self.hass,
|
|
|
|
{
|
|
|
|
"platform": "mhz19",
|
|
|
|
"monitored_conditions": ["co2", "temperature"],
|
|
|
|
mhz19.CONF_SERIAL_DEVICE: "test.serial",
|
|
|
|
},
|
|
|
|
mock_add,
|
|
|
|
)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert mock_add.call_count == 1
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch(
|
|
|
|
"pmsensor.co2sensor.read_mh_z19_with_temperature",
|
|
|
|
side_effect=OSError("test error"),
|
|
|
|
)
|
2018-03-15 20:49:49 +00:00
|
|
|
def aiohttp_client_update_oserror(self, mock_function):
|
2017-02-27 19:19:11 +00:00
|
|
|
"""Test MHZClient when library throws OSError."""
|
|
|
|
from pmsensor import co2sensor
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
client = mhz19.MHZClient(co2sensor, "test.serial")
|
2017-02-27 19:19:11 +00:00
|
|
|
client.update()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert {} == client.data
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(5001, 24))
|
2018-03-15 20:49:49 +00:00
|
|
|
def aiohttp_client_update_ppm_overflow(self, mock_function):
|
2017-02-27 19:19:11 +00:00
|
|
|
"""Test MHZClient when ppm is too high."""
|
|
|
|
from pmsensor import co2sensor
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
client = mhz19.MHZClient(co2sensor, "test.serial")
|
2017-02-27 19:19:11 +00:00
|
|
|
client.update()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert client.data.get("co2") is None
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
|
2018-03-15 20:49:49 +00:00
|
|
|
def aiohttp_client_update_good_read(self, mock_function):
|
2017-02-27 19:19:11 +00:00
|
|
|
"""Test MHZClient when ppm is too high."""
|
|
|
|
from pmsensor import co2sensor
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
client = mhz19.MHZClient(co2sensor, "test.serial")
|
2017-02-27 19:19:11 +00:00
|
|
|
client.update()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert {"temperature": 24, "co2": 1000} == client.data
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
|
2017-02-27 19:19:11 +00:00
|
|
|
def test_co2_sensor(self, mock_function):
|
|
|
|
"""Test CO2 sensor."""
|
|
|
|
from pmsensor import co2sensor
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
client = mhz19.MHZClient(co2sensor, "test.serial")
|
|
|
|
sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_CO2, None, "name")
|
2017-02-27 19:19:11 +00:00
|
|
|
sensor.update()
|
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert sensor.name == "name: CO2"
|
|
|
|
assert sensor.state == 1000
|
|
|
|
assert sensor.unit_of_measurement == CONCENTRATION_PARTS_PER_MILLION
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sensor.should_poll
|
2020-04-06 10:51:48 +00:00
|
|
|
assert sensor.device_state_attributes == {"temperature": 24}
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
|
2017-02-27 19:19:11 +00:00
|
|
|
def test_temperature_sensor(self, mock_function):
|
|
|
|
"""Test temperature sensor."""
|
|
|
|
from pmsensor import co2sensor
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
client = mhz19.MHZClient(co2sensor, "test.serial")
|
|
|
|
sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_TEMPERATURE, None, "name")
|
2017-02-27 19:19:11 +00:00
|
|
|
sensor.update()
|
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert sensor.name == "name: Temperature"
|
|
|
|
assert sensor.state == 24
|
|
|
|
assert sensor.unit_of_measurement == "°C"
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sensor.should_poll
|
2020-04-06 10:51:48 +00:00
|
|
|
assert sensor.device_state_attributes == {"co2_concentration": 1000}
|
2017-02-27 19:19:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
|
2017-02-27 19:19:11 +00:00
|
|
|
def test_temperature_sensor_f(self, mock_function):
|
|
|
|
"""Test temperature sensor."""
|
|
|
|
from pmsensor import co2sensor
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
client = mhz19.MHZClient(co2sensor, "test.serial")
|
2017-02-27 19:19:11 +00:00
|
|
|
sensor = mhz19.MHZ19Sensor(
|
2019-07-31 19:25:30 +00:00
|
|
|
client, mhz19.SENSOR_TEMPERATURE, TEMP_FAHRENHEIT, "name"
|
|
|
|
)
|
2017-02-27 19:19:11 +00:00
|
|
|
sensor.update()
|
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert sensor.state == 75.2
|