Reorg tests folder
This commit is contained in:
1 parent
fed36d2cd0
commit
0c56fde5a9
29 files changed
+14
-14
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,366 @@
|
||||
"""
|
||||
tests.test_component_http
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests Home Assistant HTTP component does what it should do.
|
||||
"""
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
import unittest
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.bootstrap as bootstrap
|
||||
import homeassistant.remote as remote
|
||||
import homeassistant.components.http as http
|
||||
from homeassistant.const import HTTP_HEADER_HA_AUTH
|
||||
|
||||
API_PASSWORD = "test1234"
|
||||
|
||||
# Somehow the socket that holds the default port does not get released
|
||||
# when we close down HA in a different test case. Until I have figured
|
||||
# out what is going on, let's run this test on a different port.
|
||||
SERVER_PORT = 8120
|
||||
|
||||
HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT)
|
||||
|
||||
HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD}
|
||||
|
||||
hass = None
|
||||
|
||||
|
||||
def _url(path=""):
|
||||
""" Helper method to generate urls. """
|
||||
return HTTP_BASE_URL + path
|
||||
|
||||
|
||||
def setUpModule(): # pylint: disable=invalid-name
|
||||
""" Initalizes a Home Assistant server. """
|
||||
global hass
|
||||
|
||||
hass = ha.HomeAssistant()
|
||||
|
||||
hass.bus.listen('test_event', lambda _: _)
|
||||
hass.states.set('test.test', 'a_state')
|
||||
|
||||
bootstrap.setup_component(
|
||||
hass, http.DOMAIN,
|
||||
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
|
||||
http.CONF_SERVER_PORT: SERVER_PORT}})
|
||||
|
||||
bootstrap.setup_component(hass, 'api')
|
||||
|
||||
hass.start()
|
||||
|
||||
|
||||
def tearDownModule(): # pylint: disable=invalid-name
|
||||
""" Stops the Home Assistant server. """
|
||||
hass.stop()
|
||||
|
||||
|
||||
class TestAPI(unittest.TestCase):
|
||||
""" Test the API. """
|
||||
|
||||
# TODO move back to http component and test with use_auth.
|
||||
def test_access_denied_without_password(self):
|
||||
req = requests.get(
|
||||
_url(remote.URL_API_STATES_ENTITY.format("test")))
|
||||
|
||||
self.assertEqual(401, req.status_code)
|
||||
|
||||
def test_access_denied_with_wrong_password(self):
|
||||
req = requests.get(
|
||||
_url(remote.URL_API_STATES_ENTITY.format("test")),
|
||||
headers={HTTP_HEADER_HA_AUTH: 'wrongpassword'})
|
||||
|
||||
self.assertEqual(401, req.status_code)
|
||||
|
||||
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),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
remote_data = [ha.State.from_dict(item) for item in req.json()]
|
||||
|
||||
self.assertEqual(hass.states.all(), remote_data)
|
||||
|
||||
def test_api_get_state(self):
|
||||
""" Test if the debug interface allows us to get a state. """
|
||||
req = requests.get(
|
||||
_url(remote.URL_API_STATES_ENTITY.format("test.test")),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
data = ha.State.from_dict(req.json())
|
||||
|
||||
state = hass.states.get("test.test")
|
||||
|
||||
self.assertEqual(state.state, data.state)
|
||||
self.assertEqual(state.last_changed, data.last_changed)
|
||||
self.assertEqual(state.attributes, data.attributes)
|
||||
|
||||
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")),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
self.assertEqual(404, req.status_code)
|
||||
|
||||
def test_api_state_change(self):
|
||||
""" Test if we can change the state of an entity that exists. """
|
||||
|
||||
hass.states.set("test.test", "not_to_be_set")
|
||||
|
||||
requests.post(_url(remote.URL_API_STATES_ENTITY.format("test.test")),
|
||||
data=json.dumps({"state": "debug_state_change2"}),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
self.assertEqual("debug_state_change2",
|
||||
hass.states.get("test.test").state)
|
||||
|
||||
# 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"
|
||||
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_STATES_ENTITY.format(
|
||||
"test_entity.that_does_not_exist")),
|
||||
data=json.dumps({'state': new_state}),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
cur_state = (hass.states.
|
||||
get("test_entity.that_does_not_exist").state)
|
||||
|
||||
self.assertEqual(201, req.status_code)
|
||||
self.assertEqual(cur_state, new_state)
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_api_state_change_with_bad_data(self):
|
||||
""" Test if API sends appropriate error if we omit state. """
|
||||
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_STATES_ENTITY.format(
|
||||
"test_entity.that_does_not_exist")),
|
||||
data=json.dumps({}),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
self.assertEqual(400, req.status_code)
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_api_fire_event_with_no_data(self):
|
||||
""" Test if the API allows us to fire an event. """
|
||||
test_value = []
|
||||
|
||||
def listener(event):
|
||||
""" Helper method that will verify our event got called. """
|
||||
test_value.append(1)
|
||||
|
||||
hass.bus.listen_once("test.event_no_data", listener)
|
||||
|
||||
requests.post(
|
||||
_url(remote.URL_API_EVENTS_EVENT.format("test.event_no_data")),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(test_value))
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_api_fire_event_with_data(self):
|
||||
""" Test if the API allows us to fire an event. """
|
||||
test_value = []
|
||||
|
||||
def listener(event):
|
||||
""" 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)
|
||||
|
||||
hass.bus.listen_once("test_event_with_data", listener)
|
||||
|
||||
requests.post(
|
||||
_url(remote.URL_API_EVENTS_EVENT.format("test_event_with_data")),
|
||||
data=json.dumps({"test": 1}),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(test_value))
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_api_fire_event_with_invalid_json(self):
|
||||
""" Test if the API allows us to fire an event. """
|
||||
test_value = []
|
||||
|
||||
def listener(event):
|
||||
""" Helper method that will verify our event got called. """
|
||||
test_value.append(1)
|
||||
|
||||
hass.bus.listen_once("test_event_bad_data", listener)
|
||||
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_EVENTS_EVENT.format("test_event_bad_data")),
|
||||
data=json.dumps('not an object'),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(422, req.status_code)
|
||||
self.assertEqual(0, len(test_value))
|
||||
|
||||
# Try now with valid but unusable JSON
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_EVENTS_EVENT.format("test_event_bad_data")),
|
||||
data=json.dumps([1, 2, 3]),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(422, req.status_code)
|
||||
self.assertEqual(0, len(test_value))
|
||||
|
||||
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),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
local = hass.bus.listeners
|
||||
|
||||
for event in req.json():
|
||||
self.assertEqual(event["listener_count"],
|
||||
local.pop(event["event"]))
|
||||
|
||||
self.assertEqual(0, len(local))
|
||||
|
||||
def test_api_get_services(self):
|
||||
""" Test if we can get a dict describing current services. """
|
||||
req = requests.get(_url(remote.URL_API_SERVICES),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
local_services = hass.services.services
|
||||
|
||||
for serv_domain in req.json():
|
||||
local = local_services.pop(serv_domain["domain"])
|
||||
|
||||
self.assertEqual(local, serv_domain["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):
|
||||
""" Helper method that will verify that our service got called. """
|
||||
test_value.append(1)
|
||||
|
||||
hass.services.register("test_domain", "test_service", listener)
|
||||
|
||||
requests.post(
|
||||
_url(remote.URL_API_SERVICES_SERVICE.format(
|
||||
"test_domain", "test_service")),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(test_value))
|
||||
|
||||
def test_api_call_service_with_data(self):
|
||||
""" Test if the API allows us to call a service. """
|
||||
test_value = []
|
||||
|
||||
def listener(service_call):
|
||||
""" 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)
|
||||
|
||||
hass.services.register("test_domain", "test_service", listener)
|
||||
|
||||
requests.post(
|
||||
_url(remote.URL_API_SERVICES_SERVICE.format(
|
||||
"test_domain", "test_service")),
|
||||
data=json.dumps({"test": 1}),
|
||||
headers=HA_HEADERS)
|
||||
|
||||
hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(test_value))
|
||||
|
||||
def test_api_event_forward(self):
|
||||
""" Test setting up event forwarding. """
|
||||
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_EVENT_FORWARD),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(400, req.status_code)
|
||||
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_EVENT_FORWARD),
|
||||
data=json.dumps({'host': '127.0.0.1'}),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(400, req.status_code)
|
||||
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_EVENT_FORWARD),
|
||||
data=json.dumps({'api_password': 'bla-di-bla'}),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(400, req.status_code)
|
||||
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_EVENT_FORWARD),
|
||||
data=json.dumps({
|
||||
'api_password': 'bla-di-bla',
|
||||
'host': '127.0.0.1',
|
||||
'port': 'abcd'
|
||||
}),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(422, req.status_code)
|
||||
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_EVENT_FORWARD),
|
||||
data=json.dumps({
|
||||
'api_password': 'bla-di-bla',
|
||||
'host': '127.0.0.1',
|
||||
'port': '8125'
|
||||
}),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(422, req.status_code)
|
||||
|
||||
# Setup a real one
|
||||
req = requests.post(
|
||||
_url(remote.URL_API_EVENT_FORWARD),
|
||||
data=json.dumps({
|
||||
'api_password': API_PASSWORD,
|
||||
'host': '127.0.0.1',
|
||||
'port': SERVER_PORT
|
||||
}),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(200, req.status_code)
|
||||
|
||||
# Delete it again..
|
||||
req = requests.delete(
|
||||
_url(remote.URL_API_EVENT_FORWARD),
|
||||
data=json.dumps({}),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(400, req.status_code)
|
||||
|
||||
req = requests.delete(
|
||||
_url(remote.URL_API_EVENT_FORWARD),
|
||||
data=json.dumps({
|
||||
'host': '127.0.0.1',
|
||||
'port': 'abcd'
|
||||
}),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(422, req.status_code)
|
||||
|
||||
req = requests.delete(
|
||||
_url(remote.URL_API_EVENT_FORWARD),
|
||||
data=json.dumps({
|
||||
'host': '127.0.0.1',
|
||||
'port': SERVER_PORT
|
||||
}),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(200, req.status_code)
|
||||
@@ -0,0 +1,120 @@
|
||||
"""
|
||||
tests.test_component_configurator
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests Configurator component.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods,protected-access
|
||||
import unittest
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.components.configurator as configurator
|
||||
from homeassistant.const import EVENT_TIME_CHANGED
|
||||
|
||||
|
||||
class TestConfigurator(unittest.TestCase):
|
||||
""" Test the chromecast module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
self.hass = ha.HomeAssistant()
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_request_least_info(self):
|
||||
""" Test request config with least amount of data. """
|
||||
|
||||
request_id = configurator.request_config(
|
||||
self.hass, "Test Request", lambda _: None)
|
||||
|
||||
self.assertEqual(
|
||||
1, len(self.hass.services.services.get(configurator.DOMAIN, [])),
|
||||
"No new service registered")
|
||||
|
||||
states = self.hass.states.all()
|
||||
|
||||
self.assertEqual(1, len(states), "Expected a new state registered")
|
||||
|
||||
state = states[0]
|
||||
|
||||
self.assertEqual(configurator.STATE_CONFIGURE, state.state)
|
||||
self.assertEqual(
|
||||
request_id, state.attributes.get(configurator.ATTR_CONFIGURE_ID))
|
||||
|
||||
def test_request_all_info(self):
|
||||
""" Test request config with all possible info. """
|
||||
|
||||
values = [
|
||||
"config_description", "config image url",
|
||||
"config submit caption", []]
|
||||
|
||||
keys = [
|
||||
configurator.ATTR_DESCRIPTION, configurator.ATTR_DESCRIPTION_IMAGE,
|
||||
configurator.ATTR_SUBMIT_CAPTION, configurator.ATTR_FIELDS]
|
||||
|
||||
exp_attr = dict(zip(keys, values))
|
||||
|
||||
exp_attr[configurator.ATTR_CONFIGURE_ID] = configurator.request_config(
|
||||
self.hass, "Test Request", lambda _: None,
|
||||
*values)
|
||||
|
||||
states = self.hass.states.all()
|
||||
|
||||
self.assertEqual(1, len(states))
|
||||
|
||||
state = states[0]
|
||||
|
||||
self.assertEqual(configurator.STATE_CONFIGURE, state.state)
|
||||
self.assertEqual(exp_attr, state.attributes)
|
||||
|
||||
def test_callback_called_on_configure(self):
|
||||
""" Test if our callback gets called when configure service called. """
|
||||
calls = []
|
||||
|
||||
request_id = configurator.request_config(
|
||||
self.hass, "Test Request", lambda _: calls.append(1))
|
||||
|
||||
self.hass.services.call(
|
||||
configurator.DOMAIN, configurator.SERVICE_CONFIGURE,
|
||||
{configurator.ATTR_CONFIGURE_ID: request_id})
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(calls), "Callback not called")
|
||||
|
||||
def test_state_change_on_notify_errors(self):
|
||||
""" Test state change on notify errors. """
|
||||
request_id = configurator.request_config(
|
||||
self.hass, "Test Request", lambda _: None)
|
||||
|
||||
error = "Oh no bad bad bad"
|
||||
|
||||
configurator.notify_errors(request_id, error)
|
||||
|
||||
state = self.hass.states.all()[0]
|
||||
|
||||
self.assertEqual(error, state.attributes.get(configurator.ATTR_ERRORS))
|
||||
|
||||
def test_notify_errors_fail_silently_on_bad_request_id(self):
|
||||
""" Test if notify errors fails silently with a bad request id. """
|
||||
configurator.notify_errors(2015, "Try this error")
|
||||
|
||||
def test_request_done_works(self):
|
||||
""" Test if calling request done works. """
|
||||
request_id = configurator.request_config(
|
||||
self.hass, "Test Request", lambda _: None)
|
||||
|
||||
configurator.request_done(request_id)
|
||||
|
||||
self.assertEqual(1, len(self.hass.states.all()))
|
||||
|
||||
self.hass.bus.fire(EVENT_TIME_CHANGED)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(0, len(self.hass.states.all()))
|
||||
|
||||
def test_request_done_fail_silently_on_bad_request_id(self):
|
||||
""" Test that request_done fails silently with a bad request id. """
|
||||
configurator.request_done(2016)
|
||||
@@ -0,0 +1,36 @@
|
||||
"""
|
||||
tests.test_component_demo
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests demo component.
|
||||
"""
|
||||
import unittest
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.components.demo as demo
|
||||
|
||||
from common import mock_http_component
|
||||
|
||||
|
||||
class TestDemo(unittest.TestCase):
|
||||
""" Test the demo module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
self.hass = ha.HomeAssistant()
|
||||
mock_http_component(self.hass)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_if_demo_state_shows_by_default(self):
|
||||
""" Test if demo state shows if we give no configuration. """
|
||||
demo.setup(self.hass, {demo.DOMAIN: {}})
|
||||
|
||||
self.assertIsNotNone(self.hass.states.get('a.Demo_Mode'))
|
||||
|
||||
def test_hiding_demo_state(self):
|
||||
""" Test if you can hide the demo card. """
|
||||
demo.setup(self.hass, {demo.DOMAIN: {'hide_demo_state': 1}})
|
||||
|
||||
self.assertIsNone(self.hass.states.get('a.Demo_Mode'))
|
||||
@@ -0,0 +1,127 @@
|
||||
"""
|
||||
tests.test_component_device_sun_light_trigger
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests device sun light trigger component.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods,protected-access
|
||||
import os
|
||||
import unittest
|
||||
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant.const import CONF_PLATFORM
|
||||
from homeassistant.components import (
|
||||
device_tracker, light, sun, device_sun_light_trigger)
|
||||
|
||||
|
||||
from common import (
|
||||
get_test_home_assistant, ensure_sun_risen, ensure_sun_set,
|
||||
trigger_device_tracker_scan)
|
||||
|
||||
|
||||
KNOWN_DEV_PATH = None
|
||||
|
||||
|
||||
def setUpModule(): # pylint: disable=invalid-name
|
||||
""" Initalizes a Home Assistant server. """
|
||||
global KNOWN_DEV_PATH
|
||||
|
||||
hass = get_test_home_assistant()
|
||||
|
||||
loader.prepare(hass)
|
||||
KNOWN_DEV_PATH = hass.config.path(
|
||||
device_tracker.KNOWN_DEVICES_FILE)
|
||||
|
||||
hass.stop()
|
||||
|
||||
with open(KNOWN_DEV_PATH, 'w') as fil:
|
||||
fil.write('device,name,track,picture\n')
|
||||
fil.write('DEV1,device 1,1,http://example.com/dev1.jpg\n')
|
||||
fil.write('DEV2,device 2,1,http://example.com/dev2.jpg\n')
|
||||
|
||||
|
||||
def tearDownModule(): # pylint: disable=invalid-name
|
||||
""" Stops the Home Assistant server. """
|
||||
os.remove(KNOWN_DEV_PATH)
|
||||
|
||||
|
||||
class TestDeviceSunLightTrigger(unittest.TestCase):
|
||||
""" Test the device sun light trigger module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
self.scanner = loader.get_component(
|
||||
'device_tracker.test').get_scanner(None, None)
|
||||
|
||||
self.scanner.reset()
|
||||
self.scanner.come_home('DEV1')
|
||||
|
||||
loader.get_component('light.test').init()
|
||||
|
||||
device_tracker.setup(self.hass, {
|
||||
device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}
|
||||
})
|
||||
|
||||
light.setup(self.hass, {
|
||||
light.DOMAIN: {CONF_PLATFORM: 'test'}
|
||||
})
|
||||
|
||||
sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_lights_on_when_sun_sets(self):
|
||||
""" Test lights go on when there is someone home and the sun sets. """
|
||||
|
||||
device_sun_light_trigger.setup(
|
||||
self.hass, {device_sun_light_trigger.DOMAIN: {}})
|
||||
|
||||
ensure_sun_risen(self.hass)
|
||||
|
||||
light.turn_off(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
ensure_sun_set(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertTrue(light.is_on(self.hass))
|
||||
|
||||
def test_lights_turn_off_when_everyone_leaves(self):
|
||||
""" Test lights turn off when everyone leaves the house. """
|
||||
light.turn_on(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
device_sun_light_trigger.setup(
|
||||
self.hass, {device_sun_light_trigger.DOMAIN: {}})
|
||||
|
||||
self.scanner.leave_home('DEV1')
|
||||
|
||||
trigger_device_tracker_scan(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertFalse(light.is_on(self.hass))
|
||||
|
||||
def test_lights_turn_on_when_coming_home_after_sun_set(self):
|
||||
""" Test lights turn on when coming home after sun set. """
|
||||
light.turn_off(self.hass)
|
||||
|
||||
ensure_sun_set(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
device_sun_light_trigger.setup(
|
||||
self.hass, {device_sun_light_trigger.DOMAIN: {}})
|
||||
|
||||
self.scanner.come_home('DEV2')
|
||||
trigger_device_tracker_scan(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertTrue(light.is_on(self.hass))
|
||||
@@ -0,0 +1,200 @@
|
||||
"""
|
||||
tests.test_component_device_tracker
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests the device tracker compoments.
|
||||
"""
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
import unittest
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
import os
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.loader as loader
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.const import (
|
||||
STATE_HOME, STATE_NOT_HOME, ATTR_ENTITY_PICTURE, CONF_PLATFORM,
|
||||
DEVICE_DEFAULT_NAME)
|
||||
import homeassistant.components.device_tracker as device_tracker
|
||||
|
||||
from common import get_test_home_assistant
|
||||
|
||||
|
||||
def setUpModule(): # pylint: disable=invalid-name
|
||||
""" Setup to ignore group errors. """
|
||||
logging.disable(logging.CRITICAL)
|
||||
|
||||
|
||||
class TestComponentsDeviceTracker(unittest.TestCase):
|
||||
""" Tests homeassistant.components.device_tracker module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" Init needed objects. """
|
||||
self.hass = get_test_home_assistant()
|
||||
loader.prepare(self.hass)
|
||||
|
||||
self.known_dev_path = self.hass.config.path(
|
||||
device_tracker.KNOWN_DEVICES_FILE)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
if os.path.isfile(self.known_dev_path):
|
||||
os.remove(self.known_dev_path)
|
||||
|
||||
def test_is_on(self):
|
||||
""" Test is_on method. """
|
||||
entity_id = device_tracker.ENTITY_ID_FORMAT.format('test')
|
||||
|
||||
self.hass.states.set(entity_id, STATE_HOME)
|
||||
|
||||
self.assertTrue(device_tracker.is_on(self.hass, entity_id))
|
||||
|
||||
self.hass.states.set(entity_id, STATE_NOT_HOME)
|
||||
|
||||
self.assertFalse(device_tracker.is_on(self.hass, entity_id))
|
||||
|
||||
def test_setup(self):
|
||||
""" Test setup method. """
|
||||
# Bogus config
|
||||
self.assertFalse(device_tracker.setup(self.hass, {}))
|
||||
|
||||
self.assertFalse(
|
||||
device_tracker.setup(self.hass, {device_tracker.DOMAIN: {}}))
|
||||
|
||||
# Test with non-existing component
|
||||
self.assertFalse(device_tracker.setup(
|
||||
self.hass, {device_tracker.DOMAIN: {CONF_PLATFORM: 'nonexisting'}}
|
||||
))
|
||||
|
||||
# Test with a bad known device file around
|
||||
with open(self.known_dev_path, 'w') as fil:
|
||||
fil.write("bad data\nbad data\n")
|
||||
|
||||
self.assertFalse(device_tracker.setup(self.hass, {
|
||||
device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}
|
||||
}))
|
||||
|
||||
def test_writing_known_devices_file(self):
|
||||
""" Test the device tracker class. """
|
||||
scanner = loader.get_component(
|
||||
'device_tracker.test').get_scanner(None, None)
|
||||
|
||||
scanner.reset()
|
||||
|
||||
scanner.come_home('DEV1')
|
||||
scanner.come_home('DEV2')
|
||||
|
||||
self.assertTrue(device_tracker.setup(self.hass, {
|
||||
device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}
|
||||
}))
|
||||
|
||||
# Ensure a new known devices file has been created.
|
||||
# Since the device_tracker uses a set internally we cannot
|
||||
# know what the order of the devices in the known devices file is.
|
||||
# To ensure all the three expected lines are there, we sort the file
|
||||
with open(self.known_dev_path) as fil:
|
||||
self.assertEqual(
|
||||
['DEV1,{},0,\n'.format(DEVICE_DEFAULT_NAME), 'DEV2,dev2,0,\n',
|
||||
'device,name,track,picture\n'],
|
||||
sorted(fil))
|
||||
|
||||
# Write one where we track dev1, dev2
|
||||
with open(self.known_dev_path, 'w') as fil:
|
||||
fil.write('device,name,track,picture\n')
|
||||
fil.write('DEV1,device 1,1,http://example.com/dev1.jpg\n')
|
||||
fil.write('DEV2,device 2,1,http://example.com/dev2.jpg\n')
|
||||
|
||||
scanner.leave_home('DEV1')
|
||||
scanner.come_home('DEV3')
|
||||
|
||||
self.hass.services.call(
|
||||
device_tracker.DOMAIN,
|
||||
device_tracker.SERVICE_DEVICE_TRACKER_RELOAD)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
dev1 = device_tracker.ENTITY_ID_FORMAT.format('device_1')
|
||||
dev2 = device_tracker.ENTITY_ID_FORMAT.format('device_2')
|
||||
dev3 = device_tracker.ENTITY_ID_FORMAT.format('DEV3')
|
||||
|
||||
now = dt_util.utcnow()
|
||||
|
||||
# Device scanner scans every 12 seconds. We need to sync our times to
|
||||
# be every 12 seconds or else the time_changed event will be ignored.
|
||||
nowAlmostMinimumGone = now + device_tracker.TIME_DEVICE_NOT_FOUND
|
||||
nowAlmostMinimumGone -= timedelta(
|
||||
seconds=12+(nowAlmostMinimumGone.second % 12))
|
||||
|
||||
nowMinimumGone = now + device_tracker.TIME_DEVICE_NOT_FOUND
|
||||
nowMinimumGone += timedelta(seconds=12-(nowMinimumGone.second % 12))
|
||||
|
||||
# Test initial is correct
|
||||
self.assertTrue(device_tracker.is_on(self.hass))
|
||||
self.assertFalse(device_tracker.is_on(self.hass, dev1))
|
||||
self.assertTrue(device_tracker.is_on(self.hass, dev2))
|
||||
self.assertIsNone(self.hass.states.get(dev3))
|
||||
|
||||
self.assertEqual(
|
||||
'http://example.com/dev1.jpg',
|
||||
self.hass.states.get(dev1).attributes.get(ATTR_ENTITY_PICTURE))
|
||||
self.assertEqual(
|
||||
'http://example.com/dev2.jpg',
|
||||
self.hass.states.get(dev2).attributes.get(ATTR_ENTITY_PICTURE))
|
||||
|
||||
# Test if dev3 got added to known dev file
|
||||
with open(self.known_dev_path) as fil:
|
||||
self.assertEqual('DEV3,dev3,0,\n', list(fil)[-1])
|
||||
|
||||
# Change dev3 to track
|
||||
with open(self.known_dev_path, 'w') as fil:
|
||||
fil.write("device,name,track,picture\n")
|
||||
fil.write('DEV1,Device 1,1,http://example.com/picture.jpg\n')
|
||||
fil.write('DEV2,Device 2,1,http://example.com/picture.jpg\n')
|
||||
fil.write('DEV3,DEV3,1,\n')
|
||||
|
||||
scanner.come_home('DEV1')
|
||||
scanner.leave_home('DEV2')
|
||||
|
||||
# reload dev file
|
||||
self.hass.services.call(
|
||||
device_tracker.DOMAIN,
|
||||
device_tracker.SERVICE_DEVICE_TRACKER_RELOAD)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
# Test what happens if a device comes home and another leaves
|
||||
self.assertTrue(device_tracker.is_on(self.hass))
|
||||
self.assertTrue(device_tracker.is_on(self.hass, dev1))
|
||||
# Dev2 will still be home because of the error margin on time
|
||||
self.assertTrue(device_tracker.is_on(self.hass, dev2))
|
||||
# dev3 should be tracked now after we reload the known devices
|
||||
self.assertTrue(device_tracker.is_on(self.hass, dev3))
|
||||
|
||||
self.assertIsNone(
|
||||
self.hass.states.get(dev3).attributes.get(ATTR_ENTITY_PICTURE))
|
||||
|
||||
# Test if device leaves what happens, test the time span
|
||||
self.hass.bus.fire(
|
||||
ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: nowAlmostMinimumGone})
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertTrue(device_tracker.is_on(self.hass))
|
||||
self.assertTrue(device_tracker.is_on(self.hass, dev1))
|
||||
# Dev2 will still be home because of the error time
|
||||
self.assertTrue(device_tracker.is_on(self.hass, dev2))
|
||||
self.assertTrue(device_tracker.is_on(self.hass, dev3))
|
||||
|
||||
# Now test if gone for longer then error margin
|
||||
self.hass.bus.fire(
|
||||
ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: nowMinimumGone})
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertTrue(device_tracker.is_on(self.hass))
|
||||
self.assertTrue(device_tracker.is_on(self.hass, dev1))
|
||||
self.assertFalse(device_tracker.is_on(self.hass, dev2))
|
||||
self.assertTrue(device_tracker.is_on(self.hass, dev3))
|
||||
@@ -0,0 +1,95 @@
|
||||
"""
|
||||
tests.test_component_http
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests Home Assistant HTTP component does what it should do.
|
||||
"""
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
import re
|
||||
import unittest
|
||||
|
||||
import requests
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.bootstrap as bootstrap
|
||||
import homeassistant.components.http as http
|
||||
from homeassistant.const import HTTP_HEADER_HA_AUTH
|
||||
|
||||
API_PASSWORD = "test1234"
|
||||
|
||||
# Somehow the socket that holds the default port does not get released
|
||||
# when we close down HA in a different test case. Until I have figured
|
||||
# out what is going on, let's run this test on a different port.
|
||||
SERVER_PORT = 8121
|
||||
|
||||
HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT)
|
||||
|
||||
HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD}
|
||||
|
||||
hass = None
|
||||
|
||||
|
||||
def _url(path=""):
|
||||
""" Helper method to generate urls. """
|
||||
return HTTP_BASE_URL + path
|
||||
|
||||
|
||||
def setUpModule(): # pylint: disable=invalid-name
|
||||
""" Initalizes a Home Assistant server. """
|
||||
global hass
|
||||
|
||||
hass = ha.HomeAssistant()
|
||||
|
||||
hass.bus.listen('test_event', lambda _: _)
|
||||
hass.states.set('test.test', 'a_state')
|
||||
|
||||
bootstrap.setup_component(
|
||||
hass, http.DOMAIN,
|
||||
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
|
||||
http.CONF_SERVER_PORT: SERVER_PORT}})
|
||||
|
||||
bootstrap.setup_component(hass, 'frontend')
|
||||
|
||||
hass.start()
|
||||
|
||||
|
||||
def tearDownModule(): # pylint: disable=invalid-name
|
||||
""" Stops the Home Assistant server. """
|
||||
hass.stop()
|
||||
|
||||
|
||||
class TestFrontend(unittest.TestCase):
|
||||
""" Test the frontend. """
|
||||
|
||||
def test_frontend_and_static(self):
|
||||
""" Tests if we can get the frontend. """
|
||||
req = requests.get(_url(""))
|
||||
|
||||
self.assertEqual(200, req.status_code)
|
||||
|
||||
# Test we can retrieve frontend.js
|
||||
frontendjs = re.search(
|
||||
r'(?P<app>\/static\/frontend-[A-Za-z0-9]{32}.html)',
|
||||
req.text)
|
||||
|
||||
self.assertIsNotNone(frontendjs)
|
||||
|
||||
req = requests.head(_url(frontendjs.groups(0)[0]))
|
||||
|
||||
self.assertEqual(200, req.status_code)
|
||||
|
||||
def test_auto_filling_in_api_password(self):
|
||||
req = requests.get(
|
||||
_url("?{}={}".format(http.DATA_API_PASSWORD, API_PASSWORD)))
|
||||
|
||||
self.assertEqual(200, req.status_code)
|
||||
|
||||
auth_text = re.search(r"auth='{}'".format(API_PASSWORD), req.text)
|
||||
|
||||
self.assertIsNotNone(auth_text)
|
||||
|
||||
def test_404(self):
|
||||
self.assertEqual(404, requests.get(_url("/not-existing")).status_code)
|
||||
|
||||
def test_we_cannot_POST_to_root(self):
|
||||
self.assertEqual(405, requests.post(_url("")).status_code)
|
||||
@@ -0,0 +1,218 @@
|
||||
"""
|
||||
tests.test_component_group
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests the group compoments.
|
||||
"""
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
import unittest
|
||||
import logging
|
||||
|
||||
import homeassistant as ha
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, STATE_HOME, STATE_UNKNOWN
|
||||
import homeassistant.components.group as group
|
||||
|
||||
|
||||
def setUpModule(): # pylint: disable=invalid-name
|
||||
""" Setup to ignore group errors. """
|
||||
logging.disable(logging.CRITICAL)
|
||||
|
||||
|
||||
class TestComponentsGroup(unittest.TestCase):
|
||||
""" Tests homeassistant.components.group module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" Init needed objects. """
|
||||
self.hass = ha.HomeAssistant()
|
||||
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
|
||||
|
||||
self.group_entity_id = test_group.entity_id
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_setup_group_with_mixed_groupable_states(self):
|
||||
""" Try to setup a group with mixed groupable states """
|
||||
self.hass.states.set('device_tracker.Paulus', STATE_HOME)
|
||||
group.setup_group(
|
||||
self.hass, 'person_and_light',
|
||||
['light.Bowl', 'device_tracker.Paulus'])
|
||||
|
||||
self.assertEqual(
|
||||
STATE_ON,
|
||||
self.hass.states.get(
|
||||
group.ENTITY_ID_FORMAT.format('person_and_light')).state)
|
||||
|
||||
def test_setup_group_with_a_non_existing_state(self):
|
||||
""" Try to setup a group with a non existing state """
|
||||
grp = group.setup_group(
|
||||
self.hass, 'light_and_nothing',
|
||||
['light.Bowl', 'non.existing'])
|
||||
|
||||
self.assertEqual(STATE_ON, grp.state)
|
||||
|
||||
def test_setup_group_with_non_groupable_states(self):
|
||||
self.hass.states.set('cast.living_room', "Plex")
|
||||
self.hass.states.set('cast.bedroom', "Netflix")
|
||||
|
||||
grp = group.setup_group(
|
||||
self.hass, 'chromecasts',
|
||||
['cast.living_room', 'cast.bedroom'])
|
||||
|
||||
self.assertEqual(STATE_UNKNOWN, grp.state)
|
||||
|
||||
def test_setup_empty_group(self):
|
||||
""" Try to setup an empty group. """
|
||||
grp = group.setup_group(self.hass, 'nothing', [])
|
||||
|
||||
self.assertEqual(STATE_UNKNOWN, grp.state)
|
||||
|
||||
def test_monitor_group(self):
|
||||
""" Test if the group keeps track of states. """
|
||||
|
||||
# Test if group setup in our init mode is ok
|
||||
self.assertIn(self.group_entity_id, self.hass.states.entity_ids())
|
||||
|
||||
group_state = self.hass.states.get(self.group_entity_id)
|
||||
self.assertEqual(STATE_ON, group_state.state)
|
||||
self.assertTrue(group_state.attributes[group.ATTR_AUTO])
|
||||
|
||||
def test_group_turns_off_if_all_off(self):
|
||||
"""
|
||||
Test if the group turns off if the last device that was on turns off.
|
||||
"""
|
||||
self.hass.states.set('light.Bowl', STATE_OFF)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
group_state = self.hass.states.get(self.group_entity_id)
|
||||
self.assertEqual(STATE_OFF, group_state.state)
|
||||
|
||||
def test_group_turns_on_if_all_are_off_and_one_turns_on(self):
|
||||
"""
|
||||
Test if group turns on if all devices were turned off and one turns on.
|
||||
"""
|
||||
# Make sure all are off.
|
||||
self.hass.states.set('light.Bowl', STATE_OFF)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
# Turn one on
|
||||
self.hass.states.set('light.Ceiling', STATE_ON)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
group_state = self.hass.states.get(self.group_entity_id)
|
||||
self.assertEqual(STATE_ON, group_state.state)
|
||||
|
||||
def test_is_on(self):
|
||||
""" Test is_on method. """
|
||||
self.assertTrue(group.is_on(self.hass, self.group_entity_id))
|
||||
self.hass.states.set('light.Bowl', STATE_OFF)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertFalse(group.is_on(self.hass, self.group_entity_id))
|
||||
|
||||
# Try on non existing state
|
||||
self.assertFalse(group.is_on(self.hass, 'non.existing'))
|
||||
|
||||
def test_expand_entity_ids(self):
|
||||
""" Test expand_entity_ids method. """
|
||||
self.assertEqual(sorted(['light.ceiling', 'light.bowl']),
|
||||
sorted(group.expand_entity_ids(
|
||||
self.hass, [self.group_entity_id])))
|
||||
|
||||
def test_expand_entity_ids_does_not_return_duplicates(self):
|
||||
""" Test that expand_entity_ids does not return duplicates. """
|
||||
self.assertEqual(
|
||||
['light.bowl', 'light.ceiling'],
|
||||
sorted(group.expand_entity_ids(
|
||||
self.hass, [self.group_entity_id, 'light.Ceiling'])))
|
||||
|
||||
self.assertEqual(
|
||||
['light.bowl', 'light.ceiling'],
|
||||
sorted(group.expand_entity_ids(
|
||||
self.hass, ['light.bowl', self.group_entity_id])))
|
||||
|
||||
def test_expand_entity_ids_ignores_non_strings(self):
|
||||
""" Test that non string elements in lists are ignored. """
|
||||
self.assertEqual([], group.expand_entity_ids(self.hass, [5, True]))
|
||||
|
||||
def test_get_entity_ids(self):
|
||||
""" Test get_entity_ids method. """
|
||||
self.assertEqual(
|
||||
['light.bowl', 'light.ceiling'],
|
||||
sorted(group.get_entity_ids(self.hass, self.group_entity_id)))
|
||||
|
||||
def test_get_entity_ids_with_domain_filter(self):
|
||||
""" Test if get_entity_ids works with a domain_filter. """
|
||||
self.hass.states.set('switch.AC', STATE_OFF)
|
||||
|
||||
mixed_group = group.Group(
|
||||
self.hass, 'mixed_group', ['light.Bowl', 'switch.AC'], False)
|
||||
|
||||
self.assertEqual(
|
||||
['switch.ac'],
|
||||
group.get_entity_ids(
|
||||
self.hass, mixed_group.entity_id, domain_filter="switch"))
|
||||
|
||||
def test_get_entity_ids_with_non_existing_group_name(self):
|
||||
""" Tests get_entity_ids with a non existing group. """
|
||||
self.assertEqual([], group.get_entity_ids(self.hass, 'non_existing'))
|
||||
|
||||
def test_get_entity_ids_with_non_group_state(self):
|
||||
""" Tests get_entity_ids with a non group state. """
|
||||
self.assertEqual([], group.get_entity_ids(self.hass, 'switch.AC'))
|
||||
|
||||
def test_group_being_init_before_first_tracked_state_is_set_to_on(self):
|
||||
""" Test if the group turns on if no states existed and now a state it is
|
||||
tracking is being added as ON. """
|
||||
test_group = group.Group(
|
||||
self.hass, 'test group', ['light.not_there_1'])
|
||||
|
||||
self.hass.states.set('light.not_there_1', STATE_ON)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
group_state = self.hass.states.get(test_group.entity_id)
|
||||
self.assertEqual(STATE_ON, group_state.state)
|
||||
|
||||
def test_group_being_init_before_first_tracked_state_is_set_to_off(self):
|
||||
""" Test if the group turns off if no states existed and now a state it is
|
||||
tracking is being added as OFF. """
|
||||
test_group = group.Group(
|
||||
self.hass, 'test group', ['light.not_there_1'])
|
||||
|
||||
self.hass.states.set('light.not_there_1', STATE_OFF)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
group_state = self.hass.states.get(test_group.entity_id)
|
||||
self.assertEqual(STATE_OFF, group_state.state)
|
||||
|
||||
def test_setup(self):
|
||||
""" Test setup method. """
|
||||
self.assertTrue(
|
||||
group.setup(
|
||||
self.hass,
|
||||
{
|
||||
group.DOMAIN: {
|
||||
'second_group': ','.join((self.group_entity_id,
|
||||
'light.Bowl'))
|
||||
}
|
||||
}))
|
||||
|
||||
group_state = self.hass.states.get(
|
||||
group.ENTITY_ID_FORMAT.format('second_group'))
|
||||
|
||||
self.assertEqual(STATE_ON, group_state.state)
|
||||
self.assertFalse(group_state.attributes[group.ATTR_AUTO])
|
||||
|
||||
def test_groups_get_unique_names(self):
|
||||
""" Two groups with same name should both have a unique entity id. """
|
||||
grp1 = group.Group(self.hass, 'Je suis Charlie')
|
||||
grp2 = group.Group(self.hass, 'Je suis Charlie')
|
||||
|
||||
self.assertNotEqual(grp1.entity_id, grp2.entity_id)
|
||||
@@ -0,0 +1,135 @@
|
||||
"""
|
||||
tests.test_component_history
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests the history component.
|
||||
"""
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
import time
|
||||
import os
|
||||
import unittest
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.components import history, recorder
|
||||
|
||||
from common import (
|
||||
mock_http_component, mock_state_change_event, get_test_home_assistant)
|
||||
|
||||
|
||||
class TestComponentHistory(unittest.TestCase):
|
||||
""" Tests homeassistant.components.history module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" Init needed objects. """
|
||||
self.hass = get_test_home_assistant(1)
|
||||
self.init_rec = False
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
if self.init_rec:
|
||||
recorder._INSTANCE.block_till_done()
|
||||
os.remove(self.hass.config.path(recorder.DB_FILE))
|
||||
|
||||
def init_recorder(self):
|
||||
recorder.setup(self.hass, {})
|
||||
self.hass.start()
|
||||
recorder._INSTANCE.block_till_done()
|
||||
self.init_rec = True
|
||||
|
||||
def test_setup(self):
|
||||
""" Test setup method of history. """
|
||||
mock_http_component(self.hass)
|
||||
self.assertTrue(history.setup(self.hass, {}))
|
||||
|
||||
def test_last_5_states(self):
|
||||
""" Test retrieving the last 5 states. """
|
||||
self.init_recorder()
|
||||
states = []
|
||||
|
||||
entity_id = 'test.last_5_states'
|
||||
|
||||
for i in range(7):
|
||||
self.hass.states.set(entity_id, "State {}".format(i))
|
||||
|
||||
if i > 1:
|
||||
states.append(self.hass.states.get(entity_id))
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
recorder._INSTANCE.block_till_done()
|
||||
|
||||
self.assertEqual(
|
||||
list(reversed(states)), history.last_5_states(entity_id))
|
||||
|
||||
def test_get_states(self):
|
||||
""" Test getting states at a specific point in time. """
|
||||
self.init_recorder()
|
||||
states = []
|
||||
|
||||
# Create 10 states for 5 different entities
|
||||
# After the first 5, sleep a second and save the time
|
||||
# history.get_states takes the latest states BEFORE point X
|
||||
|
||||
for i in range(10):
|
||||
state = ha.State(
|
||||
'test.point_in_time_{}'.format(i % 5),
|
||||
"State {}".format(i),
|
||||
{'attribute_test': i})
|
||||
|
||||
mock_state_change_event(self.hass, state)
|
||||
self.hass.pool.block_till_done()
|
||||
recorder._INSTANCE.block_till_done()
|
||||
|
||||
if i < 5:
|
||||
states.append(state)
|
||||
|
||||
if i == 4:
|
||||
time.sleep(1)
|
||||
point = dt_util.utcnow()
|
||||
|
||||
self.assertEqual(
|
||||
states,
|
||||
sorted(
|
||||
history.get_states(point), key=lambda state: state.entity_id))
|
||||
|
||||
# Test get_state here because we have a DB setup
|
||||
self.assertEqual(
|
||||
states[0], history.get_state(point, states[0].entity_id))
|
||||
|
||||
def test_state_changes_during_period(self):
|
||||
self.init_recorder()
|
||||
entity_id = 'media_player.test'
|
||||
|
||||
def set_state(state):
|
||||
self.hass.states.set(entity_id, state)
|
||||
self.hass.pool.block_till_done()
|
||||
recorder._INSTANCE.block_till_done()
|
||||
|
||||
return self.hass.states.get(entity_id)
|
||||
|
||||
set_state('idle')
|
||||
set_state('YouTube')
|
||||
|
||||
start = dt_util.utcnow()
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
states = [
|
||||
set_state('idle'),
|
||||
set_state('Netflix'),
|
||||
set_state('Plex'),
|
||||
set_state('YouTube'),
|
||||
]
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
end = dt_util.utcnow()
|
||||
|
||||
set_state('Netflix')
|
||||
set_state('Plex')
|
||||
|
||||
self.assertEqual(
|
||||
{entity_id: states},
|
||||
history.state_changes_during_period(start, end, entity_id))
|
||||
@@ -0,0 +1,61 @@
|
||||
"""
|
||||
tests.test_component_core
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests core compoments.
|
||||
"""
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
import unittest
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant.const import (
|
||||
STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF)
|
||||
import homeassistant.components as comps
|
||||
|
||||
|
||||
class TestComponentsCore(unittest.TestCase):
|
||||
""" Tests homeassistant.components module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" Init needed objects. """
|
||||
self.hass = ha.HomeAssistant()
|
||||
loader.prepare(self.hass)
|
||||
self.assertTrue(comps.setup(self.hass, {}))
|
||||
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_is_on(self):
|
||||
""" Test is_on method. """
|
||||
self.assertTrue(comps.is_on(self.hass, 'light.Bowl'))
|
||||
self.assertFalse(comps.is_on(self.hass, 'light.Ceiling'))
|
||||
self.assertTrue(comps.is_on(self.hass))
|
||||
|
||||
def test_turn_on(self):
|
||||
""" Test turn_on method. """
|
||||
runs = []
|
||||
self.hass.services.register(
|
||||
'light', SERVICE_TURN_ON, lambda x: runs.append(1))
|
||||
|
||||
comps.turn_on(self.hass, 'light.Ceiling')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(runs))
|
||||
|
||||
def test_turn_off(self):
|
||||
""" Test turn_off method. """
|
||||
runs = []
|
||||
self.hass.services.register(
|
||||
'light', SERVICE_TURN_OFF, lambda x: runs.append(1))
|
||||
|
||||
comps.turn_off(self.hass, 'light.Bowl')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(runs))
|
||||
@@ -0,0 +1,257 @@
|
||||
"""
|
||||
tests.test_component_switch
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests switch component.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods,protected-access
|
||||
import unittest
|
||||
import os
|
||||
|
||||
import homeassistant.loader as loader
|
||||
import homeassistant.util.color as color_util
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM,
|
||||
SERVICE_TURN_ON, SERVICE_TURN_OFF)
|
||||
import homeassistant.components.light as light
|
||||
|
||||
from common import mock_service, get_test_home_assistant
|
||||
|
||||
|
||||
class TestLight(unittest.TestCase):
|
||||
""" Test the switch module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
self.hass = get_test_home_assistant()
|
||||
loader.prepare(self.hass)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
||||
|
||||
if os.path.isfile(user_light_file):
|
||||
os.remove(user_light_file)
|
||||
|
||||
def test_methods(self):
|
||||
""" Test if methods call the services as expected. """
|
||||
# Test is_on
|
||||
self.hass.states.set('light.test', STATE_ON)
|
||||
self.assertTrue(light.is_on(self.hass, 'light.test'))
|
||||
|
||||
self.hass.states.set('light.test', STATE_OFF)
|
||||
self.assertFalse(light.is_on(self.hass, 'light.test'))
|
||||
|
||||
self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON)
|
||||
self.assertTrue(light.is_on(self.hass))
|
||||
|
||||
self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF)
|
||||
self.assertFalse(light.is_on(self.hass))
|
||||
|
||||
# Test turn_on
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
|
||||
light.turn_on(
|
||||
self.hass,
|
||||
entity_id='entity_id_val',
|
||||
transition='transition_val',
|
||||
brightness='brightness_val',
|
||||
rgb_color='rgb_color_val',
|
||||
xy_color='xy_color_val',
|
||||
profile='profile_val')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(turn_on_calls))
|
||||
call = turn_on_calls[-1]
|
||||
|
||||
self.assertEqual(light.DOMAIN, call.domain)
|
||||
self.assertEqual(SERVICE_TURN_ON, call.service)
|
||||
self.assertEqual('entity_id_val', call.data.get(ATTR_ENTITY_ID))
|
||||
self.assertEqual(
|
||||
'transition_val', call.data.get(light.ATTR_TRANSITION))
|
||||
self.assertEqual(
|
||||
'brightness_val', call.data.get(light.ATTR_BRIGHTNESS))
|
||||
self.assertEqual('rgb_color_val', call.data.get(light.ATTR_RGB_COLOR))
|
||||
self.assertEqual('xy_color_val', call.data.get(light.ATTR_XY_COLOR))
|
||||
self.assertEqual('profile_val', call.data.get(light.ATTR_PROFILE))
|
||||
|
||||
# Test turn_off
|
||||
turn_off_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_OFF)
|
||||
|
||||
light.turn_off(
|
||||
self.hass, entity_id='entity_id_val', transition='transition_val')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(turn_off_calls))
|
||||
call = turn_off_calls[-1]
|
||||
|
||||
self.assertEqual(light.DOMAIN, call.domain)
|
||||
self.assertEqual(SERVICE_TURN_OFF, call.service)
|
||||
self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID])
|
||||
self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION])
|
||||
|
||||
def test_services(self):
|
||||
""" Test the provided services. """
|
||||
platform = loader.get_component('light.test')
|
||||
|
||||
platform.init()
|
||||
self.assertTrue(
|
||||
light.setup(self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}}))
|
||||
|
||||
dev1, dev2, dev3 = platform.DEVICES
|
||||
|
||||
# Test init
|
||||
self.assertTrue(light.is_on(self.hass, dev1.entity_id))
|
||||
self.assertFalse(light.is_on(self.hass, dev2.entity_id))
|
||||
self.assertFalse(light.is_on(self.hass, dev3.entity_id))
|
||||
|
||||
# Test basic turn_on, turn_off services
|
||||
light.turn_off(self.hass, entity_id=dev1.entity_id)
|
||||
light.turn_on(self.hass, entity_id=dev2.entity_id)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertFalse(light.is_on(self.hass, dev1.entity_id))
|
||||
self.assertTrue(light.is_on(self.hass, dev2.entity_id))
|
||||
|
||||
# turn on all lights
|
||||
light.turn_on(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertTrue(light.is_on(self.hass, dev1.entity_id))
|
||||
self.assertTrue(light.is_on(self.hass, dev2.entity_id))
|
||||
self.assertTrue(light.is_on(self.hass, dev3.entity_id))
|
||||
|
||||
# turn off all lights
|
||||
light.turn_off(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertFalse(light.is_on(self.hass, dev1.entity_id))
|
||||
self.assertFalse(light.is_on(self.hass, dev2.entity_id))
|
||||
self.assertFalse(light.is_on(self.hass, dev3.entity_id))
|
||||
|
||||
# Ensure all attributes process correctly
|
||||
light.turn_on(self.hass, dev1.entity_id,
|
||||
transition=10, brightness=20)
|
||||
light.turn_on(
|
||||
self.hass, dev2.entity_id, rgb_color=[255, 255, 255])
|
||||
light.turn_on(self.hass, dev3.entity_id, xy_color=[.4, .6])
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
method, data = dev1.last_call('turn_on')
|
||||
self.assertEqual(
|
||||
{light.ATTR_TRANSITION: 10,
|
||||
light.ATTR_BRIGHTNESS: 20},
|
||||
data)
|
||||
|
||||
method, data = dev2.last_call('turn_on')
|
||||
self.assertEqual(
|
||||
{light.ATTR_XY_COLOR: color_util.color_RGB_to_xy(255, 255, 255)},
|
||||
data)
|
||||
|
||||
method, data = dev3.last_call('turn_on')
|
||||
self.assertEqual({light.ATTR_XY_COLOR: [.4, .6]}, data)
|
||||
|
||||
# One of the light profiles
|
||||
prof_name, prof_x, prof_y, prof_bri = 'relax', 0.5119, 0.4147, 144
|
||||
|
||||
# Test light profiles
|
||||
light.turn_on(self.hass, dev1.entity_id, profile=prof_name)
|
||||
# Specify a profile and attributes to overwrite it
|
||||
light.turn_on(
|
||||
self.hass, dev2.entity_id,
|
||||
profile=prof_name, brightness=100, xy_color=[.4, .6])
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
method, data = dev1.last_call('turn_on')
|
||||
self.assertEqual(
|
||||
{light.ATTR_BRIGHTNESS: prof_bri,
|
||||
light.ATTR_XY_COLOR: [prof_x, prof_y]},
|
||||
data)
|
||||
|
||||
method, data = dev2.last_call('turn_on')
|
||||
self.assertEqual(
|
||||
{light.ATTR_BRIGHTNESS: 100,
|
||||
light.ATTR_XY_COLOR: [.4, .6]},
|
||||
data)
|
||||
|
||||
# Test shitty data
|
||||
light.turn_on(self.hass, dev1.entity_id, profile="nonexisting")
|
||||
light.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5])
|
||||
light.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2])
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
method, data = dev1.last_call('turn_on')
|
||||
self.assertEqual({}, data)
|
||||
|
||||
method, data = dev2.last_call('turn_on')
|
||||
self.assertEqual({}, data)
|
||||
|
||||
method, data = dev3.last_call('turn_on')
|
||||
self.assertEqual({}, data)
|
||||
|
||||
# faulty attributes should not overwrite profile data
|
||||
light.turn_on(
|
||||
self.hass, dev1.entity_id,
|
||||
profile=prof_name, brightness='bright', rgb_color='yellowish')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
method, data = dev1.last_call('turn_on')
|
||||
self.assertEqual(
|
||||
{light.ATTR_BRIGHTNESS: prof_bri,
|
||||
light.ATTR_XY_COLOR: [prof_x, prof_y]},
|
||||
data)
|
||||
|
||||
def test_broken_light_profiles(self):
|
||||
""" Test light profiles. """
|
||||
platform = loader.get_component('light.test')
|
||||
platform.init()
|
||||
|
||||
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
||||
|
||||
# Setup a wrong light file
|
||||
with open(user_light_file, 'w') as user_file:
|
||||
user_file.write('id,x,y,brightness\n')
|
||||
user_file.write('I,WILL,NOT,WORK\n')
|
||||
|
||||
self.assertFalse(light.setup(
|
||||
self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}}
|
||||
))
|
||||
|
||||
def test_light_profiles(self):
|
||||
""" Test light profiles. """
|
||||
platform = loader.get_component('light.test')
|
||||
platform.init()
|
||||
|
||||
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
||||
|
||||
with open(user_light_file, 'w') as user_file:
|
||||
user_file.write('id,x,y,brightness\n')
|
||||
user_file.write('test,.4,.6,100\n')
|
||||
|
||||
self.assertTrue(light.setup(
|
||||
self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}}
|
||||
))
|
||||
|
||||
dev1, dev2, dev3 = platform.DEVICES
|
||||
|
||||
light.turn_on(self.hass, dev1.entity_id, profile='test')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
method, data = dev1.last_call('turn_on')
|
||||
|
||||
self.assertEqual(
|
||||
{light.ATTR_XY_COLOR: [.4, .6], light.ATTR_BRIGHTNESS: 100},
|
||||
data)
|
||||
@@ -0,0 +1,95 @@
|
||||
"""
|
||||
tests.test_component_logbook
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests the logbook component.
|
||||
"""
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
import unittest
|
||||
from datetime import timedelta
|
||||
|
||||
import homeassistant as ha
|
||||
from homeassistant.const import (
|
||||
EVENT_STATE_CHANGED, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.components import logbook
|
||||
|
||||
from common import get_test_home_assistant, mock_http_component
|
||||
|
||||
|
||||
class TestComponentHistory(unittest.TestCase):
|
||||
""" Tests homeassistant.components.history module. """
|
||||
|
||||
def test_setup(self):
|
||||
""" Test setup method. """
|
||||
try:
|
||||
hass = get_test_home_assistant()
|
||||
mock_http_component(hass)
|
||||
self.assertTrue(logbook.setup(hass, {}))
|
||||
finally:
|
||||
hass.stop()
|
||||
|
||||
def test_humanify_filter_sensor(self):
|
||||
""" Test humanify filter too frequent sensor values. """
|
||||
entity_id = 'sensor.bla'
|
||||
|
||||
pointA = dt_util.strip_microseconds(dt_util.utcnow().replace(minute=2))
|
||||
pointB = pointA.replace(minute=5)
|
||||
pointC = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
||||
|
||||
eventA = self.create_state_changed_event(pointA, entity_id, 10)
|
||||
eventB = self.create_state_changed_event(pointB, entity_id, 20)
|
||||
eventC = self.create_state_changed_event(pointC, entity_id, 30)
|
||||
|
||||
entries = list(logbook.humanify((eventA, eventB, eventC)))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(
|
||||
entries[0], pointB, 'bla', domain='sensor', entity_id=entity_id)
|
||||
|
||||
self.assert_entry(
|
||||
entries[1], pointC, 'bla', domain='sensor', entity_id=entity_id)
|
||||
|
||||
def test_home_assistant_start_stop_grouped(self):
|
||||
""" Tests if home assistant start and stop events are grouped if
|
||||
occuring in the same minute. """
|
||||
entries = list(logbook.humanify((
|
||||
ha.Event(EVENT_HOMEASSISTANT_STOP),
|
||||
ha.Event(EVENT_HOMEASSISTANT_START),
|
||||
)))
|
||||
|
||||
self.assertEqual(1, len(entries))
|
||||
self.assert_entry(
|
||||
entries[0], name='Home Assistant', message='restarted',
|
||||
domain=ha.DOMAIN)
|
||||
|
||||
def assert_entry(self, entry, when=None, name=None, message=None,
|
||||
domain=None, entity_id=None):
|
||||
""" Asserts an entry is what is expected """
|
||||
if when:
|
||||
self.assertEqual(when, entry.when)
|
||||
|
||||
if name:
|
||||
self.assertEqual(name, entry.name)
|
||||
|
||||
if message:
|
||||
self.assertEqual(message, entry.message)
|
||||
|
||||
if domain:
|
||||
self.assertEqual(domain, entry.domain)
|
||||
|
||||
if entity_id:
|
||||
self.assertEqual(entity_id, entry.entity_id)
|
||||
|
||||
def create_state_changed_event(self, event_time_fired, entity_id, state):
|
||||
""" Create state changed event. """
|
||||
|
||||
# Logbook only cares about state change events that
|
||||
# contain an old state but will not actually act on it.
|
||||
state = ha.State(entity_id, state).as_dict()
|
||||
|
||||
return ha.Event(EVENT_STATE_CHANGED, {
|
||||
'entity_id': entity_id,
|
||||
'old_state': state,
|
||||
'new_state': state,
|
||||
}, time_fired=event_time_fired)
|
||||
@@ -0,0 +1,82 @@
|
||||
"""
|
||||
tests.test_component_media_player
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests media_player component.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods,protected-access
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
import homeassistant as ha
|
||||
from homeassistant.const import (
|
||||
STATE_OFF,
|
||||
SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_VOLUME_UP, SERVICE_VOLUME_DOWN,
|
||||
SERVICE_MEDIA_PLAY_PAUSE, SERVICE_MEDIA_PLAY, SERVICE_MEDIA_PAUSE,
|
||||
SERVICE_MEDIA_NEXT_TRACK, SERVICE_MEDIA_PREVIOUS_TRACK, ATTR_ENTITY_ID)
|
||||
import homeassistant.components.media_player as media_player
|
||||
from common import mock_service
|
||||
|
||||
|
||||
def setUpModule(): # pylint: disable=invalid-name
|
||||
""" Setup to ignore media_player errors. """
|
||||
logging.disable(logging.CRITICAL)
|
||||
|
||||
|
||||
class TestMediaPlayer(unittest.TestCase):
|
||||
""" Test the media_player module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
self.hass = ha.HomeAssistant()
|
||||
|
||||
self.test_entity = media_player.ENTITY_ID_FORMAT.format('living_room')
|
||||
self.hass.states.set(self.test_entity, STATE_OFF)
|
||||
|
||||
self.test_entity2 = media_player.ENTITY_ID_FORMAT.format('bedroom')
|
||||
self.hass.states.set(self.test_entity2, "YouTube")
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_is_on(self):
|
||||
""" Test is_on method. """
|
||||
self.assertFalse(media_player.is_on(self.hass, self.test_entity))
|
||||
self.assertTrue(media_player.is_on(self.hass, self.test_entity2))
|
||||
|
||||
def test_services(self):
|
||||
"""
|
||||
Test if the call service methods conver to correct service calls.
|
||||
"""
|
||||
services = {
|
||||
SERVICE_TURN_ON: media_player.turn_on,
|
||||
SERVICE_TURN_OFF: media_player.turn_off,
|
||||
SERVICE_VOLUME_UP: media_player.volume_up,
|
||||
SERVICE_VOLUME_DOWN: media_player.volume_down,
|
||||
SERVICE_MEDIA_PLAY_PAUSE: media_player.media_play_pause,
|
||||
SERVICE_MEDIA_PLAY: media_player.media_play,
|
||||
SERVICE_MEDIA_PAUSE: media_player.media_pause,
|
||||
SERVICE_MEDIA_NEXT_TRACK: media_player.media_next_track,
|
||||
SERVICE_MEDIA_PREVIOUS_TRACK: media_player.media_previous_track
|
||||
}
|
||||
|
||||
for service_name, service_method in services.items():
|
||||
calls = mock_service(self.hass, media_player.DOMAIN, service_name)
|
||||
|
||||
service_method(self.hass)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(calls))
|
||||
call = calls[-1]
|
||||
self.assertEqual(media_player.DOMAIN, call.domain)
|
||||
self.assertEqual(service_name, call.service)
|
||||
|
||||
service_method(self.hass, self.test_entity)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(2, len(calls))
|
||||
call = calls[-1]
|
||||
self.assertEqual(media_player.DOMAIN, call.domain)
|
||||
self.assertEqual(service_name, call.service)
|
||||
self.assertEqual(self.test_entity,
|
||||
call.data.get(ATTR_ENTITY_ID))
|
||||
@@ -0,0 +1,70 @@
|
||||
"""
|
||||
tests.test_component_recorder
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests Recorder component.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods,protected-access
|
||||
import unittest
|
||||
import os
|
||||
|
||||
from homeassistant.const import MATCH_ALL
|
||||
from homeassistant.components import recorder
|
||||
|
||||
from common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestRecorder(unittest.TestCase):
|
||||
""" Test the chromecast module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
self.hass = get_test_home_assistant()
|
||||
recorder.setup(self.hass, {})
|
||||
self.hass.start()
|
||||
recorder._INSTANCE.block_till_done()
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
recorder._INSTANCE.block_till_done()
|
||||
os.remove(self.hass.config.path(recorder.DB_FILE))
|
||||
|
||||
def test_saving_state(self):
|
||||
""" Tests saving and restoring a state. """
|
||||
entity_id = 'test.recorder'
|
||||
state = 'restoring_from_db'
|
||||
attributes = {'test_attr': 5, 'test_attr_10': 'nice'}
|
||||
|
||||
self.hass.states.set(entity_id, state, attributes)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
recorder._INSTANCE.block_till_done()
|
||||
|
||||
states = recorder.query_states('SELECT * FROM states')
|
||||
|
||||
self.assertEqual(1, len(states))
|
||||
self.assertEqual(self.hass.states.get(entity_id), states[0])
|
||||
|
||||
def test_saving_event(self):
|
||||
""" Tests saving and restoring an event. """
|
||||
event_type = 'EVENT_TEST'
|
||||
event_data = {'test_attr': 5, 'test_attr_10': 'nice'}
|
||||
|
||||
events = []
|
||||
|
||||
def event_listener(event):
|
||||
""" Records events from eventbus. """
|
||||
if event.event_type == event_type:
|
||||
events.append(event)
|
||||
|
||||
self.hass.bus.listen(MATCH_ALL, event_listener)
|
||||
|
||||
self.hass.bus.fire(event_type, event_data)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
recorder._INSTANCE.block_till_done()
|
||||
|
||||
db_events = recorder.query_events(
|
||||
'SELECT * FROM events WHERE event_type = ?', (event_type, ))
|
||||
|
||||
self.assertEqual(events, db_events)
|
||||
@@ -0,0 +1,96 @@
|
||||
"""
|
||||
tests.test_component_sun
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests Sun component.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods,protected-access
|
||||
import unittest
|
||||
from datetime import timedelta
|
||||
|
||||
from astral import Astral
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.util.dt as dt_util
|
||||
import homeassistant.components.sun as sun
|
||||
|
||||
|
||||
class TestSun(unittest.TestCase):
|
||||
""" Test the sun module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
self.hass = ha.HomeAssistant()
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_is_on(self):
|
||||
""" Test is_on method. """
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
|
||||
self.assertTrue(sun.is_on(self.hass))
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_BELOW_HORIZON)
|
||||
self.assertFalse(sun.is_on(self.hass))
|
||||
|
||||
def test_setting_rising(self):
|
||||
""" Test retrieving sun setting and rising. """
|
||||
latitude = 32.87336
|
||||
longitude = 117.22743
|
||||
|
||||
# Compare it with the real data
|
||||
self.hass.config.latitude = latitude
|
||||
self.hass.config.longitude = longitude
|
||||
sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
||||
|
||||
astral = Astral()
|
||||
utc_now = dt_util.utcnow()
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_rising = (astral.sunrise_utc(utc_now +
|
||||
timedelta(days=mod), latitude, longitude))
|
||||
if next_rising > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
mod = -1
|
||||
while True:
|
||||
next_setting = (astral.sunset_utc(utc_now +
|
||||
timedelta(days=mod), latitude, longitude))
|
||||
if next_setting > utc_now:
|
||||
break
|
||||
mod += 1
|
||||
|
||||
self.assertEqual(next_rising, sun.next_rising_utc(self.hass))
|
||||
self.assertEqual(next_setting, sun.next_setting_utc(self.hass))
|
||||
|
||||
# Point it at a state without the proper attributes
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
|
||||
self.assertIsNone(sun.next_rising(self.hass))
|
||||
self.assertIsNone(sun.next_setting(self.hass))
|
||||
|
||||
# Point it at a non-existing state
|
||||
self.assertIsNone(sun.next_rising(self.hass, 'non.existing'))
|
||||
self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))
|
||||
|
||||
def test_state_change(self):
|
||||
""" Test if the state changes at next setting/rising. """
|
||||
self.hass.config.latitude = '32.87336'
|
||||
self.hass.config.longitude = '117.22743'
|
||||
sun.setup(self.hass, {})
|
||||
|
||||
if sun.is_on(self.hass):
|
||||
test_state = sun.STATE_BELOW_HORIZON
|
||||
test_time = sun.next_setting(self.hass)
|
||||
else:
|
||||
test_state = sun.STATE_ABOVE_HORIZON
|
||||
test_time = sun.next_rising(self.hass)
|
||||
|
||||
self.assertIsNotNone(test_time)
|
||||
|
||||
self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
|
||||
{ha.ATTR_NOW: test_time + timedelta(seconds=5)})
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
|
||||
@@ -0,0 +1,98 @@
|
||||
"""
|
||||
tests.test_component_switch
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests switch component.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods,protected-access
|
||||
import unittest
|
||||
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM
|
||||
import homeassistant.components.switch as switch
|
||||
|
||||
from common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestSwitch(unittest.TestCase):
|
||||
""" Test the switch module. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
self.hass = get_test_home_assistant()
|
||||
loader.prepare(self.hass)
|
||||
|
||||
platform = loader.get_component('switch.test')
|
||||
|
||||
platform.init()
|
||||
self.assertTrue(switch.setup(
|
||||
self.hass, {switch.DOMAIN: {CONF_PLATFORM: 'test'}}
|
||||
))
|
||||
|
||||
# Switch 1 is ON, switch 2 is OFF
|
||||
self.switch_1, self.switch_2, self.switch_3 = \
|
||||
platform.DEVICES
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_methods(self):
|
||||
""" Test is_on, turn_on, turn_off methods. """
|
||||
self.assertTrue(switch.is_on(self.hass))
|
||||
self.assertEqual(
|
||||
STATE_ON,
|
||||
self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
|
||||
self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))
|
||||
self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
|
||||
self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))
|
||||
|
||||
switch.turn_off(self.hass, self.switch_1.entity_id)
|
||||
switch.turn_on(self.hass, self.switch_2.entity_id)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertTrue(switch.is_on(self.hass))
|
||||
self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id))
|
||||
self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))
|
||||
|
||||
# Turn all off
|
||||
switch.turn_off(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertFalse(switch.is_on(self.hass))
|
||||
self.assertEqual(
|
||||
STATE_OFF,
|
||||
self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
|
||||
self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id))
|
||||
self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
|
||||
self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))
|
||||
|
||||
# Turn all on
|
||||
switch.turn_on(self.hass)
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertTrue(switch.is_on(self.hass))
|
||||
self.assertEqual(
|
||||
STATE_ON,
|
||||
self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
|
||||
self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))
|
||||
self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))
|
||||
self.assertTrue(switch.is_on(self.hass, self.switch_3.entity_id))
|
||||
|
||||
def test_setup_two_platforms(self):
|
||||
""" Test with bad config. """
|
||||
# Test if switch component returns 0 switches
|
||||
test_platform = loader.get_component('switch.test')
|
||||
test_platform.init(True)
|
||||
|
||||
loader.set_component('switch.test2', test_platform)
|
||||
test_platform.init(False)
|
||||
|
||||
self.assertTrue(switch.setup(
|
||||
self.hass, {
|
||||
switch.DOMAIN: {CONF_PLATFORM: 'test'},
|
||||
'{} 2'.format(switch.DOMAIN): {CONF_PLATFORM: 'test2'},
|
||||
}
|
||||
))
|
||||
Reference in new issue
Block a user