Skip to content

Commit

Permalink
Update platform loading path (home-assistant#20807)
Browse files Browse the repository at this point in the history
* Warn when platform loaded from an entity component folder

* Fix tests
  • Loading branch information
balloob authored Feb 7, 2019
1 parent e0f6313 commit 16159cc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
__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 = '*'
Expand Down
21 changes: 14 additions & 7 deletions homeassistant/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ def __init__(self, domain=None, dependencies=None, setup=None,
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,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test switch platform for test_embedded component."""

0 comments on commit 16159cc

Please sign in to comment.