mirror of
https://github.com/smkrv/ha-text-ai.git
synced 2026-07-27 01:24:05 +08:00
Release v2.0.0
This commit is contained in:
@@ -93,6 +93,9 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Принудительная очистка существующих записей с тем же API-ключом
|
||||||
|
await self._async_cleanup_existing_entries(user_input[CONF_API_KEY])
|
||||||
|
|
||||||
session = async_get_clientsession(self.hass)
|
session = async_get_clientsession(self.hass)
|
||||||
|
|
||||||
# Minimal API key validation
|
# Minimal API key validation
|
||||||
@@ -121,23 +124,14 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
# Проверяем существующие конфигурации
|
# Генерируем уникальный идентификатор на основе ключа
|
||||||
existing_entries = [
|
await self.async_set_unique_id(user_input[CONF_API_KEY])
|
||||||
entry for entry in self.hass.config_entries.async_entries(DOMAIN)
|
self._abort_if_unique_id_configured()
|
||||||
if entry.data.get(CONF_API_KEY) == user_input[CONF_API_KEY]
|
|
||||||
]
|
|
||||||
|
|
||||||
if existing_entries:
|
return self.async_create_entry(
|
||||||
errors["base"] = "already_configured"
|
title=f"HA Text AI ({user_input[CONF_API_PROVIDER]})",
|
||||||
else:
|
data=user_input
|
||||||
# Генерируем уникальный идентификатор на основе ключа
|
)
|
||||||
await self.async_set_unique_id(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]})",
|
|
||||||
data=user_input
|
|
||||||
)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.error(f"Unexpected error: {e}")
|
_LOGGER.error(f"Unexpected error: {e}")
|
||||||
@@ -170,6 +164,20 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors
|
errors=errors
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def _async_cleanup_existing_entries(self, api_key):
|
||||||
|
"""Cleanup existing entries with the same API key."""
|
||||||
|
existing_entries = [
|
||||||
|
entry for entry in self.hass.config_entries.async_entries(DOMAIN)
|
||||||
|
if entry.data.get(CONF_API_KEY) == api_key
|
||||||
|
]
|
||||||
|
|
||||||
|
for entry in existing_entries:
|
||||||
|
try:
|
||||||
|
await self.hass.config_entries.async_remove(entry.entry_id)
|
||||||
|
_LOGGER.info(f"Removed existing entry with ID: {entry.entry_id}")
|
||||||
|
except Exception as e:
|
||||||
|
_LOGGER.warning(f"Could not remove existing entry: {e}")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(config_entry: config_entries.ConfigEntry) -> config_entries.OptionsFlow:
|
def async_get_options_flow(config_entry: config_entries.ConfigEntry) -> config_entries.OptionsFlow:
|
||||||
|
|||||||
Reference in New Issue
Block a user