Voluptuous options flow validation for an optional string - how?


i cannot figure out how to allow an string value to be empty, see my code exampe in my options flow schema:

                        CONF_DISABLE_ATTRIBUTE_UPDATES, None
                ): str,

I also tried things like cv.Any(None, cv.string) where cv is homeassistant.helpers.config_validation which results in a ValueError. I saw a lot of code samples using this, nothing is working for me.

With the above exampe I am not able to empty the field, see the gif here.
First I try to emtpy it (which is ignored, and when debugging the config flow the user_input contains the old string). Only thing working is replacing the content with a space.

The whole source file is here: hacs-govee/config_flow.py at 7893ad19cb99c1c95ba3d7d915ccb138e151300d · LaggAt/hacs-govee · GitHub

How can I fix or work around this? Please help!

Still having the issue - anyone?

I know this is a very old topic, and probably not relevant for you anymore, but I stumbled over it because I had the same issue, until I finally found it in the documentation. It is not very easy to find, so I thought I’d add it here as well, for other with the same question.

The documentation: Data Entry Flow | Home Assistant Developer Docs

And the solution:

If you’d like to pre-fill data in the form, you have two options. The first is to use the default parameter. This will both pre-fill the field, and act as the default value in case the user leaves the field empty.

(my emphasis)

    data_schema = {
        vol.Optional("field_name", default="default value"): str,

The other alternative is to use a suggested value - this will also pre-fill the form field, but will allow the user to leave it empty if the user so wishes.

    data_schema = {
            "field_name", description={"suggested_value": "suggested value"}
        ): str,

So adding a “suggested_value” as an element in the “description” parameter is the way to do this.