Skip to content

Commit

Permalink
Allow embedded platforms (home-assistant#19948)
Browse files Browse the repository at this point in the history
* Allow embedded platforms

* Fix test
  • Loading branch information
balloob authored Jan 11, 2019
1 parent b3a08d5 commit a8f2228
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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 = '*'
Expand Down
31 changes: 27 additions & 4 deletions homeassistant/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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


Expand Down
3 changes: 2 additions & 1 deletion homeassistant/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

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__)
Expand Down
1 change: 0 additions & 1 deletion tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def test_set_component(self):
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."""
Expand Down

0 comments on commit a8f2228

Please sign in to comment.