Ejemplo n.º 1
0
async def async_setup(hass, config):
    """Set up this component using YAML."""
    if config.get(DOMAIN) is None:
        # We get her if the integration is set up using config flow
        return True

    # Print startup message
    Logger("custom_components.readme").info(
        CC_STARTUP_VERSION.format(name=DOMAIN.capitalize(),
                                  version=VERSION,
                                  issue_link=ISSUE_URL))

    # Check that all required files are present
    file_check = await check_files(hass)
    if not file_check:
        return False

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.
    ReadmeConfiguration.convert_lovelace = config[DOMAIN].get(
        "convert_lovelace")
    await add_services(hass)
    create_initial_files(hass)

    hass.async_create_task(
        hass.config_entries.flow.async_init(
            DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}))
    return True
Ejemplo n.º 2
0
async def async_setup(hass, config):
    """Provide Setup of platform."""
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))
    return True
Ejemplo n.º 3
0
async def async_setup(hass, config):
    """Set up this component using YAML."""
    if config.get(DOMAIN) is None:
        # We get here if the integration is set up using config flow
        return True
    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))
    platform_config = config[DOMAIN].get(CONF_SENSORS, {})

    # If platform is not enabled, skip.
    if not platform_config:
        return False

    for entry in platform_config:
        _LOGGER.info(
            f"Setting {entry[CONF_NAME]}({entry[CONF_FREQUENCY]}) from YAML configuration"
        )
        # If entry is not enabled, skip.
        # if not entry[CONF_ENABLED]:
        #     continue
        hass.async_create_task(
            discovery.async_load_platform(hass, SENSOR_PLATFORM, DOMAIN, entry,
                                          config))
    hass.async_create_task(
        hass.config_entries.flow.async_init(
            DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}))
    return True
Ejemplo n.º 4
0
async def async_setup(hass, config):
    """Set up this component using YAML."""
    if config.get(DOMAIN) is None:
        # config flow setup
        return True

    # log startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))
    platform_config = config[DOMAIN].get(CONF_SENSORS, {})

    # If platform is not enabled, skip.
    if not platform_config:
        return False

    for entry in platform_config:
        hass.async_create_task(
            discovery.async_load_platform(hass, PLATFORM, DOMAIN, entry,
                                          config))
    hass.async_create_task(
        hass.config_entries.flow.async_init(
            DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}))
    return True
Ejemplo n.º 5
0
async def async_setup(hass, config):
    """Set up this component using YAML."""
    if config.get(DOMAIN) is None:
        # We get here if the integration is set up using config flow
        return True

    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))

    hass.data[DOMAIN] = TTlock(hass, config)

    # Check the token validated
    try:
        hass.data[DOMAIN].check_token_file()
    except Exception:
        return False

    # Load platforms
    for platform in PLATFORMS:
        discovery.load_platform(hass, platform, DOMAIN, {}, config)

    def update_devices(event_time):
        asyncio.run_coroutine_threadsafe(hass.data[DOMAIN].async_update(),
                                         hass.loop)

    async_track_time_interval(hass, update_devices,
                              hass.data[DOMAIN].get_scan_interval())

    return True
Ejemplo n.º 6
0
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""
    # Print startup message
    Logger("custom_components.healthchecksio").info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))

    # Check that all required files are present
    file_check = await check_files(hass)
    if not file_check:
        return False

    # Create DATA dict
    if DOMAIN_DATA not in hass.data:
        hass.data[DOMAIN_DATA] = {}
        if "data" not in hass.data[DOMAIN_DATA]:
            hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.
    api_key = config_entry.data.get("api_key")
    check = config_entry.data.get("check")

    # Configure the client.
    hass.data[DOMAIN_DATA]["client"] = HealthchecksioData(hass, api_key, check)

    # Add binary_sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry,
                                                      "binary_sensor"))

    return True
Ejemplo n.º 7
0
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""
    conf = hass.data.get(DOMAIN_DATA)
    if config_entry.source == config_entries.SOURCE_IMPORT:
        if conf is None:
            hass.async_create_task(
                hass.config_entries.async_remove(config_entry.entry_id))
        return False

    # Print startup message
    Logger("custom_components.readme").info(
        CC_STARTUP_VERSION.format(name=DOMAIN.capitalize(),
                                  version=VERSION,
                                  issue_link=ISSUE_URL))

    # Check that all required files are present
    file_check = await check_files(hass)
    if not file_check:
        return False

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.
    ReadmeConfiguration.convert_lovelace = config_entry.data.get(
        "convert", False)
    await add_services(hass)
    create_initial_files(hass)

    return True
