Exemplo n.º 1
0
async def async_setup(hass: HomeAssistant, config: Config):
    """Read configuration from yaml."""

    ocpp_config = config.get(DOMAIN, {})
    if DOMAIN not in hass.data:
        hass.data[DOMAIN] = {}
    hass.data[DOMAIN][CONFIG] = ocpp_config
    _LOGGER.info(f"config = {ocpp_config}")
    return True
Exemplo n.º 2
0
async def async_setup(hass: HomeAssistant, config: Config):
    """AMS hub YAML setup."""
    if config.get(DOMAIN) is None:
        _LOGGER.info("No YAML config available, using config_entries")
        return True
    _setup(hass, config[DOMAIN])
    if not hass.config_entries.async_entries(DOMAIN):
        hass.async_create_task(
            hass.config_entries.flow.async_init(
                DOMAIN, context={"source":
                                 SOURCE_IMPORT}, data=config[DOMAIN]))
    return True
Exemplo n.º 3
0
async def async_setup(hass: HomeAssistant, config: Config):
    _LOGGER.debug("Async setup meteo swiss")

    conf = config.get(DOMAIN)
    if conf is None:
        return True

    hass.async_create_task(
        hass.config_entries.flow.async_init(DOMAIN,
                                            context={"source": SOURCE_IMPORT},
                                            data=conf))
    _LOGGER.debug("END Async setup meteo swiss")
    return True
Exemplo n.º 4
0
async def async_setup(hass: HomeAssistant, config: Config):
    """Set up from config."""
    hass.data.setdefault(DOMAIN, {})

    await add_services(hass)

    # if there are no configuration.yaml settings then terminate
    if config.get(DOMAIN) is None:
        # We get her if the integration is set up using config flow
        return True

    conf = config.get(DOMAIN, {})
    hass.data.setdefault(DOMAIN_CONFIG_YAML, conf)

    if CONF_USERNAME in conf:
        # https://www.programcreek.com/python/?code=davesmeghead%2Fvisonic%2Fvisonic-master%2Fcustom_components%2Fvisonic%2F__init__.py
        # has there been a flow configured panel connection before
        configured = set(
            entry for entry in hass.config_entries.async_entries(DOMAIN))

        # if there is not a flow configured connection previously
        #   then create a flow connection from the configuration.yaml data
        if len(configured) == 0:
            # get the configuration.yaml settings and make a 'flow' task :)
            #   this will run 'async_step_import' in config_flow.py
            log.info(
                "Importing configuration from yaml...after you can remove from yaml"
            )
            hass.async_create_task(
                hass.config_entries.flow.async_init(
                    DOMAIN,
                    context={"source": config_entries.SOURCE_IMPORT},
                    data=conf.copy()))
        else:
            log.debug(
                "Configuration from yaml already imported: you can remove from yaml"
            )

    return True
Exemplo n.º 5
0
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
    """Set up this component using YAML."""
    _LOGGER.info(STARTUP)
    if config.get(DOMAIN) is None:
        # We get her if the integration is set up using config flow
        return True

    try:
        await hass.config_entries.async_forward_entry(config, "sensor")
    except ValueError:
        pass

    hass.async_create_task(
        hass.config_entries.flow.async_init(
            DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}
        )
    )
    return True
