From a8f22287caa7f0ac1ffe33affef0d6c7d8f85542 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 11 Jan 2019 11:30:22 -0800 Subject: [PATCH] Allow embedded platforms (#19948) * Allow embedded platforms * Fix test --- .../components/{light/hue.py => hue/light.py} | 0 homeassistant/const.py | 2 +- homeassistant/loader.py | 31 ++++++++++++++++--- homeassistant/setup.py | 3 +- .../{light/test_hue.py => hue/test_light.py} | 2 +- tests/test_loader.py | 1 - 6 files changed, 31 insertions(+), 8 deletions(-) rename homeassistant/components/{light/hue.py => hue/light.py} (100%) rename tests/components/{light/test_hue.py => hue/test_light.py} (99%) diff --git a/homeassistant/components/light/hue.py b/homeassistant/components/hue/light.py similarity index 100% rename from homeassistant/components/light/hue.py rename to homeassistant/components/hue/light.py diff --git a/homeassistant/const.py b/homeassistant/const.py index a03b5fe52bc..6d1d72193fc 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -8,7 +8,7 @@ __version__ = '{}.{}'.format(__short_version__, PATCH_VERSION) REQUIRED_PYTHON_VER = (3, 5, 3) # Format for platforms -PLATFORM_FORMAT = '{}.{}' +PLATFORM_FORMAT = '{domain}.{platform}' # 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 61aacd3b233..cd22a69dab1 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -52,18 +52,43 @@ def set_component(hass, # type: HomeAssistant def get_platform(hass, # type: HomeAssistant - domain: str, platform: str) -> Optional[ModuleType]: + domain: str, platform_name: str) -> Optional[ModuleType]: """Try to load specified platform. Async friendly. """ - return get_component(hass, PLATFORM_FORMAT.format(domain, platform)) + 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 None: + _LOGGER.error("Unable to find platform %s", platform_name) + + return platform def get_component(hass, # type: HomeAssistant comp_or_platform: str) -> Optional[ModuleType]: """Try to load specified component. + Async friendly. + """ + comp = _load_file(hass, comp_or_platform) + + if comp is None: + _LOGGER.error("Unable to find component %s", comp_or_platform) + + return comp + + +def _load_file(hass, # type: HomeAssistant + comp_or_platform: str) -> Optional[ModuleType]: + """Try to load specified file. + Looks in config dir first, then built-in components. Only returns it if also found to be valid. Async friendly. @@ -134,8 +159,6 @@ def get_component(hass, # type: HomeAssistant ("Error loading %s. Make sure all " "dependencies are installed"), path) - _LOGGER.error("Unable to find component %s", comp_or_platform) - return None diff --git a/homeassistant/setup.py b/homeassistant/setup.py index cc7c4284f9c..49aae2178fc 100644 --- a/homeassistant/setup.py +++ b/homeassistant/setup.py @@ -190,7 +190,8 @@ async def async_prepare_setup_platform(hass: core.HomeAssistant, config: Dict, This method is a coroutine. """ - platform_path = PLATFORM_FORMAT.format(domain, platform_name) + platform_path = PLATFORM_FORMAT.format(domain=domain, + platform=platform_name) def log_error(msg: str) -> None: """Log helper.""" diff --git a/tests/components/light/test_hue.py b/tests/components/hue/test_light.py similarity index 99% rename from tests/components/light/test_hue.py rename to tests/components/hue/test_light.py index ad4026e7f31..023a3416968 100644 --- a/tests/components/light/test_hue.py +++ b/tests/components/hue/test_light.py @@ -11,7 +11,7 @@ import pytest from homeassistant import config_entries from homeassistant.components import hue -import homeassistant.components.light.hue as hue_light +from homeassistant.components.hue import light as hue_light from homeassistant.util import color _LOGGER = logging.getLogger(__name__) diff --git a/tests/test_loader.py b/tests/test_loader.py index 90d259c860e..5bd273ea16a 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -35,7 +35,6 @@ class TestLoader(unittest.TestCase): def test_get_component(self): """Test if get_component works.""" assert http == loader.get_component(self.hass, 'http') - assert loader.get_component(self.hass, 'light.hue') is not None def test_load_order_component(self): """Test if we can get the proper load order of components."""