Пример #1
0
    def _setup_platform(self, platform_type, platform_config,
                        discovery_info=None):
        """Setup a platform for this component."""
        platform = prepare_setup_platform(
            self.hass, self.config, self.domain, platform_type)

        if platform is None:
            return

        # Config > Platform > Component
        scan_interval = (platform_config.get(CONF_SCAN_INTERVAL) or
                         getattr(platform, 'SCAN_INTERVAL', None) or
                         self.scan_interval)
        entity_namespace = platform_config.get(CONF_ENTITY_NAMESPACE)

        key = (platform_type, scan_interval, entity_namespace)

        if key not in self._platforms:
            self._platforms[key] = EntityPlatform(self, scan_interval,
                                                  entity_namespace)
        entity_platform = self._platforms[key]

        try:
            platform.setup_platform(self.hass, platform_config,
                                    entity_platform.add_entities,
                                    discovery_info)

            self.hass.config.components.append(
                '{}.{}'.format(self.domain, platform_type))
        except Exception:  # pylint: disable=broad-except
            self.logger.exception(
                'Error while setting up platform %s', platform_type)
Пример #2
0
    def _setup_platform(self,
                        platform_type,
                        platform_config,
                        discovery_info=None):
        """Setup a platform for this component."""
        platform = prepare_setup_platform(self.hass, self.config, self.domain,
                                          platform_type)

        if platform is None:
            return

        # Config > Platform > Component
        scan_interval = platform_config.get(
            CONF_SCAN_INTERVAL,
            getattr(platform, 'SCAN_INTERVAL', self.scan_interval))
        entity_namespace = platform_config.get(CONF_ENTITY_NAMESPACE)

        try:
            platform.setup_platform(
                self.hass, platform_config,
                EntityPlatform(self, scan_interval,
                               entity_namespace).add_entities, discovery_info)

            self.hass.config.components.append('{}.{}'.format(
                self.domain, platform_type))
        except Exception:  # pylint: disable=broad-except
            self.logger.exception('Error while setting up platform %s',
                                  platform_type)
Пример #3
0
    def _setup_platform(self, platform_type, platform_config,
                        discovery_info=None):
        """ Tries to setup a platform for this component. """
        platform = prepare_setup_platform(
            self.hass, self.config, self.domain, platform_type)

        if platform is None:
            return

        platform_name = '{}.{}'.format(self.domain, platform_type)

        try:
            platform.setup_platform(
                self.hass, platform_config, self.add_entities, discovery_info)

            self.hass.config.components.append(platform_name)

        except AttributeError:
            # AttributeError if setup_platform does not exist
            # Support old deprecated method for now - 3/1/2015
            if hasattr(platform, 'get_devices'):
                self.logger.warning(
                    'Please upgrade %s to return new entities using '
                    'setup_platform. See %s/demo.py for an example.',
                    platform_name, self.domain)
                self.add_entities(
                    platform.get_devices(self.hass, platform_config))

            else:
                self.logger.exception(
                    'Error while setting up platform %s', platform_type)

        except Exception:  # pylint: disable=broad-except
            self.logger.exception(
                'Error while setting up platform %s', platform_type)
Пример #4
0
    def _setup_platform(self, platform_type, platform_config,
                        discovery_info=None):
        """Setup a platform for this component."""
        platform = prepare_setup_platform(
            self.hass, self.config, self.domain, platform_type)

        if platform is None:
            return

        # Config > Platform > Component
        scan_interval = platform_config.get(
            CONF_SCAN_INTERVAL,
            getattr(platform, 'SCAN_INTERVAL', self.scan_interval))

        try:
            platform.setup_platform(
                self.hass, platform_config,
                EntityPlatform(self, scan_interval).add_entities,
                discovery_info)

            self.hass.config.components.append(
                '{}.{}'.format(self.domain, platform_type))
        except Exception:  # pylint: disable=broad-except
            self.logger.exception(
                'Error while setting up platform %s', platform_type)
