Config Flow Import: Set Options

I am adding config flow to a custom component and using an import flow to take the setting from configuration.yaml (if they exist). This is all working fine except the ability to set options values from those that are setup in the config.yaml.

I have the options flow working which is fine to set these options values if adding via UI from scratch but I cannot work out how to set options values in the import flow from config.yaml as these cannot be set when creating the entity.

I have tried creating the config entry and then updating it with options values but because you cannot/should not await the creation then this does not work as I think it is trying to update before it is created.

Has anybody got a way of doing this to save me banging my head any more! Thanks in advance.

I would look through the last couple of release notes and find an integration that has made the shift, then shamelessly crib from it’s code.

Doing a little research, blink does this (import from yaml) and seems to do it in two lines (plus a comment) https://github.com/home-assistant/core/blob/3d253fa52ad4a9a953984b6b3ca68a6e7ddcee31/homeassistant/components/blink/config_flow.py

async def async_step_import(self, import_data):
        """Import blink config from configuration.yaml."""
        return await self.async_step_user(import_data)
1 Like

Took your advice and been looking at components that have moved over but none I have looked at so far seem to import any options settings, including blink. Will keep looking and hope that someone knows how to do this before I get back to HA v0.001! :wink:

What alerted me to blink was this post: 0.110: Speed! OpenZWave beta, HomeKit Cameras, ONVIF, Calendars

I haven’t tried it myself as I do not have blink.

pihole has moved from yaml to confog flow and has similar but certainly not identical code

    async def async_step_import(self, user_input=None):
        """Handle a flow initiated by import."""
        return await self.async_step_init(user_input, is_import=True)

I have managed to find a way to do this from: https://github.com/home-assistant/core/blob/04cfd36d06ce4becf8567ca27ea589f0550037f5/homeassistant/components/isy994/init.py

Seems you cannot import the options during config flow but need to put them in the data field and then copy them to options once config entry is created. Not perfect as will leave duff data when you change the options but it works!

Thanks for your help.
Mark

1 Like

You can’t set options during import. Only after the config entry has been loaded by hass. I update options in axis component and have done the same in both deconz and unifi a few releases back

Excellent example, as I needed to do the same thing!

You can remove the options from data when importing into options. Leaving an example here is case more people are looking for this:

    options = dict(entry.options)
    data = {}
    importable_options = [CONF_COMMAND_OFF, CONF_COMMAND_ON]
    found = False
    for key in entry.data:
        if key in importable_options and key not in options:
            options[key] = entry.data[key]
            found = True
        else:
            data[key] = entry.data[key]

    if found:
        hass.config_entries.async_update_entry(entry, data=data, options=options)