Fix async client init and options flow

This commit is contained in:
Your Name 2025-12-20 11:13:56 -06:00
parent 904280aca0
commit 3f52a4fcc4
2 changed files with 13 additions and 6 deletions

View file

@ -28,7 +28,8 @@ type GroqdConfigEntry = ConfigEntry[GroqdRuntimeData]
async def async_setup_entry(hass: HomeAssistant, entry: GroqdConfigEntry) -> bool:
"""Set up groqd from a config entry."""
api_key = entry.data[CONF_API_KEY]
entry.runtime_data = GroqdRuntimeData(client=groq.AsyncClient(api_key=api_key))
client = await hass.async_add_executor_job(groq.AsyncClient, api_key=api_key)
entry.runtime_data = GroqdRuntimeData(client=client)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
@ -36,4 +37,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: GroqdConfigEntry) -> boo
async def async_unload_entry(hass: HomeAssistant, entry: GroqdConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
try:
await entry.runtime_data.client.close()
except Exception:
pass
return unload_ok

View file

@ -137,19 +137,19 @@ class GroqdOptionsFlow(OptionsFlow):
"""Options flow for groqd."""
def __init__(self, config_entry: ConfigEntry) -> None:
self.config_entry = config_entry
self._config_entry = config_entry
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
options = dict(self.config_entry.options)
options = dict(self._config_entry.options)
if user_input is not None:
api_key = user_input.pop(CONF_API_KEY, "")
if api_key:
self.hass.config_entries.async_update_entry(
self.config_entry,
data={**self.config_entry.data, CONF_API_KEY: api_key},
self._config_entry,
data={**self._config_entry.data, CONF_API_KEY: api_key},
)
return self.async_create_entry(title="", data=user_input)