Exemplo n.º 6
0
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
    """Set up this component using YAML."""
    async def handle_add_date(call: ServiceCall) -> None:
        """Handle the add_date service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            collection_date = call.data.get(const.CONF_DATE)
            _LOGGER.debug("called add_date %s from %s", collection_date,
                          entity_id)
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                await entity.add_date(collection_date)
            except KeyError as err:
                _LOGGER.error("Failed adding date %s to %s (%s)",
                              collection_date, entity_id, err)

    async def handle_remove_date(call: ServiceCall) -> None:
        """Handle the remove_date service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            collection_date = call.data.get(const.CONF_DATE)
            _LOGGER.debug("called remove_date %s from %s", collection_date,
                          entity_id)
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                await entity.remove_date(collection_date)
            except KeyError as err:
                _LOGGER.error(
                    "Failed removing date %s from %s. Most likely, "
                    "it was removed by competing automation runing in parallel. "
                    "(%s)",
                    collection_date,
                    entity_id,
                    err,
                )

    async def handle_offset_date(call: ServiceCall) -> None:
        """Handle the offset_date service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            offset = call.data.get(const.CONF_OFFSET)
            collection_date = call.data.get(const.CONF_DATE)
            _LOGGER.debug(
                "called offset_date %s by %d days for %s",
                collection_date,
                offset,
                entity_id,
            )
            try:
                new_date = collection_date + relativedelta(days=offset)
            except TypeError as err:
                _LOGGER.error("Failed to offset the date - %s", err)
                break
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                await entity.remove_date(collection_date)
                await entity.add_date(new_date)
            except KeyError as err:
                _LOGGER.error("Failed ofsetting date for %s - %s", entity_id,
                              err)

    async def handle_update_state(call: ServiceCall) -> None:
        """Handle the update_state service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            _LOGGER.debug("called update_state for %s", entity_id)
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                await entity.async_update_state()
            except KeyError as err:
                _LOGGER.error("Failed updating state for %s - %s", entity_id,
                              err)

    async def handle_collect_garbage(call: ServiceCall) -> None:
        """Handle the collect_garbage service call."""
        for entity_id in call.data.get(CONF_ENTITY_ID):
            last_collection = call.data.get(const.ATTR_LAST_COLLECTION)
            _LOGGER.debug("called collect_garbage for %s", entity_id)
            try:
                entity = hass.data[const.DOMAIN][
                    const.SENSOR_PLATFORM][entity_id]
                if last_collection is None:
                    entity.last_collection = dt_util.now()
                else:
                    entity.last_collection = dt_util.as_local(last_collection)
                await entity.async_update_state()
            except KeyError as err:
                _LOGGER.error("Failed setting last collection for %s - %s",
                              entity_id, err)

    if const.DOMAIN not in hass.services.async_services():
        hass.services.async_register(
            const.DOMAIN,
            "collect_garbage",
            handle_collect_garbage,
            schema=COLLECT_NOW_SCHEMA,
        )
        hass.services.async_register(
            const.DOMAIN,
            "update_state",
            handle_update_state,
            schema=UPDATE_STATE_SCHEMA,
        )
        hass.services.async_register(const.DOMAIN,
                                     "add_date",
                                     handle_add_date,
                                     schema=ADD_REMOVE_DATE_SCHEMA)
        hass.services.async_register(
            const.DOMAIN,
            "remove_date",
            handle_remove_date,
            schema=ADD_REMOVE_DATE_SCHEMA,
        )
        hass.services.async_register(const.DOMAIN,
                                     "offset_date",
                                     handle_offset_date,
                                     schema=OFFSET_DATE_SCHEMA)
    else:
        _LOGGER.debug("Services already registered")

    if config.get(const.DOMAIN) is None:
        # We get here if the integration is set up using config flow
        return True

    platform_config = config[const.DOMAIN].get(const.CONF_SENSORS, {})
    # If platform is not enabled, skip.
    if not platform_config:
        return False

    for entry in hass.config_entries.async_entries(const.DOMAIN):
        if entry.source == SOURCE_IMPORT:
            _LOGGER.error("garbage_collection already imported. "
                          "Remove it from configuration.yaml now!")
            return True
    for entry in platform_config:
        _LOGGER.debug(
            "Importing %s(%s) from YAML configuration",
            entry[CONF_NAME],
            entry[const.CONF_FREQUENCY],
        )
        # Import YAML to ConfigFlow
        hass.async_create_task(
            hass.config_entries.flow.async_init(
                const.DOMAIN,
                context={"source": SOURCE_IMPORT},
                data=entry,
            ))
    return True