diff --git a/custom_components/ha_text_ai/__init__.py b/custom_components/ha_text_ai/__init__.py index d47e566..ba12f21 100644 --- a/custom_components/ha_text_ai/__init__.py +++ b/custom_components/ha_text_ai/__init__.py @@ -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: diff --git a/custom_components/ha_text_ai/config_flow.py b/custom_components/ha_text_ai/config_flow.py index e6014d9..306d116 100644 --- a/custom_components/ha_text_ai/config_flow.py +++ b/custom_components/ha_text_ai/config_flow.py @@ -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}")