mirror of
https://github.com/smkrv/ha-text-ai.git
synced 2026-07-27 09:34:01 +08:00
Release v2.0.0
This commit is contained in:
@@ -189,15 +189,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
else: # OpenAI
|
else: # OpenAI
|
||||||
headers["Authorization"] = f"Bearer {api_key}"
|
headers["Authorization"] = f"Bearer {api_key}"
|
||||||
|
|
||||||
# Проверка API с повторами
|
# Проверка API
|
||||||
for attempt in range(API_RETRY_COUNT):
|
try:
|
||||||
if await async_check_api(session, endpoint, headers, api_provider):
|
check_result = await async_check_api(session, endpoint, headers, api_provider)
|
||||||
break
|
if not check_result:
|
||||||
if attempt < API_RETRY_COUNT - 1:
|
raise ConfigEntryNotReady("API connection failed")
|
||||||
delay = 1.5 * (2 ** attempt)
|
except Exception as ex:
|
||||||
await asyncio.sleep(delay)
|
_LOGGER.error(f"API check failed: {ex}")
|
||||||
else:
|
raise ConfigEntryNotReady("Failed to connect to API")
|
||||||
raise ConfigEntryNotReady("Failed to connect to API")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Create coordinator
|
# Create coordinator
|
||||||
@@ -235,7 +234,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
_LOGGER.exception("Setup error: %s", str(ex))
|
_LOGGER.exception("Setup error: %s", str(ex))
|
||||||
raise ConfigEntryNotReady(f"Setup error: {str(ex)}") from ex
|
raise ConfigEntryNotReady(f"Setup error: {str(ex)}") from ex
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -121,14 +121,26 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
# Use a unique ID based on the API key
|
# ВАЖНОЕ ИЗМЕНЕНИЕ: создаем уникальный идентификатор
|
||||||
await self.async_set_unique_id(user_input[CONF_API_KEY])
|
unique_id = f"{user_input[CONF_API_PROVIDER]}_{user_input[CONF_API_ENDPOINT]}_{user_input[CONF_API_KEY]}"
|
||||||
self._abort_if_unique_id_configured()
|
|
||||||
|
|
||||||
return self.async_create_entry(
|
# Проверяем существующие конфигурации
|
||||||
title=f"HA Text AI ({user_input[CONF_API_PROVIDER]})",
|
existing_entries = [
|
||||||
data=user_input
|
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:
|
except Exception as e:
|
||||||
_LOGGER.error(f"Unexpected error: {e}")
|
_LOGGER.error(f"Unexpected error: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user