This commit is contained in:
Mateusz Gruszczyński
2026-09-02 09:02:56 +02:00
parent 9c6e51fc35
commit 43441d2fb7
16 changed files with 555 additions and 60 deletions
+1
View File
@@ -103,6 +103,7 @@ pub fn router(state: AppState) -> Router {
.route("/api/access-tokens", get(list_access_tokens).post(create_access_token))
.route("/api/access-tokens/:id", axum::routing::delete(delete_access_token))
.route("/api/integrations/home-assistant/test", post(test_home_assistant))
.route("/api/integrations/home-assistant/entity", post(inspect_home_assistant_entity))
.route("/api/integrations/notifications/test", post(test_notifications))
.route_layer(middleware::from_fn_with_state(state.clone(), auth));
+24
View File
@@ -8,6 +8,30 @@ async fn test_home_assistant(State(state): State<AppState>, Json(input): Json<Ha
Ok(Json(json!({"ok": true, "temperature_c": temperature, "entity_id": resolved_entity_id})))
}
#[derive(Debug, Deserialize)]
struct HaEntityRequest { entity_id: String }
async fn inspect_home_assistant_entity(State(state): State<AppState>, Json(input): Json<HaEntityRequest>) -> Result<Json<Value>, AppError> {
let settings = state.settings.read().await.clone();
let entity_id = home_assistant::resolve_entity_id(&settings.home_assistant, Some(input.entity_id.as_str()))
.filter(|value| !value.trim().is_empty())
.ok_or_else(|| AppError::BadRequest("Home Assistant entity_id is required".into()))?;
let payload = home_assistant::read_entity(&state.http, &settings.home_assistant, Some(entity_id.as_str()))
.await.map_err(|e| AppError::Device(e.to_string()))?;
let raw_state = payload.get("state").and_then(Value::as_str).unwrap_or_default().to_string();
let available = !matches!(raw_state.as_str(), "unknown" | "unavailable" | "");
Ok(Json(json!({
"ok": true,
"entity_id": entity_id,
"state": raw_state,
"available": available,
"attributes": payload.get("attributes").cloned().unwrap_or_else(|| json!({})),
"last_changed": payload.get("last_changed").cloned().unwrap_or(Value::Null),
"last_updated": payload.get("last_updated").cloned().unwrap_or(Value::Null),
})))
}
async fn test_notifications(State(state): State<AppState>, Json(mut input): Json<NotificationSettings>) -> Result<Json<Value>, AppError> {
let old = state.settings.read().await.notifications.clone();
if input.pushover_app_token.trim().is_empty() { input.pushover_app_token = old.pushover_app_token; }