1786899994

This commit is contained in:
2026-08-16 19:06:34 +02:00
parent 9f8eb12051
commit ac5c82eee3
5 changed files with 40 additions and 30 deletions

View File

@@ -5,24 +5,25 @@ use tokio::{sync::mpsc::UnboundedSender, time::sleep};
use crate::{error::AppError, messages::EventMessage};
// #[derive(Copy, Clone, Debug)]
// pub struct BatteryData {
// state_of_health: Ratio,
// state_of_charge: Ratio,
// energy: Energy,
// energy_full: Energy,
// energy_full_design: Energy,
// energy_rate: Power,
// state: State,
// voltage: ElectricPotential,
// temperature: Option<ThermodynamicTemperature>,
// technology: Technology,
// cycle_count: Option<u32>,
// time_to_full: Option<Time>,
// time_to_empty: Option<Time>,
// }
#[allow(unused)]
#[derive(Copy, Clone, Debug)]
pub struct RawBatteryData {
pub state_of_health: Ratio,
pub state_of_charge: Ratio,
pub energy: Energy,
pub energy_full: Energy,
pub energy_full_design: Energy,
pub energy_rate: Power,
pub state: State,
pub voltage: ElectricPotential,
pub temperature: Option<ThermodynamicTemperature>,
pub technology: Technology,
pub cycle_count: Option<u32>,
pub time_to_full: Option<Time>,
pub time_to_empty: Option<Time>,
}
impl From<&Battery> for BatteryData {
impl From<&Battery> for RawBatteryData {
fn from(value: &Battery) -> Self {
Self {
state_of_health: value.state_of_health(),
@@ -56,8 +57,7 @@ pub async fn battery_listener(s2c_tx: UnboundedSender<EventMessage>) -> Result<(
loop {
s2c_tx.send(EventMessage::Battery((&battery).into())).ok();
sleep(Duration::from_secs(30)).await;
sleep(Duration::from_secs(5)).await;
battery.refresh().ok();
}
}

View File

@@ -1,4 +1,4 @@
use crate::ui::*;
use crate::{battery::RawBatteryData, ui::*};
use hass_rs::HassEntity;
use slint::ToSharedString;
@@ -172,6 +172,16 @@ impl From<&crate::home_assistant::CalendarEvent> for CalendarEventData {
}
}
impl From<RawBatteryData> for BatteryData {
fn from(value: RawBatteryData) -> Self {
let state = value.state.to_shared_string();
let percent: f32 = (value.state_of_charge * 100.).into();
let percent = percent.round();
Self { state, percent }
}
}
pub fn pretty_label(value: &str) -> String {
let value = match value {
"partlycloudy" => "partly cloudy",

View File

@@ -1,6 +1,6 @@
use hass_rs::HassEntity;
use crate::{battery::BatteryData, home_assistant::CalendarEvent};
use crate::{battery::RawBatteryData, home_assistant::CalendarEvent};
#[derive(Clone)]
pub enum CommandMessage {
@@ -17,5 +17,5 @@ pub enum EventMessage {
EntityUpdated(HassEntity),
EntityRemoved(HassEntity),
CalendarUpdated(Vec<CalendarEvent>),
Battery(BatteryData),
Battery(RawBatteryData),
}

View File

@@ -1,15 +1,12 @@
slint::include_modules!();
use std::sync::Arc;
use crate::{
battery::BatteryData,
battery::RawBatteryData,
ha_ext::*,
home_assistant::CalendarEvent,
messages::{CommandMessage, EventMessage},
};
use battery::Battery;
use hass_rs::HassEntity;
use paste::paste;
use slint::{Model, ModelRc, VecModel, Weak, language::ColorScheme};
@@ -125,9 +122,12 @@ pub fn apply_calendars(app: &Weak<AppWindow>, entities: Vec<CalendarEvent>) {
}
}
pub fn apply_battery(app: &Weak<AppWindow>, batt: BatteryData) {
// ..
dbg!(batt);
pub fn apply_battery(app: &Weak<AppWindow>, batt: RawBatteryData) {
if let Err(err) = app.upgrade_in_event_loop(move |app| {
app.set_battery(batt.into());
}) {
eprintln!("Failed to upgrade in event loop.\n\t{:?}", err);
}
}
pub fn apply_state(app: &Weak<AppWindow>, entities: Vec<HassEntity>) {

View File

@@ -51,7 +51,7 @@ export struct DateTimeData {
export struct BatteryData {
state: string,
percent: string,
percent: float,
}
component Clock {