Fixed Canary temperature sensor and remapped air quality value (#11355)

* Fixed Canary temperature sensor and remapped air quality value

* Addressed review comment

* - Fixed canary tests and added more tests
- Removed py-canary requirements from tests

* Noop to trigger a build again

* - Removed py-canary requirements from tests

* Addressed PR comment

* - Updated tests
- Removed py-canary from gen_requirements_all.py

* - Fixed hound violation

* Added back py-canary to gen_requirements_all.py as it's still need in tests

* Added back py-canary to test requirements as it's still need in tests

* Address PR comment
This commit is contained in:
Joe Lu authored and Martin Hjelmare committed 2018-01-28 15:30:46 -08:00
1 parent c7efe5b7dd
commit b3bf6c4be2
3 files changed
+145 -56

No files matched your search

+83 -37
View File
@@ -3,13 +3,13 @@ import copy
import unittest
from unittest.mock import Mock
from canary.api import SensorType
from homeassistant.components.canary import DATA_CANARY
from homeassistant.components.sensor import canary
from homeassistant.components.sensor.canary import CanarySensor
from homeassistant.components.sensor.canary 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.test_canary import mock_device, mock_reading, \
mock_location
from tests.components.test_canary import mock_device, mock_location
VALID_CONFIG = {
"canary": {
@@ -55,38 +55,33 @@ class TestCanarySensorSetup(unittest.TestCase):
self.assertEqual(6, len(self.DEVICES))
def test_celsius_temperature_sensor(self):
"""Test temperature sensor with celsius."""
device = mock_device(10, "Family Room")
location = mock_location("Home", True)
data = Mock()
data.get_readings.return_value = [
mock_reading(SensorType.TEMPERATURE, 21.1234)]
sensor = CanarySensor(data, SensorType.TEMPERATURE, location, device)
sensor.update()
self.assertEqual("Home Family Room Temperature", sensor.name)
self.assertEqual("sensor_canary_10_temperature", sensor.unique_id)
self.assertEqual("°C", sensor.unit_of_measurement)
self.assertEqual(21.1, sensor.state)
def test_fahrenheit_temperature_sensor(self):
def test_temperature_sensor(self):
"""Test temperature sensor with fahrenheit."""
device = mock_device(10, "Family Room")
location = mock_location("Home", False)
data = Mock()
data.get_readings.return_value = [
mock_reading(SensorType.TEMPERATURE, 21.1567)]
data.get_reading.return_value = 21.1234
sensor = CanarySensor(data, SensorType.TEMPERATURE, location, device)
sensor = CanarySensor(data, SENSOR_TYPES[0], location, device)
sensor.update()
self.assertEqual("Home Family Room Temperature", sensor.name)
self.assertEqual("°F", sensor.unit_of_measurement)
self.assertEqual(21.2, sensor.state)
self.assertEqual("°C", sensor.unit_of_measurement)
self.assertEqual(21.12, sensor.state)
def test_temperature_sensor_with_none_sensor_value(self):
"""Test temperature sensor with fahrenheit."""
device = mock_device(10, "Family Room")
location = mock_location("Home", False)
data = Mock()
data.get_reading.return_value = None
sensor = CanarySensor(data, SENSOR_TYPES[0], location, device)
sensor.update()
self.assertEqual(None, sensor.state)
def test_humidity_sensor(self):
"""Test humidity sensor."""
@@ -94,28 +89,79 @@ class TestCanarySensorSetup(unittest.TestCase):
location = mock_location("Home")
data = Mock()
data.get_readings.return_value = [
mock_reading(SensorType.HUMIDITY, 50.4567)]
data.get_reading.return_value = 50.4567
sensor = CanarySensor(data, SensorType.HUMIDITY, location, device)
sensor = CanarySensor(data, SENSOR_TYPES[1], location, device)
sensor.update()
self.assertEqual("Home Family Room Humidity", sensor.name)
self.assertEqual("%", sensor.unit_of_measurement)
self.assertEqual(50.5, sensor.state)
self.assertEqual(50.46, sensor.state)
def test_air_quality_sensor(self):
def test_air_quality_sensor_with_very_abnormal_reading(self):
"""Test air quality sensor."""
device = mock_device(10, "Family Room")
location = mock_location("Home")
data = Mock()
data.get_readings.return_value = [
mock_reading(SensorType.AIR_QUALITY, 50.4567)]
data.get_reading.return_value = 0.4
sensor = CanarySensor(data, SensorType.AIR_QUALITY, location, device)
sensor = CanarySensor(data, SENSOR_TYPES[2], location, device)
sensor.update()
self.assertEqual("Home Family Room Air Quality", sensor.name)
self.assertEqual("", sensor.unit_of_measurement)
self.assertEqual(50.5, sensor.state)
self.assertEqual(None, sensor.unit_of_measurement)
self.assertEqual(0.4, sensor.state)
air_quality = sensor.device_state_attributes[ATTR_AIR_QUALITY]
self.assertEqual(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")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = 0.59
sensor = CanarySensor(data, SENSOR_TYPES[2], location, device)
sensor.update()
self.assertEqual("Home Family Room Air Quality", sensor.name)
self.assertEqual(None, sensor.unit_of_measurement)
self.assertEqual(0.59, sensor.state)
air_quality = sensor.device_state_attributes[ATTR_AIR_QUALITY]
self.assertEqual(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")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = 1.0
sensor = CanarySensor(data, SENSOR_TYPES[2], location, device)
sensor.update()
self.assertEqual("Home Family Room Air Quality", sensor.name)
self.assertEqual(None, sensor.unit_of_measurement)
self.assertEqual(1.0, sensor.state)
air_quality = sensor.device_state_attributes[ATTR_AIR_QUALITY]
self.assertEqual(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")
location = mock_location("Home")
data = Mock()
data.get_reading.return_value = None
sensor = CanarySensor(data, SENSOR_TYPES[2], location, device)
sensor.update()
self.assertEqual(None, sensor.state)
self.assertEqual(None, sensor.device_state_attributes)