v0.8.14
This commit is contained in:
@@ -0,0 +1,319 @@
|
||||
pub fn refresh_control_ownership(zone: &mut Zone, house_power_enabled: bool, blocked_by_group: bool) {
|
||||
let now = Utc::now();
|
||||
let (owner, source, resume_at, reason) = if !house_power_enabled {
|
||||
("global_off", "global".to_string(), None, "Whole-house power is disabled".to_string())
|
||||
} else if zone.device_manual_override {
|
||||
let source = match zone.control_source.as_str() {
|
||||
"home_assistant_direct" | "web_direct" | "external" => zone.control_source.clone(),
|
||||
_ => "external".into(),
|
||||
};
|
||||
("direct_manual", source, zone.device_manual_override_until, "Direct/manual device control has priority".to_string())
|
||||
} else if zone.local_thermostat_power.is_some() {
|
||||
let source = match zone.control_source.as_str() {
|
||||
"home_assistant_thermostat" | "web_thermostat" => zone.control_source.clone(),
|
||||
_ => "local_thermostat".into(),
|
||||
};
|
||||
let resume_at = zone.temporary_quick_thermostat.as_ref()
|
||||
.and_then(temporary_quick_thermostat_next_deadline)
|
||||
.or(zone.local_thermostat_resume_at.clone());
|
||||
let reason = if zone.local_thermostat_power == Some(false) {
|
||||
"Local thermostat is explicitly off".into()
|
||||
} else if zone.temporary_quick_thermostat.is_some() {
|
||||
"Temporary Quick Thermostat owns the zone".into()
|
||||
} else {
|
||||
"Local thermostat owns the zone".into()
|
||||
};
|
||||
("local_thermostat", source, resume_at, reason)
|
||||
} else if blocked_by_group {
|
||||
("automation", "group".to_string(), None, "Zone is blocked by a disabled group".to_string())
|
||||
} else {
|
||||
("automation", "automation".to_string(), zone.manual_override_until, "Automatic thermostat/schedule control".to_string())
|
||||
};
|
||||
if zone.control_owner != owner || zone.control_source != source {
|
||||
zone.control_since = Some(now);
|
||||
} else if zone.control_since.is_none() {
|
||||
zone.control_since = Some(now);
|
||||
}
|
||||
zone.control_owner = owner.into();
|
||||
zone.control_source = source;
|
||||
zone.control_resume_at = resume_at;
|
||||
zone.control_reason = reason;
|
||||
}
|
||||
|
||||
fn normalized_direct_source(source: &str) -> &'static str {
|
||||
if source.contains("home_assistant") { "home_assistant_direct" }
|
||||
else if source == "device.manual_control" { "web_direct" }
|
||||
else { "external" }
|
||||
}
|
||||
|
||||
pub fn reset_device_manual_override(zone: &mut Zone) -> bool {
|
||||
let now = Utc::now();
|
||||
let changed = zone.device_manual_override
|
||||
|| zone.device_manual_override_since.is_some()
|
||||
|| zone.device_manual_override_until.is_some()
|
||||
|| !zone.device_manual_override_fields.is_empty()
|
||||
|| zone.device_manual_override_baseline.is_some();
|
||||
zone.device_manual_override = false;
|
||||
zone.device_manual_override_since = None;
|
||||
zone.device_manual_override_until = None;
|
||||
zone.device_manual_override_fields.clear();
|
||||
zone.device_manual_override_baseline = None;
|
||||
if let Some(session) = zone.temporary_quick_thermostat.as_mut() {
|
||||
let pause = session.paused_at.take()
|
||||
.map(|paused_at| now.signed_duration_since(paused_at))
|
||||
.filter(|pause| *pause > chrono::Duration::zero());
|
||||
if session.activated_at.is_some() {
|
||||
if let Some(pause) = pause {
|
||||
if matches!(session.finish_kind.as_str(), "duration" | "until") {
|
||||
session.expires_at = session.expires_at.map(|at| at + pause);
|
||||
}
|
||||
if matches!(session.finish_kind.as_str(), "temperature_reached" | "temperature_stable") {
|
||||
session.safety_expires_at = session.safety_expires_at.map(|at| at + pause);
|
||||
}
|
||||
}
|
||||
session.state = "active".into();
|
||||
} else {
|
||||
// A due session blocked by manual ownership has not started its work clock.
|
||||
// Preserve the requested remaining `until` window by excluding manual wait time.
|
||||
if session.finish_kind == "until" {
|
||||
if let Some(pause) = pause {
|
||||
session.expires_at = session.expires_at.map(|at| at + pause);
|
||||
}
|
||||
}
|
||||
session.state = "scheduled".into();
|
||||
}
|
||||
session.condition_started_at = None;
|
||||
session.condition_last_observed_at = None;
|
||||
}
|
||||
if zone.control_owner == "direct_manual" {
|
||||
zone.control_owner = "automation".into();
|
||||
zone.control_source = "automation".into();
|
||||
zone.control_since = Some(now);
|
||||
zone.control_resume_at = None;
|
||||
zone.control_reason = "Manual takeover cleared; automation may resume".into();
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
fn manual_override_matches_baseline(zone: &Zone, device: &Device) -> bool {
|
||||
let Some(baseline) = zone.device_manual_override_baseline.as_ref() else { return false; };
|
||||
if zone.device_manual_override_fields.is_empty() { return false; }
|
||||
// If the unit was OFF before takeover, returning it to OFF is operationally the same
|
||||
// controller state even if the remote retained a different mode/target internally.
|
||||
// Those dormant values will be set explicitly if automation later powers the unit.
|
||||
if !baseline.power { return !device.power; }
|
||||
zone.device_manual_override_fields.iter().all(|field| match field.as_str() {
|
||||
"power" => device.power == baseline.power,
|
||||
"mode" => device.mode == baseline.mode,
|
||||
"target_temperature" => device.target_temperature.round() == baseline.target_temperature.round(),
|
||||
"fan_speed" => device.fan_speed == baseline.fan_speed,
|
||||
"quiet" => device.quiet == baseline.quiet,
|
||||
"sleep" => device.sleep == baseline.sleep,
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
|
||||
fn persist_manual_override_clear(state: &AppState, zone: &mut Zone, source: &str, restored: bool) -> Result<bool, AppError> {
|
||||
if !reset_device_manual_override(zone) { return Ok(false); }
|
||||
let now = Utc::now();
|
||||
let local_resume_rearmed = restored && rearm_local_thermostat_resume(zone, now.clone());
|
||||
zone.updated_at = now;
|
||||
state.db.save_zone(zone)?;
|
||||
state.broadcast("zone.updated", serde_json::to_value(&*zone)?);
|
||||
let (kind, message) = if restored {
|
||||
("zone.device_manual_override_restored", format!("Manual device control returned {} to its previous state", zone.name))
|
||||
} else {
|
||||
("zone.device_manual_override_cleared", format!("Manual device control ended for {}", zone.name))
|
||||
};
|
||||
state.log("info", kind, &message, json!({
|
||||
"zone_id": zone.id, "device_id": zone.device_id, "source": source,
|
||||
"local_thermostat_resume_rearmed": local_resume_rearmed,
|
||||
"local_thermostat_resume_at": zone.local_thermostat_resume_at,
|
||||
}));
|
||||
state.wake_zone_control();
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn set_device_manual_override(state: &AppState, zone: &mut Zone, fields: Vec<String>, source: &str, baseline: &Device) -> Result<(), AppError> {
|
||||
if fields.is_empty() { return Ok(()); }
|
||||
let now = Utc::now();
|
||||
if !zone.device_manual_override {
|
||||
zone.device_manual_override_since = Some(now);
|
||||
zone.device_manual_override_baseline = Some(baseline.into());
|
||||
zone.control_since = Some(now);
|
||||
}
|
||||
zone.device_manual_override = true;
|
||||
if let Some(session) = zone.temporary_quick_thermostat.as_mut() {
|
||||
// A future scheduled session has no ownership yet. Do not mark it paused until its
|
||||
// requested start actually becomes due while manual control is still present.
|
||||
if session.activated_at.is_some() || session.started_at <= now {
|
||||
if session.paused_at.is_none() { session.paused_at = Some(now); }
|
||||
session.state = "paused_manual".into();
|
||||
session.condition_started_at = None;
|
||||
session.condition_last_observed_at = None;
|
||||
}
|
||||
}
|
||||
zone.control_owner = "direct_manual".into();
|
||||
zone.control_source = normalized_direct_source(source).into();
|
||||
zone.control_reason = "Direct/manual device control has priority".into();
|
||||
zone.device_manual_override_until = if zone.enabled {
|
||||
next_schedule_boundary_utc(&zone.id, &state.db.list_schedules()?, Local::now())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
zone.control_resume_at = zone.device_manual_override_until;
|
||||
for field in fields {
|
||||
if !zone.device_manual_override_fields.iter().any(|existing| existing == &field) {
|
||||
zone.device_manual_override_fields.push(field);
|
||||
}
|
||||
}
|
||||
zone.demand = false;
|
||||
zone.demand_since = None;
|
||||
zone.updated_at = now;
|
||||
state.db.save_zone(zone)?;
|
||||
state.broadcast("zone.updated", serde_json::to_value(&*zone)?);
|
||||
state.log("info", "zone.device_manual_override", &format!("Manual device control detected for {}", zone.name), json!({
|
||||
"zone_id": zone.id,
|
||||
"device_id": zone.device_id,
|
||||
"fields": zone.device_manual_override_fields,
|
||||
"source": source,
|
||||
"override_until": zone.device_manual_override_until,
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn detect_external_device_control(state: &AppState, before: &Device, after: &Device) -> Result<(), AppError> {
|
||||
if before.id != after.id { return Ok(()); }
|
||||
for mut zone in state.db.list_zones()?.into_iter().filter(|zone| zone.device_id == after.id) {
|
||||
let raw_fields = externally_changed_control_fields(before, after, &zone);
|
||||
let controller_settling = if raw_fields.is_empty() {
|
||||
json!({ "active": false, "reason": "no_changed_control_fields" })
|
||||
} else {
|
||||
controller_settling_diagnostics(state, &after.id).await
|
||||
};
|
||||
let fields = suppress_expected_controller_changes(
|
||||
state,
|
||||
after,
|
||||
raw_fields.clone(),
|
||||
).await;
|
||||
if zone.device_manual_override && manual_override_matches_baseline(&zone, after) {
|
||||
persist_manual_override_clear(state, &mut zone, "gree_poll", true)?;
|
||||
continue;
|
||||
}
|
||||
if fields.is_empty() { continue; }
|
||||
// A disabled zone is outside controller ownership. When its manually operated unit is
|
||||
// switched off there is no takeover left to display or remember.
|
||||
if !zone.enabled && !after.power {
|
||||
persist_manual_override_clear(state, &mut zone, "gree_poll", false)?;
|
||||
continue;
|
||||
}
|
||||
state.log("info", "device.remote_control_detected", &format!("External/pilot control detected for {}", zone.name), json!({
|
||||
"zone_id": zone.id.clone(),
|
||||
"device_id": zone.device_id.clone(),
|
||||
"raw_fields": raw_fields,
|
||||
"detected_fields": fields.clone(),
|
||||
"before": device_control_snapshot(before),
|
||||
"after": device_control_snapshot(after),
|
||||
"controller_settling": controller_settling,
|
||||
"source": "gree_poll",
|
||||
"zone_state": {
|
||||
"enabled": zone.enabled,
|
||||
"control_owner": zone.control_owner.clone(),
|
||||
"control_source": zone.control_source.clone(),
|
||||
"demand": zone.demand,
|
||||
"local_thermostat_power": zone.local_thermostat_power,
|
||||
"temporary_quick_thermostat": zone.temporary_quick_thermostat.clone(),
|
||||
},
|
||||
}));
|
||||
set_device_manual_override(state, &mut zone, fields, "gree_poll", before)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_manual_command(state: &AppState, device_id: &str, command: DeviceCommand, source: &str) -> Result<Device, AppError> {
|
||||
// Keep zone -> device lock ordering consistent with Quick Thermostat/full-zone edits.
|
||||
// A device belongs to at most one thermostat zone, but keep this generic for legacy data.
|
||||
let zone_ids: Vec<String> = state.db.list_zones()?.into_iter().filter(|zone| zone.device_id == device_id).map(|zone| zone.id).collect();
|
||||
let mut _zone_guards = Vec::new();
|
||||
for zone_id in &zone_ids { _zone_guards.push(state.lock_zone_operation(zone_id).await); }
|
||||
// Keep the device lock until the zone takeover marker is persisted. Otherwise a poll
|
||||
// could observe our own just-sent command before the controller records manual ownership.
|
||||
let _device_guard = state.lock_device_operation(device_id).await;
|
||||
let before = state.db.get_device(device_id)?
|
||||
.ok_or_else(|| AppError::NotFound(format!("device {device_id}")))?;
|
||||
let effective_command = if before.online && before.communication_failures == 0 { command.changed_from(&before) } else { command.clone() };
|
||||
let fields = command_manual_control_fields(&effective_command);
|
||||
let updated = send_command_locked_inner(state, device_id, command, true, false).await?;
|
||||
if !fields.is_empty() {
|
||||
for mut zone in state.db.list_zones()?.into_iter().filter(|zone| zone.device_id == device_id) {
|
||||
if zone.device_manual_override && manual_override_matches_baseline(&zone, &updated) {
|
||||
persist_manual_override_clear(state, &mut zone, source, true)?;
|
||||
continue;
|
||||
}
|
||||
if !zone.enabled && !updated.power {
|
||||
persist_manual_override_clear(state, &mut zone, source, false)?;
|
||||
continue;
|
||||
}
|
||||
set_device_manual_override(state, &mut zone, fields.clone(), source, &before)?;
|
||||
}
|
||||
}
|
||||
Ok(updated)
|
||||
}
|
||||
|
||||
pub async fn force_house_power_off_device(state: &AppState, device_id: &str, source: &str) -> Result<Device, AppError> {
|
||||
// Global OFF is a one-shot authority transition. Clear takeover and send OFF while
|
||||
// polling for this unit is excluded; a later remote change happens after the lock and
|
||||
// is therefore preserved as a new manual takeover.
|
||||
let _device_guard = state.lock_device_operation(device_id).await;
|
||||
for mut zone in state.db.list_zones()?.into_iter().filter(|zone| zone.device_id == device_id) {
|
||||
if !reset_device_manual_override(&mut zone) { continue; }
|
||||
zone.updated_at = Utc::now();
|
||||
state.db.save_zone(&zone)?;
|
||||
state.broadcast("zone.updated", serde_json::to_value(&zone)?);
|
||||
state.log("info", "zone.device_manual_override_cleared", &format!("Automation resumed for {}", zone.name), json!({
|
||||
"zone_id": zone.id, "device_id": zone.device_id, "source": source
|
||||
}));
|
||||
}
|
||||
send_command_locked_forced(state, device_id, DeviceCommand { power: Some(false), ..Default::default() }).await
|
||||
}
|
||||
|
||||
pub async fn force_power_off_device(state: &AppState, device_id: &str) -> Result<Device, AppError> {
|
||||
let _device_guard = state.lock_device_operation(device_id).await;
|
||||
send_command_locked_forced(state, device_id, DeviceCommand { power: Some(false), ..Default::default() }).await
|
||||
}
|
||||
|
||||
/// Technical device disable is a safety transition, not just a database flag. The unit is
|
||||
/// explicitly powered off while it is still commandable, then removed from controller polling.
|
||||
pub async fn disable_device_safely(state: &AppState, device_id: &str) -> Result<Device, AppError> {
|
||||
let _device_guard = state.lock_device_operation(device_id).await;
|
||||
let mut device = state.db.get_device(device_id)?
|
||||
.ok_or_else(|| AppError::NotFound(format!("device {device_id}")))?;
|
||||
if !device.enabled { return Ok(device); }
|
||||
device = send_command_locked_forced(
|
||||
state,
|
||||
device_id,
|
||||
DeviceCommand { power: Some(false), ..Default::default() },
|
||||
).await?;
|
||||
device.enabled = false;
|
||||
device.updated_at = Utc::now();
|
||||
state.db.save_device(&device)?;
|
||||
state.broadcast("device.updated", serde_json::to_value(&device)?);
|
||||
state.wake_zone_control();
|
||||
Ok(device)
|
||||
}
|
||||
|
||||
pub fn clear_all_device_manual_overrides(state: &AppState, source: &str) -> Result<usize, AppError> {
|
||||
let mut cleared = 0usize;
|
||||
for mut zone in state.db.list_zones()? {
|
||||
if !reset_device_manual_override(&mut zone) { continue; }
|
||||
zone.updated_at = Utc::now();
|
||||
state.db.save_zone(&zone)?;
|
||||
state.broadcast("zone.updated", serde_json::to_value(&zone)?);
|
||||
state.log("info", "zone.device_manual_override_cleared", &format!("Automation resumed for {}", zone.name), json!({
|
||||
"zone_id": zone.id, "device_id": zone.device_id, "source": source
|
||||
}));
|
||||
cleared += 1;
|
||||
}
|
||||
Ok(cleared)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user