This commit is contained in:
Mateusz Gruszczyński
2026-09-15 18:47:20 +02:00
parent 7ffaba6c7a
commit 3d69e74319
37 changed files with 426 additions and 318 deletions
+46 -35
View File
@@ -130,13 +130,17 @@ fn sensor_history_with_fallback(
let mut existing: std::collections::HashSet<String> =
values.iter().map(|row| row.entity_id.clone()).collect();
for zone in state.db.list_zones()? {
let Some(entity_id) = zone
if !matches!(zone.sensor_source.as_str(), "home_assistant" | "combined") {
continue;
}
let entity_id = zone
.ha_entity_id
.as_deref()
.filter(|value| !value.trim().is_empty())
else {
.map(str::trim)
.unwrap_or("");
if entity_id.is_empty() {
continue;
};
}
if existing.contains(entity_id) {
continue;
}
@@ -162,31 +166,37 @@ fn sensor_history_with_fallback(
existing.insert(entity_id.to_string());
}
}
let outdoor_entity = outdoor_entity.trim();
if !outdoor_entity.is_empty() && !existing.contains(outdoor_entity) {
for zone in state.db.list_zones()? {
let rows =
state
.db
.list_zone_history(Some(&zone.id), since.clone(), bucket_seconds, limit)?;
let mut added = false;
for row in rows {
if let Some(temperature) = row.outdoor_temperature {
values.push(HaReading {
id: row.id,
entity_id: outdoor_entity.to_string(),
zone_id: None,
kind: "outdoor".into(),
timestamp: row.timestamp,
temperature,
});
added = true;
}
}
if added {
break;
let global_outdoor_entity = outdoor_entity.trim();
for zone in state.db.list_zones()? {
let zone_override = zone
.ha_outdoor_entity_id
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty());
let entity_id = zone_override.unwrap_or(global_outdoor_entity);
if entity_id.is_empty() || existing.contains(entity_id) {
continue;
}
let rows = state
.db
.list_zone_history(Some(&zone.id), since.clone(), bucket_seconds, limit)?;
let mut added = false;
for row in rows {
if let Some(temperature) = row.outdoor_temperature {
values.push(HaReading {
id: row.id,
entity_id: entity_id.to_string(),
zone_id: zone_override.map(|_| zone.id.clone()),
kind: "outdoor".into(),
timestamp: row.timestamp,
temperature,
});
added = true;
}
}
if added {
existing.insert(entity_id.to_string());
}
}
values.sort_by(|left, right| left.timestamp.cmp(&right.timestamp));
if values.len() > limit as usize {
@@ -332,7 +342,13 @@ async fn combined_sensor_history(
.db
.list_ha_history(entity_id, start, bucket_seconds, limit)?)
} else {
sensor_history_with_fallback(state, start, bucket_seconds, limit, outdoor_entity)
sensor_history_with_fallback(
state,
start,
bucket_seconds,
limit,
outdoor_entity,
)
}
};
if !influx.enabled || since >= cutoff {
@@ -391,13 +407,8 @@ async fn history(
let bucket_seconds = history_bucket_seconds(hours);
let limit = query.limit.unwrap_or(12_000).clamp(1, 20_000);
let scope = query.scope.as_deref().unwrap_or("zones");
let outdoor_entity = state
.settings
.read()
.await
.home_assistant
.outdoor_entity_id
.clone();
let ha_settings = state.settings.read().await.home_assistant.clone();
let outdoor_entity = ha_settings.outdoor_entity_id;
let (device_count, zone_count, ha_count) = state.db.history_counts()?;
match scope {