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:
@@ -74,7 +74,10 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
|
|||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._config_entry = config_entry
|
self._config_entry = config_entry
|
||||||
self._attr_unique_id = config_entry.entry_id
|
self._attr_unique_id = config_entry.entry_id
|
||||||
|
# Добавьте инициализацию состояния
|
||||||
|
self._current_state = STATE_INITIALIZING
|
||||||
|
|
||||||
|
# Девайс инфо
|
||||||
self._attr_device_info = {
|
self._attr_device_info = {
|
||||||
"identifiers": {(DOMAIN, self._attr_unique_id)},
|
"identifiers": {(DOMAIN, self._attr_unique_id)},
|
||||||
"name": "HA Text AI",
|
"name": "HA Text AI",
|
||||||
@@ -83,17 +86,12 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
|
|||||||
"sw_version": coordinator.api_version,
|
"sw_version": coordinator.api_version,
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return "HA Text AI"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self) -> str:
|
def icon(self) -> str:
|
||||||
"""Return the icon based on the current state."""
|
"""Return the icon based on the current state."""
|
||||||
if self._state == STATE_PROCESSING:
|
if self._current_state == STATE_PROCESSING:
|
||||||
return ENTITY_ICON_PROCESSING
|
return ENTITY_ICON_PROCESSING
|
||||||
elif self._state in [STATE_ERROR, STATE_DISCONNECTED, STATE_RATE_LIMITED]:
|
elif self._current_state in [STATE_ERROR, STATE_DISCONNECTED, STATE_RATE_LIMITED]:
|
||||||
return ENTITY_ICON_ERROR
|
return ENTITY_ICON_ERROR
|
||||||
return ENTITY_ICON
|
return ENTITY_ICON
|
||||||
|
|
||||||
@@ -103,57 +101,41 @@ class HATextAISensor(CoordinatorEntity, SensorEntity):
|
|||||||
last_response = self.coordinator.last_response
|
last_response = self.coordinator.last_response
|
||||||
if last_response and 'timestamp' in last_response:
|
if last_response and 'timestamp' in last_response:
|
||||||
return dt_util.as_local(last_response['timestamp'])
|
return dt_util.as_local(last_response['timestamp'])
|
||||||
return self._state
|
return self._current_state
|
||||||
|
|
||||||
@property
|
def _handle_coordinator_update(self) -> None:
|
||||||
def extra_state_attributes(self) -> Dict[str, Any]:
|
"""Handle updated data from the coordinator."""
|
||||||
"""Return entity specific state attributes."""
|
try:
|
||||||
attributes = {
|
last_response = self.coordinator.last_response
|
||||||
ATTR_TOTAL_RESPONSES: 0,
|
|
||||||
ATTR_MODEL: self.coordinator.model,
|
|
||||||
ATTR_TEMPERATURE: self.coordinator.temperature,
|
|
||||||
ATTR_MAX_TOKENS: self.coordinator.max_tokens,
|
|
||||||
ATTR_SYSTEM_PROMPT: self.coordinator.system_prompt,
|
|
||||||
ATTR_QUEUE_SIZE: self.coordinator._question_queue.qsize(),
|
|
||||||
ATTR_API_STATUS: self._state,
|
|
||||||
ATTR_ERROR_COUNT: self._error_count,
|
|
||||||
ATTR_LAST_ERROR: self._last_error,
|
|
||||||
ATTR_API_VERSION: self.coordinator.api_version,
|
|
||||||
ATTR_ENDPOINT_STATUS: self.coordinator.endpoint_status,
|
|
||||||
ATTR_REQUEST_COUNT: self.coordinator.request_count,
|
|
||||||
ATTR_TOKENS_USED: self.coordinator.tokens_used,
|
|
||||||
}
|
|
||||||
|
|
||||||
last_response = self.coordinator.last_response
|
if last_response:
|
||||||
if last_response:
|
if last_response.get("error"):
|
||||||
attributes.update({
|
self._current_state = STATE_ERROR
|
||||||
ATTR_RESPONSE: last_response.get("response", ""),
|
self._error_count += 1
|
||||||
ATTR_QUESTION: last_response.get("question", ""),
|
elif self.coordinator._is_processing:
|
||||||
ATTR_LAST_UPDATED: last_response.get("timestamp"),
|
self._current_state = STATE_PROCESSING
|
||||||
ATTR_MODEL: last_response.get("model", self.coordinator.model),
|
elif self.coordinator._is_rate_limited:
|
||||||
ATTR_TEMPERATURE: last_response.get("temperature", self.coordinator.temperature),
|
self._current_state = STATE_RATE_LIMITED
|
||||||
ATTR_MAX_TOKENS: last_response.get("max_tokens", self.coordinator.max_tokens),
|
elif self.coordinator._is_maintenance:
|
||||||
ATTR_RESPONSE_TIME: last_response.get("response_time"),
|
self._current_state = STATE_MAINTENANCE
|
||||||
ATTR_TOTAL_RESPONSES: len(self.coordinator._responses)
|
else:
|
||||||
})
|
self._current_state = STATE_READY
|
||||||
|
else:
|
||||||
|
self._current_state = STATE_DISCONNECTED
|
||||||
|
|
||||||
# Обработка ошибок
|
except Exception as err:
|
||||||
if last_response.get("error"):
|
_LOGGER.error("Error handling update: %s", err, exc_info=True)
|
||||||
self._last_error = last_response["error"]
|
self._error_count += 1
|
||||||
self._state = STATE_ERROR
|
self._last_error = str(err)
|
||||||
|
self._current_state = STATE_ERROR
|
||||||
|
|
||||||
return attributes
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
"""Return if entity is available."""
|
|
||||||
return self.coordinator.last_update_success
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""When entity is added to hass."""
|
"""When entity is added to hass."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
self._handle_coordinator_update()
|
self._handle_coordinator_update()
|
||||||
self._state = STATE_READY
|
self._current_state = STATE_READY
|
||||||
|
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Handle updated data from the coordinator."""
|
"""Handle updated data from the coordinator."""
|
||||||
|
|||||||
Reference in New Issue
Block a user