Hi everyone,
I’m encountering a validation issue in the config flow for a custom integration when using an optional EntitySelector
for a temperature sensor. When the user leaves this optional field empty and submits the form, a validation error occurs, preventing the configuration from proceeding.
Here’s the relevant part of my voluptuous
schema definition within the ConfigFlow
’s async_step_user
:
# (Inside async_step_user or a helper function like get_user_schema)
import voluptuous as vol
from homeassistant.helpers import selector
from homeassistant.components.sensor import SensorDeviceClass
from .const import CONF_TEMP_SENSOR # CONF_TEMP_SENSOR = "temp_sensor"
# ... other fields ...
vol.Optional(
CONF_TEMP_SENSOR, default=user_input.get(CONF_TEMP_SENSOR) # Trying to preserve input on redisplay
): selector.EntitySelector(
selector.EntitySelectorConfig(
domain="sensor", device_class=SensorDeviceClass.TEMPERATURE
)
),
# ... other fields ...
I also have logic after successful validation to remove the CONF_TEMP_SENSOR
key from the data if it’s None
or an empty string before creating the config entry:
# (Inside async_step_user, after validation passes)
temp_sensor_value = user_input.get(CONF_TEMP_SENSOR)
if not temp_sensor_value: # Checks for None or empty string
user_input.pop(CONF_TEMP_SENSOR, None) # Remove key if it exists
return self.async_create_entry(title=info["title"], data=user_input)
Problem: The validation fails before my cleanup logic runs. My hypothesis is that when the optional EntitySelector
is left empty, the frontend might submit it as CONF_TEMP_SENSOR: ""
. The vol.Optional
allows the key, but the EntitySelector
validator then rejects the empty string ""
as an invalid entity ID. The default=user_input.get(...)
might complicate this further, especially on form redisplay after other errors.
Question:
- Is my hypothesis likely correct? Is the standard behavior for an empty optional
EntitySelector
to submit""
? -
- What is the recommended way to define an optional
EntitySelector
in a config flow schema to avoid this validation error when the user intentionally leaves it blank? Should I simply remove thedefault=...
part like this?
- What is the recommended way to define an optional
-
```python
-
vol.Optional(CONF_TEMP_SENSOR): selector.EntitySelector(...)
-
```
-
- Is there a different default value (like
None
) or schema structure I should be using?
Any insights or pointers to best practices would be greatly appreciated! Thanks!
- Is there a different default value (like