From 3b38a6dd290bb0c4ebf7418e7d51f54d2a6eb8dc Mon Sep 17 00:00:00 2001 From: SMKRV Date: Sat, 23 Nov 2024 01:09:38 +0300 Subject: [PATCH] Release v2.0.0 --- custom_components/ha_text_ai/config_flow.py | 136 ++++++++++---------- 1 file changed, 65 insertions(+), 71 deletions(-) diff --git a/custom_components/ha_text_ai/config_flow.py b/custom_components/ha_text_ai/config_flow.py index 0515b9d..030c17e 100644 --- a/custom_components/ha_text_ai/config_flow.py +++ b/custom_components/ha_text_ai/config_flow.py @@ -64,9 +64,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): def _show_provider_config(self, provider): """Show configuration screen for selected provider.""" default_endpoint = DEFAULT_OPENAI_ENDPOINT if provider == API_PROVIDER_OPENAI else DEFAULT_ANTHROPIC_ENDPOINT - default_name = f"HA Text AI {len(self._async_current_entries()) + 1}" - return self.async_show_form( step_id="user", data_schema=vol.Schema({ @@ -91,84 +89,80 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): description_placeholders={"provider": provider} ) -async def _process_configuration(self, user_input): - """Validate and process user configuration.""" - errors = {} + async def _process_configuration(self, user_input): + """Validate and process user configuration.""" + errors = {} - try: - # Генерируем уникальный идентификатор один раз - unique_id = f"{user_input[CONF_API_KEY]}_{user_input[CONF_MODEL]}" - await self.async_set_unique_id(unique_id) - self._abort_if_unique_id_configured() + try: + unique_id = f"{user_input[CONF_API_KEY]}_{user_input[CONF_MODEL]}" + await self.async_set_unique_id(unique_id) + self._abort_if_unique_id_configured() - session = async_get_clientsession(self.hass) + session = async_get_clientsession(self.hass) - # Minimal API key validation - headers = { - "Authorization": f"Bearer {user_input[CONF_API_KEY]}", - "Content-Type": "application/json" - } - - # Adjust headers and endpoint based on provider - if user_input[CONF_API_PROVIDER] == API_PROVIDER_ANTHROPIC: headers = { - "x-api-key": user_input[CONF_API_KEY], - "anthropic-version": "2023-06-01" + "Authorization": f"Bearer {user_input[CONF_API_KEY]}", + "Content-Type": "application/json" } - # Basic connection test - try: - async with session.get( - f"{user_input[CONF_API_ENDPOINT]}/models", - headers=headers - ) as response: - if response.status not in [200, 404]: - errors["base"] = "cannot_connect" + if user_input[CONF_API_PROVIDER] == API_PROVIDER_ANTHROPIC: + headers = { + "x-api-key": user_input[CONF_API_KEY], + "anthropic-version": "2023-06-01" + } + + try: + async with session.get( + f"{user_input[CONF_API_ENDPOINT]}/models", + headers=headers + ) as response: + if response.status not in [200, 404]: + errors["base"] = "cannot_connect" + except Exception as e: + _LOGGER.error(f"Connection test failed: {e}") + errors["base"] = "cannot_connect" + + if not errors: + return self.async_create_entry( + title=user_input.get("name", f"HA Text AI ({user_input[CONF_API_PROVIDER]})"), + data=user_input + ) + except Exception as e: - _LOGGER.error(f"Connection test failed: {e}") - errors["base"] = "cannot_connect" + _LOGGER.error(f"Unexpected error: {e}") + errors["base"] = "unknown" - if not errors: - return self.async_create_entry( - title=user_input.get("name", f"HA Text AI ({user_input[CONF_API_PROVIDER]})"), - data=user_input - ) + return self.async_show_form( + step_id="user", + data_schema=vol.Schema({ + vol.Required("name", default=user_input.get("name", f"HA Text AI {len(self._async_current_entries()) + 1}")): str, + vol.Required(CONF_API_PROVIDER): vol.In([user_input[CONF_API_PROVIDER]]), + vol.Required(CONF_API_KEY): str, + vol.Required(CONF_MODEL, default=user_input.get(CONF_MODEL, DEFAULT_MODEL)): str, + vol.Optional(CONF_API_ENDPOINT, default=user_input.get(CONF_API_ENDPOINT, + DEFAULT_OPENAI_ENDPOINT if user_input[CONF_API_PROVIDER] == API_PROVIDER_OPENAI + else DEFAULT_ANTHROPIC_ENDPOINT)): str, + vol.Optional(CONF_TEMPERATURE, default=user_input.get(CONF_TEMPERATURE, DEFAULT_TEMPERATURE)): vol.All( + vol.Coerce(float), + vol.Range(min=MIN_TEMPERATURE, max=MAX_TEMPERATURE) + ), + vol.Optional(CONF_MAX_TOKENS, default=user_input.get(CONF_MAX_TOKENS, DEFAULT_MAX_TOKENS)): vol.All( + vol.Coerce(int), + vol.Range(min=MIN_MAX_TOKENS, max=MAX_MAX_TOKENS) + ), + vol.Optional(CONF_REQUEST_INTERVAL, default=user_input.get(CONF_REQUEST_INTERVAL, DEFAULT_REQUEST_INTERVAL)): vol.All( + vol.Coerce(float), + vol.Range(min=MIN_REQUEST_INTERVAL) + ), + }), + description_placeholders={"provider": user_input[CONF_API_PROVIDER]}, + errors=errors + ) - except Exception as e: - _LOGGER.error(f"Unexpected error: {e}") - errors["base"] = "unknown" - - # Return to provider config, preserving previous input - return self.async_show_form( - step_id="user", - data_schema=vol.Schema({ - vol.Required("name", default=user_input.get("name", f"HA Text AI {len(self._async_current_entries()) + 1}")): str, - vol.Required(CONF_API_PROVIDER): vol.In([user_input[CONF_API_PROVIDER]]), - vol.Required(CONF_API_KEY): str, - vol.Required(CONF_MODEL, default=user_input.get(CONF_MODEL, DEFAULT_MODEL)): str, - vol.Optional(CONF_API_ENDPOINT, default=user_input.get(CONF_API_ENDPOINT, - DEFAULT_OPENAI_ENDPOINT if user_input[CONF_API_PROVIDER] == API_PROVIDER_OPENAI - else DEFAULT_ANTHROPIC_ENDPOINT)): str, - vol.Optional(CONF_TEMPERATURE, default=user_input.get(CONF_TEMPERATURE, DEFAULT_TEMPERATURE)): vol.All( - vol.Coerce(float), - vol.Range(min=MIN_TEMPERATURE, max=MAX_TEMPERATURE) - ), - vol.Optional(CONF_MAX_TOKENS, default=user_input.get(CONF_MAX_TOKENS, DEFAULT_MAX_TOKENS)): vol.All( - vol.Coerce(int), - vol.Range(min=MIN_MAX_TOKENS, max=MAX_MAX_TOKENS) - ), - vol.Optional(CONF_REQUEST_INTERVAL, default=user_input.get(CONF_REQUEST_INTERVAL, DEFAULT_REQUEST_INTERVAL)): vol.All( - vol.Coerce(float), - vol.Range(min=MIN_REQUEST_INTERVAL) - ), - }), - description_placeholders={"provider": user_input[CONF_API_PROVIDER]}, - errors=errors - ) - -def async_get_options_flow(config_entry: config_entries.ConfigEntry) -> config_entries.OptionsFlow: - """Create the options flow.""" - return OptionsFlowHandler(config_entry) + @staticmethod + async def async_get_options_flow(config_entry: config_entries.ConfigEntry) -> config_entries.OptionsFlow: + """Create the options flow.""" + return OptionsFlowHandler(config_entry) class OptionsFlowHandler(config_entries.OptionsFlow): """Options flow handler."""