from enum import Enum from typing import Generic, TypeVar from .data_entry_flow import AbortFlow _T = TypeVar("_T") ConfigFlowResult = dict class ConfigEntryState(Enum): LOADED = "loaded" NOT_LOADED = "not_loaded" class ConfigEntry(Generic[_T]): def __init__(self, data, unique_id, title="entry"): self.data = dict(data) self.unique_id = unique_id self.title = title self.state = ConfigEntryState.NOT_LOADED self.runtime_data = None self.unloads = [] def async_on_unload(self, func): self.unloads.append(func) class ConfigFlow: def __init_subclass__(cls, domain=None, **kwargs): super().__init_subclass__(**kwargs) cls.domain = domain def __init__(self): self.hass = None self.unique_id = None self.existing_unique_ids = set() self.reauth_entry = None self.updated = None def async_show_form(self, step_id, data_schema=None, errors=None, description_placeholders=None): return {"type": "form", "step_id": step_id, "errors": errors or {}, "schema": data_schema, "description_placeholders": description_placeholders} def async_create_entry(self, title, data): return {"type": "create_entry", "title": title, "data": data} async def async_set_unique_id(self, unique_id): self.unique_id = unique_id def _abort_if_unique_id_configured(self): if self.unique_id in self.existing_unique_ids: raise AbortFlow("already_configured") def _abort_if_unique_id_mismatch(self, reason="unique_id_mismatch"): if self.reauth_entry.unique_id != self.unique_id: raise AbortFlow(reason) def _get_reauth_entry(self): return self.reauth_entry def async_update_reload_and_abort(self, entry, data_updates=None): entry.data.update(data_updates or {}) return {"type": "abort", "reason": "reauth_successful"}