hass-core/tests/components/sensor/test_canary.py
Joe Lu f892c3394b Add support for Canary component and platforms (#10306)
* Add Canary component

* Made some change to how canary data is updated and stored

* Updated to use py-canary:0.1.2

* Addressed flake8 warnings

* Import canary API locally

* Import canary API locally again

* Addressed pylint errors

* Updated requirements_all.txt

* Fixed incorrect unit of measurement for air quality sensor

* Added tests for Canary component and sensors

* Updated canary component to handle exception better when initializing

* Fixed tests

* Fixed tests again

* Addressed review comments

* Fixed houndci error

* Addressed comment about camera force update

* Addressed comment regarding timeout when fetching camera image

* Updated to use py-canary==0.2.2

* Increased update frequency to 30 seconds

* Added support for Canary alarm control panel

* Address review comments

* Fixed houndci error

* Fixed lint errors

* Updated test to only test setup component / platform

* Fixed flake error

* Fixed failing test

* Uptake py-canary:0.2.3

* canary.alarm_control_panel DISARM is now mapped to canary PRIVACY mode

* Fixed failing tests

* Removed unnecessary methods

* Removed polling in canary camera component and update camera info when getting camera image

* Added more tests to cover Canary sensors

* Address review comments

* Addressed review comment in tests

* Fixed pylint errors

* Excluded canary alarm_control_panel and camera from coverage calculation
2017-12-08 10:40:45 +01:00

125 lines
4.3 KiB
Python

"""The tests for the Canary sensor platform."""
import copy
import unittest
from unittest.mock import patch, Mock
from canary.api import SensorType
from homeassistant.components import canary as base_canary
from homeassistant.components.canary import DATA_CANARY
from homeassistant.components.sensor import canary
from homeassistant.components.sensor.canary import CanarySensor
from tests.common import (get_test_home_assistant)
from tests.components.test_canary import mock_device, mock_reading, \
mock_location
VALID_CONFIG = {
"canary": {
"username": "foo@bar.org",
"password": "bar",
}
}
class TestCanarySensorSetup(unittest.TestCase):
"""Test the Canary platform."""
DEVICES = []
def add_devices(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()
@patch('homeassistant.components.canary.CanaryData')
def test_setup_sensors(self, mock_canary):
"""Test the sensor setup."""
base_canary.setup(self.hass, self.config)
online_device_at_home = mock_device(20, "Dining Room", True)
offline_device_at_home = mock_device(21, "Front Yard", False)
online_device_at_work = mock_device(22, "Office", True)
self.hass.data[DATA_CANARY] = mock_canary()
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_devices, None)
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):
"""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)]
sensor = CanarySensor(data, SensorType.TEMPERATURE, 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)
def test_humidity_sensor(self):
"""Test humidity sensor."""
device = mock_device(10, "Family Room")
location = mock_location("Home")
data = Mock()
data.get_readings.return_value = [
mock_reading(SensorType.HUMIDITY, 50.4567)]
sensor = CanarySensor(data, SensorType.HUMIDITY, location, device)
sensor.update()
self.assertEqual("Home Family Room Humidity", sensor.name)
self.assertEqual("%", sensor.unit_of_measurement)
self.assertEqual(50.5, sensor.state)
def test_air_quality_sensor(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)]
sensor = CanarySensor(data, SensorType.AIR_QUALITY, 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)