From 16159cc3d0d2adcbff201246df5f46ad3c063769 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 7 Feb 2019 13:33:12 -0800 Subject: [PATCH] Update platform loading path (#20807) * Warn when platform loaded from an entity component folder * Fix tests --- homeassistant/const.py | 4 ++-- homeassistant/loader.py | 21 ++++++++++++------- tests/common.py | 2 ++ tests/test_loader.py | 14 +++++++++++++ .../custom_components/switch/test_embedded.py | 1 + 5 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 tests/testing_config/custom_components/switch/test_embedded.py diff --git a/homeassistant/const.py b/homeassistant/const.py index ba9d32e0daf47..1a3d4e2e455af 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -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 = '*' diff --git a/homeassistant/loader.py b/homeassistant/loader.py index cd22a69dab12a..d02d22cc8d28d 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -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 diff --git a/tests/common.py b/tests/common.py index 3642c5da6ec74..409b020f7286b 100644 --- a/tests/common.py +++ b/tests/common.py @@ -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, diff --git a/tests/test_loader.py b/tests/test_loader.py index 5bd273ea16a05..6fecd5086b102 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -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 diff --git a/tests/testing_config/custom_components/switch/test_embedded.py b/tests/testing_config/custom_components/switch/test_embedded.py new file mode 100644 index 0000000000000..0023aa8a1f204 --- /dev/null +++ b/tests/testing_config/custom_components/switch/test_embedded.py @@ -0,0 +1 @@ +"""Test switch platform for test_embedded component."""