Example #1
0
def get_platform(hass,  # type: HomeAssistant
                 domain: str, platform_name: str) -> Optional[ModuleType]:
    """Try to load specified platform.

    Async friendly.
    """
    platform = _load_file(hass, PLATFORM_FORMAT.format(
        domain=domain, platform=platform_name))

    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
Example #2
0
def get_platform(
        hass,  # type: HomeAssistant
        domain: str,
        platform_name: str) -> Optional[ModuleType]:
    """Try to load specified platform.

    Example invocation: get_platform(hass, 'light', 'hue')

    Async friendly.
    """
    # If the platform has a component, we will limit the platform loading path
    # to be the same source (custom/built-in).
    component = _load_file(hass, platform_name, LOOKUP_PATHS)

    # Until we have moved all platforms under their component/own folder, it
    # can be that the component is None.
    if component is not None:
        base_paths = [component.__name__.rsplit('.', 1)[0]]
    else:
        base_paths = LOOKUP_PATHS

    platform = _load_file(
        hass, PLATFORM_FORMAT.format(domain=domain, platform=platform_name),
        base_paths)

    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),
        base_paths)

    if platform is None:
        if component is None:
            extra = ""
        else:
            extra = " Search path was limited to path of component: {}".format(
                base_paths[0])
        _LOGGER.error("Unable to find platform %s.%s", platform_name, extra)
        return None

    if platform.__name__.startswith(PACKAGE_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
Example #3
0
async def async_prepare_setup_platform(hass: core.HomeAssistant, config,
                                       domain: str, platform_name: str) \
                                 -> Optional[ModuleType]:
    """Load a platform and makes sure dependencies are setup.

    This method is a coroutine.
    """
    platform_path = PLATFORM_FORMAT.format(domain, platform_name)

    def log_error(msg):
        """Log helper."""
        _LOGGER.error("Unable to prepare setup for platform %s: %s",
                      platform_path, msg)
        async_notify_setup_error(hass, platform_path)

    platform = loader.get_platform(hass, domain, platform_name)

    # Not found
    if platform is None:
        log_error("Platform not found.")
        return None

    # Already loaded
    elif platform_path in hass.config.components:
        return platform

    try:
        await async_process_deps_reqs(hass, config, platform_path, platform)
    except HomeAssistantError as err:
        log_error(str(err))
        return None

    return platform
Example #4
0
def get_platform(hass,  # type: HomeAssistant
                 domain: str, platform: str) -> Optional[ModuleType]:
    """Try to load specified platform.

    Async friendly.
    """
    return get_component(hass, PLATFORM_FORMAT.format(domain, platform))
Example #5
0
def async_prepare_setup_platform(hass: core.HomeAssistant, config, domain: str,
                                 platform_name: str) \
                                 -> Optional[ModuleType]:
    """Load a platform and makes sure dependencies are setup.

    This method is a coroutine.
    """
    platform_path = PLATFORM_FORMAT.format(domain, platform_name)

    def log_error(msg):
        """Log helper."""
        _LOGGER.error("Unable to prepare setup for platform %s: %s",
                      platform_path, msg)
        async_notify_setup_error(hass, platform_path)

    platform = loader.get_platform(domain, platform_name)

    # Not found
    if platform is None:
        log_error("Platform not found.")
        return None

    # Already loaded
    elif platform_path in hass.config.components:
        return platform

    try:
        yield from async_process_deps_reqs(
            hass, config, platform_path, platform)
    except HomeAssistantError as err:
        log_error(str(err))
        return None

    return platform
Example #6
0
def prepare_setup_platform(hass, config, domain, platform_name):
    """Load a platform and makes sure dependencies are setup."""
    _ensure_loader_prepared(hass)

    platform_path = PLATFORM_FORMAT.format(domain, platform_name)

    platform = loader.get_platform(domain, platform_name)

    # Not found
    if platform is None:
        _LOGGER.error('Unable to find platform %s', platform_path)
        return None

    # Already loaded
    elif platform_path in hass.config.components:
        return platform

    # Load dependencies
    for component in getattr(platform, 'DEPENDENCIES', []):
        if not setup_component(hass, component, config):
            _LOGGER.error(
                'Unable to prepare setup for platform %s because '
                'dependency %s could not be initialized', platform_path,
                component)
            return None

    if not _handle_requirements(hass, platform, platform_path):
        return None

    return platform
Example #7
0
def prepare_setup_platform(hass, config, domain, platform_name):
    """Load a platform and makes sure dependencies are setup."""
    _ensure_loader_prepared(hass)

    platform_path = PLATFORM_FORMAT.format(domain, platform_name)

    platform = loader.get_platform(domain, platform_name)

    # Not found
    if platform is None:
        _LOGGER.error('Unable to find platform %s', platform_path)
        return None

    # Already loaded
    elif platform_path in hass.config.components:
        return platform

    # Load dependencies
    for component in getattr(platform, 'DEPENDENCIES', []):
        if not setup_component(hass, component, config):
            _LOGGER.error(
                'Unable to prepare setup for platform %s because '
                'dependency %s could not be initialized', platform_path,
                component)
            return None

    if not _handle_requirements(hass, platform, platform_path):
        return None

    return platform
Example #8
0
def get_platform(hass,  # type: HomeAssistant
                 domain: str, platform: str) -> Optional[ModuleType]:
    """Try to load specified platform.

    Async friendly.
    """
    return get_component(hass, PLATFORM_FORMAT.format(domain, platform))
Example #9
0
def get_platform(hass,  # type: HomeAssistant
                 domain: str, platform_name: str) -> Optional[ModuleType]:
    """Try to load specified platform.

    Example invocation: get_platform(hass, 'light', 'hue')

    Async friendly.
    """
    # If the platform has a component, we will limit the platform loading path
    # to be the same source (custom/built-in).
    component = _load_file(hass, platform_name, LOOKUP_PATHS)

    # Until we have moved all platforms under their component/own folder, it
    # can be that the component is None.
    if component is not None:
        base_paths = [component.__name__.rsplit('.', 1)[0]]
    else:
        base_paths = LOOKUP_PATHS

    platform = _load_file(
        hass, PLATFORM_FORMAT.format(domain=domain, platform=platform_name),
        base_paths)

    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),
        base_paths)

    if platform is None:
        if component is None:
            extra = ""
        else:
            extra = " Search path was limited to path of component: {}".format(
                base_paths[0])
        _LOGGER.error("Unable to find platform %s.%s", platform_name, extra)
        return None

    if platform.__name__.startswith(PACKAGE_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
Example #10
0
async def async_prepare_setup_platform(hass: core.HomeAssistant,
                                       hass_config: Dict,
                                       domain: str, platform_name: str) \
                                 -> Optional[ModuleType]:
    """Load a platform and makes sure dependencies are setup.

    This method is a coroutine.
    """
    platform_path = PLATFORM_FORMAT.format(domain=domain,
                                           platform=platform_name)

    def log_error(msg: str) -> None:
        """Log helper."""
        _LOGGER.error("Unable to prepare setup for platform %s: %s",
                      platform_path, msg)
        async_notify_setup_error(hass, platform_path)

    try:
        integration = await loader.async_get_integration(hass, platform_name)
    except loader.IntegrationNotFound:
        log_error("Integration not found")
        return None

    # Process deps and reqs as soon as possible, so that requirements are
    # available when we import the platform.
    try:
        await async_process_deps_reqs(hass, hass_config, integration)
    except HomeAssistantError as err:
        log_error(str(err))
        return None

    try:
        platform = integration.get_platform(domain)
    except ImportError as exc:
        log_error("Platform not found ({}).".format(exc))
        return None

    # Already loaded
    if platform_path in hass.config.components:
        return platform

    # Platforms cannot exist on their own, they are part of their integration.
    # If the integration is not set up yet, and can be set up, set it up.
    if integration.domain not in hass.config.components:
        try:
            component = integration.get_component()
        except ImportError as exc:
            log_error("Unable to import the component ({}).".format(exc))
            return None

        if (hasattr(component, 'setup')
                or hasattr(component, 'async_setup')):
            if not await async_setup_component(
                    hass, integration.domain, hass_config
            ):
                log_error("Unable to set up component.")
                return None

    return platform
Example #11
0
    def async_register_engine(self, engine, provider, config):
        """Register a TTS provider."""
        provider.hass = self.hass
        if provider.name is None:
            provider.name = engine
        self.providers[engine] = provider

        self.hass.config.components.add(
            PLATFORM_FORMAT.format(domain=engine, platform=DOMAIN))
Example #12
0
def get_platform(hass,  # type: HomeAssistant
                 domain: str, platform_name: str) -> Optional[ModuleType]:
    """Try to load specified platform.

    Async friendly.
    """
    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
Example #13
0
def get_platform(
        hass,  # type: HomeAssistant
        domain: str,
        platform_name: str) -> Optional[ModuleType]:
    """Try to load specified platform.

    Async friendly.
    """
    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
Example #14
0
def async_prepare_setup_platform(hass: core.HomeAssistant, config, domain: str,
                                 platform_name: str) \
                                 -> Optional[ModuleType]:
    """Load a platform and makes sure dependencies are setup.

    This method is a coroutine.
    """
    if not loader.PREPARED:
        yield from hass.loop.run_in_executor(None, loader.prepare, hass)

    platform_path = PLATFORM_FORMAT.format(domain, platform_name)

    platform = loader.get_platform(domain, platform_name)

    # Not found
    if platform is None:
        _LOGGER.error('Unable to find platform %s', platform_path)
        _async_persistent_notification(hass, platform_path)
        return None

    # Already loaded
    elif platform_path in hass.config.components:
        return platform

    # Load dependencies
    for component in getattr(platform, 'DEPENDENCIES', []):
        if component in loader.DEPENDENCY_BLACKLIST:
            raise HomeAssistantError(
                '{} is not allowed to be a dependency.'.format(component))

        res = yield from async_setup_component(hass, component, config)
        if not res:
            _LOGGER.error(
                'Unable to prepare setup for platform %s because '
                'dependency %s could not be initialized', platform_path,
                component)
            _async_persistent_notification(hass, platform_path, True)
            return None

    res = yield from hass.loop.run_in_executor(
        None, _handle_requirements, hass, platform, platform_path)
    if not res:
        return None

    return platform
Example #15
0
def async_prepare_setup_platform(hass: core.HomeAssistant, config, domain: str,
                                 platform_name: str) \
                                 -> Optional[ModuleType]:
    """Load a platform and makes sure dependencies are setup.

    This method is a coroutine.
    """
    platform_path = PLATFORM_FORMAT.format(domain, platform_name)

    def log_error(msg):
        """Log helper."""
        _LOGGER.error("Unable to prepare setup for platform %s: %s",
                      platform_path, msg)
        async_notify_setup_error(hass, platform_path)

    platform = loader.get_platform(domain, platform_name)

    # Not found
    if platform is None:
        log_error("Platform not found.")
        return None

    # Already loaded
    elif platform_path in hass.config.components:
        return platform

    # Load dependencies
    if hasattr(platform, 'DEPENDENCIES'):
        dep_success = yield from _async_process_dependencies(
            hass, config, platform_path, platform.DEPENDENCIES)

        if not dep_success:
            log_error("Could not setup all dependencies.")
            return None

    if not hass.config.skip_pip and hasattr(platform, 'REQUIREMENTS'):
        req_success = yield from _async_process_requirements(
            hass, platform_path, platform.REQUIREMENTS)

        if not req_success:
            log_error("Could not install all requirements.")
            return None

    return platform
Example #16
0
def async_prepare_setup_platform(hass: core.HomeAssistant, config, domain: str,
                                 platform_name: str) \
                                 -> Optional[ModuleType]:
    """Load a platform and makes sure dependencies are setup.

    This method is a coroutine.
    """
    platform_path = PLATFORM_FORMAT.format(domain, platform_name)

    def log_error(msg):
        """Log helper."""
        _LOGGER.error('Unable to prepare setup for platform %s: %s',
                      platform_path, msg)
        async_notify_setup_error(hass, platform_path)

    platform = loader.get_platform(domain, platform_name)

    # Not found
    if platform is None:
        log_error('Platform not found.')
        return None

    # Already loaded
    elif platform_path in hass.config.components:
        return platform

    # Load dependencies
    if hasattr(platform, 'DEPENDENCIES'):
        dep_success = yield from _async_process_dependencies(
            hass, config, platform_path, platform.DEPENDENCIES)

        if not dep_success:
            log_error('Could not setup all dependencies.')
            return None

    if not hass.config.skip_pip and hasattr(platform, 'REQUIREMENTS'):
        req_success = yield from _async_process_requirements(
            hass, platform_path, platform.REQUIREMENTS)

        if not req_success:
            log_error('Could not install all requirements.')
            return None

    return platform
Example #17
0
def prepare_setup_platform(hass: core.HomeAssistant, config, domain: str, platform_name: str) -> Optional[ModuleType]:
    """Load a platform and makes sure dependencies are setup."""
    _ensure_loader_prepared(hass)

    platform_path = PLATFORM_FORMAT.format(domain, platform_name)

    platform = loader.get_platform(domain, platform_name)

    # Not found
    if platform is None:
        _LOGGER.error("Unable to find platform %s", platform_path)

        _PERSISTENT_PLATFORMS.add(platform_path)
        message = (
            "Unable to find the following platforms: "
            + ", ".join(list(_PERSISTENT_PLATFORMS))
            + "(please check your configuration)"
        )
        persistent_notification.create(hass, message, "Invalid platforms", "platform_errors")
        return None

    # Already loaded
    elif platform_path in hass.config.components:
        return platform

    # Load dependencies
    for component in getattr(platform, "DEPENDENCIES", []):
        if not setup_component(hass, component, config):
            _LOGGER.error(
                "Unable to prepare setup for platform %s because " "dependency %s could not be initialized",
                platform_path,
                component,
            )
            return None

    if not _handle_requirements(hass, platform, platform_path):
        return None

    return platform
Example #18
0
def prepare_setup_platform(hass: core.HomeAssistant, config, domain: str,
                           platform_name: str) -> Optional[ModuleType]:
    """Load a platform and makes sure dependencies are setup."""
    _ensure_loader_prepared(hass)

    platform_path = PLATFORM_FORMAT.format(domain, platform_name)

    platform = loader.get_platform(domain, platform_name)

    # Not found
    if platform is None:
        _LOGGER.error('Unable to find platform %s', platform_path)

        _PERSISTENT_PLATFORMS.add(platform_path)
        message = ('Unable to find the following platforms: ' +
                   ', '.join(list(_PERSISTENT_PLATFORMS)) +
                   '(please check your configuration)')
        persistent_notification.create(hass, message, 'Invalid platforms',
                                       'platform_errors')
        return None

    # Already loaded
    elif platform_path in hass.config.components:
        return platform

    # Load dependencies
    for component in getattr(platform, 'DEPENDENCIES', []):
        if not setup_component(hass, component, config):
            _LOGGER.error(
                'Unable to prepare setup for platform %s because '
                'dependency %s could not be initialized', platform_path,
                component)
            return None

    if not _handle_requirements(hass, platform, platform_path):
        return None

    return platform
Example #19
0
def get_platform(domain: str, platform: str) -> Optional[ModuleType]:
    """Try to load specified platform."""
    return get_component(PLATFORM_FORMAT.format(domain, platform))
Example #20
0
def get_platform(domain, platform):
    """Try to load specified platform."""
    return get_component(PLATFORM_FORMAT.format(domain, platform))