1
0
mirror of https://github.com/home-assistant/core.git synced 2025-08-18 01:40:11 +00:00
Files
.github
docs
homeassistant
script
tests
auth
components
air_quality
alarm_control_panel
alert
alexa
ambient_station
api
arlo
auth
automation
binary_sensor
calendar
camera
canary
cast
climate
cloud
config
configurator
conversation
counter
cover
daikin
datadog
deconz
default_config
demo
device_sun_light_trigger
device_tracker
dialogflow
discovery
duckdns
dyson
ecobee
emulated_hue
emulated_roku
esphome
fan
feedreader
ffmpeg
folder_watcher
freedns
fritzbox
frontend
geo_location
geofency
google
google_assistant
google_domains
google_pubsub
gpslogger
graphite
group
hangouts
hassio
history
history_graph
homekit
homekit_controller
homematicip_cloud
http
huawei_lte
hue
ifttt
image_processing
influxdb
init
input_boolean
input_datetime
input_number
input_select
input_text
intent_script
introduction
ios
ipma
kira
light
litejet
locative
lock
logbook
logentries
logger
lovelace
luftdaten
mailbox
mailgun
media_player
melissa
microsoft_face
mochad
mqtt
mqtt_eventstream
mqtt_statestream
mythicbeastsdns
namecheapdns
ness_alarm
nest
no_ip
notify
nuheat
onboarding
openuv
owntracks
panel_custom
panel_iframe
persistent_notification
person
pilight
plant
point
prometheus
proximity
ps4
python_script
qwikswitch
rainmachine
recorder
remember_the_milk
remote
rest_command
rflink
rfxtrx
ring
rss_feed_template
scene
script
sensor
__init__.py
test_api_streams.py
test_awair.py
test_bom.py
test_canary.py
test_coinmarketcap.py
test_command_line.py
test_darksky.py
test_dsmr.py
test_dte_energy_bridge.py
test_dyson.py
test_efergy.py
test_entur_public_transport.py
test_fail2ban.py
test_fido.py
test_file.py
test_filesize.py
test_filter.py
test_folder.py
test_foobot.py
test_geo_rss_events.py
test_google_wifi.py
test_hddtemp.py
test_history_stats.py
test_hydroquebec.py
test_imap_email_content.py
test_integration.py
test_islamic_prayer_times.py
test_jewish_calendar.py
test_london_air.py
test_mfi.py
test_mhz19.py
test_min_max.py
test_moldindicator.py
test_moon.py
test_mqtt_room.py
test_nsw_fuel_station.py
test_openhardwaremonitor.py
test_pilight.py
test_radarr.py
test_random.py
test_rest.py
test_rflink.py
test_rfxtrx.py
test_ring.py
test_rmvtransport.py
test_season.py
test_sigfox.py
test_simulated.py
test_sleepiq.py
test_sonarr.py
test_sql.py
test_srp_energy.py
test_startca.py
test_statistics.py
test_tcp.py
test_teksavvy.py
test_template.py
test_time_date.py
test_transport_nsw.py
test_uk_transport.py
test_uptime.py
test_version.py
test_vultr.py
test_worldclock.py
test_wsdot.py
test_wunderground.py
test_yr.py
test_yweather.py
shell_command
shopping_list
simplisafe
sleepiq
smartthings
smhi
snips
sonos
spaceapi
spc
splunk
statsd
sun
switch
system_health
system_log
tellduslive
timer
toon
tplink
tradfri
tts
twilio
unifi
updater
upnp
utility_meter
vacuum
verisure
vultr
wake_on_lan
water_heater
weather
webhook
weblink
webostv
websocket_api
xiaomi_miio
zha
zone
zwave
__init__.py
conftest.py
fixtures
helpers
mock
resources
scripts
test_util
testing_config
util
__init__.py
common.py
conftest.py
test_bootstrap.py
test_config.py
test_config_entries.py
test_core.py
test_data_entry_flow.py
test_loader.py
test_main.py
test_requirements.py
test_setup.py
virtualization
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hound.yml
.ignore
.readthedocs.yml
.travis.yml
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE.md
MANIFEST.in
README.rst
mypy.ini
pylintrc
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
setup.cfg
setup.py
tox.ini
core/tests/components/sensor/test_mhz19.py
Paulus Schoutsen 08fe7c3ece Pytest tests ()
* Convert core tests

* Convert component tests to use pytest assert

* Lint 🤷‍♂️

* Fix test

* Fix 3 typos in docs
2018-10-24 12:10:05 +02:00

122 lines
4.8 KiB
Python

