Update platform loading path (#20807)

* Warn when platform loaded from an entity component folder

* Fix tests
This commit is contained in:
Paulus Schoutsen 2019-02-07 13:33:12 -08:00 committed by GitHub
parent e0f63132e8
commit 16159cc3d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 9 deletions

View file

@ -7,8 +7,8 @@ __short_version__ = '{}.{}'.format(MAJOR_VERSION, MINOR_VERSION)
__version__ = '{}.{}'.format(__short_version__, PATCH_VERSION)
REQUIRED_PYTHON_VER = (3, 5, 3)
# Format for platforms
PLATFORM_FORMAT = '{domain}.{platform}'
# Format for platform files
PLATFORM_FORMAT = '{platform}.{domain}'
# Can be used to specify a catch all when registering state or event listeners.
MATCH_ALL = '*'

View file

@ -45,9 +45,7 @@ def set_component(hass, # type: HomeAssistant
Async friendly.
"""
cache = hass.data.get(DATA_KEY)
if cache is None:
cache = hass.data[DATA_KEY] = {}
cache = hass.data.setdefault(DATA_KEY, {})
cache[comp_name] = component
@ -60,13 +58,22 @@ def get_platform(hass, # type: HomeAssistant
platform = _load_file(hass, PLATFORM_FORMAT.format(
domain=domain, platform=platform_name))
if platform is None:
# Turn it around for legacy purposes
platform = _load_file(hass, PLATFORM_FORMAT.format(
domain=platform_name, platform=domain))
if platform is not None:
return platform
# Legacy platform check: light/hue.py
platform = _load_file(hass, PLATFORM_FORMAT.format(
domain=platform_name, platform=domain))
if platform is None:
_LOGGER.error("Unable to find platform %s", platform_name)
return None
if platform.__name__.startswith(PATH_CUSTOM_COMPONENTS):
_LOGGER.warning(
"Integrations need to be in their own folder. Change %s/%s.py to "
"%s/%s.py. This will stop working soon.",
domain, platform_name, platform_name, domain)
return platform

View file

@ -486,6 +486,8 @@ class MockModule:
class MockPlatform:
"""Provide a fake platform."""
__name__ = "homeassistant.components.light.bla"
# pylint: disable=invalid-name
def __init__(self, setup_platform=None, dependencies=None,
platform_schema=None, async_setup_platform=None,

View file

@ -132,3 +132,17 @@ async def test_log_warning_custom_component(hass, caplog):
loader.get_component(hass, 'light.test')
assert 'You are using a custom component for light.test' in caplog.text
async def test_get_platform(hass, caplog):
"""Test get_platform."""
# Test we prefer embedded over normal platforms."""
embedded_platform = loader.get_platform(hass, 'switch', 'test_embedded')
assert embedded_platform.__name__ == \
'custom_components.test_embedded.switch'
caplog.clear()
legacy_platform = loader.get_platform(hass, 'switch', 'test')
assert legacy_platform.__name__ == 'custom_components.switch.test'
assert 'Integrations need to be in their own folder.' in caplog.text

View file

@ -0,0 +1 @@
"""Test switch platform for test_embedded component."""