Allow embedded platforms (#19948)

* Allow embedded platforms

* Fix test
This commit is contained in:
Paulus Schoutsen 2019-01-11 11:30:22 -08:00 committed by GitHub
parent b3a08d5876
commit a8f22287ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 8 deletions

View File

@ -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 = '*'

View File

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

View File

@ -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."""

View File

@ -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__)

View File

@ -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."""