mirror of
https://github.com/smkrv/ha-text-ai.git
synced 2026-07-27 17:44:01 +08:00
Release v2.0.0
This commit is contained in:
@@ -4,7 +4,7 @@ import voluptuous as vol
|
|||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY, CONF_NAME
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
@@ -41,13 +41,18 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""Initialize the config flow."""
|
||||||
|
self._provider = None
|
||||||
|
|
||||||
async def async_step_user(self, user_input: Optional[Dict[str, Any]] = None) -> FlowResult:
|
async def async_step_user(self, user_input: Optional[Dict[str, Any]] = None) -> FlowResult:
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
if user_input is None:
|
if user_input is None:
|
||||||
return self._show_provider_selection()
|
return self._show_provider_selection()
|
||||||
|
|
||||||
if "provider" in user_input:
|
if "provider" in user_input:
|
||||||
return self._show_provider_config(user_input["provider"])
|
self._provider = user_input["provider"]
|
||||||
|
return self._show_provider_config(self._provider)
|
||||||
|
|
||||||
return await self._process_configuration(user_input)
|
return await self._process_configuration(user_input)
|
||||||
|
|
||||||
@@ -68,6 +73,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=vol.Schema({
|
data_schema=vol.Schema({
|
||||||
|
vol.Required(CONF_NAME): str, # Добавлено поле имени
|
||||||
vol.Required(CONF_API_PROVIDER): vol.In([provider]),
|
vol.Required(CONF_API_PROVIDER): vol.In([provider]),
|
||||||
vol.Required(CONF_API_KEY): str,
|
vol.Required(CONF_API_KEY): str,
|
||||||
vol.Required(CONF_MODEL, default=DEFAULT_MODEL): str,
|
vol.Required(CONF_MODEL, default=DEFAULT_MODEL): str,
|
||||||
@@ -93,8 +99,10 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Принудительная очистка существующих записей с тем же API-ключом
|
# Создаем уникальный идентификатор на основе имени и эндпоинта
|
||||||
await self._async_cleanup_existing_entries(user_input[CONF_API_KEY])
|
unique_id = f"{user_input[CONF_NAME]}_{user_input[CONF_API_ENDPOINT]}"
|
||||||
|
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)
|
||||||
|
|
||||||
@@ -104,7 +112,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Adjust headers and endpoint based on provider
|
# Adjust headers based on provider
|
||||||
if user_input[CONF_API_PROVIDER] == API_PROVIDER_ANTHROPIC:
|
if user_input[CONF_API_PROVIDER] == API_PROVIDER_ANTHROPIC:
|
||||||
headers = {
|
headers = {
|
||||||
"x-api-key": user_input[CONF_API_KEY],
|
"x-api-key": user_input[CONF_API_KEY],
|
||||||
@@ -124,12 +132,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=f"HA Text AI ({user_input[CONF_API_PROVIDER]})",
|
title=user_input[CONF_NAME],
|
||||||
data=user_input
|
data=user_input
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -142,7 +146,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=vol.Schema({
|
data_schema=vol.Schema({
|
||||||
vol.Required(CONF_API_PROVIDER): vol.In([user_input[CONF_API_PROVIDER]]),
|
vol.Required(CONF_API_PROVIDER): vol.In([user_input[CONF_API_PROVIDER]]),
|
||||||
vol.Required(CONF_API_KEY): str,
|
vol.Required(CONF_API_KEY, default=user_input.get(CONF_API_KEY, "")): str,
|
||||||
vol.Required(CONF_MODEL, default=user_input.get(CONF_MODEL, DEFAULT_MODEL)): str,
|
vol.Required(CONF_MODEL, default=user_input.get(CONF_MODEL, DEFAULT_MODEL)): str,
|
||||||
vol.Optional(CONF_API_ENDPOINT, default=user_input.get(CONF_API_ENDPOINT,
|
vol.Optional(CONF_API_ENDPOINT, default=user_input.get(CONF_API_ENDPOINT,
|
||||||
DEFAULT_OPENAI_ENDPOINT if user_input[CONF_API_PROVIDER] == API_PROVIDER_OPENAI
|
DEFAULT_OPENAI_ENDPOINT if user_input[CONF_API_PROVIDER] == API_PROVIDER_OPENAI
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"bluetooth": [],
|
"bluetooth": [],
|
||||||
"codeowners": ["@smkrv"],
|
"codeowners": ["@smkrv"],
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
|
"multiple_instances": true,
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"documentation": "https://github.com/smkrv/ha-text-ai",
|
"documentation": "https://github.com/smkrv/ha-text-ai",
|
||||||
"integration_type": "service",
|
"integration_type": "service",
|
||||||
@@ -23,5 +24,6 @@
|
|||||||
"ssdp": [],
|
"ssdp": [],
|
||||||
"usb": [],
|
"usb": [],
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"zeroconf": []
|
"zeroconf": [],
|
||||||
|
"icon": "icons/icon.png"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,8 @@
|
|||||||
"temperature": "Response creativity (0-2, lower = more focused and consistent)",
|
"temperature": "Response creativity (0-2, lower = more focused and consistent)",
|
||||||
"max_tokens": "Maximum response length (1-4096 tokens)",
|
"max_tokens": "Maximum response length (1-4096 tokens)",
|
||||||
"api_endpoint": "Custom API endpoint URL (optional)",
|
"api_endpoint": "Custom API endpoint URL (optional)",
|
||||||
"request_interval": "Minimum time between requests in seconds (min: 0.1)"
|
"request_interval": "Minimum time between requests in seconds (min: 0.1)",
|
||||||
|
"name": "Integration name"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user