diff --git a/ha_test/config/custom_components/light/test.py b/ha_test/config/custom_components/light/test.py new file mode 100644 index 000000000000..757099ddca04 --- /dev/null +++ b/ha_test/config/custom_components/light/test.py @@ -0,0 +1,29 @@ +""" +custom_components.light.test +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Provides a mock switch platform. + +Call init before using it in your tests to ensure clean test data. +""" +import homeassistant.components as components +from ha_test.helper import MockToggleDevice + + +DEVICES = [] + + +def init(empty=False): + """ (re-)initalizes the platform with devices. """ + global DEVICES + + DEVICES = [] if empty else [ + MockToggleDevice('Ceiling', components.STATE_ON), + MockToggleDevice('Ceiling', components.STATE_OFF), + MockToggleDevice(None, components.STATE_OFF) + ] + + +def get_lights(hass, config): + """ Returns mock devices. """ + return DEVICES diff --git a/ha_test/config/custom_components/switch/test.py b/ha_test/config/custom_components/switch/test.py new file mode 100644 index 000000000000..927aca24feb6 --- /dev/null +++ b/ha_test/config/custom_components/switch/test.py @@ -0,0 +1,29 @@ +""" +custom_components.switch.test +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Provides a mock switch platform. + +Call init before using it in your tests to ensure clean test data. +""" +import homeassistant.components as components +from ha_test.helper import MockToggleDevice + + +DEVICES = [] + + +def init(empty=False): + """ (re-)initalizes the platform with devices. """ + global DEVICES + + DEVICES = [] if empty else [ + MockToggleDevice('AC', components.STATE_ON), + MockToggleDevice('AC', components.STATE_OFF), + MockToggleDevice(None, components.STATE_OFF) + ] + + +def get_switches(hass, config): + """ Returns mock devices. """ + return DEVICES diff --git a/test/mock_toggledevice_platform.py b/ha_test/helper.py similarity index 53% rename from test/mock_toggledevice_platform.py rename to ha_test/helper.py index f864301ade4d..109b4185ba59 100644 --- a/test/mock_toggledevice_platform.py +++ b/ha_test/helper.py @@ -1,16 +1,48 @@ """ -test.mock.switch_platform -~~~~~~~~~~~~~~~~~~~~~~~~~ +ha_test.helper +~~~~~~~~~~~~~ -Provides a mock switch platform. - -Call init before using it in your tests to ensure clean test data. +Helper method for writing tests. """ +import os + +import homeassistant as ha import homeassistant.components as components +def get_test_home_assistant(): + """ Returns a Home Assistant object pointing at test config dir. """ + hass = ha.HomeAssistant() + hass.config_dir = os.path.join(os.path.dirname(__file__), "config") + + return hass + + +def mock_service(hass, domain, service): + """ + Sets up a fake service. + Returns a list that logs all calls to fake service. + """ + calls = [] + + hass.services.register( + domain, service, lambda call: calls.append(call)) + + return calls + + +class MockModule(object): + """ Provides a fake module. """ + + def __init__(self, domain, dependencies=[], setup=None): + self.DOMAIN = domain + self.DEPENDENCIES = dependencies + # Setup a mock setup if none given. + self.setup = lambda hass, config: False if setup is None else setup + + class MockToggleDevice(components.ToggleDevice): - """ Fake switch. """ + """ Provides a mock toggle device. """ def __init__(self, name, state): self.name = name self.state = state @@ -42,23 +74,3 @@ class MockToggleDevice(components.ToggleDevice): else: return next(call for call in reversed(self.calls) if call[0] == method) - -DEVICES = [] - - -def init(empty=False): - """ (re-)initalizes the platform with devices. """ - global DEVICES - - DEVICES = [] if empty else [ - MockToggleDevice('AC', components.STATE_ON), - MockToggleDevice('AC', components.STATE_OFF), - MockToggleDevice(None, components.STATE_OFF) - ] - - -def get_switches(hass, config): - """ Returns mock devices. """ - return DEVICES - -get_lights = get_switches diff --git a/test/test_component_chromecast.py b/ha_test/test_component_chromecast.py similarity index 97% rename from test/test_component_chromecast.py rename to ha_test/test_component_chromecast.py index 82cb511c5b7e..3b80c700606b 100644 --- a/test/test_component_chromecast.py +++ b/ha_test/test_component_chromecast.py @@ -1,6 +1,6 @@ """ -test.test_component_chromecast -~~~~~~~~~~~ +ha_test.test_component_chromecast +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests Chromecast component. """ diff --git a/test/test_component_core.py b/ha_test/test_component_core.py similarity index 97% rename from test/test_component_core.py rename to ha_test/test_component_core.py index 24d66a6a3bb9..2a4a942a5ba0 100644 --- a/test/test_component_core.py +++ b/ha_test/test_component_core.py @@ -1,6 +1,6 @@ """ -test.test_component_core -~~~~~~~~~~~~~~~~~~~~~~~~ +ha_test.test_component_core +~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests core compoments. """ diff --git a/test/test_component_demo.py b/ha_test/test_component_demo.py similarity index 97% rename from test/test_component_demo.py rename to ha_test/test_component_demo.py index dd3e47cb97e9..a510759a8ea2 100644 --- a/test/test_component_demo.py +++ b/ha_test/test_component_demo.py @@ -1,6 +1,6 @@ """ -test.test_component_demo -~~~~~~~~~~~~~~~~~~~~~~~~ +ha_test.test_component_demo +~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests demo component. """ diff --git a/test/test_component_group.py b/ha_test/test_component_group.py similarity index 99% rename from test/test_component_group.py rename to ha_test/test_component_group.py index 80c63ae9b0cd..d1d9dccbb27f 100644 --- a/test/test_component_group.py +++ b/ha_test/test_component_group.py @@ -1,6 +1,6 @@ """ -test.test_component_group -~~~~~~~~~~~~~~~~~~~~~~~~~ +ha_test.test_component_group +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests the group compoments. """ diff --git a/test/test_component_http.py b/ha_test/test_component_http.py similarity index 99% rename from test/test_component_http.py rename to ha_test/test_component_http.py index 2a840d394993..15c2966292a7 100644 --- a/test/test_component_http.py +++ b/ha_test/test_component_http.py @@ -1,6 +1,6 @@ """ -test.test_component_http -~~~~~~~~~~~~~~~~~~~~~~~~ +ha_test.test_component_http +~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests Home Assistant HTTP component does what it should do. """ diff --git a/test/test_component_light.py b/ha_test/test_component_light.py similarity index 82% rename from test/test_component_light.py rename to ha_test/test_component_light.py index 255187ea2537..3d4b5c1f3eba 100644 --- a/test/test_component_light.py +++ b/ha_test/test_component_light.py @@ -1,6 +1,6 @@ """ -test.test_component_switch -~~~~~~~~~~~~~~~~~~~~~~~~~~ +ha_test.test_component_switch +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests switch component. """ @@ -11,11 +11,11 @@ import os import homeassistant as ha import homeassistant.loader as loader import homeassistant.util as util -import homeassistant.components as components +from homeassistant.components import ( + get_component, ATTR_ENTITY_ID, STATE_ON, STATE_OFF, + SERVICE_TURN_ON, SERVICE_TURN_OFF) import homeassistant.components.light as light -import mock_toggledevice_platform - from helper import mock_service, get_test_home_assistant @@ -25,7 +25,6 @@ class TestLight(unittest.TestCase): def setUp(self): # pylint: disable=invalid-name self.hass = get_test_home_assistant() loader.prepare(self.hass) - loader.set_component('light.test', mock_toggledevice_platform) def tearDown(self): # pylint: disable=invalid-name """ Stop down stuff we started. """ @@ -39,21 +38,21 @@ class TestLight(unittest.TestCase): def test_methods(self): """ Test if methods call the services as expected. """ # Test is_on - self.hass.states.set('light.test', components.STATE_ON) + self.hass.states.set('light.test', STATE_ON) self.assertTrue(light.is_on(self.hass, 'light.test')) - self.hass.states.set('light.test', components.STATE_OFF) + 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, components.STATE_ON) + 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, components.STATE_OFF) + 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, components.SERVICE_TURN_ON) + self.hass, light.DOMAIN, SERVICE_TURN_ON) light.turn_on( self.hass, @@ -70,17 +69,19 @@ class TestLight(unittest.TestCase): call = turn_on_calls[-1] self.assertEqual(light.DOMAIN, call.domain) - self.assertEqual(components.SERVICE_TURN_ON, call.service) - self.assertEqual('entity_id_val', call.data[components.ATTR_ENTITY_ID]) - self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION]) - self.assertEqual('brightness_val', call.data[light.ATTR_BRIGHTNESS]) - self.assertEqual('rgb_color_val', call.data[light.ATTR_RGB_COLOR]) - self.assertEqual('xy_color_val', call.data[light.ATTR_XY_COLOR]) - self.assertEqual('profile_val', call.data[light.ATTR_PROFILE]) + 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, components.SERVICE_TURN_OFF) + self.hass, light.DOMAIN, SERVICE_TURN_OFF) light.turn_off( self.hass, entity_id='entity_id_val', transition='transition_val') @@ -91,17 +92,19 @@ class TestLight(unittest.TestCase): call = turn_off_calls[-1] self.assertEqual(light.DOMAIN, call.domain) - self.assertEqual(components.SERVICE_TURN_OFF, call.service) - self.assertEqual('entity_id_val', call.data[components.ATTR_ENTITY_ID]) + 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. """ - mock_toggledevice_platform.init() + platform = get_component('light.test') + + platform.init() self.assertTrue( light.setup(self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'test'}})) - dev1, dev2, dev3 = mock_toggledevice_platform.get_lights(None, None) + dev1, dev2, dev3 = platform.get_lights(None, None) # Test init self.assertTrue(light.is_on(self.hass, dev1.entity_id)) @@ -224,10 +227,10 @@ class TestLight(unittest.TestCase): )) # Test if light component returns 0 lightes - mock_toggledevice_platform.init(True) + platform = get_component('light.test') + platform.init(True) - self.assertEqual( - [], mock_toggledevice_platform.get_lights(None, None)) + self.assertEqual([], platform.get_lights(None, None)) self.assertFalse(light.setup( self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'test'}} @@ -235,7 +238,8 @@ class TestLight(unittest.TestCase): def test_light_profiles(self): """ Test light profiles. """ - mock_toggledevice_platform.init() + platform = get_component('light.test') + platform.init() user_light_file = self.hass.get_config_path(light.LIGHT_PROFILES_FILE) @@ -259,7 +263,7 @@ class TestLight(unittest.TestCase): self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'test'}} )) - dev1, dev2, dev3 = mock_toggledevice_platform.get_lights(None, None) + dev1, dev2, dev3 = platform.get_lights(None, None) light.turn_on(self.hass, dev1.entity_id, profile='test') diff --git a/test/test_component_sun.py b/ha_test/test_component_sun.py similarity index 98% rename from test/test_component_sun.py rename to ha_test/test_component_sun.py index d37c8f678f99..a83f8de51a7a 100644 --- a/test/test_component_sun.py +++ b/ha_test/test_component_sun.py @@ -1,6 +1,6 @@ """ -test.test_component_sun -~~~~~~~~~~~~~~~~~~~~~~~ +ha_test.test_component_sun +~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests Sun component. """ diff --git a/test/test_component_switch.py b/ha_test/test_component_switch.py similarity index 84% rename from test/test_component_switch.py rename to ha_test/test_component_switch.py index f81fcdd8f890..0df05f0617ba 100644 --- a/test/test_component_switch.py +++ b/ha_test/test_component_switch.py @@ -1,6 +1,6 @@ """ -test.test_component_switch -~~~~~~~~~~~~~~~~~~~~~~~~~~ +ha_test.test_component_switch +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests switch component. """ @@ -9,28 +9,29 @@ import unittest import homeassistant as ha import homeassistant.loader as loader -import homeassistant.components as components +from homeassistant.components import get_component, STATE_ON, STATE_OFF import homeassistant.components.switch as switch -import mock_toggledevice_platform +from helper import get_test_home_assistant class TestSwitch(unittest.TestCase): """ Test the switch module. """ def setUp(self): # pylint: disable=invalid-name - self.hass = ha.HomeAssistant() + self.hass = get_test_home_assistant() loader.prepare(self.hass) - loader.set_component('switch.test', mock_toggledevice_platform) - mock_toggledevice_platform.init() + platform = get_component('switch.test') + + platform.init() self.assertTrue(switch.setup( self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}} )) # Switch 1 is ON, switch 2 is OFF self.switch_1, self.switch_2, self.switch_3 = \ - mock_toggledevice_platform.get_switches(None, None) + platform.get_switches(None, None) def tearDown(self): # pylint: disable=invalid-name """ Stop down stuff we started. """ @@ -40,7 +41,7 @@ class TestSwitch(unittest.TestCase): """ Test is_on, turn_on, turn_off methods. """ self.assertTrue(switch.is_on(self.hass)) self.assertEqual( - components.STATE_ON, + 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)) @@ -62,7 +63,7 @@ class TestSwitch(unittest.TestCase): self.assertFalse(switch.is_on(self.hass)) self.assertEqual( - components.STATE_OFF, + 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)) @@ -75,7 +76,7 @@ class TestSwitch(unittest.TestCase): self.assertTrue(switch.is_on(self.hass)) self.assertEqual( - components.STATE_ON, + 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)) @@ -93,10 +94,10 @@ class TestSwitch(unittest.TestCase): )) # Test if switch component returns 0 switches - mock_toggledevice_platform.init(True) + get_component('switch.test').init(True) self.assertEqual( - [], mock_toggledevice_platform.get_switches(None, None)) + [], get_component('switch.test').get_switches(None, None)) self.assertFalse(switch.setup( self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}} diff --git a/test/test_core.py b/ha_test/test_core.py similarity index 99% rename from test/test_core.py rename to ha_test/test_core.py index 012aef2a1f34..7c6dc2a902ca 100644 --- a/test/test_core.py +++ b/ha_test/test_core.py @@ -1,6 +1,6 @@ """ -test.test_core -~~~~~~~~~~~~~~ +ha_test.test_core +~~~~~~~~~~~~~~~~~ Provides tests to verify that Home Assistant core works. """ diff --git a/test/test_loader.py b/ha_test/test_loader.py similarity index 90% rename from test/test_loader.py rename to ha_test/test_loader.py index a00fc02e2507..4d9bc190145a 100644 --- a/test/test_loader.py +++ b/ha_test/test_loader.py @@ -1,6 +1,6 @@ """ -test.test_loader -~~~~~~~~~~~~~~~~~~ +ha_ha_test.test_loader +~~~~~~~~~~~~~~~~~~~~~~ Provides tests to verify that we can load components. """ @@ -10,7 +10,6 @@ import unittest import homeassistant.loader as loader import homeassistant.components.http as http -import mock_toggledevice_platform from helper import get_test_home_assistant, MockModule @@ -26,16 +25,15 @@ class TestLoader(unittest.TestCase): def test_set_component(self): """ Test if set_component works. """ - loader.set_component('switch.test', mock_toggledevice_platform) + loader.set_component('switch.test', http) - self.assertEqual( - mock_toggledevice_platform, loader.get_component('switch.test')) + self.assertEqual(http, loader.get_component('switch.test')) def test_get_component(self): """ Test if get_component works. """ self.assertEqual(http, loader.get_component('http')) - self.assertIsNotNone(loader.get_component('custom_one')) + self.assertIsNotNone(loader.get_component('switch.test')) def test_load_order_component(self): """ Test if we can get the proper load order of components. """ diff --git a/test/test_remote.py b/ha_test/test_remote.py similarity index 99% rename from test/test_remote.py rename to ha_test/test_remote.py index 1b093b1c56a0..cc317d639609 100644 --- a/test/test_remote.py +++ b/ha_test/test_remote.py @@ -1,6 +1,6 @@ """ -test.remote -~~~~~~~~~~~ +ha_test.remote +~~~~~~~~~~~~~~ Tests Home Assistant remote methods and classes. Uses port 8122 for master, 8123 for slave diff --git a/test/test_util.py b/ha_test/test_util.py similarity index 99% rename from test/test_util.py rename to ha_test/test_util.py index 1edb4b92a8c9..ee48ef3a7846 100644 --- a/test/test_util.py +++ b/ha_test/test_util.py @@ -1,6 +1,6 @@ """ -test.test_util -~~~~~~~~~~~~~~ +ha_test.test_util +~~~~~~~~~~~~~~~~~ Tests Home Assistant util methods. """ diff --git a/homeassistant/loader.py b/homeassistant/loader.py index e537eb964502..ad8655896943 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -62,9 +62,12 @@ def prepare(hass): # just might output more errors. for fil in os.listdir(custom_path): if os.path.isdir(os.path.join(custom_path, fil)): - AVAILABLE_COMPONENTS.append('custom_components.{}'.format(fil)) + if fil != '__pycache__': + AVAILABLE_COMPONENTS.append( + 'custom_components.{}'.format(fil)) else: + # For files we will strip out .py extension AVAILABLE_COMPONENTS.append( 'custom_components.{}'.format(fil[0:-3])) diff --git a/run_tests.sh b/run_tests.sh index 26934d045d4c..c5b93d4440d2 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -2,4 +2,4 @@ pylint homeassistant flake8 homeassistant --exclude bower_components,external -python3 -m unittest discover test +python3 -m unittest discover ha_test diff --git a/test/config/custom_components/custom_one.py b/test/config/custom_components/custom_one.py deleted file mode 100644 index 53371c590468..000000000000 --- a/test/config/custom_components/custom_one.py +++ /dev/null @@ -1,3 +0,0 @@ -""" -Module to be loaded by the Loader test. -""" diff --git a/test/helper.py b/test/helper.py deleted file mode 100644 index ed0c2c74e828..000000000000 --- a/test/helper.py +++ /dev/null @@ -1,40 +0,0 @@ -""" -test.helper -~~~~~~~~~~~ - -Helper method for writing tests. -""" -import os - -import homeassistant as ha - - -def get_test_home_assistant(): - """ Returns a Home Assistant object pointing at test config dir. """ - hass = ha.HomeAssistant() - hass.config_dir = os.path.join(os.path.dirname(__file__), "config") - - return hass - - -def mock_service(hass, domain, service): - """ - Sets up a fake service. - Returns a list that logs all calls to fake service. - """ - calls = [] - - hass.services.register( - domain, service, lambda call: calls.append(call)) - - return calls - - -class MockModule(object): - """ Provides a fake module. """ - - def __init__(self, domain, dependencies=[], setup=None): - self.DOMAIN = domain - self.DEPENDENCIES = dependencies - # Setup a mock setup if none given. - self.setup = lambda hass, config: False if setup is None else setup