Пример #5
0
def setup(hass, config):
    """ Sets up the device tracker. """

    if not validate_config(config, {DOMAIN: [CONF_PLATFORM]}, _LOGGER):
        return False

    tracker_type = config[DOMAIN].get(CONF_PLATFORM)

    tracker_implementation = \
        prepare_setup_platform(hass, config, DOMAIN, tracker_type)

    if tracker_implementation is None:
        _LOGGER.error("Unknown device_tracker type specified: %s.",
                      tracker_type)

        return False

    device_scanner = tracker_implementation.get_scanner(hass, config)

    if device_scanner is None:
        _LOGGER.error("Failed to initialize device scanner: %s",
                      tracker_type)

        return False

    seconds = util.convert(config[DOMAIN].get(CONF_SECONDS), int,
                           DEFAULT_CONF_SECONDS)

    track_new_devices = config[DOMAIN].get(TRACK_NEW_DEVICES) or False
    _LOGGER.info("Tracking new devices: %s", track_new_devices)

    tracker = DeviceTracker(hass, device_scanner, seconds, track_new_devices)

    # We only succeeded if we got to parse the known devices file
    return not tracker.invalid_known_devices_file
Пример #6
0
def _resolve_platform(method, hass, config, platform):
    """ Find automation platform. """
    if platform is None:
        return None
    platform = prepare_setup_platform(hass, config, DOMAIN, platform)

    if platform is None or not hasattr(platform, method):
        _LOGGER.error("Unknown automation platform specified for %s: %s",
                      method, platform)
        return None

    return platform
Пример #7
0
def _resolve_platform(method, hass, config, platform):
    """Find the automation platform."""
    if platform is None:
        return None
    platform = prepare_setup_platform(hass, config, DOMAIN, platform)

    if platform is None or not hasattr(platform, method):
        _LOGGER.error("Unknown automation platform specified for %s: %s",
                      method, platform)
        return None

    return platform
Пример #8
0
def _setup_discovery(hass, config):
    """Try to start the discovery of MQTT devices."""
    conf = config.get(DOMAIN, {})

    discovery = prepare_setup_platform(hass, config, DOMAIN, 'discovery')

    if discovery is None:
        _LOGGER.error("Unable to load MQTT discovery")
        return None

    success = discovery.async_start(hass, conf[CONF_DISCOVERY_PREFIX], config)

    return success
Пример #9
0
def setup(hass, config):
    """Setup the notify services."""
    success = False

    descriptions = load_yaml_config_file(
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    for platform, p_config in config_per_platform(config, DOMAIN):
        notify_implementation = bootstrap.prepare_setup_platform(
            hass, config, DOMAIN, platform)

        if notify_implementation is None:
            _LOGGER.error("Unknown notification service specified.")
            continue

        notify_service = notify_implementation.get_service(hass, p_config)

        if notify_service is None:
            _LOGGER.error("Failed to initialize notification service %s",
                          platform)
            continue

        def notify_message(notify_service, call):
            """Handle sending notification message service calls."""
            message = call.data.get(ATTR_MESSAGE)

            if message is None:
                _LOGGER.error('Received call to %s without attribute %s',
                              call.service, ATTR_MESSAGE)
                return

            title = template.render(
                hass, call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT))
            target = call.data.get(ATTR_TARGET)
            message = template.render(hass, message)
            data = call.data.get(ATTR_DATA)

            notify_service.send_message(message,
                                        title=title,
                                        target=target,
                                        data=data)

        service_call_handler = partial(notify_message, notify_service)
        service_notify = p_config.get(CONF_NAME, SERVICE_NOTIFY)
        hass.services.register(DOMAIN, service_notify, service_call_handler,
                               descriptions.get(SERVICE_NOTIFY))
        success = True

    return success
Пример #10
0
def setup(hass, config):
    """Setup the notify services."""
    success = False

    descriptions = load_yaml_config_file(
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    for platform, p_config in config_per_platform(config, DOMAIN):
        notify_implementation = bootstrap.prepare_setup_platform(
            hass, config, DOMAIN, platform)

        if notify_implementation is None:
            _LOGGER.error("Unknown notification service specified.")
            continue

        notify_service = notify_implementation.get_service(hass, p_config)

        if notify_service is None:
            _LOGGER.error("Failed to initialize notification service %s",
                          platform)
            continue

        def notify_message(notify_service, call):
            """Handle sending notification message service calls."""
            message = call.data.get(ATTR_MESSAGE)

            if message is None:
                _LOGGER.error(
                    'Received call to %s without attribute %s',
                    call.service, ATTR_MESSAGE)
                return

            title = template.render(
                hass, call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT))
            target = call.data.get(ATTR_TARGET)
            message = template.render(hass, message)
            data = call.data.get(ATTR_DATA)

            notify_service.send_message(message, title=title, target=target,
                                        data=data)

        service_call_handler = partial(notify_message, notify_service)
        service_notify = p_config.get(CONF_NAME, SERVICE_NOTIFY)
        hass.services.register(DOMAIN, service_notify, service_call_handler,
                               descriptions.get(SERVICE_NOTIFY))
        success = True

    return success
