mirror of
https://github.com/smkrv/ha-text-ai.git
synced 2026-07-28 10:04:00 +08:00
Release v2.0.0
This commit is contained in:
@@ -19,7 +19,7 @@ class HATextAICoordinator(DataUpdateCoordinator):
|
|||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
client: Any,
|
client: Any,
|
||||||
model: str,
|
model: str,
|
||||||
update_interval: int, # Изменен тип на int
|
update_interval: int,
|
||||||
instance_name: str,
|
instance_name: str,
|
||||||
max_tokens: int = 1000,
|
max_tokens: int = 1000,
|
||||||
temperature: float = 0.7,
|
temperature: float = 0.7,
|
||||||
@@ -27,14 +27,13 @@ class HATextAICoordinator(DataUpdateCoordinator):
|
|||||||
is_anthropic: bool = False,
|
is_anthropic: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize coordinator."""
|
"""Initialize coordinator."""
|
||||||
# Преобразуем update_interval в timedelta
|
|
||||||
update_interval_td = timedelta(seconds=update_interval)
|
update_interval_td = timedelta(seconds=update_interval)
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
name=f"HA Text AI {instance_name}",
|
name=f"HA Text AI {instance_name}",
|
||||||
update_interval=update_interval_td, # Передаем timedelta
|
update_interval=update_interval_td,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.instance_name = instance_name
|
self.instance_name = instance_name
|
||||||
@@ -68,6 +67,15 @@ class HATextAICoordinator(DataUpdateCoordinator):
|
|||||||
self.last_response = None
|
self.last_response = None
|
||||||
self.endpoint_status = "initialized"
|
self.endpoint_status = "initialized"
|
||||||
|
|
||||||
|
self.last_response = {
|
||||||
|
"timestamp": dt_util.utcnow().isoformat(),
|
||||||
|
"question": "",
|
||||||
|
"response": "",
|
||||||
|
"model": model,
|
||||||
|
"instance": instance_name,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_processing(self) -> bool:
|
def is_processing(self) -> bool:
|
||||||
"""Return True if currently processing a request."""
|
"""Return True if currently processing a request."""
|
||||||
|
|||||||
@@ -106,12 +106,12 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
|
|||||||
@property
|
@property
|
||||||
def native_value(self) -> StateType:
|
def native_value(self) -> StateType:
|
||||||
"""Return the native value of the sensor."""
|
"""Return the native value of the sensor."""
|
||||||
if self.coordinator.data is None:
|
if not self.coordinator.data:
|
||||||
return STATE_DISCONNECTED
|
return STATE_DISCONNECTED
|
||||||
|
|
||||||
state = self.coordinator.data.get("state", STATE_READY)
|
status = self.coordinator.data.get("status", STATE_READY)
|
||||||
self._current_state = state
|
self._current_state = status
|
||||||
return state
|
return status
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self) -> str:
|
def icon(self) -> str:
|
||||||
@@ -133,48 +133,52 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
|
|||||||
ATTR_LAST_ERROR: self._last_error,
|
ATTR_LAST_ERROR: self._last_error,
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.coordinator.data:
|
if not self.coordinator.data:
|
||||||
data = self.coordinator.data
|
return attributes
|
||||||
|
|
||||||
# Основные атрибуты
|
data = self.coordinator.data
|
||||||
for attr in [
|
|
||||||
ATTR_TOTAL_RESPONSES,
|
# Основные атрибуты
|
||||||
ATTR_AVG_RESPONSE_TIME,
|
for attr in [
|
||||||
ATTR_LAST_REQUEST_TIME,
|
ATTR_TOTAL_RESPONSES,
|
||||||
ATTR_IS_PROCESSING,
|
ATTR_AVG_RESPONSE_TIME,
|
||||||
ATTR_IS_RATE_LIMITED,
|
ATTR_LAST_REQUEST_TIME,
|
||||||
ATTR_IS_MAINTENANCE,
|
ATTR_IS_PROCESSING,
|
||||||
ATTR_API_VERSION,
|
ATTR_IS_RATE_LIMITED,
|
||||||
ATTR_ENDPOINT_STATUS,
|
ATTR_IS_MAINTENANCE,
|
||||||
ATTR_PERFORMANCE_METRICS,
|
ATTR_API_VERSION,
|
||||||
ATTR_HISTORY_SIZE,
|
ATTR_ENDPOINT_STATUS,
|
||||||
ATTR_UPTIME,
|
ATTR_PERFORMANCE_METRICS,
|
||||||
ATTR_SYSTEM_PROMPT,
|
ATTR_HISTORY_SIZE,
|
||||||
|
ATTR_UPTIME,
|
||||||
|
ATTR_SYSTEM_PROMPT,
|
||||||
|
]:
|
||||||
|
value = data.get(attr)
|
||||||
|
if value is not None:
|
||||||
|
attributes[attr] = value
|
||||||
|
|
||||||
|
# Метрики
|
||||||
|
metrics = data.get("metrics", {})
|
||||||
|
if isinstance(metrics, dict):
|
||||||
|
for metric in [
|
||||||
|
METRIC_TOTAL_TOKENS,
|
||||||
|
METRIC_PROMPT_TOKENS,
|
||||||
|
METRIC_COMPLETION_TOKENS,
|
||||||
|
METRIC_SUCCESSFUL_REQUESTS,
|
||||||
|
METRIC_FAILED_REQUESTS,
|
||||||
|
METRIC_AVERAGE_LATENCY,
|
||||||
|
METRIC_MAX_LATENCY,
|
||||||
|
METRIC_MIN_LATENCY,
|
||||||
]:
|
]:
|
||||||
if attr in data:
|
value = metrics.get(metric)
|
||||||
attributes[attr] = data[attr]
|
if value is not None:
|
||||||
|
attributes[metric] = value
|
||||||
|
|
||||||
# Метрики
|
# Последний ответ
|
||||||
if "metrics" in data:
|
last_response = data.get("last_response", {})
|
||||||
metrics = data["metrics"]
|
if isinstance(last_response, dict):
|
||||||
for metric in [
|
attributes[ATTR_RESPONSE] = last_response.get("response", "")
|
||||||
METRIC_TOTAL_TOKENS,
|
attributes[ATTR_QUESTION] = last_response.get("question", "")
|
||||||
METRIC_PROMPT_TOKENS,
|
|
||||||
METRIC_COMPLETION_TOKENS,
|
|
||||||
METRIC_SUCCESSFUL_REQUESTS,
|
|
||||||
METRIC_FAILED_REQUESTS,
|
|
||||||
METRIC_AVERAGE_LATENCY,
|
|
||||||
METRIC_MAX_LATENCY,
|
|
||||||
METRIC_MIN_LATENCY,
|
|
||||||
]:
|
|
||||||
if metric in metrics:
|
|
||||||
attributes[metric] = metrics[metric]
|
|
||||||
|
|
||||||
# Последний ответ
|
|
||||||
if "last_response" in data:
|
|
||||||
last_response = data["last_response"]
|
|
||||||
attributes[ATTR_RESPONSE] = last_response.get("response", "")
|
|
||||||
attributes[ATTR_QUESTION] = last_response.get("question", "")
|
|
||||||
|
|
||||||
return attributes
|
return attributes
|
||||||
|
|
||||||
@@ -185,7 +189,7 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
|
|||||||
|
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Handle updated data from the coordinator."""
|
"""Handle updated data from the coordinator."""
|
||||||
if self.coordinator.data is None:
|
if not self.coordinator.data:
|
||||||
self._current_state = STATE_DISCONNECTED
|
self._current_state = STATE_DISCONNECTED
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
@@ -208,8 +212,9 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
|
|||||||
self._current_state = STATE_READY
|
self._current_state = STATE_READY
|
||||||
|
|
||||||
# Обновляем метрики
|
# Обновляем метрики
|
||||||
if "metrics" in data:
|
metrics = data.get("metrics", {})
|
||||||
self._error_count = data["metrics"].get("total_errors", self._error_count)
|
if isinstance(metrics, dict):
|
||||||
|
self._error_count = metrics.get("total_errors", self._error_count)
|
||||||
|
|
||||||
self._last_update = data.get("last_update")
|
self._last_update = data.get("last_update")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user