Example #1
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    yaml_config = hass.data[DOMAIN][YAML_CONFIG] or {}

    _async_import_options_from_data_if_missing(hass, entry)
    entry.async_on_unload(entry.add_update_listener(_async_update_listener))

    entity_filter = yaml_config.get(const.CONF_FILTER, {})
    if is_config_filter_empty(
            entity_filter) and const.CONF_FILTER in entry.options:
        entity_filter = entry.options[const.CONF_FILTER]

    config = Config(hass=hass,
                    entry=entry,
                    should_expose=FILTER_SCHEMA(entity_filter),
                    entity_config=yaml_config.get(const.CONF_ENTITY_CONFIG,
                                                  {}))
    await config.async_init()
    hass.data[DOMAIN][CONFIG] = config

    if config.is_cloud_connection:
        cloud_manager = CloudManager(hass, config,
                                     async_get_clientsession(hass))
        hass.data[DOMAIN][CLOUD_MANAGER] = cloud_manager

        hass.loop.create_task(cloud_manager.connect())
        entry.async_on_unload(
            hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP,
                                       cloud_manager.disconnect))

    await async_start_notifier(hass)

    return True
Example #2
0
async def async_setup(hass: HomeAssistant, yaml_config: ConfigType) -> bool:
    """Activate Azure EH component from yaml.

    Adds an empty filter to hass data.
    Tries to get a filter from yaml, if present set to hass data.
    If config is empty after getting the filter, return, otherwise emit
    deprecated warning and pass the rest to the config flow.
    """
    hass.data.setdefault(DOMAIN, {DATA_FILTER: FILTER_SCHEMA({})})
    if DOMAIN not in yaml_config:
        return True
    hass.data[DOMAIN][DATA_FILTER] = yaml_config[DOMAIN].pop(CONF_FILTER)

    if not yaml_config[DOMAIN]:
        return True
    _LOGGER.warning(
        "Loading Azure Event Hub completely via yaml config is deprecated; Only the \
        Filter can be set in yaml, the rest is done through a config flow and has \
        been imported, all other keys but filter can be deleted from configuration.yaml"
    )
    hass.async_create_task(
        hass.config_entries.flow.async_init(DOMAIN,
                                            context={"source": SOURCE_IMPORT},
                                            data=yaml_config[DOMAIN]))
    return True