Пример #11
0
def _setup_server(hass, config):
    """Try to start embedded MQTT broker."""
    conf = config.get(DOMAIN, {})

    # Only setup if embedded config passed in or no broker specified
    if CONF_EMBEDDED not in conf and CONF_BROKER in conf:
        return None

    server = prepare_setup_platform(hass, config, DOMAIN, 'server')

    if server is None:
        _LOGGER.error("Unable to load embedded server")
        return None

    success, broker_config = server.start(hass, conf.get(CONF_EMBEDDED))

    return success and broker_config
Пример #12
0
def _setup_server(hass, config):
    """Try to start embedded MQTT broker."""
    conf = config.get(DOMAIN, {})

    # Only setup if embedded config passed in or no broker specified
    if CONF_EMBEDDED not in conf and CONF_BROKER in conf:
        return None

    server = prepare_setup_platform(hass, config, DOMAIN, 'server')

    if server is None:
        _LOGGER.error('Unable to load embedded server.')
        return None

    success, broker_config = server.start(hass, conf.get(CONF_EMBEDDED))

    return success and broker_config
Пример #13
0
def setup(hass, config):
    """ Sets up notify services. """
    success = False

    descriptions = load_yaml_config_file(
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    for platform, p_config in config_per_platform(config, DOMAIN, _LOGGER):
        # get platform
        notify_implementation = bootstrap.prepare_setup_platform(
            hass, config, DOMAIN, platform)

        if notify_implementation is None:
            _LOGGER.error("Unknown notification service specified.")
            continue

        # create platform service
        notify_service = notify_implementation.get_service(hass, p_config)

        if notify_service is None:
            _LOGGER.error("Failed to initialize notification service %s",
                          platform)
            continue

        # create service handler
        def notify_message(notify_service, call):
            """ Handle sending notification message service calls. """
            message = call.data.get(ATTR_MESSAGE)

            if message is None:
                return

            title = call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
            target = call.data.get(ATTR_TARGET)

            notify_service.send_message(message, title=title, target=target)

        # register service
        service_call_handler = partial(notify_message, notify_service)
        service_notify = p_config.get(CONF_NAME, SERVICE_NOTIFY)
        hass.services.register(DOMAIN, service_notify, service_call_handler,
                               descriptions.get(service_notify))
        success = True

    return success
Пример #14
0
def setup(hass, config):
    """ Sets up automation. """

    for p_type, p_config in config_per_platform(config, DOMAIN, _LOGGER):
        platform = prepare_setup_platform(hass, config, DOMAIN, p_type)

        if platform is None:
            _LOGGER.error("Unknown automation platform specified: %s", p_type)
            continue

        if platform.register(hass, p_config, _get_action(hass, p_config)):
            _LOGGER.info(
                "Initialized %s rule %s", p_type, p_config.get(CONF_ALIAS, ""))
        else:
            _LOGGER.error(
                "Error setting up rule %s", p_config.get(CONF_ALIAS, ""))

    return True
Пример #15
0
    def _setup_platform(self, platform_type, platform_config,
                        discovery_info=None):
        """ Tries to setup a platform for this component. """
        platform = prepare_setup_platform(
            self.hass, self.config, self.domain, platform_type)

        if platform is None:
            return

        try:
            platform.setup_platform(
                self.hass, platform_config, self.add_entities, discovery_info)
        except Exception:  # pylint: disable=broad-except
            self.logger.exception(
                'Error while setting up platform %s', platform_type)
            return

        platform_name = '{}.{}'.format(self.domain, platform_type)
        self.hass.config.components.append(platform_name)
Пример #16
0
    def _setup_platform(self,
                        platform_type,
                        platform_config,
                        discovery_info=None):
        """ Tries to setup a platform for this component. """
        platform = prepare_setup_platform(self.hass, self.config, self.domain,
                                          platform_type)

        if platform is None:
            return

        try:
            platform.setup_platform(self.hass, platform_config,
                                    self.add_entities, discovery_info)
        except Exception:  # pylint: disable=broad-except
            self.logger.exception('Error while setting up platform %s',
                                  platform_type)
            return

        platform_name = '{}.{}'.format(self.domain, platform_type)
        self.hass.config.components.append(platform_name)
Пример #17
0
    def setup_platform(p_type, p_config, disc_info=None):
        """Setup a device tracker platform."""
        platform = prepare_setup_platform(hass, config, DOMAIN, p_type)
        if platform is None:
            return

        try:
            if hasattr(platform, 'get_scanner'):
                scanner = platform.get_scanner(hass, {DOMAIN: p_config})

                if scanner is None:
                    _LOGGER.error('Error setting up platform %s', p_type)
                    return

                setup_scanner_platform(hass, p_config, scanner, tracker.see)
                return

            if not platform.setup_scanner(hass, p_config, tracker.see):
                _LOGGER.error('Error setting up platform %s', p_type)
        except Exception:  # pylint: disable=broad-except
            _LOGGER.exception('Error setting up platform %s', p_type)
Пример #18
0
    def setup_platform(p_type, p_config, disc_info=None):
        """Setup a device tracker platform."""
        platform = prepare_setup_platform(hass, config, DOMAIN, p_type)
        if platform is None:
            return

        try:
            if hasattr(platform, 'get_scanner'):
                scanner = platform.get_scanner(hass, {DOMAIN: p_config})

                if scanner is None:
                    _LOGGER.error('Error setting up platform %s', p_type)
                    return

                setup_scanner_platform(hass, p_config, scanner, tracker.see)
                return

            if not platform.setup_scanner(hass, p_config, tracker.see):
                _LOGGER.error('Error setting up platform %s', p_type)
        except Exception:  # pylint: disable=broad-except
            _LOGGER.exception('Error setting up platform %s', p_type)
Пример #19
0
    def _setup_platform(self, platform_type, platform_config, discovery_info=None):
        """Setup a platform for this component."""
        platform = prepare_setup_platform(self.hass, self.config, self.domain, platform_type)

        if platform is None:
            return

        # Config > Platform > Component
        scan_interval = platform_config.get(CONF_SCAN_INTERVAL, getattr(platform, "SCAN_INTERVAL", self.scan_interval))
        entity_namespace = platform_config.get(CONF_ENTITY_NAMESPACE)

        try:
            platform.setup_platform(
                self.hass,
                platform_config,
                EntityPlatform(self, scan_interval, entity_namespace).add_entities,
                discovery_info,
            )

            self.hass.config.components.append("{}.{}".format(self.domain, platform_type))
        except Exception:  # pylint: disable=broad-except
            self.logger.exception("Error while setting up platform %s", platform_type)
Пример #20
0
def setup(hass, config):
    """Setup the notify services."""
    success = False

    descriptions = load_yaml_config_file(
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    targets = {}

    for platform, p_config in config_per_platform(config, DOMAIN):
        notify_implementation = bootstrap.prepare_setup_platform(
            hass, config, DOMAIN, platform)

        if notify_implementation is None:
            _LOGGER.error("Unknown notification service specified")
            continue

        notify_service = notify_implementation.get_service(hass, p_config)

        if notify_service is None:
            _LOGGER.error("Failed to initialize notification service %s",
                          platform)
            continue

        def notify_message(notify_service, call):
            """Handle sending notification message service calls."""
            kwargs = {}
            message = call.data[ATTR_MESSAGE]
            title = call.data.get(ATTR_TITLE)

            if title:
                title.hass = hass
                kwargs[ATTR_TITLE] = title.render()

            if targets.get(call.service) is not None:
                kwargs[ATTR_TARGET] = [targets[call.service]]
            elif call.data.get(ATTR_TARGET) is not None:
                kwargs[ATTR_TARGET] = call.data.get(ATTR_TARGET)

            message.hass = hass
            kwargs[ATTR_MESSAGE] = message.render()
            kwargs[ATTR_DATA] = call.data.get(ATTR_DATA)

            notify_service.send_message(**kwargs)

        service_call_handler = partial(notify_message, notify_service)

        if hasattr(notify_service, 'targets'):
            platform_name = (p_config.get(CONF_NAME) or platform)
            for name, target in notify_service.targets.items():
                target_name = slugify('{}_{}'.format(platform_name, name))
                targets[target_name] = target
                hass.services.register(DOMAIN, target_name,
                                       service_call_handler,
                                       descriptions.get(SERVICE_NOTIFY),
                                       schema=NOTIFY_SERVICE_SCHEMA)

        platform_name = (p_config.get(CONF_NAME) or SERVICE_NOTIFY)
        platform_name_slug = slugify(platform_name)

        hass.services.register(
            DOMAIN, platform_name_slug, service_call_handler,
            descriptions.get(SERVICE_NOTIFY), schema=NOTIFY_SERVICE_SCHEMA)
        success = True

    return success
Пример #21
0
    def setup_notify_platform(platform, p_config=None, discovery_info=None):
        """Set up a notify platform."""
        if p_config is None:
            p_config = {}
        if discovery_info is None:
            discovery_info = {}

        notify_implementation = bootstrap.prepare_setup_platform(
            hass, config, DOMAIN, platform)

        if notify_implementation is None:
            _LOGGER.error("Unknown notification service specified")
            return False

        notify_service = notify_implementation.get_service(
            hass, p_config, discovery_info)

        if notify_service is None:
            _LOGGER.error("Failed to initialize notification service %s",
                          platform)
            return False

        def notify_message(notify_service, call):
            """Handle sending notification message service calls."""
            kwargs = {}
            message = call.data[ATTR_MESSAGE]
            title = call.data.get(ATTR_TITLE)

            if title:
                title.hass = hass
                kwargs[ATTR_TITLE] = title.render()

            if targets.get(call.service) is not None:
                kwargs[ATTR_TARGET] = [targets[call.service]]
            elif call.data.get(ATTR_TARGET) is not None:
                kwargs[ATTR_TARGET] = call.data.get(ATTR_TARGET)

            message.hass = hass
            kwargs[ATTR_MESSAGE] = message.render()
            kwargs[ATTR_DATA] = call.data.get(ATTR_DATA)

            notify_service.send_message(**kwargs)

        service_call_handler = partial(notify_message, notify_service)

        if hasattr(notify_service, 'targets'):
            platform_name = (
                p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or
                platform)
            for name, target in notify_service.targets.items():
                target_name = slugify('{}_{}'.format(platform_name, name))
                targets[target_name] = target
                hass.services.register(DOMAIN, target_name,
                                       service_call_handler,
                                       descriptions.get(SERVICE_NOTIFY),
                                       schema=NOTIFY_SERVICE_SCHEMA)

        platform_name = (
            p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or
            SERVICE_NOTIFY)
        platform_name_slug = slugify(platform_name)

        hass.services.register(
            DOMAIN, platform_name_slug, service_call_handler,
            descriptions.get(SERVICE_NOTIFY), schema=NOTIFY_SERVICE_SCHEMA)

        return True
Пример #22
0
def setup(hass, config):
    """Setup the notify services."""
    success = False

    descriptions = load_yaml_config_file(
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    targets = {}

    for platform, p_config in config_per_platform(config, DOMAIN):
        notify_implementation = bootstrap.prepare_setup_platform(
            hass, config, DOMAIN, platform)

        if notify_implementation is None:
            _LOGGER.error("Unknown notification service specified.")
            continue

        notify_service = notify_implementation.get_service(hass, p_config)

        if notify_service is None:
            _LOGGER.error("Failed to initialize notification service %s",
                          platform)
            continue

        def notify_message(notify_service, call):
            """Handle sending notification message service calls."""
            kwargs = {}
            message = call.data[ATTR_MESSAGE]
            title = call.data.get(ATTR_TITLE)

            if title:
                title.hass = hass
                kwargs[ATTR_TITLE] = title.render()

            if targets.get(call.service) is not None:
                kwargs[ATTR_TARGET] = [targets[call.service]]
            else:
                kwargs[ATTR_TARGET] = call.data.get(ATTR_TARGET)

            message.hass = hass
            kwargs[ATTR_MESSAGE] = message.render()
            kwargs[ATTR_DATA] = call.data.get(ATTR_DATA)

            notify_service.send_message(**kwargs)

        service_call_handler = partial(notify_message, notify_service)

        if hasattr(notify_service, 'targets'):
            platform_name = (p_config.get(CONF_NAME) or platform)
            for name, target in notify_service.targets.items():
                target_name = slugify("{}_{}".format(platform_name, name))
                targets[target_name] = target
                hass.services.register(DOMAIN,
                                       target_name,
                                       service_call_handler,
                                       descriptions.get(SERVICE_NOTIFY),
                                       schema=NOTIFY_SERVICE_SCHEMA)

        platform_name = (p_config.get(CONF_NAME) or SERVICE_NOTIFY)
        platform_name_slug = slugify(platform_name)

        hass.services.register(DOMAIN,
                               platform_name_slug,
                               service_call_handler,
                               descriptions.get(SERVICE_NOTIFY),
                               schema=NOTIFY_SERVICE_SCHEMA)
        success = True

    return success