"""Tests for MH-Z19 sensor."""
import unittest
from unittest.mock import patch, DEFAULT, Mock
from homeassistant.setup import setup_component
from homeassistant.components.sensor import DOMAIN
import homeassistant.components.sensor.mhz19 as mhz19
from homeassistant.const import TEMP_FAHRENHEIT
from tests.common import get_test_home_assistant, assert_setup_component
class TestMHZ19Sensor(unittest.TestCase):
"""Test the MH-Z19 sensor."""
hass = None
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_setup_missing_config(self):
"""Test setup with configuration missing required entries."""
with assert_setup_component(0):
assert setup_component(self.hass, DOMAIN, {
'sensor': {'platform': 'mhz19'}})
@patch('pmsensor.co2sensor.read_mh_z19', side_effect=OSError('test error'))
def test_setup_failed_connect(self, mock_co2):
"""Test setup when connection error occurs."""
assert not mhz19.setup_platform(self.hass, {
'platform': 'mhz19',
mhz19.CONF_SERIAL_DEVICE: 'test.serial',
}, None)
def test_setup_connected(self):
"""Test setup when connection succeeds."""
with patch.multiple('pmsensor.co2sensor', read_mh_z19=DEFAULT,
read_mh_z19_with_temperature=DEFAULT):
from pmsensor.co2sensor import read_mh_z19_with_temperature
read_mh_z19_with_temperature.return_value = None
mock_add = Mock()
assert mhz19.setup_platform(self.hass, {
'platform': 'mhz19',
'monitored_conditions': ['co2', 'temperature'],
mhz19.CONF_SERIAL_DEVICE: 'test.serial',
}, mock_add)
assert 1 == mock_add.call_count
@patch('pmsensor.co2sensor.read_mh_z19_with_temperature',
side_effect=OSError('test error'))
def aiohttp_client_update_oserror(self, mock_function):
"""Test MHZClient when library throws OSError."""
from pmsensor import co2sensor
client = mhz19.MHZClient(co2sensor, 'test.serial')
client.update()
assert {} == client.data
@patch('pmsensor.co2sensor.read_mh_z19_with_temperature',
return_value=(5001, 24))
def aiohttp_client_update_ppm_overflow(self, mock_function):
"""Test MHZClient when ppm is too high."""
from pmsensor import co2sensor
client = mhz19.MHZClient(co2sensor, 'test.serial')
client.update()
assert client.data.get('co2') is None
@patch('pmsensor.co2sensor.read_mh_z19_with_temperature',
return_value=(1000, 24))
def aiohttp_client_update_good_read(self, mock_function):
"""Test MHZClient when ppm is too high."""
from pmsensor import co2sensor
client = mhz19.MHZClient(co2sensor, 'test.serial')
client.update()
assert {'temperature': 24, 'co2': 1000} == client.data
@patch('pmsensor.co2sensor.read_mh_z19_with_temperature',
return_value=(1000, 24))
def test_co2_sensor(self, mock_function):
"""Test CO2 sensor."""
from pmsensor import co2sensor
client = mhz19.MHZClient(co2sensor, 'test.serial')
sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_CO2, None, 'name')
sensor.update()
assert 'name: CO2' == sensor.name
assert 1000 == sensor.state
assert 'ppm' == sensor.unit_of_measurement
assert sensor.should_poll
assert {'temperature': 24} == sensor.device_state_attributes
@patch('pmsensor.co2sensor.read_mh_z19_with_temperature',
return_value=(1000, 24))
def test_temperature_sensor(self, mock_function):
"""Test temperature sensor."""
from pmsensor import co2sensor
client = mhz19.MHZClient(co2sensor, 'test.serial')
sensor = mhz19.MHZ19Sensor(
client, mhz19.SENSOR_TEMPERATURE, None, 'name')
sensor.update()
assert 'name: Temperature' == sensor.name
assert 24 == sensor.state
assert '°C' == sensor.unit_of_measurement
assert sensor.should_poll
assert {'co2_concentration': 1000} == sensor.device_state_attributes
@patch('pmsensor.co2sensor.read_mh_z19_with_temperature',
return_value=(1000, 24))
def test_temperature_sensor_f(self, mock_function):
"""Test temperature sensor."""
from pmsensor import co2sensor
client = mhz19.MHZClient(co2sensor, 'test.serial')
sensor = mhz19.MHZ19Sensor(
client, mhz19.SENSOR_TEMPERATURE, TEMP_FAHRENHEIT, 'name')
sensor.update()
assert 75.2 == sensor.state