teslatte/examples/basic.rs
gak a64a04e3d7
feat: rename and add api calls (energy_sites)
- rename api/cli energy_sites to products (to match the api)
- add energy_sites site_status
- add energy_sites live_status
- add energy_sites site_info
2023-09-05 13:48:16 +10:00

63 lines
1.7 KiB
Rust

use std::env;
use teslatte::auth::AccessToken;
use teslatte::products::Product;
use teslatte::Api;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let api = match env::var("TESLA_ACCESS_TOKEN") {
Ok(t) => Api::new(AccessToken(t), None),
Err(_) => {
let api = Api::from_interactive_url().await.unwrap();
println!("TOKEN: {:?}", api.access_token);
api
}
};
let vehicles = api.vehicles().await.unwrap();
dbg!(&*vehicles);
if !vehicles.is_empty() {
let vehicle_data = api.vehicle_data(&vehicles[0].id).await.unwrap();
dbg!(&*vehicle_data);
} else {
println!("No vehicles found!");
}
let products = api.products().await.unwrap();
dbg!(&*products);
if !products.is_empty() {
for product in &*products {
match product {
Product::Vehicle(v) => {
dbg!(v);
}
Product::Solar(e) => {
let site_info = api.energy_sites_site_info(&e.energy_site_id).await.unwrap();
dbg!(&*site_info);
let live_info = api
.energy_sites_live_status(&e.energy_site_id)
.await
.unwrap();
dbg!(&*live_info);
}
Product::Powerwall(p) => {
let live_info = api
.energy_sites_live_status(&p.energy_site_id)
.await
.unwrap();
dbg!(&*live_info);
}
}
}
} else {
println!("No products found!");
}
}