v0.14.0
This commit is contained in:
@@ -95,3 +95,36 @@ async fn test_notifications(
|
||||
.map_err(AppError::Device)?;
|
||||
Ok(Json(json!({"ok": true})))
|
||||
}
|
||||
|
||||
async fn list_home_assistant_energy_sensors(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Value>, AppError> {
|
||||
let settings = state.settings.read().await.home_assistant.clone();
|
||||
let entities = home_assistant::list_entities(&state.http, &settings)
|
||||
.await
|
||||
.map_err(|error| AppError::Device(error.to_string()))?;
|
||||
let sensors = entities
|
||||
.into_iter()
|
||||
.filter_map(|entity| {
|
||||
let attributes = entity.get("attributes")?.as_object()?;
|
||||
let device_class = attributes.get("device_class")?.as_str()?;
|
||||
let state_class = attributes.get("state_class")?.as_str()?;
|
||||
let unit = attributes.get("unit_of_measurement")?.as_str()?;
|
||||
if device_class != "energy"
|
||||
|| !matches!(state_class, "total" | "total_increasing")
|
||||
|| !matches!(unit.to_ascii_lowercase().as_str(), "wh" | "kwh")
|
||||
{
|
||||
return None;
|
||||
}
|
||||
Some(json!({
|
||||
"entity_id": entity.get("entity_id").and_then(Value::as_str).unwrap_or_default(),
|
||||
"name": attributes.get("friendly_name").and_then(Value::as_str).unwrap_or_default(),
|
||||
"state": entity.get("state").and_then(Value::as_str).unwrap_or_default(),
|
||||
"unit": unit,
|
||||
"device_class": device_class,
|
||||
"state_class": state_class,
|
||||
}))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
Ok(Json(json!({"sensors": sensors})))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user