2023-08-29 13:11:28 +10:00
|
|
|
use crate::cli::print_json;
|
2023-09-05 13:48:16 +10:00
|
|
|
use crate::energy_sites::{CalendarHistoryValues, HistoryKind, HistoryPeriod};
|
|
|
|
use crate::products::EnergySiteId;
|
2023-10-20 12:18:38 +11:00
|
|
|
use crate::OwnerApi;
|
2023-08-29 11:33:03 +10:00
|
|
|
use chrono::DateTime;
|
|
|
|
use clap::{Args, Subcommand};
|
|
|
|
use miette::{IntoDiagnostic, WrapErr};
|
|
|
|
|
|
|
|
#[derive(Debug, Subcommand)]
|
|
|
|
pub enum EnergySiteCommand {
|
2023-09-05 13:48:16 +10:00
|
|
|
SiteStatus,
|
|
|
|
LiveStatus,
|
|
|
|
SiteInfo,
|
2023-08-29 11:33:03 +10:00
|
|
|
CalendarHistory(CalendarHistoryArgs),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Args)]
|
|
|
|
pub struct EnergySiteArgs {
|
|
|
|
pub id: EnergySiteId,
|
|
|
|
|
|
|
|
#[clap(subcommand)]
|
|
|
|
pub command: EnergySiteCommand,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnergySiteArgs {
|
2023-10-20 12:18:38 +11:00
|
|
|
pub async fn run(&self, api: &OwnerApi) -> miette::Result<()> {
|
2023-08-29 11:33:03 +10:00
|
|
|
match &self.command {
|
2023-09-05 13:48:16 +10:00
|
|
|
EnergySiteCommand::SiteStatus => {
|
|
|
|
print_json(api.energy_sites_site_status(&self.id).await);
|
|
|
|
}
|
|
|
|
EnergySiteCommand::LiveStatus => {
|
|
|
|
print_json(api.energy_sites_live_status(&self.id).await);
|
|
|
|
}
|
|
|
|
EnergySiteCommand::SiteInfo => {
|
|
|
|
print_json(api.energy_sites_site_info(&self.id).await);
|
|
|
|
}
|
2023-08-29 11:33:03 +10:00
|
|
|
EnergySiteCommand::CalendarHistory(args) => {
|
|
|
|
let start_date = args
|
|
|
|
.start
|
|
|
|
.as_ref()
|
2023-08-29 11:41:40 +10:00
|
|
|
.map(|s| DateTime::parse_from_rfc3339(s).into_diagnostic())
|
2023-08-29 11:33:03 +10:00
|
|
|
.transpose()
|
|
|
|
.wrap_err("start_date")?;
|
|
|
|
let end_date = args
|
|
|
|
.end
|
|
|
|
.as_ref()
|
2023-08-29 11:41:40 +10:00
|
|
|
.map(|s| DateTime::parse_from_rfc3339(s).into_diagnostic())
|
2023-08-29 11:33:03 +10:00
|
|
|
.transpose()
|
|
|
|
.wrap_err("end_date")?;
|
|
|
|
let values = CalendarHistoryValues {
|
|
|
|
site_id: self.id.clone(),
|
|
|
|
kind: args.kind.clone(),
|
|
|
|
period: args.period.clone(),
|
|
|
|
start_date,
|
|
|
|
end_date,
|
|
|
|
};
|
2023-08-29 16:13:43 +10:00
|
|
|
print_json(api.energy_sites_calendar_history(&values).await);
|
2023-08-29 11:33:03 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 13:55:02 +10:00
|
|
|
|
|
|
|
/// Show the calendar history of an energy site. This is the same data that is shown in the Tesla app.
|
|
|
|
///
|
|
|
|
/// Use `energy_site_id` as the ID.
|
|
|
|
///
|
|
|
|
/// The `kind` argument must be `energy` or `power`.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// teslatte api energy-site 1234567890 calendar-history power -s "2022-01-01T00:00:00Z" -e 2023-01-01T00:00:00Z -p month
|
|
|
|
#[derive(Debug, Args)]
|
|
|
|
pub struct CalendarHistoryArgs {
|
|
|
|
/// `energy` or `power`
|
|
|
|
pub kind: HistoryKind,
|
|
|
|
|
|
|
|
#[clap(short, long, default_value = "day")]
|
|
|
|
pub period: HistoryPeriod,
|
|
|
|
|
|
|
|
/// ISO8601 date-time for the start of the period, e.g. 2000-01-01T00:00:00Z
|
|
|
|
#[clap(short, long)]
|
|
|
|
pub start: Option<String>,
|
|
|
|
|
|
|
|
/// ISO8601 date-time for the end of the period, e.g. 2025-01-01T00:00:00Z
|
|
|
|
#[clap(short, long)]
|
|
|
|
pub end: Option<String>,
|
|
|
|
}
|