Example #3
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up HomeKit from a config entry."""
    _async_import_options_from_data_if_missing(hass, entry)

    conf = entry.data
    options = entry.options

    name = conf[CONF_NAME]
    port = conf[CONF_PORT]
    _LOGGER.debug("Begin setup HomeKit for %s", name)

    # ip_address and advertise_ip are yaml only
    ip_address = conf.get(CONF_IP_ADDRESS)
    advertise_ip = conf.get(CONF_ADVERTISE_IP)
    # exclude_accessory_mode is only used for config flow
    # to indicate that the config entry was setup after
    # we started creating config entries for entities that
    # to run in accessory mode and that we should never include
    # these entities on the bridge. For backwards compatibility
    # with users who have not migrated yet we do not do exclude
    # these entities by default as we cannot migrate automatically
    # since it requires a re-pairing.
    exclude_accessory_mode = conf.get(CONF_EXCLUDE_ACCESSORY_MODE,
                                      DEFAULT_EXCLUDE_ACCESSORY_MODE)
    homekit_mode = options.get(CONF_HOMEKIT_MODE, DEFAULT_HOMEKIT_MODE)
    entity_config = options.get(CONF_ENTITY_CONFIG, {}).copy()
    auto_start = options.get(CONF_AUTO_START, DEFAULT_AUTO_START)
    entity_filter = FILTER_SCHEMA(options.get(CONF_FILTER, {}))

    homekit = HomeKit(
        hass,
        name,
        port,
        ip_address,
        entity_filter,
        exclude_accessory_mode,
        entity_config,
        homekit_mode,
        advertise_ip,
        entry.entry_id,
        entry.title,
    )

    hass.data[DOMAIN][entry.entry_id] = {
        HOMEKIT: homekit,
        UNDO_UPDATE_LISTENER:
        entry.add_update_listener(_async_update_listener),
    }

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, homekit.async_stop)

    if hass.state == CoreState.running:
        await homekit.async_start()
    elif auto_start:
        hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED,
                                   homekit.async_start)

    return True
Example #4
0
def test_filter_schema():
    """Test filter schema."""
    conf = {
        'include_domains': ['light'],
        'include_entities': ['switch.kitchen'],
        'exclude_domains': ['cover'],
        'exclude_entities': ['light.kitchen']
    }
    filt = FILTER_SCHEMA(conf)
    assert filt.config == conf
def test_filter_schema():
    """Test filter schema."""
    conf = {
        "include_domains": ["light"],
        "include_entities": ["switch.kitchen"],
        "exclude_domains": ["cover"],
        "exclude_entities": ["light.kitchen"],
    }
    filt = FILTER_SCHEMA(conf)
    assert filt.config == conf
Example #6
0
def test_filter_schema():
    """Test filter schema."""
    conf = {
        "include_domains": ["light"],
        "include_entities": ["switch.kitchen"],
        "exclude_domains": ["cover"],
        "exclude_entities": ["light.kitchen"],
    }
    filt = FILTER_SCHEMA(conf)
    conf.update({"include_entity_globs": [], "exclude_entity_globs": []})
    assert filt.config == conf
    assert not filt.empty_filter
Example #7
0
def test_filter_schema_with_globs():
    """Test filter schema with glob options."""
    conf = {
        "include_domains": ["light"],
        "include_entity_globs": ["sensor.kitchen_*"],
        "include_entities": ["switch.kitchen"],
        "exclude_domains": ["cover"],
        "exclude_entity_globs": ["sensor.weather_*"],
        "exclude_entities": ["light.kitchen"],
    }
    filt = FILTER_SCHEMA(conf)
    assert filt.config == conf
Example #8
0
def test_filter_schema_empty():
    """Test filter schema."""
    conf = {}
    filt = FILTER_SCHEMA(conf)
    conf.update({
        "include_domains": [],
        "include_entities": [],
        "exclude_domains": [],
        "exclude_entities": [],
        "include_entity_globs": [],
        "exclude_entity_globs": [],
    })
    assert filt.config == conf
    assert filt.empty_filter
Example #9
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Do the setup based on the config entry and the filter from yaml."""
    hass.data.setdefault(DOMAIN, {DATA_FILTER: FILTER_SCHEMA({})})
    hub = AzureEventHub(
        hass,
        entry,
        hass.data[DOMAIN][DATA_FILTER],
    )
    try:
        await hub.async_test_connection()
    except EventHubError as err:
        raise ConfigEntryNotReady(
            "Could not connect to Azure Event Hub") from err
    hass.data[DOMAIN][DATA_HUB] = hub
    entry.async_on_unload(entry.add_update_listener(async_update_listener))
    await hub.async_start()
    return True
Example #10
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    yaml_config = await async_integration_yaml_config(hass, DOMAIN)
    if yaml_config is None:
        raise ConfigEntryNotReady('Configuration is missing or invalid')

    _async_update_config_entry_from_yaml(hass, entry, yaml_config)
    _async_import_options_from_data_if_missing(hass, entry)
    entry.async_on_unload(entry.add_update_listener(_async_update_listener))

    yaml_domain_config = yaml_config.get(DOMAIN, {})
    filters = yaml_domain_config.get(const.CONF_FILTER, {})
    if is_config_filter_empty(
            yaml_domain_config) and const.CONF_FILTER in entry.options:
        filters = entry.options[const.CONF_FILTER]

    config = Config(hass=hass,
                    entry=entry,
                    should_expose=FILTER_SCHEMA(filters),
                    entity_config=yaml_domain_config.get(
                        const.CONF_ENTITY_CONFIG, {}))
    await config.async_init()
    hass.data[DOMAIN][CONFIG] = config

    if config.is_cloud_connection:
        cloud_manager = CloudManager(hass, config,
                                     async_get_clientsession(hass))
        hass.data[DOMAIN][CLOUD_MANAGER] = cloud_manager

        hass.loop.create_task(cloud_manager.connect())
        entry.async_on_unload(
            hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP,
                                       cloud_manager.disconnect))

    await async_start_notifier(hass)

    return True
