core/tests/test_remote.py

312 lines
10 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""Test Home Assistant remote methods and classes."""
2014-11-23 17:51:16 +00:00
# pylint: disable=protected-access,too-many-public-methods
import unittest
2016-05-14 07:58:36 +00:00
import eventlet
2015-08-17 03:44:46 +00:00
import homeassistant.core as ha
import homeassistant.bootstrap as bootstrap
2014-11-23 17:51:16 +00:00
import homeassistant.remote as remote
import homeassistant.components.http as http
from homeassistant.const import HTTP_HEADER_HA_AUTH, EVENT_STATE_CHANGED
2016-04-16 07:55:35 +00:00
import homeassistant.util.dt as dt_util
2014-11-23 17:51:16 +00:00
2016-02-14 23:08:23 +00:00
from tests.common import get_test_instance_port, get_test_home_assistant
2014-11-23 17:51:16 +00:00
API_PASSWORD = "test1234"
MASTER_PORT = get_test_instance_port()
SLAVE_PORT = get_test_instance_port()
BROKEN_PORT = get_test_instance_port()
HTTP_BASE_URL = "http://127.0.0.1:{}".format(MASTER_PORT)
2014-11-23 17:51:16 +00:00
HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD}
2014-11-23 17:51:16 +00:00
broken_api = remote.API('127.0.0.1', BROKEN_PORT)
hass, slave, master_api = None, None, None
2014-11-24 06:18:51 +00:00
2014-11-23 17:51:16 +00:00
def _url(path=""):
2016-03-09 09:25:50 +00:00
"""Helper method to generate URLs."""
2014-11-23 17:51:16 +00:00
return HTTP_BASE_URL + path
def setUpModule(): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Initalization of a Home Assistant server and Slave instance."""
2016-02-15 07:01:49 +00:00
global hass, slave, master_api
2014-11-23 17:51:16 +00:00
2016-02-14 23:08:23 +00:00
hass = get_test_home_assistant()
2014-11-23 17:51:16 +00:00
2014-11-23 20:57:29 +00:00
hass.bus.listen('test_event', lambda _: _)
hass.states.set('test.test', 'a_state')
2014-11-23 17:51:16 +00:00
bootstrap.setup_component(
hass, http.DOMAIN,
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
http.CONF_SERVER_PORT: MASTER_PORT}})
bootstrap.setup_component(hass, 'api')
2014-11-23 17:51:16 +00:00
2014-11-23 20:57:29 +00:00
hass.start()
2014-11-23 17:51:16 +00:00
2016-05-14 07:58:36 +00:00
# Give eventlet time to start
# TODO fix this
eventlet.sleep(0.05)
master_api = remote.API("127.0.0.1", API_PASSWORD, MASTER_PORT)
2014-11-23 17:51:16 +00:00
2014-11-23 20:57:29 +00:00
# Start slave
slave = remote.HomeAssistant(master_api)
2015-08-30 08:51:49 +00:00
bootstrap.setup_component(
slave, http.DOMAIN,
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
http.CONF_SERVER_PORT: SLAVE_PORT}})
2014-11-23 17:51:16 +00:00
2014-11-23 20:57:29 +00:00
slave.start()
2014-11-23 17:51:16 +00:00
2016-05-14 07:58:36 +00:00
# Give eventlet time to start
# TODO fix this
eventlet.sleep(0.05)
2014-11-23 17:51:16 +00:00
2014-11-24 06:18:51 +00:00
def tearDownModule(): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Stop the Home Assistant server and slave."""
2014-11-23 20:57:29 +00:00
slave.stop()
hass.stop()
2014-11-23 17:51:16 +00:00
class TestRemoteMethods(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the homeassistant.remote module."""
2014-11-23 17:51:16 +00:00
2016-02-15 07:01:49 +00:00
def tearDown(self):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
2016-02-15 07:01:49 +00:00
slave.pool.block_till_done()
hass.pool.block_till_done()
2014-11-23 17:51:16 +00:00
def test_validate_api(self):
2016-03-09 09:25:50 +00:00
"""Test Python API validate_api."""
2014-11-23 20:57:29 +00:00
self.assertEqual(remote.APIStatus.OK, remote.validate_api(master_api))
2014-11-23 17:51:16 +00:00
self.assertEqual(
remote.APIStatus.INVALID_PASSWORD,
remote.validate_api(
remote.API("127.0.0.1", API_PASSWORD + "A", MASTER_PORT)))
2014-11-23 17:51:16 +00:00
self.assertEqual(
remote.APIStatus.CANNOT_CONNECT, remote.validate_api(broken_api))
2014-11-23 17:51:16 +00:00
def test_get_event_listeners(self):
2016-03-09 09:25:50 +00:00
"""Test Python API get_event_listeners."""
2014-11-23 20:57:29 +00:00
local_data = hass.bus.listeners
remote_data = remote.get_event_listeners(master_api)
2014-11-23 17:51:16 +00:00
for event in remote_data:
self.assertEqual(local_data.pop(event["event"]),
event["listener_count"])
self.assertEqual(len(local_data), 0)
2014-11-29 05:01:44 +00:00
self.assertEqual({}, remote.get_event_listeners(broken_api))
2014-11-23 17:51:16 +00:00
def test_fire_event(self):
2016-03-09 09:25:50 +00:00
"""Test Python API fire_event."""
2014-11-23 17:51:16 +00:00
test_value = []
2015-01-20 07:40:51 +00:00
def listener(event):
2016-03-09 09:25:50 +00:00
"""Helper method that will verify our event got called."""
2014-11-23 17:51:16 +00:00
test_value.append(1)
2014-11-29 07:19:59 +00:00
hass.bus.listen_once("test.event_no_data", listener)
2014-11-23 20:57:29 +00:00
remote.fire_event(master_api, "test.event_no_data")
hass.pool.block_till_done()
2014-11-23 17:51:16 +00:00
self.assertEqual(1, len(test_value))
2014-11-29 05:01:44 +00:00
# Should not trigger any exception
remote.fire_event(broken_api, "test.event_no_data")
2014-11-23 17:51:16 +00:00
def test_get_state(self):
2016-03-09 09:25:50 +00:00
"""Test Python API get_state."""
2014-11-23 17:51:16 +00:00
self.assertEqual(
2014-11-23 20:57:29 +00:00
hass.states.get('test.test'),
remote.get_state(master_api, 'test.test'))
2014-11-23 17:51:16 +00:00
2014-11-29 05:01:44 +00:00
self.assertEqual(None, remote.get_state(broken_api, 'test.test'))
2014-11-23 17:51:16 +00:00
def test_get_states(self):
2016-03-09 09:25:50 +00:00
"""Test Python API get_state_entity_ids."""
2014-11-29 05:01:44 +00:00
self.assertEqual(hass.states.all(), remote.get_states(master_api))
self.assertEqual([], remote.get_states(broken_api))
2014-11-23 17:51:16 +00:00
def test_remove_state(self):
2016-03-09 09:25:50 +00:00
"""Test Python API set_state."""
hass.states.set('test.remove_state', 'set_test')
self.assertIn('test.remove_state', hass.states.entity_ids())
remote.remove_state(master_api, 'test.remove_state')
self.assertNotIn('test.remove_state', hass.states.entity_ids())
2014-11-23 17:51:16 +00:00
def test_set_state(self):
2016-03-09 09:25:50 +00:00
"""Test Python API set_state."""
remote.set_state(master_api, 'test.test', 'set_test')
2014-11-23 17:51:16 +00:00
2015-08-22 04:51:54 +00:00
state = hass.states.get('test.test')
self.assertIsNotNone(state)
self.assertEqual('set_test', state.state)
2014-11-23 17:51:16 +00:00
2014-11-29 05:01:44 +00:00
self.assertFalse(remote.set_state(broken_api, 'test.test', 'set_test'))
def test_set_state_with_push(self):
"""TestPython API set_state with push option."""
events = []
hass.bus.listen(EVENT_STATE_CHANGED, events.append)
remote.set_state(master_api, 'test.test', 'set_test_2')
remote.set_state(master_api, 'test.test', 'set_test_2')
hass.bus._pool.block_till_done()
self.assertEqual(1, len(events))
remote.set_state(
master_api, 'test.test', 'set_test_2', force_update=True)
hass.bus._pool.block_till_done()
self.assertEqual(2, len(events))
2014-11-23 17:51:16 +00:00
def test_is_state(self):
2016-03-09 09:25:50 +00:00
"""Test Python API is_state."""
2014-11-23 17:51:16 +00:00
self.assertTrue(
2014-11-23 20:57:29 +00:00
remote.is_state(master_api, 'test.test',
hass.states.get('test.test').state))
2014-11-23 17:51:16 +00:00
2014-11-29 05:01:44 +00:00
self.assertFalse(
remote.is_state(broken_api, 'test.test',
hass.states.get('test.test').state))
2014-11-23 17:51:16 +00:00
def test_get_services(self):
2016-03-09 09:25:50 +00:00
"""Test Python API get_services."""
2014-11-23 20:57:29 +00:00
local_services = hass.services.services
2014-11-23 17:51:16 +00:00
2014-11-23 20:57:29 +00:00
for serv_domain in remote.get_services(master_api):
2014-11-23 17:51:16 +00:00
local = local_services.pop(serv_domain["domain"])
self.assertEqual(local, serv_domain["services"])
2014-11-29 05:01:44 +00:00
self.assertEqual({}, remote.get_services(broken_api))
2014-11-23 17:51:16 +00:00
def test_call_service(self):
2016-03-09 09:25:50 +00:00
"""Test Python API services.call."""
2014-11-23 17:51:16 +00:00
test_value = []
2015-01-20 07:40:51 +00:00
def listener(service_call):
2016-03-09 09:25:50 +00:00
"""Helper method that will verify that our service got called."""
2014-11-23 17:51:16 +00:00
test_value.append(1)
2014-11-23 20:57:29 +00:00
hass.services.register("test_domain", "test_service", listener)
2014-11-23 17:51:16 +00:00
2014-11-23 20:57:29 +00:00
remote.call_service(master_api, "test_domain", "test_service")
2014-11-23 17:51:16 +00:00
hass.pool.block_till_done()
2014-11-23 17:51:16 +00:00
self.assertEqual(1, len(test_value))
2014-11-29 05:01:44 +00:00
# Should not raise an exception
remote.call_service(broken_api, "test_domain", "test_service")
2016-02-15 07:01:49 +00:00
def test_json_encoder(self):
2016-03-09 09:25:50 +00:00
"""Test the JSON Encoder."""
2016-02-15 07:01:49 +00:00
ha_json_enc = remote.JSONEncoder()
state = hass.states.get('test.test')
self.assertEqual(state.as_dict(), ha_json_enc.default(state))
# Default method raises TypeError if non HA object
self.assertRaises(TypeError, ha_json_enc.default, 1)
2016-04-16 07:55:35 +00:00
now = dt_util.utcnow()
self.assertEqual(now.isoformat(), ha_json_enc.default(now))
2014-11-23 17:51:16 +00:00
class TestRemoteClasses(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the homeassistant.remote module."""
2014-11-23 17:51:16 +00:00
2016-02-15 07:01:49 +00:00
def tearDown(self):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
2016-02-15 07:01:49 +00:00
slave.pool.block_till_done()
hass.pool.block_till_done()
2014-11-23 17:51:16 +00:00
def test_home_assistant_init(self):
2016-03-09 09:25:50 +00:00
"""Test HomeAssistant init."""
# Wrong password
2014-11-23 17:51:16 +00:00
self.assertRaises(
ha.HomeAssistantError, remote.HomeAssistant,
remote.API('127.0.0.1', API_PASSWORD + 'A', 8124))
# Wrong port
self.assertRaises(
ha.HomeAssistantError, remote.HomeAssistant,
remote.API('127.0.0.1', API_PASSWORD, BROKEN_PORT))
2014-11-23 17:51:16 +00:00
def test_statemachine_init(self):
2016-03-09 10:15:04 +00:00
"""Test if remote.StateMachine copies all states on init."""
2016-02-15 07:01:49 +00:00
self.assertEqual(sorted(hass.states.all()),
sorted(slave.states.all()))
2014-11-23 17:51:16 +00:00
def test_statemachine_set(self):
2016-03-09 09:25:50 +00:00
"""Test if setting the state on a slave is recorded."""
2014-11-23 20:57:29 +00:00
slave.states.set("remote.test", "remote.statemachine test")
2014-11-23 17:51:16 +00:00
# Wait till slave tells master
slave.pool.block_till_done()
2014-11-23 17:51:16 +00:00
# Wait till master gives updated state
hass.pool.block_till_done()
2016-05-14 07:58:36 +00:00
eventlet.sleep(0.01)
2014-11-23 17:51:16 +00:00
self.assertEqual("remote.statemachine test",
2014-11-23 20:57:29 +00:00
slave.states.get("remote.test").state)
2014-11-23 17:51:16 +00:00
def test_statemachine_remove_from_master(self):
2016-03-09 09:25:50 +00:00
"""Remove statemachine from master."""
hass.states.set("remote.master_remove", "remove me!")
hass.pool.block_till_done()
2016-05-14 07:58:36 +00:00
eventlet.sleep(0.01)
self.assertIn('remote.master_remove', slave.states.entity_ids())
hass.states.remove("remote.master_remove")
hass.pool.block_till_done()
2016-05-14 07:58:36 +00:00
eventlet.sleep(0.01)
self.assertNotIn('remote.master_remove', slave.states.entity_ids())
def test_statemachine_remove_from_slave(self):
2016-03-09 09:25:50 +00:00
"""Remove statemachine from slave."""
hass.states.set("remote.slave_remove", "remove me!")
hass.pool.block_till_done()
2016-05-14 07:58:36 +00:00
eventlet.sleep(0.01)
self.assertIn('remote.slave_remove', slave.states.entity_ids())
self.assertTrue(slave.states.remove("remote.slave_remove"))
slave.pool.block_till_done()
hass.pool.block_till_done()
2016-05-14 07:58:36 +00:00
eventlet.sleep(0.01)
self.assertNotIn('remote.slave_remove', slave.states.entity_ids())
2014-11-23 17:51:16 +00:00
def test_eventbus_fire(self):
2016-03-09 09:25:50 +00:00
"""Test if events fired from the eventbus get fired."""
2014-11-23 17:51:16 +00:00
test_value = []
2015-01-20 07:40:51 +00:00
def listener(event):
2016-03-09 09:25:50 +00:00
"""Helper method that will verify our event got called."""
2014-11-23 17:51:16 +00:00
test_value.append(1)
2014-11-29 07:19:59 +00:00
slave.bus.listen_once("test.event_no_data", listener)
2014-11-23 20:57:29 +00:00
slave.bus.fire("test.event_no_data")
2014-11-23 17:51:16 +00:00
# Wait till slave tells master
slave.pool.block_till_done()
2014-11-23 17:51:16 +00:00
# Wait till master gives updated event
hass.pool.block_till_done()
2016-05-14 07:58:36 +00:00
eventlet.sleep(0.01)
2014-11-23 17:51:16 +00:00
self.assertEqual(1, len(test_value))