Add env feature for clap, add command to set volume.
This commit is contained in:
parent
338ebc36b1
commit
4a37e9ff9b
2 changed files with 18 additions and 6 deletions
|
@ -7,7 +7,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.56"
|
anyhow = "1.0.56"
|
||||||
clap = { version = "3.1.6", features = ["cargo", "unicode", "derive"] }
|
clap = { version = "3.1.6", features = ["cargo", "unicode", "derive", "env"] }
|
||||||
color-eyre = "0.6.1"
|
color-eyre = "0.6.1"
|
||||||
html-escape = "0.2.11"
|
html-escape = "0.2.11"
|
||||||
reqwest = { version = "0.11.10", features = ["blocking", "json"] }
|
reqwest = { version = "0.11.10", features = ["blocking", "json"] }
|
||||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -5,7 +5,7 @@ use clap::Parser;
|
||||||
use color_eyre::{eyre::WrapErr, Result};
|
use color_eyre::{eyre::WrapErr, Result};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use tracing::debug;
|
use tracing::{debug, trace};
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
/// Bert
|
/// Bert
|
||||||
|
@ -21,11 +21,11 @@ struct Opts {
|
||||||
entity: String,
|
entity: String,
|
||||||
|
|
||||||
/// API token
|
/// API token
|
||||||
#[clap(short, long)]
|
#[clap(short, long, env)]
|
||||||
token: Option<String>,
|
token: Option<String>,
|
||||||
|
|
||||||
/// File with the API token
|
/// File with the API token
|
||||||
#[clap(long)]
|
#[clap(long, env)]
|
||||||
token_file: Option<PathBuf>,
|
token_file: Option<PathBuf>,
|
||||||
|
|
||||||
/// Use HTTP instead of HTTPS
|
/// Use HTTP instead of HTTPS
|
||||||
|
@ -47,6 +47,8 @@ enum Command {
|
||||||
VolumeUp,
|
VolumeUp,
|
||||||
/// Lower volume
|
/// Lower volume
|
||||||
VolumeDown,
|
VolumeDown,
|
||||||
|
/// Set volume to value
|
||||||
|
Volume { volume: u8 },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Command {
|
impl Display for Command {
|
||||||
|
@ -55,6 +57,7 @@ impl Display for Command {
|
||||||
Command::PlayPause => write!(f, "Play/Pause"),
|
Command::PlayPause => write!(f, "Play/Pause"),
|
||||||
Command::VolumeUp => write!(f, "Volume Up"),
|
Command::VolumeUp => write!(f, "Volume Up"),
|
||||||
Command::VolumeDown => write!(f, "Volume Down"),
|
Command::VolumeDown => write!(f, "Volume Down"),
|
||||||
|
Command::Volume { volume } => write!(f, "Volume {volume}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,9 +94,11 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
if let Some(token) = token {
|
if let Some(token) = token {
|
||||||
if let Some(cmd) = cmd {
|
if let Some(cmd) = cmd {
|
||||||
|
debug!("Calling service '{cmd}' on entity '{entity}'");
|
||||||
call_service(cmd, host, entity, insecure, token)
|
call_service(cmd, host, entity, insecure, token)
|
||||||
.wrap_err_with(|| format!("Unable to execute command {cmd}"))?;
|
.wrap_err_with(|| format!("Unable to execute command {cmd}"))?;
|
||||||
} else {
|
} else {
|
||||||
|
debug!("Retrieving state of '{entity}' on '{host}'");
|
||||||
get_now_playing(host, entity, insecure, token)
|
get_now_playing(host, entity, insecure, token)
|
||||||
.wrap_err("Unable to retrieve now playing info")?;
|
.wrap_err("Unable to retrieve now playing info")?;
|
||||||
}
|
}
|
||||||
|
@ -131,7 +136,7 @@ fn get_now_playing(host: String, entity: String, insecure: bool, token: String)
|
||||||
.send()?
|
.send()?
|
||||||
.json::<serde_json::Value>()?;
|
.json::<serde_json::Value>()?;
|
||||||
|
|
||||||
debug!("Response: {:#?}", response);
|
trace!("Response: {:#?}", response);
|
||||||
|
|
||||||
if response["state"] == "playing" {
|
if response["state"] == "playing" {
|
||||||
let attributes = &response["attributes"];
|
let attributes = &response["attributes"];
|
||||||
|
@ -180,6 +185,7 @@ fn call_service(
|
||||||
Command::PlayPause => "media_play_pause",
|
Command::PlayPause => "media_play_pause",
|
||||||
Command::VolumeUp => "volume_up",
|
Command::VolumeUp => "volume_up",
|
||||||
Command::VolumeDown => "volume_down",
|
Command::VolumeDown => "volume_down",
|
||||||
|
Command::Volume { .. } => "volume_set",
|
||||||
};
|
};
|
||||||
|
|
||||||
let url = format!(
|
let url = format!(
|
||||||
|
@ -189,7 +195,13 @@ fn call_service(
|
||||||
cmd
|
cmd
|
||||||
);
|
);
|
||||||
|
|
||||||
let body = json!({ "entity_id": entity }).to_string();
|
let body = match command {
|
||||||
|
Command::Volume { volume } => {
|
||||||
|
json!({ "entity_id": entity, "volume_level": volume.clamp(0, 100) as f32 / 100. })
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
_ => json!({ "entity_id": entity }).to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
let client = reqwest::blocking::Client::new();
|
let client = reqwest::blocking::Client::new();
|
||||||
let response = client
|
let response = client
|
||||||
|
|
Loading…
Reference in a new issue