Hi, creating and editing a config is working nicely, however, I’m getting an error.
2024-12-18 12:30:14.824 DEBUG (MainThread) [custom_components.afvalwijzer.config_flow] Config entry: <ConfigEntry entry_id=01JFCJRQJQKH3JCPJ57N9DPTWZ version=1 domain=afvalwijzer title=Afvalwijzer state=ConfigEntryState.LOADED unique_id=None>
2024-12-18 12:30:16.569 DEBUG (MainThread) [custom_components.afvalwijzer.config_flow] Config entry: <ConfigEntry entry_id=01JFCJRQJQKH3JCPJ57N9DPTWZ version=1 domain=afvalwijzer title=Afvalwijzer state=ConfigEntryState.LOADED unique_id=None>
2024-12-18 12:30:16.569 DEBUG (MainThread) [custom_components.afvalwijzer.config_flow] Updated options: {'date_isoformat': False, 'default_label': 'Geen', 'exclude_list': '', 'exclude_pickup_today': True, 'postal_code': '5146EA', 'provider': 'mijnafvalwijzer', 'street_number': '1', 'suffix': ''}
2024-12-18 12:30:16.569 DEBUG (MainThread) [custom_components.afvalwijzer.config_flow] Attempting to update config entry with options: {'date_isoformat': False, 'default_label': 'Geen', 'exclude_list': '', 'exclude_pickup_today': True, 'postal_code': '5146EA', 'provider': 'mijnafvalwijzer', 'street_number': '73', 'suffix': ''}
2024-12-18 12:30:16.569 ERROR (MainThread) [custom_components.afvalwijzer.config_flow] Error updating config entry: object bool can't be used in 'await' expression
Code:
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
import logging
from .const.const import (
DOMAIN,
CONF_COLLECTOR,
CONF_POSTAL_CODE,
CONF_STREET_NUMBER,
CONF_SUFFIX,
CONF_EXCLUDE_PICKUP_TODAY,
CONF_DATE_ISOFORMAT,
CONF_DEFAULT_LABEL,
CONF_EXCLUDE_LIST,
)
DATA_SCHEMA = vol.Schema({
vol.Required(CONF_COLLECTOR): cv.string,
vol.Required(CONF_POSTAL_CODE): cv.string,
vol.Required(CONF_STREET_NUMBER): cv.string,
vol.Optional(CONF_SUFFIX, default=""): cv.string,
vol.Optional(CONF_EXCLUDE_PICKUP_TODAY, default=True): cv.boolean,
vol.Optional(CONF_DATE_ISOFORMAT, default=False): cv.boolean,
vol.Optional(CONF_DEFAULT_LABEL, default="geen"): cv.string,
vol.Optional(CONF_EXCLUDE_LIST, default=""): cv.string,
})
class AfvalwijzerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Afvalwijzer."""
VERSION = 1
async def async_step_user(self, user_input=None):
"""Handle the initial step."""
errors = {}
if user_input is not None:
# Perform validation
if not self._validate_postal_code(user_input.get(CONF_POSTAL_CODE)):
errors["postal_code"] = "config.error.invalid_postal_code"
elif not self._validate_street_number(user_input.get(CONF_STREET_NUMBER)):
errors["street_number"] = "config.error.invalid_street_number"
else:
# Validation passed, create the entry
return self.async_create_entry(title="Afvalwijzer", data=user_input)
return self.async_show_form(
step_id="user",
data_schema=DATA_SCHEMA,
errors=errors,
description_placeholders={
"provider": "e.g., mijnafvalwijzer",
"postal_code": "e.g., 1234AB",
},
)
@staticmethod
@callback
def async_get_options_flow(config_entry):
return AfvalwijzerOptionsFlow()
def _validate_postal_code(self, postal_code):
"""Validate the postal code format."""
return (
isinstance(postal_code, str)
and len(postal_code) == 6
and postal_code[:4].isdigit()
and postal_code[4:].isalpha()
)
def _validate_street_number(self, street_number):
"""Validate the street number."""
return street_number.isdigit()
class AfvalwijzerOptionsFlow(config_entries.OptionsFlow):
"""Handle options for Afvalwijzer."""
async def async_step_init(self, user_input=None):
"""Handle options configuration."""
_LOGGER = logging.getLogger(__name__)
_LOGGER.debug(f"Config entry: {self.config_entry}")
# Ensure self.config_entry is valid
if not self.config_entry:
_LOGGER.error("Config entry is invalid.")
return self.async_abort(reason="invalid_config_entry")
current_options = self.config_entry.options
if user_input is not None:
# Merge the user input with the current options
updated_options = {**current_options, **user_input}
# Ensure updated_options is a valid dictionary
_LOGGER.debug(f"Updated options: {updated_options}")
if not isinstance(updated_options, dict):
_LOGGER.error("Updated options must be a dictionary.")
return self.async_abort(reason="invalid_options")
# Try to update the config entry with the options
try:
_LOGGER.debug(f"Attempting to update config entry with options: {updated_options}")
await self.hass.config_entries.async_update_entry(
self.config_entry, options=updated_options
)
except Exception as e:
_LOGGER.error(f"Error updating config entry: {e}")
return self.async_abort(reason="update_failed")
# After updating, return to the main page or close the flow
return self.async_create_entry(title="", data=updated_options)
# Show the form with the existing values as defaults
options_schema = vol.Schema({
vol.Required(CONF_COLLECTOR, default=current_options.get(CONF_COLLECTOR, "")): cv.string,
vol.Required(CONF_POSTAL_CODE, default=current_options.get(CONF_POSTAL_CODE, "")): cv.string,
vol.Required(CONF_STREET_NUMBER, default=current_options.get(CONF_STREET_NUMBER, "")): cv.string,
vol.Optional(CONF_SUFFIX, default=current_options.get(CONF_SUFFIX, "")): cv.string,
vol.Optional(CONF_EXCLUDE_PICKUP_TODAY, default=current_options.get(CONF_EXCLUDE_PICKUP_TODAY, "")): cv.boolean,
vol.Optional(CONF_DATE_ISOFORMAT, default=current_options.get(CONF_DATE_ISOFORMAT, "")): cv.boolean,
vol.Optional(CONF_DEFAULT_LABEL, default=current_options.get(CONF_DEFAULT_LABEL, "")): cv.string,
vol.Optional(CONF_EXCLUDE_LIST, default=current_options.get(CONF_EXCLUDE_LIST, "")): cv.string,
})
return self.async_show_form(
step_id="init", data_schema=options_schema, errors={},
description_placeholders={}
)
I’ve tried a lot on the boolean part. Maybe I’m missing something completely. The docs about config_flow are somewhat confusing to me to be honest (lack of knowledge I assume). Hope someone can help me out. Thanks.
Happy to share more info if needed. Repo / full code can be found here: GitHub - xirixiz/homeassistant-afvalwijzer: Provides sensors for some Dutch waste collectors