Files
ha-text-ai/custom_components/ha_text_ai/__init__.py
T

376 lines
13 KiB
Python
Raw Normal View History

2024-11-19 14:35:45 +03:00
"""The HA Text AI integration."""
2024-11-18 00:43:28 +03:00
import logging
2024-11-19 18:53:51 +03:00
from typing import Any, Dict, Optional
2024-11-20 01:02:27 +03:00
import asyncio
import voluptuous as vol
import json
from datetime import datetime
2024-11-14 18:32:41 +03:00
2024-11-18 00:43:28 +03:00
from homeassistant.config_entries import ConfigEntry
2024-11-19 12:45:26 +03:00
from homeassistant.const import CONF_API_KEY
2024-11-19 18:53:51 +03:00
from homeassistant.core import HomeAssistant, ServiceCall, callback
2024-11-20 01:02:27 +03:00
from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError
2024-11-19 14:35:45 +03:00
from homeassistant.helpers import aiohttp_client
2024-11-19 16:25:44 +03:00
from homeassistant.helpers import config_validation as cv
2024-11-20 01:02:27 +03:00
from async_timeout import timeout
2024-11-14 18:32:41 +03:00
2024-11-19 18:53:51 +03:00
from .coordinator import HATextAICoordinator
2024-11-19 16:25:44 +03:00
from .const import (
DOMAIN,
PLATFORMS,
CONF_MODEL,
CONF_TEMPERATURE,
CONF_MAX_TOKENS,
CONF_API_ENDPOINT,
CONF_REQUEST_INTERVAL,
DEFAULT_MODEL,
DEFAULT_TEMPERATURE,
DEFAULT_MAX_TOKENS,
DEFAULT_API_ENDPOINT,
DEFAULT_REQUEST_INTERVAL,
2024-11-20 01:02:27 +03:00
API_VERSION,
API_MODELS_PATH,
API_CHAT_PATH,
API_TIMEOUT,
API_RETRY_COUNT,
API_BACKOFF_FACTOR,
LOGGER_NAME,
STATE_ERROR,
STATE_READY,
STATE_PROCESSING,
STATE_RATE_LIMITED,
STATE_MAINTENANCE,
STATE_DISCONNECTED,
STATE_RETRYING,
STATE_QUEUED,
STATE_UPDATING,
SUPPORTED_MODELS,
EVENT_RESPONSE_RECEIVED,
EVENT_ERROR_OCCURRED,
EVENT_STATE_CHANGED,
2024-11-19 16:25:44 +03:00
)
2024-11-14 18:32:41 +03:00
2024-11-20 01:02:27 +03:00
_LOGGER = logging.getLogger(LOGGER_NAME)
2024-11-14 18:32:41 +03:00
2024-11-19 16:25:44 +03:00
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
2024-11-20 01:02:27 +03:00
# Service validation schemas
SERVICE_SCHEMA_ASK_QUESTION = vol.Schema({
vol.Required("question"): cv.string,
vol.Optional("system_prompt"): cv.string,
vol.Optional("model"): vol.In(SUPPORTED_MODELS),
vol.Optional("temperature"): vol.All(
vol.Coerce(float), vol.Range(min=0, max=2)
),
vol.Optional("max_tokens"): vol.All(
vol.Coerce(int), vol.Range(min=1, max=4096)
),
vol.Optional("priority"): vol.Boolean,
})
SERVICE_SCHEMA_GET_HISTORY = vol.Schema({
vol.Optional("limit", default=10): vol.All(
vol.Coerce(int), vol.Range(min=1, max=100)
),
vol.Optional("filter_model"): vol.In(SUPPORTED_MODELS),
vol.Optional("start_date"): cv.datetime,
vol.Optional("include_metadata"): vol.Boolean,
})
SERVICE_SCHEMA_SET_SYSTEM_PROMPT = vol.Schema({
vol.Required("prompt"): cv.string,
})
async def async_check_api(session, endpoint: str, headers: dict, is_anthropic: bool = False) -> bool:
"""Check API availability for different providers."""
try:
if is_anthropic:
check_url = f"{endpoint}/v1/models"
else:
check_url = f"{endpoint}/{API_VERSION}/{API_MODELS_PATH}"
async with timeout(API_TIMEOUT):
async with session.get(check_url, headers=headers) as response:
if response.status == 200:
return True
elif response.status == 401:
raise ConfigEntryNotReady("Invalid API key")
elif response.status == 429:
_LOGGER.warning("Rate limit exceeded during API check")
return False
else:
_LOGGER.error("API check failed with status: %d", response.status)
return False
except Exception as ex:
_LOGGER.error("API check error: %s", str(ex))
return False
2024-11-18 11:35:23 +03:00
async def async_setup(hass: HomeAssistant, config: dict[str, Any]) -> bool:
2024-11-19 14:35:45 +03:00
"""Set up the HA Text AI component."""
2024-11-20 01:02:27 +03:00
hass.data.setdefault(DOMAIN, {
"coordinators": {},
"metrics": {
"total_requests": 0,
"total_tokens": 0,
"errors": {},
"model_usage": {},
}
})
2024-11-18 00:43:28 +03:00
return True
2024-11-18 11:35:23 +03:00
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2024-11-19 14:35:45 +03:00
"""Set up HA Text AI from a config entry."""
2024-11-18 11:35:23 +03:00
try:
2024-11-19 14:35:45 +03:00
session = aiohttp_client.async_get_clientsession(hass)
2024-11-20 01:02:27 +03:00
# Determine API type based on model
model = entry.data.get(CONF_MODEL, DEFAULT_MODEL)
is_anthropic = any(model.startswith("claude") for model in SUPPORTED_MODELS)
api_key = entry.data[CONF_API_KEY]
endpoint = entry.data.get(CONF_API_ENDPOINT, DEFAULT_API_ENDPOINT).rstrip('/')
# Configure headers based on API type
headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}
if is_anthropic:
headers["x-api-key"] = api_key
headers["anthropic-version"] = "2023-06-01"
else:
headers["Authorization"] = f"Bearer {api_key}"
# Check API with retries
for attempt in range(API_RETRY_COUNT):
if await async_check_api(session, endpoint, headers, is_anthropic):
break
if attempt < API_RETRY_COUNT - 1:
delay = API_BACKOFF_FACTOR * (2 ** attempt)
await asyncio.sleep(delay)
else:
raise ConfigEntryNotReady("Failed to connect to API")
# Create and initialize coordinator
2024-11-18 11:35:23 +03:00
coordinator = HATextAICoordinator(
hass,
2024-11-20 01:02:27 +03:00
api_key=api_key,
endpoint=endpoint,
model=model,
2024-11-19 16:25:44 +03:00
temperature=entry.data.get(CONF_TEMPERATURE, DEFAULT_TEMPERATURE),
max_tokens=entry.data.get(CONF_MAX_TOKENS, DEFAULT_MAX_TOKENS),
request_interval=entry.data.get(CONF_REQUEST_INTERVAL, DEFAULT_REQUEST_INTERVAL),
2024-11-19 14:35:45 +03:00
session=session,
2024-11-20 01:02:27 +03:00
is_anthropic=is_anthropic
2024-11-18 11:35:23 +03:00
)
2024-11-20 01:02:27 +03:00
# Initialize coordinator
await coordinator.async_config_entry_first_refresh()
2024-11-19 14:35:45 +03:00
2024-11-20 01:02:27 +03:00
# Check coordinator status
if coordinator.endpoint_status == "auth_error":
raise ConfigEntryNotReady("Authentication failed")
elif coordinator.endpoint_status == "rate_limited":
_LOGGER.warning("API rate limited during setup")
elif coordinator.endpoint_status == "maintenance":
raise ConfigEntryNotReady("API is in maintenance mode")
elif coordinator.endpoint_status == "error":
raise ConfigEntryNotReady("API error during setup")
elif not coordinator.last_update_success:
raise ConfigEntryNotReady("Failed to initialize coordinator")
2024-11-19 14:35:45 +03:00
2024-11-18 11:35:23 +03:00
hass.data[DOMAIN][entry.entry_id] = coordinator
2024-11-19 18:53:51 +03:00
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
2024-11-20 01:02:27 +03:00
# Register event handlers
@callback
def handle_state_change(event):
"""Handle state changes."""
if event.data.get("entity_id").startswith(f"{DOMAIN}."):
_LOGGER.debug("State changed: %s", event.data)
hass.bus.async_listen(EVENT_STATE_CHANGED, handle_state_change)
# Register services
2024-11-19 18:53:51 +03:00
async def async_ask_question(call: ServiceCall) -> None:
"""Handle the ask_question service call."""
question = call.data.get("question", "")
if not question:
_LOGGER.error("No question provided in service call")
return
request_params = {}
2024-11-20 01:02:27 +03:00
for param in ["system_prompt", "model", "temperature", "max_tokens"]:
if param in call.data:
request_params[param] = call.data[param]
2024-11-19 18:53:51 +03:00
try:
await coordinator.async_ask_question(question, **request_params)
except Exception as err:
_LOGGER.error("Error asking question: %s", str(err))
async def async_clear_history(call: ServiceCall) -> None:
"""Handle the clear_history service call."""
try:
coordinator._responses.clear()
await coordinator.async_refresh()
_LOGGER.info("History cleared successfully")
except Exception as err:
_LOGGER.error("Error clearing history: %s", str(err))
async def async_get_history(call: ServiceCall) -> dict:
"""Handle the get_history service call."""
try:
2024-11-20 01:02:27 +03:00
limit = min(int(call.data.get("limit", 10)), 100)
filter_model = str(call.data.get("filter_model", ""))
start_date = call.data.get("start_date")
include_metadata = call.data.get("include_metadata", False)
2024-11-19 18:53:51 +03:00
responses = coordinator._responses
2024-11-20 01:02:27 +03:00
metrics = {
"total_requests": coordinator.request_count,
"total_tokens": coordinator.tokens_used,
"api_version": coordinator.api_version,
"endpoint_status": coordinator.endpoint_status,
"error_count": coordinator.error_count
}
filtered_responses = responses.copy()
2024-11-19 18:53:51 +03:00
if filter_model:
filtered_responses = {
2024-11-20 01:02:27 +03:00
k: v for k, v in filtered_responses.items()
2024-11-19 18:53:51 +03:00
if v.get("model") == filter_model
}
2024-11-20 01:02:27 +03:00
if start_date:
filtered_responses = {
k: v for k, v in filtered_responses.items()
if v.get("timestamp") >= start_date
}
if not include_metadata:
filtered_responses = {
k: {
"question": v["question"],
"response": v["response"],
"timestamp": v["timestamp"]
} for k, v in filtered_responses.items()
}
2024-11-19 18:53:51 +03:00
sorted_responses = dict(
sorted(
filtered_responses.items(),
key=lambda x: x[1]["timestamp"],
reverse=True
)[:limit]
)
2024-11-20 01:02:27 +03:00
return {
"metrics": metrics,
"responses": sorted_responses
}
2024-11-19 18:53:51 +03:00
except Exception as err:
_LOGGER.error("Error getting history: %s", str(err))
return {}
async def async_set_system_prompt(call: ServiceCall) -> None:
"""Handle the set_system_prompt service call."""
2024-11-20 01:02:27 +03:00
try:
prompt = str(call.data.get("prompt", "")).strip()
if prompt:
2024-11-19 18:53:51 +03:00
coordinator.system_prompt = prompt
_LOGGER.info("System prompt updated successfully")
2024-11-20 01:02:27 +03:00
else:
_LOGGER.error("Empty prompt provided")
except Exception as err:
_LOGGER.error("Error setting system prompt: %s", str(err))
2024-11-19 18:53:51 +03:00
2024-11-20 01:02:27 +03:00
# Register services with validation
2024-11-19 18:53:51 +03:00
hass.services.async_register(
DOMAIN,
"ask_question",
2024-11-20 01:02:27 +03:00
async_ask_question,
schema=SERVICE_SCHEMA_ASK_QUESTION
2024-11-19 18:53:51 +03:00
)
hass.services.async_register(
DOMAIN,
"clear_history",
async_clear_history
)
hass.services.async_register(
DOMAIN,
"get_history",
2024-11-20 01:02:27 +03:00
async_get_history,
schema=SERVICE_SCHEMA_GET_HISTORY
2024-11-19 18:53:51 +03:00
)
hass.services.async_register(
DOMAIN,
"set_system_prompt",
2024-11-20 01:02:27 +03:00
async_set_system_prompt,
schema=SERVICE_SCHEMA_SET_SYSTEM_PROMPT
2024-11-19 18:53:51 +03:00
)
2024-11-19 14:35:45 +03:00
_LOGGER.info(
"Successfully set up HA Text AI with model: %s",
2024-11-19 16:25:44 +03:00
entry.data.get(CONF_MODEL, DEFAULT_MODEL)
2024-11-19 14:35:45 +03:00
)
return True
2024-11-18 11:35:23 +03:00
except Exception as ex:
2024-11-20 01:02:27 +03:00
_LOGGER.exception("Setup error: %s", str(ex))
2024-11-19 18:53:51 +03:00
raise ConfigEntryNotReady from ex
2024-11-18 11:35:23 +03:00
2024-11-18 00:43:28 +03:00
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
2024-11-19 14:35:45 +03:00
try:
2024-11-20 01:02:27 +03:00
coordinator = hass.data[DOMAIN].get(entry.entry_id)
if coordinator:
# Clear queue and history
coordinator._responses.clear()
while not coordinator._question_queue.empty():
try:
coordinator._question_queue.get_nowait()
coordinator._question_queue.task_done()
except Exception:
pass
2024-11-19 14:00:33 +03:00
2024-11-20 01:02:27 +03:00
# Close connection
await coordinator.async_shutdown()
# Remove services
for service in ["ask_question", "clear_history", "get_history", "set_system_prompt"]:
2024-11-19 18:53:51 +03:00
hass.services.async_remove(DOMAIN, service)
2024-11-19 14:35:45 +03:00
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
2024-11-20 01:02:27 +03:00
hass.data[DOMAIN].pop(entry.entry_id)
2024-11-18 00:43:28 +03:00
2024-11-19 14:35:45 +03:00
return unload_ok
2024-11-18 11:35:23 +03:00
2024-11-19 14:35:45 +03:00
except Exception as ex:
_LOGGER.exception("Error unloading entry: %s", str(ex))
return False
2024-11-20 01:02:27 +03:00
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrate old entry."""
_LOGGER.debug("Migrating from version %s", entry.version)
if entry.version == 1:
new = {**entry.data}
# Migrate settings
if CONF_MODEL in new and new[CONF_MODEL] not in SUPPORTED_MODELS:
new[CONF_MODEL] = DEFAULT_MODEL
entry.version = 2
hass.config_entries.async_update_entry(entry, data=new)
return True