2016-03-09 09:25:50 +00:00
|
|
|
"""Test to verify that Home Assistant core works."""
|
2014-11-23 17:51:16 +00:00
|
|
|
# pylint: disable=protected-access,too-many-public-methods
|
2014-11-24 06:18:51 +00:00
|
|
|
# pylint: disable=too-few-public-methods
|
2016-09-30 19:57:24 +00:00
|
|
|
import asyncio
|
2014-11-23 17:51:16 +00:00
|
|
|
import unittest
|
2016-09-20 06:39:49 +00:00
|
|
|
from unittest.mock import patch, MagicMock
|
2015-09-13 05:56:49 +00:00
|
|
|
from datetime import datetime, timedelta
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
import pytz
|
|
|
|
|
2015-08-17 03:53:17 +00:00
|
|
|
import homeassistant.core as ha
|
2016-09-13 02:16:14 +00:00
|
|
|
from homeassistant.exceptions import InvalidEntityFormatError
|
2015-08-04 16:16:10 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-08-09 03:42:25 +00:00
|
|
|
from homeassistant.util.unit_system import (METRIC_SYSTEM)
|
2015-08-04 16:16:10 +00:00
|
|
|
from homeassistant.const import (
|
2016-10-05 05:19:12 +00:00
|
|
|
__version__, EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM)
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
PST = pytz.timezone('America/Los_Angeles')
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
def test_split_entity_id():
|
|
|
|
"""Test split_entity_id."""
|
|
|
|
assert ha.split_entity_id('domain.object_id') == ['domain', 'object_id']
|
2016-08-09 03:21:40 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
|
|
|
|
def test_async_add_job_schedule_callback():
|
|
|
|
"""Test that we schedule coroutines and add jobs to the job pool."""
|
|
|
|
hass = MagicMock()
|
|
|
|
job = MagicMock()
|
|
|
|
|
|
|
|
ha.HomeAssistant.async_add_job(hass, ha.callback(job))
|
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 1
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 0
|
|
|
|
assert len(hass.add_job.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
@patch('asyncio.iscoroutinefunction', return_value=True)
|
|
|
|
def test_async_add_job_schedule_coroutinefunction(mock_iscoro):
|
|
|
|
"""Test that we schedule coroutines and add jobs to the job pool."""
|
|
|
|
hass = MagicMock()
|
|
|
|
job = MagicMock()
|
|
|
|
|
|
|
|
ha.HomeAssistant.async_add_job(hass, job)
|
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 0
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 1
|
|
|
|
assert len(hass.add_job.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
@patch('asyncio.iscoroutinefunction', return_value=False)
|
|
|
|
def test_async_add_job_add_threaded_job_to_pool(mock_iscoro):
|
|
|
|
"""Test that we schedule coroutines and add jobs to the job pool."""
|
|
|
|
hass = MagicMock()
|
|
|
|
job = MagicMock()
|
|
|
|
|
|
|
|
ha.HomeAssistant.async_add_job(hass, job)
|
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 0
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 0
|
|
|
|
assert len(hass.add_job.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_async_run_job_calls_callback():
|
|
|
|
"""Test that the callback annotation is respected."""
|
|
|
|
hass = MagicMock()
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def job():
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
ha.HomeAssistant.async_run_job(hass, ha.callback(job))
|
|
|
|
assert len(calls) == 1
|
|
|
|
assert len(hass.async_add_job.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_async_run_job_delegates_non_async():
|
|
|
|
"""Test that the callback annotation is respected."""
|
|
|
|
hass = MagicMock()
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def job():
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
ha.HomeAssistant.async_run_job(hass, job)
|
|
|
|
assert len(calls) == 0
|
|
|
|
assert len(hass.async_add_job.mock_calls) == 1
|
2016-08-09 03:21:40 +00:00
|
|
|
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
class TestHomeAssistant(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Home Assistant core classes."""
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass = get_test_home_assistant(0)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Stop everything that was started."""
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.stop()
|
2014-11-23 20:57:29 +00:00
|
|
|
|
2016-10-05 04:00:36 +00:00
|
|
|
# This test hangs on `loop.add_signal_handler`
|
|
|
|
# def test_start_and_sigterm(self):
|
|
|
|
# """Start the test."""
|
|
|
|
# calls = []
|
|
|
|
# self.hass.bus.listen_once(EVENT_HOMEASSISTANT_START,
|
|
|
|
# lambda event: calls.append(1))
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2016-10-05 04:00:36 +00:00
|
|
|
# self.hass.start()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2016-10-05 04:00:36 +00:00
|
|
|
# self.assertEqual(1, len(calls))
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2016-10-05 04:00:36 +00:00
|
|
|
# self.hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
|
|
|
# lambda event: calls.append(1))
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2016-10-05 04:00:36 +00:00
|
|
|
# os.kill(os.getpid(), signal.SIGTERM)
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2016-10-05 04:00:36 +00:00
|
|
|
# self.hass.block_till_done()
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2016-10-05 04:00:36 +00:00
|
|
|
# self.assertEqual(1, len(calls))
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
class TestEvent(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""A Test Event class."""
|
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
def test_eq(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test events."""
|
2015-08-04 16:16:10 +00:00
|
|
|
now = dt_util.utcnow()
|
|
|
|
data = {'some': 'attr'}
|
|
|
|
event1, event2 = [
|
|
|
|
ha.Event('some_type', data, time_fired=now)
|
|
|
|
for _ in range(2)
|
|
|
|
]
|
|
|
|
|
|
|
|
self.assertEqual(event1, event2)
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_repr(self):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Test that repr method works."""
|
2014-11-23 17:51:16 +00:00
|
|
|
self.assertEqual(
|
|
|
|
"<Event TestEvent[L]>",
|
|
|
|
str(ha.Event("TestEvent")))
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
"<Event TestEvent[R]: beer=nice>",
|
|
|
|
str(ha.Event("TestEvent",
|
|
|
|
{"beer": "nice"},
|
|
|
|
ha.EventOrigin.remote)))
|
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
def test_as_dict(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test as dictionary."""
|
2015-08-04 16:16:10 +00:00
|
|
|
event_type = 'some_type'
|
|
|
|
now = dt_util.utcnow()
|
|
|
|
data = {'some': 'attr'}
|
|
|
|
|
|
|
|
event = ha.Event(event_type, data, ha.EventOrigin.local, now)
|
|
|
|
expected = {
|
|
|
|
'event_type': event_type,
|
|
|
|
'data': data,
|
|
|
|
'origin': 'LOCAL',
|
2016-04-16 07:55:35 +00:00
|
|
|
'time_fired': now,
|
2015-08-04 16:16:10 +00:00
|
|
|
}
|
|
|
|
self.assertEqual(expected, event.as_dict())
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
class TestEventBus(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test EventBus methods."""
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
self.bus = self.hass.bus
|
2014-11-23 17:51:16 +00:00
|
|
|
self.bus.listen('test_event', lambda x: len)
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop down stuff we started."""
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.stop()
|
2014-11-23 20:57:29 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_add_remove_listener(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test remove_listener method."""
|
2014-11-23 17:51:16 +00:00
|
|
|
old_count = len(self.bus.listeners)
|
|
|
|
|
2016-02-14 06:57:40 +00:00
|
|
|
def listener(_): pass
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2016-10-18 02:38:41 +00:00
|
|
|
unsub = self.bus.listen('test', listener)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(old_count + 1, len(self.bus.listeners))
|
|
|
|
|
|
|
|
# Remove listener
|
2016-10-18 02:38:41 +00:00
|
|
|
unsub()
|
2014-11-23 17:51:16 +00:00
|
|
|
self.assertEqual(old_count, len(self.bus.listeners))
|
|
|
|
|
2016-10-18 02:38:41 +00:00
|
|
|
# Should do nothing now
|
|
|
|
unsub()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
def test_unsubscribe_listener(self):
|
|
|
|
"""Test unsubscribe listener from returned function."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def listener(event):
|
|
|
|
"""Mock listener."""
|
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
unsub = self.bus.listen('test', listener)
|
|
|
|
|
|
|
|
self.bus.fire('test')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
unsub()
|
|
|
|
|
|
|
|
self.bus.fire('event')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
2016-10-18 02:38:41 +00:00
|
|
|
def test_listen_once_event_with_callback(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test listen_once_event method."""
|
2014-11-29 07:19:59 +00:00
|
|
|
runs = []
|
|
|
|
|
2016-10-18 02:38:41 +00:00
|
|
|
@ha.callback
|
|
|
|
def event_handler(event):
|
|
|
|
runs.append(event)
|
|
|
|
|
|
|
|
self.bus.listen_once('test_event', event_handler)
|
|
|
|
|
|
|
|
self.bus.fire('test_event')
|
|
|
|
# Second time it should not increase runs
|
|
|
|
self.bus.fire('test_event')
|
|
|
|
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(1, len(runs))
|
|
|
|
|
|
|
|
def test_listen_once_event_with_coroutine(self):
|
|
|
|
"""Test listen_once_event method."""
|
|
|
|
runs = []
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def event_handler(event):
|
|
|
|
runs.append(event)
|
|
|
|
|
|
|
|
self.bus.listen_once('test_event', event_handler)
|
|
|
|
|
|
|
|
self.bus.fire('test_event')
|
|
|
|
# Second time it should not increase runs
|
|
|
|
self.bus.fire('test_event')
|
|
|
|
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(1, len(runs))
|
|
|
|
|
|
|
|
def test_listen_once_event_with_thread(self):
|
|
|
|
"""Test listen_once_event method."""
|
|
|
|
runs = []
|
|
|
|
|
|
|
|
def event_handler(event):
|
|
|
|
runs.append(event)
|
|
|
|
|
|
|
|
self.bus.listen_once('test_event', event_handler)
|
2014-11-29 07:19:59 +00:00
|
|
|
|
|
|
|
self.bus.fire('test_event')
|
|
|
|
# Second time it should not increase runs
|
|
|
|
self.bus.fire('test_event')
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-29 07:19:59 +00:00
|
|
|
self.assertEqual(1, len(runs))
|
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
def test_thread_event_listener(self):
|
|
|
|
"""Test a event listener listeners."""
|
|
|
|
thread_calls = []
|
|
|
|
|
|
|
|
def thread_listener(event):
|
|
|
|
thread_calls.append(event)
|
|
|
|
|
|
|
|
self.bus.listen('test_thread', thread_listener)
|
|
|
|
self.bus.fire('test_thread')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
assert len(thread_calls) == 1
|
|
|
|
|
|
|
|
def test_callback_event_listener(self):
|
|
|
|
"""Test a event listener listeners."""
|
|
|
|
callback_calls = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def callback_listener(event):
|
|
|
|
callback_calls.append(event)
|
|
|
|
|
|
|
|
self.bus.listen('test_callback', callback_listener)
|
|
|
|
self.bus.fire('test_callback')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
assert len(callback_calls) == 1
|
|
|
|
|
|
|
|
def test_coroutine_event_listener(self):
|
|
|
|
"""Test a event listener listeners."""
|
|
|
|
coroutine_calls = []
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def coroutine_listener(event):
|
|
|
|
coroutine_calls.append(event)
|
|
|
|
|
|
|
|
self.bus.listen('test_coroutine', coroutine_listener)
|
|
|
|
self.bus.fire('test_coroutine')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
assert len(coroutine_calls) == 1
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
class TestState(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test State methods."""
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
def test_init(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test state.init."""
|
2014-11-23 17:51:16 +00:00
|
|
|
self.assertRaises(
|
2015-08-30 02:44:59 +00:00
|
|
|
InvalidEntityFormatError, ha.State,
|
2014-11-23 17:51:16 +00:00
|
|
|
'invalid_entity_format', 'test_state')
|
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
def test_domain(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test domain."""
|
2015-08-04 16:16:10 +00:00
|
|
|
state = ha.State('some_domain.hello', 'world')
|
|
|
|
self.assertEqual('some_domain', state.domain)
|
|
|
|
|
|
|
|
def test_object_id(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test object ID."""
|
2015-08-04 16:16:10 +00:00
|
|
|
state = ha.State('domain.hello', 'world')
|
|
|
|
self.assertEqual('hello', state.object_id)
|
|
|
|
|
|
|
|
def test_name_if_no_friendly_name_attr(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if there is no friendly name."""
|
2015-08-04 16:16:10 +00:00
|
|
|
state = ha.State('domain.hello_world', 'world')
|
|
|
|
self.assertEqual('hello world', state.name)
|
|
|
|
|
|
|
|
def test_name_if_friendly_name_attr(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if there is a friendly name."""
|
2015-08-04 16:16:10 +00:00
|
|
|
name = 'Some Unique Name'
|
|
|
|
state = ha.State('domain.hello_world', 'world',
|
|
|
|
{ATTR_FRIENDLY_NAME: name})
|
|
|
|
self.assertEqual(name, state.name)
|
|
|
|
|
|
|
|
def test_dict_conversion(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test conversion of dict."""
|
2015-08-04 16:16:10 +00:00
|
|
|
state = ha.State('domain.hello', 'world', {'some': 'attr'})
|
|
|
|
self.assertEqual(state, ha.State.from_dict(state.as_dict()))
|
|
|
|
|
|
|
|
def test_dict_conversion_with_wrong_data(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test conversion with wrong data."""
|
2015-08-04 16:16:10 +00:00
|
|
|
self.assertIsNone(ha.State.from_dict(None))
|
|
|
|
self.assertIsNone(ha.State.from_dict({'state': 'yes'}))
|
|
|
|
self.assertIsNone(ha.State.from_dict({'entity_id': 'yes'}))
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_repr(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test state.repr."""
|
2016-04-16 07:55:35 +00:00
|
|
|
self.assertEqual("<state happy.happy=on @ 1984-12-08T12:00:00+00:00>",
|
2014-11-23 17:51:16 +00:00
|
|
|
str(ha.State(
|
|
|
|
"happy.happy", "on",
|
|
|
|
last_changed=datetime(1984, 12, 8, 12, 0, 0))))
|
|
|
|
|
2015-01-02 16:48:20 +00:00
|
|
|
self.assertEqual(
|
2016-04-16 07:55:35 +00:00
|
|
|
"<state happy.happy=on; brightness=144 @ "
|
|
|
|
"1984-12-08T12:00:00+00:00>",
|
2015-01-02 16:48:20 +00:00
|
|
|
str(ha.State("happy.happy", "on", {"brightness": 144},
|
|
|
|
datetime(1984, 12, 8, 12, 0, 0))))
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestStateMachine(unittest.TestCase):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Test State machine methods."""
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass = get_test_home_assistant(0)
|
|
|
|
self.states = self.hass.states
|
2014-11-23 17:51:16 +00:00
|
|
|
self.states.set("light.Bowl", "on")
|
|
|
|
self.states.set("switch.AC", "off")
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop down stuff we started."""
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.stop()
|
2014-11-23 20:57:29 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_is_state(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test is_state method."""
|
2014-11-23 17:51:16 +00:00
|
|
|
self.assertTrue(self.states.is_state('light.Bowl', 'on'))
|
|
|
|
self.assertFalse(self.states.is_state('light.Bowl', 'off'))
|
|
|
|
self.assertFalse(self.states.is_state('light.Non_existing', 'on'))
|
|
|
|
|
2015-12-31 20:58:18 +00:00
|
|
|
def test_is_state_attr(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test is_state_attr method."""
|
2015-12-31 20:58:18 +00:00
|
|
|
self.states.set("light.Bowl", "on", {"brightness": 100})
|
|
|
|
self.assertTrue(
|
|
|
|
self.states.is_state_attr('light.Bowl', 'brightness', 100))
|
|
|
|
self.assertFalse(
|
|
|
|
self.states.is_state_attr('light.Bowl', 'friendly_name', 200))
|
|
|
|
self.assertFalse(
|
|
|
|
self.states.is_state_attr('light.Bowl', 'friendly_name', 'Bowl'))
|
|
|
|
self.assertFalse(
|
|
|
|
self.states.is_state_attr('light.Non_existing', 'brightness', 100))
|
|
|
|
|
2014-11-29 07:19:59 +00:00
|
|
|
def test_entity_ids(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test get_entity_ids method."""
|
2014-11-29 07:19:59 +00:00
|
|
|
ent_ids = self.states.entity_ids()
|
|
|
|
self.assertEqual(2, len(ent_ids))
|
2015-02-06 08:17:30 +00:00
|
|
|
self.assertTrue('light.bowl' in ent_ids)
|
|
|
|
self.assertTrue('switch.ac' in ent_ids)
|
2014-11-29 07:19:59 +00:00
|
|
|
|
|
|
|
ent_ids = self.states.entity_ids('light')
|
|
|
|
self.assertEqual(1, len(ent_ids))
|
2015-02-06 08:17:30 +00:00
|
|
|
self.assertTrue('light.bowl' in ent_ids)
|
2014-11-29 07:19:59 +00:00
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
def test_all(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test everything."""
|
2015-08-04 16:16:10 +00:00
|
|
|
states = sorted(state.entity_id for state in self.states.all())
|
|
|
|
self.assertEqual(['light.bowl', 'switch.ac'], states)
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_remove(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test remove method."""
|
2016-02-14 06:57:40 +00:00
|
|
|
events = []
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.bus.listen(EVENT_STATE_CHANGED,
|
|
|
|
lambda event: events.append(event))
|
2016-02-14 06:57:40 +00:00
|
|
|
|
|
|
|
self.assertIn('light.bowl', self.states.entity_ids())
|
2015-02-06 08:17:30 +00:00
|
|
|
self.assertTrue(self.states.remove('light.bowl'))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-14 06:57:40 +00:00
|
|
|
|
|
|
|
self.assertNotIn('light.bowl', self.states.entity_ids())
|
|
|
|
self.assertEqual(1, len(events))
|
|
|
|
self.assertEqual('light.bowl', events[0].data.get('entity_id'))
|
|
|
|
self.assertIsNotNone(events[0].data.get('old_state'))
|
|
|
|
self.assertEqual('light.bowl', events[0].data['old_state'].entity_id)
|
|
|
|
self.assertIsNone(events[0].data.get('new_state'))
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
# If it does not exist, we should get False
|
|
|
|
self.assertFalse(self.states.remove('light.Bowl'))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-14 06:57:40 +00:00
|
|
|
self.assertEqual(1, len(events))
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2014-12-27 07:26:39 +00:00
|
|
|
def test_case_insensitivty(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test insensitivty."""
|
2014-12-27 07:26:39 +00:00
|
|
|
runs = []
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.bus.listen(EVENT_STATE_CHANGED,
|
|
|
|
lambda event: runs.append(event))
|
2014-12-27 07:26:39 +00:00
|
|
|
|
|
|
|
self.states.set('light.BOWL', 'off')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-12-27 07:26:39 +00:00
|
|
|
|
|
|
|
self.assertTrue(self.states.is_state('light.bowl', 'off'))
|
|
|
|
self.assertEqual(1, len(runs))
|
|
|
|
|
2015-01-02 16:48:20 +00:00
|
|
|
def test_last_changed_not_updated_on_same_state(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test to not update the existing, same state."""
|
2015-01-02 16:48:20 +00:00
|
|
|
state = self.states.get('light.Bowl')
|
|
|
|
|
2015-09-13 05:56:49 +00:00
|
|
|
future = dt_util.utcnow() + timedelta(hours=10)
|
2015-01-02 16:48:20 +00:00
|
|
|
|
2015-09-13 05:56:49 +00:00
|
|
|
with patch('homeassistant.util.dt.utcnow', return_value=future):
|
|
|
|
self.states.set("light.Bowl", "on", {'attr': 'triggers_change'})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-01-02 16:48:20 +00:00
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
state2 = self.states.get('light.Bowl')
|
|
|
|
assert state2 is not None
|
|
|
|
assert state.last_changed == state2.last_changed
|
2015-01-02 16:48:20 +00:00
|
|
|
|
2016-06-26 07:33:23 +00:00
|
|
|
def test_force_update(self):
|
|
|
|
"""Test force update option."""
|
|
|
|
events = []
|
2016-10-05 03:44:32 +00:00
|
|
|
self.hass.bus.listen(EVENT_STATE_CHANGED, lambda ev: events.append(ev))
|
2016-06-26 07:33:23 +00:00
|
|
|
|
|
|
|
self.states.set('light.bowl', 'on')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-06-26 07:33:23 +00:00
|
|
|
self.assertEqual(0, len(events))
|
|
|
|
|
|
|
|
self.states.set('light.bowl', 'on', None, True)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-06-26 07:33:23 +00:00
|
|
|
self.assertEqual(1, len(events))
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
class TestServiceCall(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test ServiceCall class."""
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_repr(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test repr method."""
|
2014-11-23 17:51:16 +00:00
|
|
|
self.assertEqual(
|
|
|
|
"<ServiceCall homeassistant.start>",
|
|
|
|
str(ha.ServiceCall('homeassistant', 'start')))
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
"<ServiceCall homeassistant.start: fast=yes>",
|
|
|
|
str(ha.ServiceCall('homeassistant', 'start', {"fast": "yes"})))
|
|
|
|
|
|
|
|
|
|
|
|
class TestServiceRegistry(unittest.TestCase):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Test ServicerRegistry methods."""
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
self.services = self.hass.services
|
2016-08-10 02:41:45 +00:00
|
|
|
self.services.register("Test_Domain", "TEST_SERVICE", lambda x: None)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop down stuff we started."""
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.stop()
|
2014-11-23 20:57:29 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_has_service(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test has_service method."""
|
2014-11-23 17:51:16 +00:00
|
|
|
self.assertTrue(
|
2016-08-10 02:41:45 +00:00
|
|
|
self.services.has_service("tesT_domaiN", "tesT_servicE"))
|
2015-08-04 16:16:10 +00:00
|
|
|
self.assertFalse(
|
|
|
|
self.services.has_service("test_domain", "non_existing"))
|
|
|
|
self.assertFalse(
|
|
|
|
self.services.has_service("non_existing", "test_service"))
|
|
|
|
|
|
|
|
def test_services(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test services."""
|
2015-08-04 16:16:10 +00:00
|
|
|
expected = {
|
2015-09-27 06:17:04 +00:00
|
|
|
'test_domain': {'test_service': {'description': '', 'fields': {}}}
|
2015-08-04 16:16:10 +00:00
|
|
|
}
|
|
|
|
self.assertEqual(expected, self.services.services)
|
|
|
|
|
|
|
|
def test_call_with_blocking_done_in_time(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test call with blocking."""
|
2015-08-04 16:16:10 +00:00
|
|
|
calls = []
|
2016-09-30 19:57:24 +00:00
|
|
|
|
|
|
|
def service_handler(call):
|
|
|
|
"""Service handler."""
|
|
|
|
calls.append(call)
|
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
self.services.register("test_domain", "register_calls",
|
2016-09-30 19:57:24 +00:00
|
|
|
service_handler)
|
2015-08-04 16:16:10 +00:00
|
|
|
|
|
|
|
self.assertTrue(
|
2016-08-10 02:41:45 +00:00
|
|
|
self.services.call('test_domain', 'REGISTER_CALLS', blocking=True))
|
2015-08-04 16:16:10 +00:00
|
|
|
self.assertEqual(1, len(calls))
|
|
|
|
|
|
|
|
def test_call_non_existing_with_blocking(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test non-existing with blocking."""
|
2016-09-13 02:16:14 +00:00
|
|
|
prior = ha.SERVICE_CALL_LIMIT
|
|
|
|
try:
|
|
|
|
ha.SERVICE_CALL_LIMIT = 0.01
|
|
|
|
assert not self.services.call('test_domain', 'i_do_not_exist',
|
|
|
|
blocking=True)
|
|
|
|
finally:
|
|
|
|
ha.SERVICE_CALL_LIMIT = prior
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2016-09-30 19:57:24 +00:00
|
|
|
def test_async_service(self):
|
|
|
|
"""Test registering and calling an async service."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def service_handler(call):
|
|
|
|
"""Service handler coroutine."""
|
|
|
|
calls.append(call)
|
|
|
|
|
|
|
|
self.services.register('test_domain', 'register_calls',
|
|
|
|
service_handler)
|
|
|
|
self.assertTrue(
|
|
|
|
self.services.call('test_domain', 'REGISTER_CALLS', blocking=True))
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(1, len(calls))
|
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
def test_callback_service(self):
|
|
|
|
"""Test registering and calling an async service."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def service_handler(call):
|
|
|
|
"""Service handler coroutine."""
|
|
|
|
calls.append(call)
|
|
|
|
|
|
|
|
self.services.register('test_domain', 'register_calls',
|
|
|
|
service_handler)
|
|
|
|
self.assertTrue(
|
|
|
|
self.services.call('test_domain', 'REGISTER_CALLS', blocking=True))
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(1, len(calls))
|
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
|
|
|
|
class TestConfig(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test configuration methods."""
|
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2015-08-04 16:16:10 +00:00
|
|
|
self.config = ha.Config()
|
2016-08-09 03:21:40 +00:00
|
|
|
self.assertIsNone(self.config.config_dir)
|
2015-08-04 16:16:10 +00:00
|
|
|
|
|
|
|
def test_path_with_file(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test get_config_path method."""
|
2016-08-09 03:21:40 +00:00
|
|
|
self.config.config_dir = '/tmp/ha-config'
|
|
|
|
self.assertEqual("/tmp/ha-config/test.conf",
|
2015-08-04 16:16:10 +00:00
|
|
|
self.config.path("test.conf"))
|
|
|
|
|
|
|
|
def test_path_with_dir_and_file(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test get_config_path method."""
|
2016-08-09 03:21:40 +00:00
|
|
|
self.config.config_dir = '/tmp/ha-config'
|
|
|
|
self.assertEqual("/tmp/ha-config/dir/test.conf",
|
|
|
|
self.config.path("dir", "test.conf"))
|
2015-08-04 16:16:10 +00:00
|
|
|
|
|
|
|
def test_as_dict(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test as dict."""
|
2016-10-03 07:04:43 +00:00
|
|
|
self.config.config_dir = '/tmp/ha-config'
|
2015-08-04 16:16:10 +00:00
|
|
|
expected = {
|
|
|
|
'latitude': None,
|
|
|
|
'longitude': None,
|
2016-07-31 20:24:49 +00:00
|
|
|
CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(),
|
2015-08-04 16:16:10 +00:00
|
|
|
'location_name': None,
|
|
|
|
'time_zone': 'UTC',
|
|
|
|
'components': [],
|
2016-10-03 07:04:43 +00:00
|
|
|
'config_dir': '/tmp/ha-config',
|
2015-10-26 04:00:22 +00:00
|
|
|
'version': __version__,
|
2015-08-04 16:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(expected, self.config.as_dict())
|
|
|
|
|
|
|
|
|
|
|
|
class TestWorkerPool(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test WorkerPool methods."""
|
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
def test_exception_during_job(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test exception during a job."""
|
2015-08-04 16:16:10 +00:00
|
|
|
pool = ha.create_worker_pool(1)
|
|
|
|
|
|
|
|
def malicious_job(_):
|
|
|
|
raise Exception("Test breaking worker pool")
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def register_call(_):
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
pool.add_job(ha.JobPriority.EVENT_DEFAULT, (malicious_job, None))
|
|
|
|
pool.add_job(ha.JobPriority.EVENT_DEFAULT, (register_call, None))
|
|
|
|
pool.block_till_done()
|
|
|
|
self.assertEqual(1, len(calls))
|
2016-09-20 06:39:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestWorkerPoolMonitor(object):
|
|
|
|
"""Test monitor_worker_pool."""
|
|
|
|
|
|
|
|
@patch('homeassistant.core._LOGGER.warning')
|
|
|
|
def test_worker_pool_monitor(self, mock_warning, event_loop):
|
|
|
|
"""Test we log an error and increase threshold."""
|
|
|
|
hass = MagicMock()
|
|
|
|
hass.pool.worker_count = 3
|
|
|
|
schedule_handle = MagicMock()
|
|
|
|
hass.loop.call_later.return_value = schedule_handle
|
|
|
|
|
2016-10-18 02:38:41 +00:00
|
|
|
ha._async_monitor_worker_pool(hass)
|
2016-09-20 06:39:49 +00:00
|
|
|
assert hass.loop.call_later.called
|
|
|
|
assert hass.bus.async_listen_once.called
|
|
|
|
assert not schedule_handle.called
|
|
|
|
|
|
|
|
check_threshold = hass.loop.call_later.mock_calls[0][1][1]
|
|
|
|
|
|
|
|
hass.pool.queue_size = 8
|
|
|
|
check_threshold()
|
|
|
|
assert not mock_warning.called
|
|
|
|
|
|
|
|
hass.pool.queue_size = 9
|
|
|
|
check_threshold()
|
|
|
|
assert mock_warning.called
|
|
|
|
|
|
|
|
mock_warning.reset_mock()
|
|
|
|
assert not mock_warning.called
|
|
|
|
|
|
|
|
check_threshold()
|
|
|
|
assert not mock_warning.called
|
|
|
|
|
|
|
|
hass.pool.queue_size = 17
|
|
|
|
check_threshold()
|
|
|
|
assert not mock_warning.called
|
|
|
|
|
|
|
|
hass.pool.queue_size = 18
|
|
|
|
check_threshold()
|
|
|
|
assert mock_warning.called
|
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
hass.bus.async_listen_once.mock_calls[0][1][1](None)
|
2016-09-20 06:39:49 +00:00
|
|
|
assert schedule_handle.cancel.called
|
|
|
|
|
|
|
|
|
|
|
|
class TestAsyncCreateTimer(object):
|
|
|
|
"""Test create timer."""
|
|
|
|
|
|
|
|
@patch('homeassistant.core.asyncio.Event')
|
|
|
|
@patch('homeassistant.core.dt_util.utcnow')
|
|
|
|
def test_create_timer(self, mock_utcnow, mock_event, event_loop):
|
|
|
|
"""Test create timer fires correctly."""
|
|
|
|
hass = MagicMock()
|
|
|
|
now = mock_utcnow()
|
|
|
|
event = mock_event()
|
|
|
|
now.second = 1
|
|
|
|
mock_utcnow.reset_mock()
|
|
|
|
|
2016-10-18 02:38:41 +00:00
|
|
|
ha._async_create_timer(hass)
|
2016-09-20 06:39:49 +00:00
|
|
|
assert len(hass.bus.async_listen_once.mock_calls) == 2
|
|
|
|
start_timer = hass.bus.async_listen_once.mock_calls[1][1][1]
|
|
|
|
|
|
|
|
event_loop.run_until_complete(start_timer(None))
|
|
|
|
assert hass.loop.create_task.called
|
|
|
|
|
|
|
|
timer = hass.loop.create_task.mock_calls[0][1][0]
|
|
|
|
event.is_set.side_effect = False, False, True
|
|
|
|
event_loop.run_until_complete(timer)
|
|
|
|
assert len(mock_utcnow.mock_calls) == 1
|
|
|
|
|
|
|
|
assert hass.loop.call_soon.called
|
|
|
|
event_type, event_data = hass.loop.call_soon.mock_calls[0][1][1:]
|
|
|
|
|
|
|
|
assert ha.EVENT_TIME_CHANGED == event_type
|
|
|
|
assert {ha.ATTR_NOW: now} == event_data
|
|
|
|
|
|
|
|
stop_timer = hass.bus.async_listen_once.mock_calls[0][1][1]
|
2016-10-05 03:44:32 +00:00
|
|
|
stop_timer(None)
|
2016-09-20 06:39:49 +00:00
|
|
|
assert event.set.called
|