3535 CONF_MAX_TOKENS ,
3636 CONF_API_ENDPOINT ,
3737 CONF_REQUEST_INTERVAL ,
38+ CONF_API_TIMEOUT ,
3839 CONF_API_PROVIDER ,
3940 CONF_CONTEXT_MESSAGES ,
4041 API_PROVIDER_OPENAI ,
5152 DEFAULT_DEEPSEEK_ENDPOINT ,
5253 DEFAULT_GEMINI_ENDPOINT ,
5354 DEFAULT_REQUEST_INTERVAL ,
55+ DEFAULT_API_TIMEOUT ,
5456 DEFAULT_CONTEXT_MESSAGES ,
55- API_TIMEOUT ,
5657 SERVICE_ASK_QUESTION ,
5758 SERVICE_CLEAR_HISTORY ,
5859 SERVICE_GET_HISTORY ,
@@ -268,7 +269,7 @@ def copy_file():
268269
269270 return True
270271
271- async def async_check_api (session , endpoint : str , headers : dict , provider : str ) -> bool :
272+ async def async_check_api (session , endpoint : str , headers : dict , provider : str , api_timeout : int = DEFAULT_API_TIMEOUT ) -> bool :
272273 """Check API availability for different providers."""
273274 try :
274275 if provider == API_PROVIDER_GEMINI :
@@ -285,7 +286,7 @@ async def async_check_api(session, endpoint: str, headers: dict, provider: str)
285286 else : # OpenAI
286287 check_url = f"{ endpoint } /models"
287288
288- async with timeout (API_TIMEOUT ):
289+ async with timeout (api_timeout ):
289290 async with session .get (check_url , headers = headers ) as response :
290291 if response .status in [200 , 404 ]:
291292 return True
@@ -311,22 +312,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
311312 _LOGGER .error ("API provider not specified" )
312313 raise ConfigEntryNotReady ("API provider is required" )
313314
314- # Get configuration
315+ # Get configuration (merge data with options to apply any runtime changes)
316+ config = {** entry .data , ** entry .options }
315317 session = aiohttp_client .async_get_clientsession (hass )
316- api_provider = entry . data .get (CONF_API_PROVIDER )
317- model = entry . data .get (CONF_MODEL , DEFAULT_MODEL )
318- endpoint = entry . data .get (
318+ api_provider = config .get (CONF_API_PROVIDER )
319+ model = config .get (CONF_MODEL , DEFAULT_MODEL )
320+ endpoint = config .get (
319321 CONF_API_ENDPOINT ,
320322 DEFAULT_OPENAI_ENDPOINT if api_provider == API_PROVIDER_OPENAI
321323 else DEFAULT_ANTHROPIC_ENDPOINT
322324 ).rstrip ('/' )
323- api_key = entry .data [CONF_API_KEY ]
325+ api_key = entry .data [CONF_API_KEY ] # API key stays in data, not in options
324326 instance_name = entry .data .get (CONF_NAME , entry .entry_id )
325- request_interval = entry .data .get (CONF_REQUEST_INTERVAL , DEFAULT_REQUEST_INTERVAL )
326- max_tokens = entry .data .get (CONF_MAX_TOKENS , DEFAULT_MAX_TOKENS )
327- temperature = entry .data .get (CONF_TEMPERATURE , DEFAULT_TEMPERATURE )
328- max_history_size = entry .data .get (CONF_MAX_HISTORY_SIZE , DEFAULT_MAX_HISTORY )
329- context_messages = entry .data .get (CONF_CONTEXT_MESSAGES , DEFAULT_CONTEXT_MESSAGES )
327+ request_interval = config .get (CONF_REQUEST_INTERVAL , DEFAULT_REQUEST_INTERVAL )
328+ api_timeout = config .get (CONF_API_TIMEOUT , DEFAULT_API_TIMEOUT )
329+ max_tokens = config .get (CONF_MAX_TOKENS , DEFAULT_MAX_TOKENS )
330+ temperature = config .get (CONF_TEMPERATURE , DEFAULT_TEMPERATURE )
331+ max_history_size = config .get (CONF_MAX_HISTORY_SIZE , DEFAULT_MAX_HISTORY )
332+ context_messages = config .get (CONF_CONTEXT_MESSAGES , DEFAULT_CONTEXT_MESSAGES )
330333 is_anthropic = api_provider == API_PROVIDER_ANTHROPIC
331334
332335 headers = {
@@ -340,7 +343,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
340343 else :
341344 headers ["Authorization" ] = f"Bearer { api_key } "
342345
343- if not await async_check_api (session , endpoint , headers , api_provider ):
346+ if not await async_check_api (session , endpoint , headers , api_provider , api_timeout ):
344347 raise ConfigEntryNotReady ("API connection failed" )
345348
346349 _LOGGER .debug ("Creating API client for %s with endpoint %s" , api_provider , endpoint )
@@ -351,6 +354,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
351354 headers = headers ,
352355 api_provider = api_provider ,
353356 model = model ,
357+ api_timeout = api_timeout ,
354358 )
355359
356360 coordinator = HATextAICoordinator (
@@ -364,6 +368,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
364368 max_history_size = max_history_size ,
365369 context_messages = context_messages ,
366370 is_anthropic = is_anthropic ,
371+ api_timeout = api_timeout ,
367372 )
368373
369374 _LOGGER .debug (f"Created coordinator for { instance_name } " )
0 commit comments