core/tests/components/canary/test_sensor.py

205 lines
6.9 KiB
Python
Raw Normal View History

"""The tests for the Canary sensor platform."""
import copy
import unittest
2018-01-07 23:05:19 +00:00
from unittest.mock import Mock
from homeassistant.components.canary import DATA_CANARY
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
from homeassistant.components.canary import sensor as canary
from homeassistant.components.canary.sensor import CanarySensor, \
SENSOR_TYPES, ATTR_AIR_QUALITY, STATE_AIR_QUALITY_NORMAL, \
STATE_AIR_QUALITY_ABNORMAL, STATE_AIR_QUALITY_VERY_ABNORMAL
from tests.common import (get_test_home_assistant)
from tests.components.canary.test_init import mock_device, mock_location
VALID_CONFIG = {
"canary": {
"username": "foo@bar.org",
"password": "bar",
}
}
class TestCanarySensorSetup(unittest.TestCase):
"""Test the Canary platform."""
DEVICES = []
def add_entities(self, devices, action):
"""Mock add devices."""
for device in devices:
self.DEVICES.append(device)
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.config = copy.deepcopy(VALID_CONFIG)
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
2018-01-07 23:05:19 +00:00
def test_setup_sensors(self):
"""Test the sensor setup."""
online_device_at_home = mock_device(20, "Dining Room", True, "Canary")
offline_device_at_home = mock_device(21, "Front Yard", False, "Canary")
online_device_at_work = mock_device(22, "Office", True, "Canary")
2018-01-07 23:05:19 +00:00
self.hass.data[DATA_CANARY] = Mock()
self.hass.data[DATA_CANARY].locations = [
mock_location("Home", True, devices=[online_device_at_home,
offline_device_at_home]),
mock_location("Work", True, devices=[online_device_at_work]),
]
canary.setup_platform(self.hass, self.config, self.add_entities, None)
assert 6 == len(self.DEVICES)
def test_temperature_sensor(self):
"""Test temperature sensor with fahrenheit."""
device = mock_device(10, "Family Room", "Canary")
location = mock_location("Home", False)
data = Mock()
data.get_reading.return_value = 21.1234
sensor = CanarySensor(data, SENSOR_TYPES[0], location, device)
sensor.update()
assert "Home Family Room Temperature" == sensor.name
assert "°C" == sensor.unit_of_measurement
assert 21.12 == sensor.state
assert "mdi:thermometer" == sensor.icon
def test_temperature_sensor_with_none_sensor_value(self):
"""Test temperature sensor with fahrenheit."""
device = mock_device(10, "Family Room", "Canary")
location = mock_location("Home", False)
data = Mock()
data.get_reading.return_value = None
sensor = CanarySensor(data, SENSOR_TYPES[0], location, device)
sensor.update()
assert sensor.state is None
def test_humidity_sensor(self):
"""Test humidity sensor."""
device = mock_device(10, "Family Room", "Canary")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = 50.4567
sensor = CanarySensor(data, SENSOR_TYPES[1], location, device)
sensor.update()
assert "Home Family Room Humidity" == sensor.name
assert "%" == sensor.unit_of_measurement
assert 50.46 == sensor.state
assert "mdi:water-percent" == sensor.icon
def test_air_quality_sensor_with_very_abnormal_reading(self):
"""Test air quality sensor."""
device = mock_device(10, "Family Room", "Canary")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = 0.4
sensor = CanarySensor(data, SENSOR_TYPES[2], location, device)
sensor.update()
assert "Home Family Room Air Quality" == sensor.name
assert sensor.unit_of_measurement is None
assert 0.4 == sensor.state
assert "mdi:weather-windy" == sensor.icon
air_quality = sensor.device_state_attributes[ATTR_AIR_QUALITY]
assert STATE_AIR_QUALITY_VERY_ABNORMAL == air_quality
def test_air_quality_sensor_with_abnormal_reading(self):
"""Test air quality sensor."""
device = mock_device(10, "Family Room", "Canary")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = 0.59
sensor = CanarySensor(data, SENSOR_TYPES[2], location, device)
sensor.update()
assert "Home Family Room Air Quality" == sensor.name
assert sensor.unit_of_measurement is None
assert 0.59 == sensor.state
assert "mdi:weather-windy" == sensor.icon
air_quality = sensor.device_state_attributes[ATTR_AIR_QUALITY]
assert STATE_AIR_QUALITY_ABNORMAL == air_quality
def test_air_quality_sensor_with_normal_reading(self):
"""Test air quality sensor."""
device = mock_device(10, "Family Room", "Canary")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = 1.0
sensor = CanarySensor(data, SENSOR_TYPES[2], location, device)
sensor.update()
assert "Home Family Room Air Quality" == sensor.name
assert sensor.unit_of_measurement is None
assert 1.0 == sensor.state
assert "mdi:weather-windy" == sensor.icon
air_quality = sensor.device_state_attributes[ATTR_AIR_QUALITY]
assert STATE_AIR_QUALITY_NORMAL == air_quality
def test_air_quality_sensor_with_none_sensor_value(self):
"""Test air quality sensor."""
device = mock_device(10, "Family Room", "Canary")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = None
sensor = CanarySensor(data, SENSOR_TYPES[2], location, device)
sensor.update()
assert sensor.state is None
assert sensor.device_state_attributes is None
def test_battery_sensor(self):
"""Test battery sensor."""
device = mock_device(10, "Family Room", "Canary Flex")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = 70.4567
sensor = CanarySensor(data, SENSOR_TYPES[4], location, device)
sensor.update()
assert "Home Family Room Battery" == sensor.name
assert "%" == sensor.unit_of_measurement
assert 70.46 == sensor.state
assert "mdi:battery-70" == sensor.icon
def test_wifi_sensor(self):
"""Test battery sensor."""
device = mock_device(10, "Family Room", "Canary Flex")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = -57
sensor = CanarySensor(data, SENSOR_TYPES[3], location, device)
sensor.update()
assert "Home Family Room Wifi" == sensor.name
assert "dBm" == sensor.unit_of_measurement
assert -57 == sensor.state
assert "mdi:wifi" == sensor.icon