From 3f52a4fcc4e33959ff7b7a11e54cf8812fa2038e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 20 Dec 2025 11:13:56 -0600 Subject: [PATCH] Fix async client init and options flow --- custom_components/groqd/__init__.py | 11 +++++++++-- custom_components/groqd/config_flow.py | 8 ++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/custom_components/groqd/__init__.py b/custom_components/groqd/__init__.py index 85004b7..1d3fd2b 100644 --- a/custom_components/groqd/__init__.py +++ b/custom_components/groqd/__init__.py @@ -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 diff --git a/custom_components/groqd/config_flow.py b/custom_components/groqd/config_flow.py index 4c0e85f..d79aa97 100644 --- a/custom_components/groqd/config_flow.py +++ b/custom_components/groqd/config_flow.py @@ -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)