2016-03-09 09:25:50 +00:00
|
|
|
"""Test to verify that we can load components."""
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=protected-access
|
2017-07-16 16:23:06 +00:00
|
|
|
import asyncio
|
2014-11-23 17:51:16 +00:00
|
|
|
import unittest
|
|
|
|
|
2017-07-16 16:23:06 +00:00
|
|
|
import pytest
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
import homeassistant.loader as loader
|
|
|
|
import homeassistant.components.http as http
|
|
|
|
|
2017-07-16 16:23:06 +00:00
|
|
|
from tests.common import (
|
|
|
|
get_test_home_assistant, MockModule, async_mock_service)
|
2014-11-25 08:20:36 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
class TestLoader(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the loader module."""
|
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def setUp(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup tests."""
|
2014-11-26 07:15:58 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def tearDown(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2014-11-29 04:22:08 +00:00
|
|
|
self.hass.stop()
|
2014-11-23 20:57:29 +00:00
|
|
|
|
2014-11-25 08:20:36 +00:00
|
|
|
def test_set_component(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if set_component works."""
|
2018-05-01 18:57:30 +00:00
|
|
|
comp = object()
|
|
|
|
loader.set_component(self.hass, 'switch.test_set', comp)
|
2014-11-25 08:20:36 +00:00
|
|
|
|
2018-05-07 00:54:56 +00:00
|
|
|
assert loader.get_component(self.hass, 'switch.test_set') is comp
|
2014-11-25 08:20:36 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_get_component(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if get_component works."""
|
2018-05-01 18:57:30 +00:00
|
|
|
self.assertEqual(http, loader.get_component(self.hass, 'http'))
|
|
|
|
self.assertIsNotNone(loader.get_component(self.hass, 'light.hue'))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
|
|
|
def test_load_order_component(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if we can get the proper load order of components."""
|
2018-05-01 18:57:30 +00:00
|
|
|
loader.set_component(self.hass, 'mod1', MockModule('mod1'))
|
|
|
|
loader.set_component(self.hass, 'mod2', MockModule('mod2', ['mod1']))
|
|
|
|
loader.set_component(self.hass, 'mod3', MockModule('mod3', ['mod2']))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
|
|
|
self.assertEqual(
|
2018-05-01 18:57:30 +00:00
|
|
|
['mod1', 'mod2', 'mod3'],
|
|
|
|
loader.load_order_component(self.hass, 'mod3'))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
|
|
|
# Create circular dependency
|
2018-05-01 18:57:30 +00:00
|
|
|
loader.set_component(self.hass, 'mod1', MockModule('mod1', ['mod3']))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
2018-05-01 18:57:30 +00:00
|
|
|
self.assertEqual([], loader.load_order_component(self.hass, 'mod3'))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
|
|
|
# Depend on non-existing component
|
2018-05-01 18:57:30 +00:00
|
|
|
loader.set_component(self.hass, 'mod1',
|
|
|
|
MockModule('mod1', ['nonexisting']))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
2018-05-01 18:57:30 +00:00
|
|
|
self.assertEqual([], loader.load_order_component(self.hass, 'mod1'))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
|
|
|
# Try to get load order for non-existing component
|
2018-05-01 18:57:30 +00:00
|
|
|
self.assertEqual([], loader.load_order_component(self.hass, 'mod1'))
|
2017-07-16 16:23:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_component_loader(hass):
|
|
|
|
"""Test loading components."""
|
|
|
|
components = loader.Components(hass)
|
|
|
|
assert components.http.CONFIG_SCHEMA is http.CONFIG_SCHEMA
|
|
|
|
assert hass.components.http.CONFIG_SCHEMA is http.CONFIG_SCHEMA
|
|
|
|
|
|
|
|
|
|
|
|
def test_component_loader_non_existing(hass):
|
|
|
|
"""Test loading components."""
|
|
|
|
components = loader.Components(hass)
|
|
|
|
with pytest.raises(ImportError):
|
|
|
|
components.non_existing
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_component_wrapper(hass):
|
|
|
|
"""Test component wrapper."""
|
|
|
|
calls = async_mock_service(hass, 'light', 'turn_on')
|
|
|
|
|
|
|
|
components = loader.Components(hass)
|
|
|
|
components.light.async_turn_on('light.test')
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|
2017-10-08 15:17:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_helpers_wrapper(hass):
|
|
|
|
"""Test helpers wrapper."""
|
|
|
|
helpers = loader.Helpers(hass)
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
|
|
|
def discovery_callback(service, discovered):
|
|
|
|
"""Handle discovery callback."""
|
|
|
|
result.append(discovered)
|
|
|
|
|
|
|
|
helpers.discovery.async_listen('service_name', discovery_callback)
|
|
|
|
|
|
|
|
yield from helpers.discovery.async_discover('service_name', 'hello')
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result == ['hello']
|
2018-05-07 00:54:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_custom_component_name(hass):
|
|
|
|
"""Test the name attribte of custom components."""
|
|
|
|
comp = loader.get_component(hass, 'test_standalone')
|
|
|
|
assert comp.__name__ == 'custom_components.test_standalone'
|
|
|
|
assert comp.__package__ == 'custom_components'
|
|
|
|
|
|
|
|
comp = loader.get_component(hass, 'test_package')
|
|
|
|
assert comp.__name__ == 'custom_components.test_package'
|
|
|
|
assert comp.__package__ == 'custom_components.test_package'
|
|
|
|
|
|
|
|
comp = loader.get_component(hass, 'light.test')
|
|
|
|
assert comp.__name__ == 'custom_components.light.test'
|
|
|
|
assert comp.__package__ == 'custom_components.light'
|
2018-05-07 09:25:48 +00:00
|
|
|
|
|
|
|
# Test custom components is mounted
|
|
|
|
from custom_components.test_package import TEST
|
|
|
|
assert TEST == 5
|