Release v2.0.0

This commit is contained in:
SMKRV
2024-11-22 11:36:34 +03:00
parent bb8195c0d1
commit 30d69e7ed1
2 changed files with 27 additions and 17 deletions
+8 -10
View File
@@ -189,15 +189,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
else: # OpenAI
headers["Authorization"] = f"Bearer {api_key}"
# Проверка API с повторами
for attempt in range(API_RETRY_COUNT):
if await async_check_api(session, endpoint, headers, api_provider):
break
if attempt < API_RETRY_COUNT - 1:
delay = 1.5 * (2 ** attempt)
await asyncio.sleep(delay)
else:
raise ConfigEntryNotReady("Failed to connect to API")
# Проверка API
try:
check_result = await async_check_api(session, endpoint, headers, api_provider)
if not check_result:
raise ConfigEntryNotReady("API connection failed")
except Exception as ex:
_LOGGER.error(f"API check failed: {ex}")
raise ConfigEntryNotReady("Failed to connect to API")
try:
# Create coordinator
@@ -235,7 +234,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.exception("Setup error: %s", str(ex))
raise ConfigEntryNotReady(f"Setup error: {str(ex)}") from ex
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
try:
+19 -7
View File
@@ -121,14 +121,26 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors["base"] = "cannot_connect"
if not errors:
# Use a unique ID based on the API key
await self.async_set_unique_id(user_input[CONF_API_KEY])
self._abort_if_unique_id_configured()
# ВАЖНОЕ ИЗМЕНЕНИЕ: создаем уникальный идентификатор
unique_id = f"{user_input[CONF_API_PROVIDER]}_{user_input[CONF_API_ENDPOINT]}_{user_input[CONF_API_KEY]}"
return self.async_create_entry(
title=f"HA Text AI ({user_input[CONF_API_PROVIDER]})",
data=user_input
)
# Проверяем существующие конфигурации
existing_entries = [
entry for entry in self.hass.config_entries.async_entries(DOMAIN)
if entry.data.get(CONF_API_PROVIDER) == user_input[CONF_API_PROVIDER]
and entry.data.get(CONF_API_ENDPOINT) == user_input[CONF_API_ENDPOINT]
]
if existing_entries:
errors["base"] = "already_configured"
else:
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=f"HA Text AI ({user_input[CONF_API_PROVIDER]})",
data=user_input
)
except Exception as e:
_LOGGER.error(f"Unexpected error: {e}")