2017-12-08 09:40:45 +00:00
|
|
|
"""The tests for the Canary component."""
|
|
|
|
import unittest
|
2019-12-09 17:56:21 +00:00
|
|
|
from unittest.mock import MagicMock, PropertyMock, patch
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
from homeassistant import setup
|
2019-12-09 17:56:21 +00:00
|
|
|
import homeassistant.components.canary as canary
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
|
2018-03-30 21:38:29 +00:00
|
|
|
def mock_device(device_id, name, is_online=True, device_type_name=None):
|
2017-12-08 09:40:45 +00:00
|
|
|
"""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)
|
2019-07-31 19:25:30 +00:00
|
|
|
type(device).device_type = PropertyMock(
|
|
|
|
return_value={"id": 1, "name": device_type_name}
|
|
|
|
)
|
2017-12-08 09:40:45 +00:00
|
|
|
return device
|
|
|
|
|
|
|
|
|
2018-01-30 22:44:05 +00:00
|
|
|
def mock_location(name, is_celsius=True, devices=None):
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Mock Canary Location class."""
|
|
|
|
location = MagicMock()
|
|
|
|
type(location).name = PropertyMock(return_value=name)
|
|
|
|
type(location).is_celsius = PropertyMock(return_value=is_celsius)
|
2018-01-30 22:44:05 +00:00
|
|
|
type(location).devices = PropertyMock(return_value=devices or [])
|
2017-12-08 09:40:45 +00:00
|
|
|
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()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("homeassistant.components.canary.CanaryData.update")
|
|
|
|
@patch("canary.api.Api.login")
|
2017-12-08 09:40:45 +00:00
|
|
|
def test_setup_with_valid_config(self, mock_login, mock_update):
|
|
|
|
"""Test setup component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"canary": {"username": "foo@bar.org", "password": "bar"}}
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert setup.setup_component(self.hass, canary.DOMAIN, config)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
mock_update.assert_called_once_with()
|
|
|
|
mock_login.assert_called_once_with()
|
|
|
|
|
|
|
|
def test_setup_with_missing_password(self):
|
|
|
|
"""Test setup component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"canary": {"username": "foo@bar.org"}}
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not setup.setup_component(self.hass, canary.DOMAIN, config)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
def test_setup_with_missing_username(self):
|
|
|
|
"""Test setup component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"canary": {"password": "bar"}}
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not setup.setup_component(self.hass, canary.DOMAIN, config)
|