Ejemplo n.º 8
0
async def async_setup(hass: HomeAssistant, config):
    hass.data[DOMAIN] = {}

    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=PROJECT,
                                  version=VERSION,
                                  issue_link=ISSUE_LINK))

    return True
async def async_setup(hass, config):
    """Set up this component using YAML."""
    if config.get(DOMAIN) is None:
        # We get here if the integration is set up using config flow
        return True

    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN, version=VERSION, issue_link=ISSUE_URL)
    )

    # Check that all required files are present
    file_check = await check_files(hass)
    if not file_check:
        return False

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.
    hostname = config[DOMAIN].get(CONF_HOSTNAME)
    port = config[DOMAIN].get(CONF_PORT)

    # Configure the client.
    client = Client(hostname, port)
    hass.data[DOMAIN_DATA]["client"] = HDMIMatrixData(hass, client)

    # Load platforms
    for platform in PLATFORMS:
        # Get platform specific configuration
        platform_config = config[DOMAIN].get(platform, {})

        # If platform is not enabled, skip.
        if not platform_config:
            continue

        for entry in platform_config:
            entry_config = entry

            # If entry is not enabled, skip.
            if not entry_config[CONF_ENABLED]:
                continue

            hass.async_create_task(
                discovery.async_load_platform(
                    hass, platform, DOMAIN, entry_config, config
                )
            )
    hass.async_create_task(
        hass.config_entries.flow.async_init(
            DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}
        )
    )
    return True
Ejemplo n.º 10
0
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""
    _LOGGER.info("TEST: async_setup_entry\n%s", config_entry.data)
    conf = hass.data.get(DOMAIN_DATA)
    if config_entry.source == config_entries.SOURCE_IMPORT:
        _LOGGER.info("TEST: Import")
        if conf is None:
            hass.async_create_task(
                hass.config_entries.async_remove(config_entry.entry_id))
        return False

    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))

    # Check that all required files are present
    file_check = await check_files(hass)
    if not file_check:
        return False

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.
    # username = config_entry.data.get(CONF_USERNAME)
    # password = config_entry.data.get(CONF_PASSWORD)
    host = '127.0.0.1'
    port = 5000

    # Configure the client.
    # client = Client(username, password)
    client = OctoPrint(host, port)
    hass.data[DOMAIN_DATA]["client"] = OctoprintData(hass, client)

    # Add binary_sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry,
                                                      "binary_sensor"))

    # Add sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, "sensor"))

    # Add switch
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, "switch"))

    return True
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""
    conf = hass.data.get(DOMAIN_DATA)
    if config_entry.source == config_entries.SOURCE_IMPORT:
        if conf is None:
            hass.async_create_task(
                hass.config_entries.async_remove(config_entry.entry_id)
            )
        return False

    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN, version=VERSION, issue_link=ISSUE_URL)
    )

    # Check that all required files are present
    file_check = await check_files(hass)
    if not file_check:
        return False

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.
    hostname = config_entry.data.get(CONF_HOSTNAME)
    port = config_entry.data.get(CONF_PORT)

    # Configure the client.
    client = Client(hostname, port)
    hass.data[DOMAIN_DATA]["client"] = HDMIMatrixData(hass, client)

    # Add binary_sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, "binary_sensor")
    )

    # Add sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, "sensor")
    )

    # Add switch
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, "switch")
    )

    return True
Ejemplo n.º 12
0
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""
    if config_entry.source == config_entries.SOURCE_IMPORT:
        # We get here if the integration is set up using YAML
        hass.async_create_task(hass.config_entries.async_remove(config_entry.entry_id))
        return False
    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN, version=VERSION, issue_link=ISSUE_URL)
    )
    config_entry.options = config_entry.data
    config_entry.add_update_listener(update_listener)
    # Add sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, PLATFORM)
    )
    return True
Ejemplo n.º 13
0
async def async_setup(hass, config):
    """Set up this component."""
    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))

    # Check that all required files are present
    file_check = await check_files(hass)
    if not file_check:
        return False

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.
    username = config[DOMAIN].get(CONF_USERNAME)
    password = config[DOMAIN].get(CONF_PASSWORD)

    # Configure the client.
    client = Client(username, password)
    hass.data[DOMAIN_DATA]["client"] = BlueprintData(hass, client)

    # Load platforms
    for platform in PLATFORMS:
        # Get platform specific configuration
        platform_config = config[DOMAIN].get(platform, {})

        # If platform is not enabled, skip.
        if not platform_config:
            continue

        for entry in platform_config:
            entry_config = entry

            # If entry is not enabled, skip.
            if not entry_config[CONF_ENABLED]:
                continue

            hass.async_create_task(
                discovery.async_load_platform(hass, platform, DOMAIN,
                                              entry_config, config))
    return True
