From 76bc9708d323fd63acce0912058b4cf38b29075d Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Sun, 21 Jan 2024 10:06:14 +1100 Subject: [PATCH] log sentry mode --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/api_interface.rs | 32 ++++++++++++++++++++++++++++++++ src/types.rs | 16 ++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index be8171f..5c51c4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2567,7 +2567,7 @@ dependencies = [ [[package]] name = "tesla-charge-controller" -version = "1.0.12" +version = "1.0.13" dependencies = [ "async-channel", "chrono", diff --git a/Cargo.toml b/Cargo.toml index d51620c..e353f4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tesla-charge-controller" -version = "1.0.12" +version = "1.0.13" edition = "2021" license = "MITNFA" description = "Controls Tesla charge rate based on solar charge data" diff --git a/src/api_interface.rs b/src/api_interface.rs index c3696e5..294621d 100644 --- a/src/api_interface.rs +++ b/src/api_interface.rs @@ -40,6 +40,8 @@ struct Metrics { cabin_overheat_protection: CabinOverheatProtectionGauges, hvac_auto: HvacAutoRequestGauges, home: Gauge, + sentry_mode: Gauge, + sentry_mode_available: Gauge, } impl Metrics { @@ -88,6 +90,10 @@ impl Metrics { let hvac_auto = HvacAutoRequestGauges::new(); describe_gauge!("tesla_home", "Is home"); let home = gauge!("tesla_home"); + describe_gauge!("tesla_sentry_mode", "Sentry mode"); + let sentry_mode = gauge!("tesla_sentry_mode"); + describe_gauge!("tesla_sentry_mode_available", "Sentry mode available"); + let sentry_mode_available = gauge!("tesla_sentry_mode_available"); Self { battery_level, @@ -109,6 +115,8 @@ impl Metrics { cabin_overheat_protection, hvac_auto, home, + sentry_mode, + sentry_mode_available, } } } @@ -459,6 +467,15 @@ impl TeslaInterface { state.climate_state = Some(new_climate_state); } + if let Some(new_vehicle_state) = new_state.vehicle_state { + self.metrics + .sentry_mode + .set(obf(new_vehicle_state.sentry_mode)); + self.metrics + .sentry_mode_available + .set(obf(new_vehicle_state.sentry_mode_available)); + state.vehicle_state = Some(new_vehicle_state); + } } Err(e) => { self.metrics.tesla_online.set(0.); @@ -518,6 +535,16 @@ impl TeslaInterface { v.into() }); + let vehicle_state = self + .api + .vehicle_data(&GetVehicleData { + vehicle_id: self.vehicle.id, + endpoints: vec![Endpoint::VehicleState].into(), + }) + .await? + .vehicle_state + .map(|v| v.into()); + let (location_data, _) = self .api .vehicle_data(&GetVehicleData { @@ -555,6 +582,7 @@ impl TeslaInterface { charge_state, location_data, climate_state, + vehicle_state, }) } } @@ -566,3 +594,7 @@ fn bf(value: bool) -> f64 { 0.0 } } + +fn obf(value: Option) -> f64 { + value.map_or(-1., bf) +} diff --git a/src/types.rs b/src/types.rs index 7c198cb..c4fc70c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -9,6 +9,7 @@ pub struct CarState { pub charge_state: Option, pub location_data: Option, pub climate_state: Option, + pub vehicle_state: Option, } impl CarState { @@ -19,6 +20,21 @@ impl CarState { } } +#[derive(Clone, Copy, Serialize, Deserialize, Debug)] +pub struct VehicleState { + pub sentry_mode: Option, + pub sentry_mode_available: Option, +} + +impl From for VehicleState { + fn from(value: teslatte::vehicles::VehicleState) -> Self { + Self { + sentry_mode: value.sentry_mode, + sentry_mode_available: value.sentry_mode_available, + } + } +} + #[derive(Clone, Copy, Serialize, Deserialize, Debug)] pub struct ClimateState { pub inside_temp: f64,