2013-09-28 18:09:36 +00:00
|
|
|
"""
|
|
|
|
homeassistant.test
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides tests to verify that Home Assistant modules do what they should do.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import time
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2013-10-13 16:40:38 +00:00
|
|
|
import homeassistant as ha
|
2013-10-25 10:05:58 +00:00
|
|
|
import homeassistant.remote as remote
|
2013-12-11 08:07:30 +00:00
|
|
|
import homeassistant.components.httpinterface as hah
|
2013-10-28 00:39:54 +00:00
|
|
|
|
2013-09-28 18:09:36 +00:00
|
|
|
API_PASSWORD = "test1234"
|
|
|
|
|
2013-10-29 07:22:38 +00:00
|
|
|
HTTP_BASE_URL = "http://127.0.0.1:{}".format(hah.SERVER_PORT)
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-11-11 00:46:48 +00:00
|
|
|
|
2013-11-01 18:34:43 +00:00
|
|
|
def _url(path=""):
|
|
|
|
""" Helper method to generate urls. """
|
|
|
|
return HTTP_BASE_URL + path
|
|
|
|
|
2013-11-11 00:46:48 +00:00
|
|
|
|
|
|
|
class HAHelper(object): # pylint: disable=too-few-public-methods
|
2013-11-01 18:34:43 +00:00
|
|
|
""" Helper class to keep track of current running HA instance. """
|
|
|
|
core = None
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-11-11 00:46:48 +00:00
|
|
|
|
2013-11-01 18:34:43 +00:00
|
|
|
def ensure_homeassistant_started():
|
|
|
|
""" Ensures home assistant is started. """
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-11-01 18:34:43 +00:00
|
|
|
if not HAHelper.core:
|
2013-11-20 07:48:08 +00:00
|
|
|
core = {'bus': ha.Bus()}
|
|
|
|
core['statemachine'] = ha.StateMachine(core['bus'])
|
2013-10-29 07:22:38 +00:00
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
core['bus'].listen_event('test_event', len)
|
2013-11-10 17:31:34 +00:00
|
|
|
core['statemachine'].set_state('test', 'a_state')
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
hah.HTTPInterface(core['bus'], core['statemachine'],
|
2013-11-11 00:46:48 +00:00
|
|
|
API_PASSWORD)
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
core['bus'].fire_event(ha.EVENT_HOMEASSISTANT_START)
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-11-01 18:34:43 +00:00
|
|
|
# Give objects time to startup
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
HAHelper.core = core
|
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
return HAHelper.core['bus'], HAHelper.core['statemachine']
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-11-11 00:46:48 +00:00
|
|
|
|
2013-11-01 18:34:43 +00:00
|
|
|
# pylint: disable=too-many-public-methods
|
|
|
|
class TestHTTPInterface(unittest.TestCase):
|
|
|
|
""" Test the HTTP debug interface and API. """
|
2013-10-08 06:55:19 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls): # pylint: disable=invalid-name
|
|
|
|
""" things to be run when tests are started. """
|
2013-11-20 07:48:08 +00:00
|
|
|
cls.bus, cls.statemachine = ensure_homeassistant_started()
|
2013-10-08 06:55:19 +00:00
|
|
|
|
2013-09-28 18:09:36 +00:00
|
|
|
def test_debug_interface(self):
|
2013-10-08 06:55:19 +00:00
|
|
|
""" Test if we can login by comparing not logged in screen to
|
|
|
|
logged in screen. """
|
2013-10-29 07:22:38 +00:00
|
|
|
|
|
|
|
with_pw = requests.get(
|
2013-11-11 00:46:48 +00:00
|
|
|
_url("/?api_password={}".format(API_PASSWORD)))
|
2013-10-29 07:22:38 +00:00
|
|
|
|
2013-11-01 18:34:43 +00:00
|
|
|
without_pw = requests.get(_url())
|
2013-10-29 07:22:38 +00:00
|
|
|
|
|
|
|
self.assertNotEqual(without_pw.text, with_pw.text)
|
2013-09-28 18:09:36 +00:00
|
|
|
|
|
|
|
def test_api_password(self):
|
2013-10-08 06:55:19 +00:00
|
|
|
""" Test if we get access denied if we omit or provide
|
|
|
|
a wrong api password. """
|
2013-11-01 19:28:18 +00:00
|
|
|
req = requests.get(
|
2014-01-20 07:37:40 +00:00
|
|
|
_url(hah.URL_API_STATES_ENTITY.format("test")))
|
2013-09-28 18:09:36 +00:00
|
|
|
|
|
|
|
self.assertEqual(req.status_code, 401)
|
|
|
|
|
2013-11-01 19:28:18 +00:00
|
|
|
req = requests.get(
|
2014-01-20 07:37:40 +00:00
|
|
|
_url(hah.URL_API_STATES_ENTITY.format("test")),
|
2013-11-11 00:46:48 +00:00
|
|
|
params={"api_password": "not the password"})
|
2013-09-28 18:09:36 +00:00
|
|
|
|
|
|
|
self.assertEqual(req.status_code, 401)
|
|
|
|
|
2013-11-05 01:16:27 +00:00
|
|
|
def test_debug_change_state(self):
|
|
|
|
""" Test if we can change a state from the debug interface. """
|
2013-11-09 22:23:26 +00:00
|
|
|
self.statemachine.set_state("test.test", "not_to_be_set_state")
|
2013-11-05 01:16:27 +00:00
|
|
|
|
|
|
|
requests.post(_url(hah.URL_CHANGE_STATE),
|
2014-01-20 07:37:40 +00:00
|
|
|
data={"entity_id": "test.test",
|
2013-11-11 00:46:48 +00:00
|
|
|
"new_state": "debug_state_change2",
|
|
|
|
"api_password": API_PASSWORD})
|
2013-11-05 01:16:27 +00:00
|
|
|
|
2014-01-20 03:10:40 +00:00
|
|
|
self.assertEqual(self.statemachine.get_state("test.test").state,
|
2013-11-05 01:16:27 +00:00
|
|
|
"debug_state_change2")
|
|
|
|
|
|
|
|
def test_debug_fire_event(self):
|
|
|
|
""" Test if we can fire an event from the debug interface. """
|
|
|
|
test_value = []
|
|
|
|
|
|
|
|
def listener(event): # pylint: disable=unused-argument
|
|
|
|
""" Helper method that will verify that our event got called and
|
|
|
|
that test if our data came through. """
|
|
|
|
if "test" in event.data:
|
|
|
|
test_value.append(1)
|
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.bus.listen_once_event("test_event_with_data", listener)
|
2013-11-05 01:16:27 +00:00
|
|
|
|
|
|
|
requests.post(
|
|
|
|
_url(hah.URL_FIRE_EVENT),
|
|
|
|
data={"event_type": "test_event_with_data",
|
2013-11-11 00:46:48 +00:00
|
|
|
"event_data": '{"test": 1}',
|
|
|
|
"api_password": API_PASSWORD})
|
2013-11-05 01:16:27 +00:00
|
|
|
|
|
|
|
# Allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
def test_api_list_state_entities(self):
|
|
|
|
""" Test if the debug interface allows us to list state entities. """
|
2013-11-01 18:34:43 +00:00
|
|
|
req = requests.get(_url(hah.URL_API_STATES),
|
2013-11-11 00:46:48 +00:00
|
|
|
data={"api_password": API_PASSWORD})
|
2013-10-25 10:05:58 +00:00
|
|
|
|
|
|
|
data = req.json()
|
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
self.assertEqual(self.statemachine.entity_ids,
|
|
|
|
data['entity_ids'])
|
2013-10-25 10:05:58 +00:00
|
|
|
|
|
|
|
def test_api_get_state(self):
|
2013-10-29 07:22:38 +00:00
|
|
|
""" Test if the debug interface allows us to get a state. """
|
|
|
|
req = requests.get(
|
2014-01-20 07:37:40 +00:00
|
|
|
_url(hah.URL_API_STATES_ENTITY.format("test")),
|
2013-11-11 00:46:48 +00:00
|
|
|
data={"api_password": API_PASSWORD})
|
2013-10-25 10:05:58 +00:00
|
|
|
|
2014-01-20 03:10:40 +00:00
|
|
|
data = ha.State.from_json_dict(req.json())
|
2013-10-25 10:05:58 +00:00
|
|
|
|
|
|
|
state = self.statemachine.get_state("test")
|
2013-10-28 00:39:54 +00:00
|
|
|
|
2014-01-20 03:10:40 +00:00
|
|
|
self.assertEqual(data.state, state.state)
|
|
|
|
self.assertEqual(data.last_changed, state.last_changed)
|
|
|
|
self.assertEqual(data.attributes, state.attributes)
|
2013-10-25 10:05:58 +00:00
|
|
|
|
2013-11-01 18:34:43 +00:00
|
|
|
def test_api_get_non_existing_state(self):
|
|
|
|
""" Test if the debug interface allows us to get a state. """
|
|
|
|
req = requests.get(
|
2014-01-20 07:37:40 +00:00
|
|
|
_url(hah.URL_API_STATES_ENTITY.format("does_not_exist")),
|
2013-11-11 00:46:48 +00:00
|
|
|
params={"api_password": API_PASSWORD})
|
2013-11-01 18:34:43 +00:00
|
|
|
|
|
|
|
self.assertEqual(req.status_code, 422)
|
2013-10-25 10:05:58 +00:00
|
|
|
|
2013-09-28 18:09:36 +00:00
|
|
|
def test_api_state_change(self):
|
2014-01-20 07:37:40 +00:00
|
|
|
""" Test if we can change the state of an entity that exists. """
|
2013-10-05 19:28:18 +00:00
|
|
|
|
2013-11-09 22:23:26 +00:00
|
|
|
self.statemachine.set_state("test.test", "not_to_be_set_state")
|
2013-10-05 19:28:18 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
requests.post(_url(hah.URL_API_STATES_ENTITY.format("test.test")),
|
2013-11-11 00:46:48 +00:00
|
|
|
data={"new_state": "debug_state_change2",
|
|
|
|
"api_password": API_PASSWORD})
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2014-01-20 03:10:40 +00:00
|
|
|
self.assertEqual(self.statemachine.get_state("test.test").state,
|
2013-10-08 06:55:19 +00:00
|
|
|
"debug_state_change2")
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-10-08 06:55:19 +00:00
|
|
|
# pylint: disable=invalid-name
|
2014-01-20 07:37:40 +00:00
|
|
|
def test_api_state_change_of_non_existing_entity(self):
|
2013-10-08 06:55:19 +00:00
|
|
|
""" Test if the API allows us to change a state of
|
2014-01-20 07:37:40 +00:00
|
|
|
a non existing entity. """
|
2013-10-08 06:55:19 +00:00
|
|
|
|
|
|
|
new_state = "debug_state_change"
|
2013-10-05 19:28:18 +00:00
|
|
|
|
2013-10-29 07:22:38 +00:00
|
|
|
req = requests.post(
|
2014-01-20 07:37:40 +00:00
|
|
|
_url(hah.URL_API_STATES_ENTITY.format(
|
|
|
|
"test_entity_that_does_not_exist")),
|
2013-11-11 00:46:48 +00:00
|
|
|
data={"new_state": new_state,
|
|
|
|
"api_password": API_PASSWORD})
|
2013-10-08 06:55:19 +00:00
|
|
|
|
|
|
|
cur_state = (self.statemachine.
|
2014-01-20 07:37:40 +00:00
|
|
|
get_state("test_entity_that_does_not_exist").state)
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-10-29 07:22:38 +00:00
|
|
|
self.assertEqual(req.status_code, 201)
|
2013-10-08 06:55:19 +00:00
|
|
|
self.assertEqual(cur_state, new_state)
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-10-08 06:55:19 +00:00
|
|
|
# pylint: disable=invalid-name
|
2013-09-28 18:09:36 +00:00
|
|
|
def test_api_fire_event_with_no_data(self):
|
|
|
|
""" Test if the API allows us to fire an event. """
|
|
|
|
test_value = []
|
|
|
|
|
2013-10-08 06:55:19 +00:00
|
|
|
def listener(event): # pylint: disable=unused-argument
|
2013-09-28 18:09:36 +00:00
|
|
|
""" Helper method that will verify our event got called. """
|
|
|
|
test_value.append(1)
|
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.bus.listen_once_event("test.event_no_data", listener)
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-10-29 07:22:38 +00:00
|
|
|
requests.post(
|
2013-11-09 22:23:26 +00:00
|
|
|
_url(hah.URL_API_EVENTS_EVENT.format("test.event_no_data")),
|
2013-11-11 00:46:48 +00:00
|
|
|
data={"api_password": API_PASSWORD})
|
2013-09-28 18:09:36 +00:00
|
|
|
|
|
|
|
# Allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|
|
|
|
|
2013-10-08 06:55:19 +00:00
|
|
|
# pylint: disable=invalid-name
|
2013-09-28 18:09:36 +00:00
|
|
|
def test_api_fire_event_with_data(self):
|
|
|
|
""" Test if the API allows us to fire an event. """
|
|
|
|
test_value = []
|
|
|
|
|
2013-10-08 06:55:19 +00:00
|
|
|
def listener(event): # pylint: disable=unused-argument
|
2013-09-28 18:09:36 +00:00
|
|
|
""" Helper method that will verify that our event got called and
|
|
|
|
that test if our data came through. """
|
|
|
|
if "test" in event.data:
|
|
|
|
test_value.append(1)
|
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.bus.listen_once_event("test_event_with_data", listener)
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-10-29 07:22:38 +00:00
|
|
|
requests.post(
|
2013-11-04 23:18:39 +00:00
|
|
|
_url(hah.URL_API_EVENTS_EVENT.format("test_event_with_data")),
|
2013-11-11 00:46:48 +00:00
|
|
|
data={"event_data": '{"test": 1}',
|
|
|
|
"api_password": API_PASSWORD})
|
2013-09-28 18:09:36 +00:00
|
|
|
|
|
|
|
# Allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|
|
|
|
|
2013-10-08 06:55:19 +00:00
|
|
|
# pylint: disable=invalid-name
|
2013-09-28 18:09:36 +00:00
|
|
|
def test_api_fire_event_with_invalid_json(self):
|
|
|
|
""" Test if the API allows us to fire an event. """
|
|
|
|
test_value = []
|
|
|
|
|
2013-10-08 06:55:19 +00:00
|
|
|
def listener(event): # pylint: disable=unused-argument
|
2013-09-28 18:09:36 +00:00
|
|
|
""" Helper method that will verify our event got called. """
|
|
|
|
test_value.append(1)
|
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.bus.listen_once_event("test_event_with_bad_data", listener)
|
2013-09-28 18:09:36 +00:00
|
|
|
|
2013-10-29 07:22:38 +00:00
|
|
|
req = requests.post(
|
2013-11-01 18:34:43 +00:00
|
|
|
_url(hah.URL_API_EVENTS_EVENT.format("test_event")),
|
2013-11-11 00:46:48 +00:00
|
|
|
data={"event_data": 'not json',
|
|
|
|
"api_password": API_PASSWORD})
|
2013-09-28 18:09:36 +00:00
|
|
|
|
|
|
|
# It shouldn't but if it fires, allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
2013-11-01 18:34:43 +00:00
|
|
|
self.assertEqual(req.status_code, 422)
|
2013-09-28 18:09:36 +00:00
|
|
|
self.assertEqual(len(test_value), 0)
|
2013-10-25 10:05:58 +00:00
|
|
|
|
2013-11-01 19:28:18 +00:00
|
|
|
def test_api_get_event_listeners(self):
|
|
|
|
""" Test if we can get the list of events being listened for. """
|
|
|
|
req = requests.get(_url(hah.URL_API_EVENTS),
|
2013-11-11 00:46:48 +00:00
|
|
|
params={"api_password": API_PASSWORD})
|
2013-11-01 19:28:18 +00:00
|
|
|
|
|
|
|
data = req.json()
|
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.assertEqual(data['event_listeners'], self.bus.event_listeners)
|
|
|
|
|
|
|
|
def test_api_get_services(self):
|
|
|
|
""" Test if we can get a dict describing current services. """
|
|
|
|
req = requests.get(_url(hah.URL_API_SERVICES),
|
|
|
|
params={"api_password": API_PASSWORD})
|
|
|
|
|
|
|
|
data = req.json()
|
|
|
|
|
|
|
|
self.assertEqual(data['services'], self.bus.services)
|
|
|
|
|
|
|
|
def test_api_call_service_no_data(self):
|
|
|
|
""" Test if the API allows us to call a service. """
|
|
|
|
test_value = []
|
|
|
|
|
|
|
|
def listener(service_call): # pylint: disable=unused-argument
|
|
|
|
""" Helper method that will verify that our service got called. """
|
|
|
|
test_value.append(1)
|
|
|
|
|
|
|
|
self.bus.register_service("test_domain", "test_service", listener)
|
|
|
|
|
|
|
|
requests.post(
|
|
|
|
_url(hah.URL_API_SERVICES_SERVICE.format(
|
|
|
|
"test_domain", "test_service")),
|
|
|
|
data={"api_password": API_PASSWORD})
|
|
|
|
|
|
|
|
# Allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|
|
|
|
|
|
|
|
def test_api_call_service_with_data(self):
|
|
|
|
""" Test if the API allows us to call a service. """
|
|
|
|
test_value = []
|
|
|
|
|
|
|
|
def listener(service_call): # pylint: disable=unused-argument
|
|
|
|
""" Helper method that will verify that our service got called and
|
|
|
|
that test if our data came through. """
|
|
|
|
if "test" in service_call.data:
|
|
|
|
test_value.append(1)
|
|
|
|
|
|
|
|
self.bus.register_service("test_domain", "test_service", listener)
|
|
|
|
|
|
|
|
requests.post(
|
|
|
|
_url(hah.URL_API_SERVICES_SERVICE.format(
|
|
|
|
"test_domain", "test_service")),
|
|
|
|
data={"service_data": '{"test": 1}',
|
|
|
|
"api_password": API_PASSWORD})
|
|
|
|
|
|
|
|
# Allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|
2013-11-01 19:28:18 +00:00
|
|
|
|
2013-11-11 00:46:48 +00:00
|
|
|
|
2013-11-01 18:34:43 +00:00
|
|
|
class TestRemote(unittest.TestCase):
|
|
|
|
""" Test the homeassistant.remote module. """
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls): # pylint: disable=invalid-name
|
|
|
|
""" things to be run when tests are started. """
|
2013-11-20 07:48:08 +00:00
|
|
|
cls.bus, cls.statemachine = ensure_homeassistant_started()
|
2013-11-01 18:34:43 +00:00
|
|
|
|
|
|
|
cls.remote_sm = remote.StateMachine("127.0.0.1", API_PASSWORD)
|
2013-11-20 07:48:08 +00:00
|
|
|
cls.remote_eb = remote.Bus("127.0.0.1", API_PASSWORD)
|
2013-11-01 18:34:43 +00:00
|
|
|
cls.sm_with_remote_eb = ha.StateMachine(cls.remote_eb)
|
|
|
|
cls.sm_with_remote_eb.set_state("test", "a_state")
|
|
|
|
|
2013-10-25 10:05:58 +00:00
|
|
|
# pylint: disable=invalid-name
|
2014-01-20 07:37:40 +00:00
|
|
|
def test_remote_sm_list_state_entities(self):
|
|
|
|
""" Test if the debug interface allows us to list state entity ids. """
|
2013-11-01 18:34:43 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
self.assertEqual(self.statemachine.entity_ids,
|
|
|
|
self.remote_sm.entity_ids)
|
2013-11-01 18:34:43 +00:00
|
|
|
|
|
|
|
def test_remote_sm_get_state(self):
|
2014-01-20 07:37:40 +00:00
|
|
|
""" Test if debug interface allows us to get state of an entity. """
|
2013-11-01 18:34:43 +00:00
|
|
|
remote_state = self.remote_sm.get_state("test")
|
|
|
|
|
|
|
|
state = self.statemachine.get_state("test")
|
|
|
|
|
2014-01-20 03:10:40 +00:00
|
|
|
self.assertEqual(remote_state.state, state.state)
|
|
|
|
self.assertEqual(remote_state.last_changed, state.last_changed)
|
|
|
|
self.assertEqual(remote_state.attributes, state.attributes)
|
2013-11-01 18:34:43 +00:00
|
|
|
|
|
|
|
def test_remote_sm_get_non_existing_state(self):
|
2014-01-20 07:37:40 +00:00
|
|
|
""" Test remote state machine to get state of non existing entity. """
|
2013-11-01 18:34:43 +00:00
|
|
|
self.assertEqual(self.remote_sm.get_state("test_does_not_exist"), None)
|
|
|
|
|
|
|
|
def test_remote_sm_state_change(self):
|
2014-01-20 07:37:40 +00:00
|
|
|
""" Test if we can change the state of an existing entity. """
|
2013-11-01 18:34:43 +00:00
|
|
|
|
|
|
|
self.remote_sm.set_state("test", "set_remotely", {"test": 1})
|
|
|
|
|
|
|
|
state = self.statemachine.get_state("test")
|
|
|
|
|
2014-01-20 03:10:40 +00:00
|
|
|
self.assertEqual(state.state, "set_remotely")
|
|
|
|
self.assertEqual(state.attributes['test'], 1)
|
2013-11-01 18:34:43 +00:00
|
|
|
|
2013-11-01 19:28:18 +00:00
|
|
|
def test_remote_eb_listening_for_same(self):
|
|
|
|
""" Test if remote EB correctly reports listener overview. """
|
2013-11-20 07:48:08 +00:00
|
|
|
self.assertEqual(self.bus.event_listeners,
|
|
|
|
self.remote_eb.event_listeners)
|
2013-11-01 18:34:43 +00:00
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
2013-10-25 10:05:58 +00:00
|
|
|
def test_remote_eb_fire_event_with_no_data(self):
|
2013-11-20 07:48:08 +00:00
|
|
|
""" Test if the remote bus allows us to fire an event. """
|
2013-10-25 10:05:58 +00:00
|
|
|
test_value = []
|
|
|
|
|
|
|
|
def listener(event): # pylint: disable=unused-argument
|
|
|
|
""" Helper method that will verify our event got called. """
|
|
|
|
test_value.append(1)
|
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.bus.listen_once_event("test_event_no_data", listener)
|
2013-10-25 10:05:58 +00:00
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.remote_eb.fire_event("test_event_no_data")
|
2013-10-25 10:05:58 +00:00
|
|
|
|
|
|
|
# Allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def test_remote_eb_fire_event_with_data(self):
|
2013-11-20 07:48:08 +00:00
|
|
|
""" Test if the remote bus allows us to fire an event. """
|
2013-10-25 10:05:58 +00:00
|
|
|
test_value = []
|
|
|
|
|
|
|
|
def listener(event): # pylint: disable=unused-argument
|
|
|
|
""" Helper method that will verify our event got called. """
|
|
|
|
if event.data["test"] == 1:
|
|
|
|
test_value.append(1)
|
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.bus.listen_once_event("test_event_with_data", listener)
|
|
|
|
|
|
|
|
self.remote_eb.fire_event("test_event_with_data", {"test": 1})
|
|
|
|
|
|
|
|
# Allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def test_remote_eb_call_service_with_no_data(self):
|
|
|
|
""" Test if the remote bus allows us to fire a service. """
|
|
|
|
test_value = []
|
|
|
|
|
|
|
|
def listener(service_call): # pylint: disable=unused-argument
|
|
|
|
""" Helper method that will verify our service got called. """
|
|
|
|
test_value.append(1)
|
|
|
|
|
|
|
|
self.bus.register_service("test_domain", "test_service", listener)
|
|
|
|
|
|
|
|
self.remote_eb.call_service("test_domain", "test_service")
|
|
|
|
|
|
|
|
# Allow the service call to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def test_remote_eb_call_service_with_data(self):
|
|
|
|
""" Test if the remote bus allows us to fire an event. """
|
|
|
|
test_value = []
|
|
|
|
|
|
|
|
def listener(service_call): # pylint: disable=unused-argument
|
|
|
|
""" Helper method that will verify our service got called. """
|
|
|
|
if service_call.data["test"] == 1:
|
|
|
|
test_value.append(1)
|
|
|
|
|
|
|
|
self.bus.register_service("test_domain", "test_service", listener)
|
2013-10-25 10:05:58 +00:00
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.remote_eb.call_service("test_domain", "test_service", {"test": 1})
|
2013-10-25 10:05:58 +00:00
|
|
|
|
|
|
|
# Allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|
|
|
|
|
2013-10-29 01:45:35 +00:00
|
|
|
def test_local_sm_with_remote_eb(self):
|
|
|
|
""" Test if we get the event if we change a state on a
|
2013-11-20 07:48:08 +00:00
|
|
|
StateMachine connected to a remote bus. """
|
2013-10-29 01:45:35 +00:00
|
|
|
test_value = []
|
|
|
|
|
|
|
|
def listener(event): # pylint: disable=unused-argument
|
|
|
|
""" Helper method that will verify our event got called. """
|
|
|
|
test_value.append(1)
|
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
self.bus.listen_once_event(ha.EVENT_STATE_CHANGED, listener)
|
2013-10-29 01:45:35 +00:00
|
|
|
|
|
|
|
self.sm_with_remote_eb.set_state("test", "local sm with remote eb")
|
|
|
|
|
|
|
|
# Allow the event to take place
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(test_value), 1)
|