core/homeassistant/test.py

434 lines
14 KiB
Python
Raw Normal View History

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
2014-10-17 07:17:02 +00:00
import json
2013-09-28 18:09:36 +00:00
import requests
import homeassistant as ha
import homeassistant.remote as remote
import homeassistant.components.http as http
2013-09-28 18:09:36 +00:00
API_PASSWORD = "test1234"
HTTP_BASE_URL = "http://127.0.0.1:{}".format(remote.SERVER_PORT)
2013-09-28 18:09:36 +00:00
2014-10-17 07:17:02 +00:00
HA_HEADERS = {remote.AUTH_HEADER: API_PASSWORD}
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. """
hass = None
slave = 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
if not HAHelper.hass:
hass = ha.HomeAssistant()
2013-10-29 07:22:38 +00:00
2014-10-17 07:17:02 +00:00
hass.bus.listen('test_event', lambda _: _)
hass.states.set('test.test', 'a_state')
2013-09-28 18:09:36 +00:00
http.setup(hass,
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD}})
2013-09-28 18:09:36 +00:00
hass.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.hass = hass
2013-11-01 18:34:43 +00:00
return HAHelper.hass
2013-09-28 18:09:36 +00:00
2013-11-11 00:46:48 +00:00
def ensure_slave_started():
""" Ensure a home assistant slave is started. """
ensure_homeassistant_started()
if not HAHelper.slave:
local_api = remote.API("127.0.0.1", API_PASSWORD, 8124)
remote_api = remote.API("127.0.0.1", API_PASSWORD)
slave = remote.HomeAssistant(remote_api, local_api)
http.setup(slave,
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
http.CONF_SERVER_PORT: 8124}})
slave.start()
# Give objects time to startup
time.sleep(1)
HAHelper.slave = slave
return HAHelper.slave
2013-11-01 18:34:43 +00:00
# pylint: disable=too-many-public-methods
class TestHTTP(unittest.TestCase):
2013-11-01 18:34:43 +00:00
""" Test the HTTP debug interface and API. """
@classmethod
def setUpClass(cls): # pylint: disable=invalid-name
""" things to be run when tests are started. """
cls.hass = ensure_homeassistant_started()
2013-09-28 18:09:36 +00:00
def test_api_password(self):
""" Test if we get access denied if we omit or provide
a wrong api password. """
req = requests.get(
_url(remote.URL_API_STATES_ENTITY.format("test")))
2013-09-28 18:09:36 +00:00
self.assertEqual(req.status_code, 401)
req = requests.get(
_url(remote.URL_API_STATES_ENTITY.format("test")),
2014-10-17 07:17:02 +00:00
headers={remote.AUTH_HEADER: 'wrongpassword'})
2013-09-28 18:09:36 +00:00
self.assertEqual(req.status_code, 401)
def test_api_list_state_entities(self):
""" Test if the debug interface allows us to list state entities. """
req = requests.get(_url(remote.URL_API_STATES),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
2014-10-17 07:17:02 +00:00
remote_data = [ha.State.from_dict(item) for item in req.json()]
2014-10-17 07:17:02 +00:00
self.assertEqual(self.hass.states.all(), remote_data)
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(
_url(remote.URL_API_STATES_ENTITY.format("test.test")),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
2014-01-23 03:40:19 +00:00
data = ha.State.from_dict(req.json())
state = self.hass.states.get("test.test")
self.assertEqual(data.state, state.state)
self.assertEqual(data.last_changed, state.last_changed)
self.assertEqual(data.attributes, state.attributes)
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(
_url(remote.URL_API_STATES_ENTITY.format("does_not_exist")),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
2013-11-01 18:34:43 +00:00
2014-10-17 07:17:02 +00:00
self.assertEqual(req.status_code, 404)
2013-09-28 18:09:36 +00:00
def test_api_state_change(self):
""" Test if we can change the state of an entity that exists. """
self.hass.states.set("test.test", "not_to_be_set")
requests.post(_url(remote.URL_API_STATES_ENTITY.format("test.test")),
2014-10-17 07:17:02 +00:00
data=json.dumps({"state": "debug_state_change2",
"api_password": API_PASSWORD}))
2013-09-28 18:09:36 +00:00
self.assertEqual(self.hass.states.get("test.test").state,
"debug_state_change2")
2013-09-28 18:09:36 +00:00
# pylint: disable=invalid-name
def test_api_state_change_of_non_existing_entity(self):
""" Test if the API allows us to change a state of
a non existing entity. """
new_state = "debug_state_change"
2013-10-29 07:22:38 +00:00
req = requests.post(
_url(remote.URL_API_STATES_ENTITY.format(
"test_entity.that_does_not_exist")),
2014-10-17 07:17:02 +00:00
data=json.dumps({"state": new_state,
"api_password": API_PASSWORD}))
cur_state = (self.hass.states.
get("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)
self.assertEqual(cur_state, new_state)
2013-09-28 18:09:36 +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 = []
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)
self.hass.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(
_url(remote.URL_API_EVENTS_EVENT.format("test.event_no_data")),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
2013-09-28 18:09:36 +00:00
# Allow the event to take place
time.sleep(1)
self.assertEqual(len(test_value), 1)
# 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 = []
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)
self.hass.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(
_url(remote.URL_API_EVENTS_EVENT.format("test_event_with_data")),
2014-11-03 01:52:51 +00:00
data=json.dumps({"test": 1}),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
2013-09-28 18:09:36 +00:00
# Allow the event to take place
time.sleep(1)
self.assertEqual(len(test_value), 1)
# 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 = []
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)
self.hass.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(
_url(remote.URL_API_EVENTS_EVENT.format("test_event")),
2014-11-03 01:52:51 +00:00
data=json.dumps('not an object'),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
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)
def test_api_get_event_listeners(self):
""" Test if we can get the list of events being listened for. """
req = requests.get(_url(remote.URL_API_EVENTS),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
2014-10-20 06:37:43 +00:00
local = self.hass.bus.listeners
for event in req.json():
self.assertEqual(event["listener_count"],
local.pop(event["event"]))
self.assertEqual(len(local), 0)
def test_api_get_services(self):
""" Test if we can get a dict describing current services. """
req = requests.get(_url(remote.URL_API_SERVICES),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
2014-10-20 06:37:43 +00:00
local_services = self.hass.services.services
for serv_domain in req.json():
local = local_services.pop(serv_domain["domain"])
self.assertEqual(serv_domain["services"], local)
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.hass.services.register("test_domain", "test_service", listener)
requests.post(
_url(remote.URL_API_SERVICES_SERVICE.format(
"test_domain", "test_service")),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
# 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.hass.services.register("test_domain", "test_service", listener)
requests.post(
_url(remote.URL_API_SERVICES_SERVICE.format(
"test_domain", "test_service")),
2014-10-20 01:41:06 +00:00
data=json.dumps({"test": 1}),
2014-10-17 07:17:02 +00:00
headers=HA_HEADERS)
# Allow the event to take place
time.sleep(1)
self.assertEqual(len(test_value), 1)
2013-11-11 00:46:48 +00:00
class TestRemoteMethods(unittest.TestCase):
2013-11-01 18:34:43 +00:00
""" Test the homeassistant.remote module. """
@classmethod
def setUpClass(cls): # pylint: disable=invalid-name
""" things to be run when tests are started. """
cls.hass = ensure_homeassistant_started()
2013-11-01 18:34:43 +00:00
cls.api = remote.API("127.0.0.1", API_PASSWORD)
2013-11-01 18:34:43 +00:00
def test_get_event_listeners(self):
""" Test Python API get_event_listeners. """
2014-10-20 06:37:43 +00:00
local = self.hass.bus.listeners
2013-11-01 18:34:43 +00:00
2014-10-20 06:37:43 +00:00
for event in remote.get_event_listeners(self.api):
self.assertEqual(event["listener_count"],
local.pop(event["event"]))
self.assertEqual(len(local), 0)
2013-11-01 18:34:43 +00:00
def test_fire_event(self):
""" Test Python API fire_event. """
test_value = []
def listener(event): # pylint: disable=unused-argument
""" Helper method that will verify our event got called. """
test_value.append(1)
self.hass.listen_once_event("test.event_no_data", listener)
remote.fire_event(self.api, "test.event_no_data")
# Allow the event to take place
time.sleep(1)
self.assertEqual(len(test_value), 1)
def test_get_state(self):
""" Test Python API get_state. """
self.assertEqual(
remote.get_state(self.api, 'test.test'),
self.hass.states.get('test.test'))
def test_get_states(self):
""" Test Python API get_state_entity_ids. """
self.assertEqual(
remote.get_states(self.api), self.hass.states.all())
def test_set_state(self):
""" Test Python API set_state. """
remote.set_state(self.api, 'test.test', 'set_test')
self.assertEqual(self.hass.states.get('test.test').state, 'set_test')
def test_is_state(self):
""" Test Python API is_state. """
self.assertTrue(
remote.is_state(self.api, 'test.test',
self.hass.states.get('test.test').state))
def test_get_services(self):
""" Test Python API get_services. """
2014-10-20 06:37:43 +00:00
local_services = self.hass.services.services
for serv_domain in remote.get_services(self.api):
local = local_services.pop(serv_domain["domain"])
self.assertEqual(serv_domain["services"], local)
def test_call_service(self):
""" Test Python API call_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.hass.services.register("test_domain", "test_service", listener)
remote.call_service(self.api, "test_domain", "test_service")
# Allow the event to take place
time.sleep(1)
self.assertEqual(len(test_value), 1)
class TestRemoteClasses(unittest.TestCase):
""" Test the homeassistant.remote module. """
@classmethod
def setUpClass(cls): # pylint: disable=invalid-name
""" things to be run when tests are started. """
cls.hass = ensure_homeassistant_started()
cls.slave = ensure_slave_started()
def test_statemachine_init(self):
""" Tests if remote.StateMachine copies all states on init. """
2014-10-20 06:37:43 +00:00
for state in self.hass.states.all():
self.assertEqual(
self.slave.states.get(state.entity_id), state)
def test_statemachine_set(self):
""" Tests if setting the state on a slave is recorded. """
self.slave.states.set("remote.test", "remote.statemachine test")
# Allow interaction between 2 instances
time.sleep(1)
self.assertEqual(self.slave.states.get("remote.test").state,
"remote.statemachine test")
def test_eventbus_fire(self):
""" Test if events fired from the eventbus get fired. """
test_value = []
def listener(event): # pylint: disable=unused-argument
""" Helper method that will verify our event got called. """
test_value.append(1)
self.slave.listen_once_event("test.event_no_data", listener)
self.slave.bus.fire("test.event_no_data")
# Allow the event to take place
time.sleep(1)
self.assertEqual(len(test_value), 1)