mirror of
https://github.com/smkrv/ha-text-ai.git
synced 2026-07-31 11:43:55 +08:00
Release v2.0.0
This commit is contained in:
@@ -91,13 +91,15 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
description_placeholders={"provider": provider}
|
description_placeholders={"provider": provider}
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _process_configuration(self, user_input):
|
async def _process_configuration(self, user_input):
|
||||||
"""Validate and process user configuration."""
|
"""Validate and process user configuration."""
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Принудительная очистка существующих записей с тем же API-ключом
|
# Генерируем уникальный идентификатор один раз
|
||||||
await self._async_cleanup_existing_entries(user_input[CONF_API_KEY])
|
unique_id = f"{user_input[CONF_API_KEY]}_{user_input[CONF_MODEL]}"
|
||||||
|
await self.async_set_unique_id(unique_id)
|
||||||
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
session = async_get_clientsession(self.hass)
|
session = async_get_clientsession(self.hass)
|
||||||
|
|
||||||
@@ -127,12 +129,8 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
# Генерируем уникальный идентификатор на основе ключа
|
|
||||||
await self.async_set_unique_id(user_input[CONF_API_KEY])
|
|
||||||
self._abort_if_unique_id_configured()
|
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=user_input.get("name", f"HA Text AI ({user_input[CONF_API_PROVIDER]})"), # Используем введенное имя
|
title=user_input.get("name", f"HA Text AI ({user_input[CONF_API_PROVIDER]})"),
|
||||||
data=user_input
|
data=user_input
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -168,22 +166,6 @@ 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
|
|
||||||
@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:
|
||||||
"""Create the options flow."""
|
"""Create the options flow."""
|
||||||
return OptionsFlowHandler(config_entry)
|
return OptionsFlowHandler(config_entry)
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import certifi
|
|||||||
import aiohttp
|
import aiohttp
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
from homeassistant import config_entries
|
||||||
from homeassistant.helpers import aiohttp_client
|
from homeassistant.helpers import aiohttp_client
|
||||||
from openai import AsyncOpenAI, APIError, AuthenticationError, RateLimitError
|
from openai import AsyncOpenAI, APIError, AuthenticationError, RateLimitError
|
||||||
from anthropic import AsyncAnthropic
|
from anthropic import AsyncAnthropic
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
import voluptuous as vol
|
||||||
|
from homeassistant import config_entries
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
|
|||||||
Reference in New Issue
Block a user