Hello. I’m trying to create a new entity thru the OptionsFlowHandler in my config_flow.py file. I want to offer the user two options: to add or remove entities based on the options provided in the first form before displaying a second form. The flow itself works splendidly - I can choose to either add or remove devices and a second form follows providing the selection of devices to be created or removed. Below I’ve provided the code for adding devices (leaving aside the remove function). However, when I choose which devices to add, the async_create_entry
doesn’t run inside of my __init__.py file. I’ve also provided a short snipet of this file below.
config_flow.py:
class OptionsFlowHandler(OptionsFlow):
"""options flow"""
def __init__(self, config_entry):
"""options flow initialization"""
self.config_entry = config_entry
async def async_step_init(self, user_input: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""options initialization step"""
if user_input is not None:
if user_input[ADD_REMOVE_CONF] == ADD_CONF:
return await self.async_step_add()
else:
return await self.async_step_remove()
options_schema = vol.Schema({
vol.Required(ADD_REMOVE_CONF): vol.In([ADD_CONF, REMOVE_CONF])
})
return self.async_show_form(step_id="init", data_schema=options_schema, errors={})
async def async_step_add(self, user_input: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
if user_input is not None:
# PROBLEM HERE
return self.async_create_entry(title=DOMAIN, data=user_input)
add_schema = vol.Schema({
vol.Required(CONF_LOCKS): cv.multi_select(dict(ble_devices.items()))
})
return self.async_show_form(step_id="add", data_schema=add_schema, errors={})
__init__.py:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Setup from config entry"""
_LOGGER.debug(entry.data)
# Forward setup to platform
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
entry, "lock"
)
)
entry.async_on_unload(entry.add_update_listener(update_listener))
return True
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle Sure Lock options update"""
_LOGGER.debug(f"Reloading entry... {entry.data}")
await hass.config_entries.async_reload(entry.entry_id)
I would expect the async_setup_entry
function to be called inside of the __init__.py file, but when I check the logs, no entry data is printed. This function works fine during integration initialization, but only fails when I try to dynamically add new entities.
As an aside, I also want to be able to remove entities following a similar method (using the async_unload_entry
function rather than async_setup_entry
). Are there any similar pitfalls I might encounter?