Ejemplo n.º 14
0
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""

    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN, version=VERSION, issue_link=ISSUE_URL)
    )

    # Check that all required files are present
    file_check = await check_files(hass)
    if not file_check:
        return False

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}

    register_websocket_handlers(hass)
    await start_discovery(hass, hass.data[DOMAIN_DATA], config_entry)

    return True
Ejemplo n.º 15
0
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""
    if config_entry.source == config_entries.SOURCE_IMPORT:
        # We get here if the integration is set up using YAML
        hass.async_create_task(
            hass.config_entries.async_remove(config_entry.entry_id))
        return False
    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))
    _LOGGER.info(
        f"Setting {config_entry.title}({config_entry.data[CONF_FREQUENCY]}) from ConfigFlow"
    )
    # Backward compatibility - clean-up (can be removed later?)
    config_entry.options = {}
    config_entry.add_update_listener(update_listener)
    # Add sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, PLATFORM))
    return True
Ejemplo n.º 16
0
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""
    _LOGGER.info("init init")

    conf = hass.data.get(DOMAIN_DATA)
    if config_entry.source == config_entries.SOURCE_IMPORT:
        if conf is None:
            hass.async_create_task(
                hass.config_entries.async_remove(config_entry.entry_id))
        return False

    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))

    # Create DATA dict
    #hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.

    # Configure the client.
    g = hass.data[DOMAIN_DATA]["gtasks_obj"]
    default_list = hass.data[DOMAIN_DATA]["default_list"]
    hass.data[DOMAIN_DATA]["client"] = GtasksData(hass, g, default_list)

    # Add binary_sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry,
                                                      "binary_sensor"))

    # Add sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, "sensor"))

    @callback
    def new_task(call):
        title = call.data.get(ATTR_TASK_TITLE)
        due_date = call.data.get(ATTR_DUE_DATE, None)
        task_list = call.data.get(ATTR_LIST_TITLE, default_list)
        try:
            g.new_task(title=title, due_date=due_date, task_list=task_list)
        except Exception as e:
            _LOGGER.exception(e)

    @callback
    def new_list(call):
        new_task_list = call.data.get(ATTR_LIST_TITLE)
        try:
            g.new_list(title=new_task_list)
        except Exception as e:
            _LOGGER.exception(e)

    @callback
    def complete_task(call):
        task_to_complete = call.data.get(ATTR_TASK_TITLE)
        task_list = call.data.get(ATTR_LIST_TITLE, default_list)
        try:
            list = g.get_list(task_list)
            for t in list:
                if t.title == task_to_complete:
                    t.complete = True
                    break
        except Exception as e:
            _LOGGER.exception(e)

    #Register "new_task" service
    hass.services.async_register(DOMAIN,
                                 SERVICE_NEW_TASK,
                                 new_task,
                                 schema=NEW_TASK_SCHEMA)

    #Register "new_list" service
    hass.services.async_register(DOMAIN,
                                 SERVICE_NEW_LIST,
                                 new_list,
                                 schema=NEW_LIST_SCHEMA)

    #Register "comple_task" service
    hass.services.async_register(DOMAIN,
                                 SERVICE_COMPLETE_TASK,
                                 complete_task,
                                 schema=COMPLETE_TASK_SCHEMA)

    return True
Ejemplo n.º 17
0
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""
    from pygrocy import Grocy, TransactionType
    from datetime import datetime
    import iso8601

    conf = hass.data.get(DOMAIN_DATA)
    if config_entry.source == config_entries.SOURCE_IMPORT:
        if conf is None:
            hass.async_create_task(
                hass.config_entries.async_remove(config_entry.entry_id))
        return False

    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))

    # Check that all required files are present
    if not await hass.async_add_executor_job(check_files, hass):
        return False

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.
    url = config_entry.data.get(CONF_URL)
    api_key = config_entry.data.get(CONF_API_KEY)
    verify_ssl = config_entry.data.get(CONF_VERIFY_SSL)
    port_number = config_entry.data.get(CONF_PORT)
    hash_key = hashlib.md5(api_key.encode("utf-8") +
                           url.encode("utf-8")).hexdigest()

    # Configure the client.
    grocy = Grocy(url, api_key, port_number, verify_ssl)
    hass.data[DOMAIN_DATA]["client"] = GrocyData(hass, grocy)
    hass.data[DOMAIN_DATA]["hash_key"] = hash_key

    # Add sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, "sensor"))
    # Add sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry,
                                                      "binary_sensor"))

    @callback
    def handle_add_product(call):
        product_id = call.data["product_id"]
        amount = call.data.get("amount", 0)
        price = call.data.get("price", None)
        grocy.add_product(product_id, amount, price)

    hass.services.async_register(DOMAIN, "add_product", handle_add_product)

    @callback
    def handle_consume_product(call):
        product_id = call.data["product_id"]
        amount = call.data.get("amount", 0)
        spoiled = call.data.get("spoiled", False)

        transaction_type_raw = call.data.get("transaction_type", None)
        transaction_type = TransactionType.CONSUME

        if transaction_type_raw is not None:
            transaction_type = TransactionType[transaction_type_raw]
        grocy.consume_product(product_id,
                              amount,
                              spoiled=spoiled,
                              transaction_type=transaction_type)

    hass.services.async_register(DOMAIN, "consume_product",
                                 handle_consume_product)

    @callback
    def handle_execute_chore(call):
        chore_id = call.data["chore_id"]
        done_by = call.data.get("done_by", None)
        tracked_time_str = call.data.get("tracked_time", None)

        tracked_time = datetime.now()
        if tracked_time_str is not None:
            tracked_time = iso8601.parse_date(tracked_time_str)
        grocy.execute_chore(chore_id, done_by, tracked_time)
        asyncio.run_coroutine_threadsafe(
            entity_component.async_update_entity(hass, "sensor.grocy_chores"),
            hass.loop)

    hass.services.async_register(DOMAIN, "execute_chore", handle_execute_chore)

    return True
