Release v2.0.0

This commit is contained in:
SMKRV
2024-11-25 00:05:15 +03:00
parent a4925fc943
commit b00f600cd9
3 changed files with 92 additions and 54 deletions
+3
View File
@@ -245,6 +245,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
is_anthropic=is_anthropic, is_anthropic=is_anthropic,
) )
coordinator.data = coordinator._initial_state.copy()
_LOGGER.debug(f"Initial state set for coordinator {instance_name}")
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
+69 -33
View File
@@ -8,6 +8,7 @@ from typing import Any, Dict, List, Optional
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from homeassistant.exceptions import HomeAssistantError
from .const import ( from .const import (
DOMAIN, DOMAIN,
@@ -21,8 +22,6 @@ from .const import (
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
class HATextAICoordinator(DataUpdateCoordinator): class HATextAICoordinator(DataUpdateCoordinator):
"""Class to manage fetching data from the API."""
def __init__( def __init__(
self, self,
hass: HomeAssistant, hass: HomeAssistant,
@@ -36,16 +35,7 @@ class HATextAICoordinator(DataUpdateCoordinator):
is_anthropic: bool = False, is_anthropic: bool = False,
) -> None: ) -> None:
"""Initialize coordinator.""" """Initialize coordinator."""
update_interval_td = timedelta(seconds=update_interval)
self.instance_name = instance_name self.instance_name = instance_name
super().__init__(
hass,
_LOGGER,
name=f"HA Text AI {instance_name}",
update_interval=update_interval_td,
)
self.hass = hass self.hass = hass
self.client = client self.client = client
self.model = model self.model = model
@@ -54,13 +44,10 @@ class HATextAICoordinator(DataUpdateCoordinator):
self.max_history_size = max_history_size self.max_history_size = max_history_size
self.is_anthropic = is_anthropic self.is_anthropic = is_anthropic
# Register instance # Initialize with default state
self.hass.data.setdefault(DOMAIN, {}) self._initial_state = {
self.hass.data[DOMAIN][instance_name] = self "state": STATE_READY,
"metrics": {
self._system_prompt = None
self._conversation_history = []
self._performance_metrics = {
"total_tokens": 0, "total_tokens": 0,
"prompt_tokens": 0, "prompt_tokens": 0,
"completion_tokens": 0, "completion_tokens": 0,
@@ -70,30 +57,58 @@ class HATextAICoordinator(DataUpdateCoordinator):
"average_latency": 0, "average_latency": 0,
"max_latency": 0, "max_latency": 0,
"min_latency": float('inf'), "min_latency": float('inf'),
} },
"last_response": {
self._is_processing = False
self._is_rate_limited = False
self._is_maintenance = False
self.endpoint_status = "ready"
# Initialize last_response
self.last_response = {
"timestamp": dt_util.utcnow().isoformat(), "timestamp": dt_util.utcnow().isoformat(),
"question": "", "question": "",
"response": "", "response": "",
"model": model, "model": model,
"instance": instance_name, "instance": instance_name,
"error": None "error": None
},
"is_processing": False,
"is_rate_limited": False,
"is_maintenance": False,
"endpoint_status": "ready",
"uptime": 0,
"system_prompt": None,
"history_size": 0,
"conversation_history": [],
} }
update_interval_td = timedelta(seconds=update_interval)
super().__init__(
hass,
_LOGGER,
name=f"HA Text AI {instance_name}",
update_interval=update_interval_td,
)
# Register instance
self.hass.data.setdefault(DOMAIN, {})
self.hass.data[DOMAIN][instance_name] = self
self._system_prompt = None
self._conversation_history = []
self._performance_metrics = self._initial_state["metrics"].copy()
self._is_processing = False
self._is_rate_limited = False
self._is_maintenance = False
self.endpoint_status = "ready"
self.last_response = self._initial_state["last_response"].copy()
self._start_time = dt_util.utcnow() self._start_time = dt_util.utcnow()
_LOGGER.info(f"Initialized HA Text AI coordinator with instance: {instance_name}") _LOGGER.info(f"Initialized HA Text AI coordinator with instance: {instance_name}")
async def _async_update_data(self) -> Dict[str, Any]: async def _async_update_data(self) -> Dict[str, Any]:
"""Update data via library.""" """Update data via library."""
return { try:
"state": self._get_current_state(), current_state = self._get_current_state()
_LOGGER.debug(f"Updating data for {self.instance_name}, current state: {current_state}")
data = {
"state": current_state,
"metrics": self._performance_metrics, "metrics": self._performance_metrics,
"last_response": self.last_response, "last_response": self.last_response,
"is_processing": self._is_processing, "is_processing": self._is_processing,
@@ -106,6 +121,31 @@ class HATextAICoordinator(DataUpdateCoordinator):
"conversation_history": self._conversation_history, "conversation_history": self._conversation_history,
} }
# Validate data
if not isinstance(data, dict):
raise ValueError("Invalid data format")
_LOGGER.debug(f"Updated data for {self.instance_name}: {data}")
return data
except Exception as err:
_LOGGER.error(f"Error updating data for {self.instance_name}: {err}")
return self._initial_state
async def async_update_ha_state(self) -> None:
"""Update Home Assistant state."""
try:
_LOGGER.debug(f"Requesting state update for {self.instance_name}")
await self.async_request_refresh()
# Force update of all entities
for entity_id in self.hass.states.async_entity_ids():
if entity_id.startswith(f"sensor.ha_text_ai_{self.instance_name}"):
self.hass.states.async_set(entity_id, self._get_current_state())
except Exception as err:
_LOGGER.error(f"Error updating HA state for {self.instance_name}: {err}")
def _get_current_state(self) -> str: def _get_current_state(self) -> str:
"""Get current state based on internal flags.""" """Get current state based on internal flags."""
if self._is_processing: if self._is_processing:
@@ -315,7 +355,3 @@ class HATextAICoordinator(DataUpdateCoordinator):
"""Set system prompt.""" """Set system prompt."""
self._system_prompt = prompt self._system_prompt = prompt
await self.async_update_ha_state() await self.async_update_ha_state()
async def async_update_ha_state(self) -> None:
"""Update Home Assistant state."""
await self.async_request_refresh()
+2 -3
View File
@@ -9,9 +9,8 @@ ask_question:
description: Name of the HA Text AI instance to use description: Name of the HA Text AI instance to use
required: true required: true
selector: selector:
entity: select:
integration: ha_text_ai options: "{{ states.sensor | selectattr('entity_id', 'search', 'ha_text_ai_') | map(attribute='entity_id') | list }}"
domain: sensor
question: question:
name: Question name: Question