Release v2.0.0

This commit is contained in:
SMKRV
2024-11-24 17:55:39 +03:00
parent b94d859849
commit d03078cfd4
2 changed files with 63 additions and 50 deletions
+11 -3
View File
@@ -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."""
+21 -16
View File
@@ -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,7 +133,9 @@ 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:
return attributes
data = self.coordinator.data data = self.coordinator.data
# Основные атрибуты # Основные атрибуты
@@ -151,12 +153,13 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
ATTR_UPTIME, ATTR_UPTIME,
ATTR_SYSTEM_PROMPT, ATTR_SYSTEM_PROMPT,
]: ]:
if attr in data: value = data.get(attr)
attributes[attr] = data[attr] if value is not None:
attributes[attr] = value
# Метрики # Метрики
if "metrics" in data: metrics = data.get("metrics", {})
metrics = data["metrics"] if isinstance(metrics, dict):
for metric in [ for metric in [
METRIC_TOTAL_TOKENS, METRIC_TOTAL_TOKENS,
METRIC_PROMPT_TOKENS, METRIC_PROMPT_TOKENS,
@@ -167,12 +170,13 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
METRIC_MAX_LATENCY, METRIC_MAX_LATENCY,
METRIC_MIN_LATENCY, METRIC_MIN_LATENCY,
]: ]:
if metric in metrics: value = metrics.get(metric)
attributes[metric] = metrics[metric] if value is not None:
attributes[metric] = value
# Последний ответ # Последний ответ
if "last_response" in data: last_response = data.get("last_response", {})
last_response = data["last_response"] if isinstance(last_response, dict):
attributes[ATTR_RESPONSE] = last_response.get("response", "") attributes[ATTR_RESPONSE] = last_response.get("response", "")
attributes[ATTR_QUESTION] = last_response.get("question", "") attributes[ATTR_QUESTION] = last_response.get("question", "")
@@ -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")