Example #11
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up HomeKit from a config entry."""
    _async_import_options_from_data_if_missing(hass, entry)

    conf = entry.data
    options = entry.options

    name = conf[CONF_NAME]
    port = conf[CONF_PORT]
    _LOGGER.debug("Begin setup HomeKit for %s", name)

    # If the previous instance hasn't cleaned up yet
    # we need to wait a bit
    if not await hass.async_add_executor_job(port_is_available, port):
        _LOGGER.warning("The local port %s is in use", port)
        raise ConfigEntryNotReady

    if CONF_ENTRY_INDEX in conf and conf[CONF_ENTRY_INDEX] == 0:
        _LOGGER.debug("Migrating legacy HomeKit data for %s", name)
        hass.async_add_executor_job(
            migrate_filesystem_state_data_for_primary_imported_entry_id,
            hass,
            entry.entry_id,
        )

    aid_storage = AccessoryAidStorage(hass, entry.entry_id)

    await aid_storage.async_initialize()
    # ip_address and advertise_ip are yaml only
    ip_address = conf.get(CONF_IP_ADDRESS)
    advertise_ip = conf.get(CONF_ADVERTISE_IP)

    entity_config = options.get(CONF_ENTITY_CONFIG, {}).copy()
    auto_start = options.get(CONF_AUTO_START, DEFAULT_AUTO_START)
    safe_mode = options.get(CONF_SAFE_MODE, DEFAULT_SAFE_MODE)
    entity_filter = FILTER_SCHEMA(options.get(CONF_FILTER, {}))

    homekit = HomeKit(
        hass,
        name,
        port,
        ip_address,
        entity_filter,
        entity_config,
        safe_mode,
        advertise_ip,
        entry.entry_id,
    )
    zeroconf_instance = await zeroconf.async_get_instance(hass)
    await hass.async_add_executor_job(homekit.setup, zeroconf_instance)

    undo_listener = entry.add_update_listener(_async_update_listener)

    hass.data[DOMAIN][entry.entry_id] = {
        AID_STORAGE: aid_storage,
        HOMEKIT: homekit,
        UNDO_UPDATE_LISTENER: undo_listener,
    }

    if hass.state == CoreState.running:
        await homekit.async_start()
    elif auto_start:
        hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED,
                                   homekit.async_start)

    return True
Example #12
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up HomeKit from a config entry."""
    _async_import_options_from_data_if_missing(hass, entry)

    conf = entry.data
    options = entry.options

    name = conf[CONF_NAME]
    port = conf[CONF_PORT]
    _LOGGER.debug("Begin setup HomeKit for %s", name)

    aid_storage = AccessoryAidStorage(hass, entry.entry_id)

    await aid_storage.async_initialize()
    # ip_address and advertise_ip are yaml only
    ip_address = conf.get(CONF_IP_ADDRESS)
    advertise_ip = conf.get(CONF_ADVERTISE_IP)
    # exclude_accessory_mode is only used for config flow
    # to indicate that the config entry was setup after
    # we started creating config entries for entities that
    # to run in accessory mode and that we should never include
    # these entities on the bridge. For backwards compatibility
    # with users who have not migrated yet we do not do exclude
    # these entities by default as we cannot migrate automatically
    # since it requires a re-pairing.
    exclude_accessory_mode = conf.get(CONF_EXCLUDE_ACCESSORY_MODE,
                                      DEFAULT_EXCLUDE_ACCESSORY_MODE)
    homekit_mode = options.get(CONF_HOMEKIT_MODE, DEFAULT_HOMEKIT_MODE)
    entity_config = options.get(CONF_ENTITY_CONFIG, {}).copy()
    auto_start = options.get(CONF_AUTO_START, DEFAULT_AUTO_START)
    entity_filter = FILTER_SCHEMA(options.get(CONF_FILTER, {}))

    homekit = HomeKit(
        hass,
        name,
        port,
        ip_address,
        entity_filter,
        exclude_accessory_mode,
        entity_config,
        homekit_mode,
        advertise_ip,
        entry.entry_id,
        entry.title,
    )
    zeroconf_instance = await zeroconf.async_get_instance(hass)

    # If the previous instance hasn't cleaned up yet
    # we need to wait a bit
    try:
        await hass.async_add_executor_job(homekit.setup, zeroconf_instance)
    except (OSError, AttributeError) as ex:
        _LOGGER.warning(
            "%s could not be setup because the local port %s is in use", name,
            port)
        raise ConfigEntryNotReady from ex

    undo_listener = entry.add_update_listener(_async_update_listener)

    hass.data[DOMAIN][entry.entry_id] = {
        AID_STORAGE: aid_storage,
        HOMEKIT: homekit,
        UNDO_UPDATE_LISTENER: undo_listener,
    }

    if hass.state == CoreState.running:
        await homekit.async_start()
    elif auto_start:
        hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED,
                                   homekit.async_start)

    return True