Ejemplo n.º 18
0
async def async_setup_entry(hass, config_entry):
    """Set up this integration using UI."""
    conf = hass.data.get(DOMAIN_DATA)
    if config_entry.source == config_entries.SOURCE_IMPORT:
        if conf is None:
            hass.async_create_task(
                hass.config_entries.async_remove(config_entry.entry_id))
        return False

    # Print startup message
    _LOGGER.info(
        CC_STARTUP_VERSION.format(name=DOMAIN,
                                  version=VERSION,
                                  issue_link=ISSUE_URL))

    # Create DATA dict
    hass.data[DOMAIN_DATA] = {}

    # Get "global" configuration.
    username = config_entry.data.get(CONF_USERNAME)
    password = config_entry.data.get(CONF_PASSWORD)
    default_list = config_entry.data.get(CONF_DEFAULT_LIST)

    # Configure the client.
    keep = gkeepapi.Keep()
    try:
        keep.login(username, password)
    except Exception as e:
        _LOGGER.exception(e)
        return False
    await hass.async_add_executor_job(keep.sync)
    all_list = await hass.async_add_executor_job(keep.all)
    for list in all_list:
        if list.title == default_list:
            hass.data[DOMAIN_DATA]["gkeep"] = GkeepData(hass, keep, list)
            break
    else:
        return False

    # Add sensor
    hass.async_add_job(
        hass.config_entries.async_forward_entry_setup(config_entry, "sensor"))

    @callback
    def item_checked(call):
        item_title = call.data.get(ATTR_ITEM_TITLE)
        item_checked = call.data.get(ATTR_ITEM_CHECKED)
        gkeep = hass.data[DOMAIN_DATA]["gkeep"]
        try:
            for item in gkeep.list.items:
                if item.text == item_title:
                    item.checked = item_checked
                    break
            gkeep.gkeep.sync()
        except Exception as e:
            _LOGGER.exception(e)

    #Register "item_checked" service
    hass.services.async_register(DOMAIN,
                                 SERVICE_ITEM_CHECKED,
                                 item_checked,
                                 schema=CHECKED_ITEM_SCHEMA)

    @callback
    def new_item(call):
        item_title = call.data.get(ATTR_ITEM_TITLE)
        gkeep = hass.data[DOMAIN_DATA]["gkeep"]
        try:
            for item in gkeep.list.items:
                if item.text == item_title:
                    item.checked = False
                    break
            else:
                gkeep.list.add(item_title, False)
            gkeep.gkeep.sync()
        except Exception as e:
            _LOGGER.exception(e)

    #Register "new_item" service
    hass.services.async_register(DOMAIN,
                                 SERVICE_NEW_ITEM,
                                 new_item,
                                 schema=NEW_ITEM_SCHEMA)

    def handle_sensor_change(entity_id, old_state, new_state):
        if old_state is None:
            return
        old_list = old_state.attributes['items']
        new_list = new_state.attributes['items']
        gkeep = hass.data[DOMAIN_DATA]["gkeep"]
        return asyncio.run_coroutine_threadsafe(
            gkeep.update_from_sensor(hass, old_list, new_list),
            hass.loop).result()

    event.async_track_state_change(
        hass,
        '{}.{}_{}'.format(SENSOR_NAME, DOMAIN,
                          hass.data[DOMAIN_DATA]["gkeep"].list.title.lower()),
        handle_sensor_change)

    return True