Files
homeassistant-core/tests/test_loader.py
T

155 lines
5.4 KiB
Python
Raw Normal View History

2016-03-09 10:25:50 +01:00
"""Test to verify that we can load components."""
import pytest
2014-11-23 09:51:16 -08:00
import homeassistant.loader as loader
from homeassistant.components import http, hue
from homeassistant.components.hue import light as hue_light
2014-11-23 09:51:16 -08:00
from tests.common import MockModule, async_mock_service, mock_integration
2014-11-25 00:20:36 -08:00
2014-11-23 09:51:16 -08:00
async def test_component_dependencies(hass):
"""Test if we can get the proper load order of components."""
mock_integration(hass, MockModule('mod1'))
mock_integration(hass, MockModule('mod2', ['mod1']))
mock_integration(hass, MockModule('mod3', ['mod2']))
2014-11-28 15:34:42 -08:00
assert {'mod1', 'mod2', 'mod3'} == \
await loader.async_component_dependencies(hass, 'mod3')
2014-11-28 15:34:42 -08:00
# Create circular dependency
mock_integration(hass, MockModule('mod1', ['mod3']))
2014-11-28 15:34:42 -08:00
with pytest.raises(loader.CircularDependency):
print(await loader.async_component_dependencies(hass, 'mod3'))
2014-11-28 15:34:42 -08:00
# Depend on non-existing component
mock_integration(hass, MockModule('mod1', ['nonexisting']))
2014-11-28 15:34:42 -08:00
with pytest.raises(loader.IntegrationNotFound):
print(await loader.async_component_dependencies(hass, 'mod1'))
2014-11-28 15:34:42 -08:00
# Try to get dependencies for non-existing component
with pytest.raises(loader.IntegrationNotFound):
print(await loader.async_component_dependencies(hass, 'nonexisting'))
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
async def test_component_wrapper(hass):
"""Test component wrapper."""
2018-09-27 23:13:11 +02:00
calls = async_mock_service(hass, 'persistent_notification', 'create')
components = loader.Components(hass)
2018-09-27 23:13:11 +02:00
components.persistent_notification.async_create('message')
await hass.async_block_till_done()
assert len(calls) == 1
2017-10-08 08:17:54 -07:00
async def test_helpers_wrapper(hass):
2017-10-08 08:17:54 -07:00
"""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)
2019-04-14 16:59:06 -07:00
await helpers.discovery.async_discover('service_name', 'hello', None, {})
await hass.async_block_till_done()
2017-10-08 08:17:54 -07:00
assert result == ['hello']
async def test_custom_component_name(hass):
"""Test the name attribte of custom components."""
2019-04-14 22:31:01 -07:00
integration = await loader.async_get_integration(hass, 'test_standalone')
int_comp = integration.get_component()
assert int_comp.__name__ == 'custom_components.test_standalone'
assert int_comp.__package__ == 'custom_components'
comp = hass.components.test_standalone
assert comp.__name__ == 'custom_components.test_standalone'
assert comp.__package__ == 'custom_components'
2019-04-14 22:31:01 -07:00
integration = await loader.async_get_integration(hass, 'test_package')
int_comp = integration.get_component()
assert int_comp.__name__ == 'custom_components.test_package'
assert int_comp.__package__ == 'custom_components.test_package'
comp = hass.components.test_package
assert comp.__name__ == 'custom_components.test_package'
assert comp.__package__ == 'custom_components.test_package'
2019-04-14 22:31:01 -07:00
integration = await loader.async_get_integration(hass, 'test')
platform = integration.get_platform('light')
assert platform.__name__ == 'custom_components.test.light'
assert platform.__package__ == 'custom_components.test'
# Test custom components is mounted
from custom_components.test_package import TEST
assert TEST == 5
2018-06-27 15:21:32 -04:00
async def test_log_warning_custom_component(hass, caplog):
"""Test that we log a warning when loading a custom component."""
2019-04-14 22:31:01 -07:00
hass.components.test_standalone
assert 'You are using a custom integration for test_standalone' \
in caplog.text
2018-06-27 15:21:32 -04:00
2019-04-14 22:31:01 -07:00
await loader.async_get_integration(hass, 'test')
assert 'You are using a custom integration for test ' in caplog.text
2019-02-07 13:33:12 -08:00
async def test_get_integration(hass):
"""Test resolving integration."""
integration = await loader.async_get_integration(hass, 'hue')
assert hue == integration.get_component()
assert hue_light == integration.get_platform('light')
async def test_get_integration_legacy(hass):
"""Test resolving integration."""
integration = await loader.async_get_integration(hass, 'test_embedded')
assert integration.get_component().DOMAIN == 'test_embedded'
assert integration.get_platform('switch') is not None
async def test_get_integration_custom_component(hass):
"""Test resolving integration."""
integration = await loader.async_get_integration(hass, 'test_package')
print(integration)
assert integration.get_component().DOMAIN == 'test_package'
assert integration.name == 'Test Package'
def test_integration_properties(hass):
"""Test integration properties."""
integration = loader.Integration(
hass, 'homeassistant.components.hue', None, {
'name': 'Philips Hue',
'domain': 'hue',
'dependencies': ['test-dep'],
'requirements': ['test-req==1.0.0'],
})
assert integration.name == "Philips Hue"
assert integration.domain == 'hue'
assert integration.dependencies == ['test-dep']
assert integration.requirements == ['test-req==1.0.0']