Release v2.0.0

This commit is contained in:
SMKRV
2024-11-25 02:05:13 +03:00
parent 351a8b18dd
commit 7f46380054
3 changed files with 60 additions and 101 deletions
+13 -46
View File
@@ -1,12 +1,4 @@
# config_flow.py
"""Config flow for HA text AI integration.
This module defines a configuration flow for the HA text AI integration in Home Assistant.
The flow guides the user through the setup process, including the selection of API provider,
configuration of API settings, and validation of the entered data. It ensures that the
configuration entries are unique and establish a valid connection to the AI API service.
"""
"""Config flow for HA text AI integration."""
import logging
from typing import Any, Dict, Optional
@@ -56,10 +48,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self._provider = None
async def async_step_user(self, user_input: Optional[Dict[str, Any]] = None) -> FlowResult:
"""Handle the initial step.
This step asks the user to select an API provider.
"""
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
step_id="user",
@@ -77,11 +66,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return await self.async_step_provider()
async def async_step_provider(self, user_input: Optional[Dict[str, Any]] = None) -> FlowResult:
"""Handle provider configuration step.
This step collects additional configuration details
based on the chosen provider like API key, model, etc.
"""
"""Handle provider configuration step."""
if user_input is None:
default_endpoint = (
DEFAULT_OPENAI_ENDPOINT if self._provider == API_PROVIDER_OPENAI
@@ -113,21 +98,21 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors=self._errors
)
# Проверка уникальности имени
instance_name = user_input[CONF_NAME]
await self._async_validate_name(instance_name)
if self._errors:
return await self.async_step_provider()
# Проверка API подключения
if not await self._async_validate_api(user_input):
return await self.async_step_provider()
# Создание записи конфигурации
return await self._create_entry(user_input)
async def _async_validate_name(self, name: str) -> bool:
"""Validate that the name is unique.
Ensure no existing configuration entry has the same name.
"""
"""Validate that the name is unique."""
for entry in self._async_current_entries():
if entry.data.get(CONF_NAME) == name:
self._errors["name"] = "name_exists"
@@ -135,10 +120,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return True
async def _async_validate_api(self, user_input: Dict[str, Any]) -> bool:
"""Validate API connection.
Test the API connection to ensure the provided API details are correct.
"""
"""Validate API connection."""
try:
session = async_get_clientsession(self.hass)
headers = self._get_api_headers(user_input)
@@ -164,10 +146,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return False
def _get_api_headers(self, user_input: Dict[str, Any]) -> Dict[str, str]:
"""Get API headers based on provider.
Returns appropriate headers for API requests depending on the provider.
"""
"""Get API headers based on provider."""
api_key = user_input[CONF_API_KEY]
if self._provider == API_PROVIDER_ANTHROPIC:
@@ -182,10 +161,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
}
async def _create_entry(self, user_input: Dict[str, Any]) -> FlowResult:
"""Create the config entry.
Constructs and adds the configuration entry to Home Assistant.
"""
"""Create the config entry."""
instance_name = user_input[CONF_NAME]
unique_id = f"{DOMAIN}_{instance_name}_{self._provider}".lower().replace(" ", "_")
@@ -202,10 +178,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
@staticmethod
@callback
def async_get_options_flow(config_entry: config_entries.ConfigEntry) -> config_entries.OptionsFlow:
"""Get the options flow for this handler.
Provides access to configuration options after initial setup.
"""
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)
@@ -213,17 +186,11 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handle options flow."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow.
Prepare for managing configurable options of the integration.
"""
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input: Optional[Dict[str, Any]] = None) -> FlowResult:
"""Manage the options.
Allows modification of configurable parameters post initial setup.
"""
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)