Release v2.0.0

This commit is contained in:
SMKRV
2024-11-23 01:09:38 +03:00
parent 7f8d8be5fb
commit 3b38a6dd29
+2 -8
View File
@@ -64,9 +64,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
def _show_provider_config(self, provider): def _show_provider_config(self, provider):
"""Show configuration screen for selected provider.""" """Show configuration screen for selected provider."""
default_endpoint = DEFAULT_OPENAI_ENDPOINT if provider == API_PROVIDER_OPENAI else DEFAULT_ANTHROPIC_ENDPOINT 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}" default_name = f"HA Text AI {len(self._async_current_entries()) + 1}"
return self.async_show_form( return self.async_show_form(
step_id="user", step_id="user",
data_schema=vol.Schema({ data_schema=vol.Schema({
@@ -96,27 +94,23 @@ async def _process_configuration(self, user_input):
errors = {} errors = {}
try: try:
# Генерируем уникальный идентификатор один раз
unique_id = f"{user_input[CONF_API_KEY]}_{user_input[CONF_MODEL]}" unique_id = f"{user_input[CONF_API_KEY]}_{user_input[CONF_MODEL]}"
await self.async_set_unique_id(unique_id) await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
session = async_get_clientsession(self.hass) session = async_get_clientsession(self.hass)
# Minimal API key validation
headers = { headers = {
"Authorization": f"Bearer {user_input[CONF_API_KEY]}", "Authorization": f"Bearer {user_input[CONF_API_KEY]}",
"Content-Type": "application/json" "Content-Type": "application/json"
} }
# Adjust headers and endpoint based on provider
if user_input[CONF_API_PROVIDER] == API_PROVIDER_ANTHROPIC: if user_input[CONF_API_PROVIDER] == API_PROVIDER_ANTHROPIC:
headers = { headers = {
"x-api-key": user_input[CONF_API_KEY], "x-api-key": user_input[CONF_API_KEY],
"anthropic-version": "2023-06-01" "anthropic-version": "2023-06-01"
} }
# Basic connection test
try: try:
async with session.get( async with session.get(
f"{user_input[CONF_API_ENDPOINT]}/models", f"{user_input[CONF_API_ENDPOINT]}/models",
@@ -138,7 +132,6 @@ async def _process_configuration(self, user_input):
_LOGGER.error(f"Unexpected error: {e}") _LOGGER.error(f"Unexpected error: {e}")
errors["base"] = "unknown" errors["base"] = "unknown"
# Return to provider config, preserving previous input
return self.async_show_form( return self.async_show_form(
step_id="user", step_id="user",
data_schema=vol.Schema({ data_schema=vol.Schema({
@@ -166,7 +159,8 @@ async def _process_configuration(self, user_input):
errors=errors errors=errors
) )
def async_get_options_flow(config_entry: config_entries.ConfigEntry) -> config_entries.OptionsFlow: @staticmethod
async def async_get_options_flow(config_entry: config_entries.ConfigEntry) -> config_entries.OptionsFlow:
"""Create the options flow.""" """Create the options flow."""
return OptionsFlowHandler(config_entry) return OptionsFlowHandler(config_entry)