hass-core/tests/components/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

85 lines
2.6 KiB
Python

"""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))