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
This commit is contained in:
1 parent
929d49ed6f
commit
f892c3394b
10 files changed
+608
No files matched your search
@@ -0,0 +1,125 @@
|
||||
"""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)
|
||||
@@ -0,0 +1,85 @@
|
||||
"""The tests for the Canary component."""
|
||||
import unittest
|
||||
from unittest.mock import patch, MagicMock, PropertyMock
|
||||
|
||||
import homeassistant.components.canary as canary
|
||||
from homeassistant import setup
|
||||
from tests.common import (
|
||||
get_test_home_assistant)
|
||||
|
||||
|
||||
def mock_device(device_id, name, is_online=True):
|
||||
"""Mock Canary Device class."""
|
||||
device = MagicMock()
|
||||
type(device).device_id = PropertyMock(return_value=device_id)
|
||||
type(device).name = PropertyMock(return_value=name)
|
||||
type(device).is_online = PropertyMock(return_value=is_online)
|
||||
return device
|
||||
|
||||
|
||||
def mock_location(name, is_celsius=True, devices=[]):
|
||||
"""Mock Canary Location class."""
|
||||
location = MagicMock()
|
||||
type(location).name = PropertyMock(return_value=name)
|
||||
type(location).is_celsius = PropertyMock(return_value=is_celsius)
|
||||
type(location).devices = PropertyMock(return_value=devices)
|
||||
return location
|
||||
|
||||
|
||||
def mock_reading(sensor_type, sensor_value):
|
||||
"""Mock Canary Reading class."""
|
||||
reading = MagicMock()
|
||||
type(reading).sensor_type = PropertyMock(return_value=sensor_type)
|
||||
type(reading).value = PropertyMock(return_value=sensor_value)
|
||||
return reading
|
||||
|
||||
|
||||
class TestCanary(unittest.TestCase):
|
||||
"""Tests the Canary component."""
|
||||
|
||||
def setUp(self):
|
||||
"""Initialize values for this test case class."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@patch('homeassistant.components.canary.CanaryData.update')
|
||||
@patch('canary.api.Api.login')
|
||||
def test_setup_with_valid_config(self, mock_login, mock_update):
|
||||
"""Test setup component."""
|
||||
config = {
|
||||
"canary": {
|
||||
"username": "foo@bar.org",
|
||||
"password": "bar",
|
||||
}
|
||||
}
|
||||
|
||||
self.assertTrue(
|
||||
setup.setup_component(self.hass, canary.DOMAIN, config))
|
||||
|
||||
mock_update.assert_called_once_with()
|
||||
mock_login.assert_called_once_with()
|
||||
|
||||
def test_setup_with_missing_password(self):
|
||||
"""Test setup component."""
|
||||
config = {
|
||||
"canary": {
|
||||
"username": "foo@bar.org",
|
||||
}
|
||||
}
|
||||
|
||||
self.assertFalse(
|
||||
setup.setup_component(self.hass, canary.DOMAIN, config))
|
||||
|
||||
def test_setup_with_missing_username(self):
|
||||
"""Test setup component."""
|
||||
config = {
|
||||
"canary": {
|
||||
"password": "bar",
|
||||
}
|
||||
}
|
||||
|
||||
self.assertFalse(
|
||||
setup.setup_component(self.hass, canary.DOMAIN, config))
|
||||
Reference in new issue
Block a user