Release v2.0.0

This commit is contained in:
SMKRV
2024-11-22 13:51:23 +03:00
parent 558ff7d141
commit accbc5635e
2 changed files with 37 additions and 79 deletions
+2 -1
View File
@@ -563,7 +563,8 @@ class HATextAICoordinator(DataUpdateCoordinator):
"""Return the last response.""" """Return the last response."""
if not self._responses: if not self._responses:
return None return None
return next(iter(self._responses.values()))
return list(self._responses.values())[-1]
def reset_error_count(self) -> None: def reset_error_count(self) -> None:
"""Reset error counter.""" """Reset error counter."""
+35 -78
View File
@@ -17,7 +17,7 @@ from homeassistant.util import dt as dt_util
from .const import ( from .const import (
DOMAIN, DOMAIN,
CONF_API_PROVIDER, # Новый импорт CONF_API_PROVIDER,
ATTR_QUESTION, ATTR_QUESTION,
ATTR_RESPONSE, ATTR_RESPONSE,
ATTR_LAST_UPDATED, ATTR_LAST_UPDATED,
@@ -103,19 +103,11 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
@property @property
def state(self) -> StateType: def state(self) -> StateType:
"""Return the state of the sensor.""" """Return the state of the sensor."""
if not self.coordinator.data or not self.coordinator.last_update_success: last_response = self.coordinator.last_response
return None if last_response and 'timestamp' in last_response:
return dt_util.as_local(last_response['timestamp'])
try: return None
if self.coordinator.data and isinstance(self.coordinator.data, dict):
last_update = self.coordinator.data.get("last_update")
if isinstance(last_update, datetime):
return dt_util.as_local(last_update)
return last_update
return None
except Exception as err:
_LOGGER.error("Error getting state: %s", err, exc_info=True)
return None
@property @property
def extra_state_attributes(self) -> Dict[str, Any]: def extra_state_attributes(self) -> Dict[str, Any]:
@@ -136,59 +128,25 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
ATTR_TOKENS_USED: self.coordinator.tokens_used, ATTR_TOKENS_USED: self.coordinator.tokens_used,
} }
if not self.coordinator.data: last_response = self.coordinator.last_response
return attributes if last_response:
attributes.update({
ATTR_RESPONSE: last_response.get("response", ""),
ATTR_QUESTION: last_response.get("question", ""),
ATTR_LAST_UPDATED: last_response.get("timestamp"),
ATTR_MODEL: last_response.get("model", self.coordinator.model),
ATTR_TEMPERATURE: last_response.get("temperature", self.coordinator.temperature),
ATTR_MAX_TOKENS: last_response.get("max_tokens", self.coordinator.max_tokens),
ATTR_RESPONSE_TIME: last_response.get("response_time"),
ATTR_TOTAL_RESPONSES: len(self.coordinator._responses)
})
try: # Обработка ошибок
history = list(self.coordinator._responses.items()) if last_response.get("error"):
if history: self._last_error = last_response["error"]
last_question, last_data = history[-1] self._state = STATE_ERROR
if isinstance(last_data, dict): return attributes
last_response = last_data.get("response", "")
last_updated = last_data.get("timestamp") or self.coordinator.data.get("last_update")
response_time = last_data.get("response_time")
model = last_data.get("model", self.coordinator.model)
temperature = last_data.get("temperature", self.coordinator.temperature)
max_tokens = last_data.get("max_tokens", self.coordinator.max_tokens)
error = last_data.get("error")
if error:
self._last_error = error
self._state = STATE_ERROR
else:
last_response = str(last_data)
last_updated = self.coordinator.data.get("last_update")
response_time = None
model = self.coordinator.model
temperature = self.coordinator.temperature
max_tokens = self.coordinator.max_tokens
if isinstance(last_updated, datetime):
last_updated = dt_util.as_local(last_updated)
attributes.update({
ATTR_QUESTION: last_question,
ATTR_RESPONSE: last_response,
ATTR_LAST_UPDATED: last_updated,
ATTR_TOTAL_RESPONSES: len(history),
ATTR_MODEL: model,
ATTR_TEMPERATURE: temperature,
ATTR_MAX_TOKENS: max_tokens,
})
if response_time is not None:
attributes[ATTR_RESPONSE_TIME] = response_time
return attributes
except Exception as err:
_LOGGER.error("Error getting attributes: %s", err, exc_info=True)
self._error_count += 1
self._last_error = str(err)
self._state = STATE_ERROR
return attributes
@property @property
def available(self) -> bool: def available(self) -> bool:
@@ -204,24 +162,23 @@ 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."""
try: try:
if self.coordinator.data: last_response = self.coordinator.last_response
if self.coordinator._is_ready:
if self.coordinator._is_processing: if last_response:
self._state = STATE_PROCESSING if last_response.get("error"):
elif self.coordinator._is_rate_limited: self._state = STATE_ERROR
self._state = STATE_RATE_LIMITED self._error_count += 1
elif self.coordinator._is_maintenance: elif self.coordinator._is_processing:
self._state = STATE_MAINTENANCE self._state = STATE_PROCESSING
else: elif self.coordinator._is_rate_limited:
self._state = STATE_READY self._state = STATE_RATE_LIMITED
elif self.coordinator._is_maintenance:
self._state = STATE_MAINTENANCE
else: else:
self._state = STATE_DISCONNECTED self._state = STATE_READY
else: else:
self._state = STATE_DISCONNECTED self._state = STATE_DISCONNECTED
if self._state == STATE_ERROR:
self._error_count += 1
except Exception as err: except Exception as err:
_LOGGER.error("Error handling update: %s", err, exc_info=True) _LOGGER.error("Error handling update: %s", err, exc_info=True)
self._error_count += 1 self._error_count += 1