Merge branch 'release/0.4.3'

This commit is contained in:
Erwin Boskma 2022-05-11 16:25:44 +02:00
commit 4faf850a48
Signed by: erwin
GPG key ID: 270B20D17394F7E5
5 changed files with 67 additions and 76 deletions

2
Cargo.lock generated
View file

@ -421,7 +421,7 @@ dependencies = [
[[package]] [[package]]
name = "ha-now-playing" name = "ha-now-playing"
version = "0.4.2" version = "0.4.3"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-tungstenite", "async-tungstenite",

View file

@ -1,7 +1,7 @@
[package] [package]
name = "ha-now-playing" name = "ha-now-playing"
description = "Retrieves the state of a media_player entity from Home Assistant" description = "Retrieves the state of a media_player entity from Home Assistant"
version = "0.4.2" version = "0.4.3"
authors = ["Erwin Boskma <erwin@datarift.nl>"] authors = ["Erwin Boskma <erwin@datarift.nl>"]
edition = "2021" edition = "2021"

View file

@ -7,11 +7,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1649096192, "lastModified": 1651574473,
"narHash": "sha256-7O8e+eZEYeU+ET98u/zW5epuoN/xYx9G+CIh4DjZVzY=", "narHash": "sha256-wQhFORvRjo8LB2hTmETmv6cbyKGDPbfWqvZ/0chnDE4=",
"owner": "nmattia", "owner": "nmattia",
"repo": "naersk", "repo": "naersk",
"rev": "d626f73332a8f587b613b0afe7293dd0777be07d", "rev": "f21309b38e1da0d61b881b6b6d41b81c1aed4e1d",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -23,16 +23,16 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1648219316, "lastModified": 1652172129,
"narHash": "sha256-Ctij+dOi0ZZIfX5eMhgwugfvB+WZSrvVNAyAuANOsnQ=", "narHash": "sha256-8e2JMrswaKe02P8rYfvIMoc59pNuw6h/GYu8DBE1c+0=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "30d3d79b7d3607d56546dd2a6b49e156ba0ec634", "rev": "f419dc5763c2b3c5580e396dea065b6d8b58ee27",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixpkgs-unstable", "ref": "nixos-unstable",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
@ -55,11 +55,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1649298784, "lastModified": 1652236937,
"narHash": "sha256-rUwQKjBOZiRpzgkG3h9OM5pUyxRYIV45ohgqZJtSZtE=", "narHash": "sha256-anMSkIvaxB3jV74nTvPcE8fH7bZ5LPuc2wXGvt50Oe0=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "c6fe2a14f10cb85659b8848bac87e4ff29a00f9a", "rev": "18c816d2d10317f4a8816925a12cb4bbdd0821de",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -70,11 +70,11 @@
}, },
"utils": { "utils": {
"locked": { "locked": {
"lastModified": 1648297722, "lastModified": 1649676176,
"narHash": "sha256-W+qlPsiZd8F3XkzXOzAoR+mpFqzm3ekQkJNa+PIh1BQ=", "narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "0f8662f1319ad6abf89b3380dd2722369fc51ade", "rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -1,7 +1,7 @@
{ {
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
utils.url = "github:numtide/flake-utils"; utils.url = "github:numtide/flake-utils";
naersk = { naersk = {
url = "github:nmattia/naersk/master"; url = "github:nmattia/naersk/master";

View file

@ -195,63 +195,50 @@ impl Display for MessageType {
impl Message { impl Message {
fn auth() -> Self { fn auth() -> Self {
let mut msg = Self::default(); Self {
msg.message_type = MessageType::Auth; message_type: MessageType::Auth,
msg ..Default::default()
}
} }
fn subscribe_events() -> Self { fn subscribe_events() -> Self {
let mut msg = Self::default(); Self {
msg.message_type = MessageType::SubscribeEvents; message_type: MessageType::SubscribeEvents,
msg ..Default::default()
}
} }
fn get_states() -> Self { fn get_states() -> Self {
let mut msg = Self::default(); Self {
msg.message_type = MessageType::GetStates; message_type: MessageType::GetStates,
msg ..Default::default()
}
} }
fn ping() -> Self { fn ping() -> Self {
let mut msg = Self::default(); Self {
msg.message_type = MessageType::Ping; message_type: MessageType::Ping,
msg ..Default::default()
}
} }
fn pong() -> Self { fn pong() -> Self {
let mut msg = Self::default(); Self {
msg.message_type = MessageType::Pong; message_type: MessageType::Pong,
msg ..Default::default()
}
} }
fn to_json(self) -> String { fn to_json(&self) -> String {
serde_json::to_string(&self).unwrap() serde_json::to_string(&self).unwrap()
} }
fn to_message(self) -> tungstenite::Message { fn to_message(&self) -> tungstenite::Message {
tungstenite::Message::from(self.to_json()) tungstenite::Message::from(self.to_json())
} }
} }
impl HomeAssistant { impl HomeAssistant {
// pub(crate) fn new(
// host: String,
// entity: String,
// insecure: bool,
// token: String,
// format: OutputFormat,
// ) -> Self {
// Self {
// token,
// entity,
// insecure,
// host,
// format,
// id: 1,
// auth_complete: false,
// }
// }
pub(crate) fn builder() -> HomeAssistantBuilder { pub(crate) fn builder() -> HomeAssistantBuilder {
HomeAssistantBuilder::new() HomeAssistantBuilder::new()
} }
@ -278,32 +265,36 @@ impl HomeAssistant {
loop { loop {
tokio::select! { tokio::select! {
msg = ws_stream.next() => match msg { msg = ws_stream.next() => match msg {
Some(msg) => { Some(msg) => match msg {
let msg = msg?; Ok(msg) => {
match self.handle_message(msg).await {
match self.handle_message(msg).await { Ok(response_messages) => {
Ok(response_messages) => { for response in response_messages {
for response in response_messages { ws_stream.send(response).await?
ws_stream.send(response).await? }
} }
} Err(e) => {
Err(e) => { if let Some(err) = e.downcast_ref::<HomeAssistantError>() {
if let Some(err) = e.downcast_ref::<HomeAssistantError>() { match err {
match err { HomeAssistantError::EmptyMessage => {
HomeAssistantError::EmptyMessage => { debug!("Received empty message, ignoring...")
debug!("Received empty message, ignoring...") }
} HomeAssistantError::UnhandledMessage(message_type) => {
HomeAssistantError::UnhandledMessage(message_type) => { debug!(
debug!( "Received '{message_type}', we're currently not handling that."
"Received '{message_type}', we're currently not handling that." )
) }
} HomeAssistantError::JsonError(e) => error!("{e}"),
HomeAssistantError::JsonError(e) => error!("{e}"), }
} else {
error!("{e}");
} }
} else {
error!("{e}");
} }
} }
},
Err(e) => {
error!("Error in receiving message: {e}");
break;
} }
} }
None => break, None => break,
@ -327,7 +318,7 @@ impl HomeAssistant {
&mut self, &mut self,
msg: tungstenite::Message, msg: tungstenite::Message,
) -> Result<Vec<tungstenite::Message>> { ) -> Result<Vec<tungstenite::Message>> {
if msg.len() == 0 { if msg.is_empty() {
bail!(HomeAssistantError::EmptyMessage); bail!(HomeAssistantError::EmptyMessage);
} }
@ -417,7 +408,7 @@ impl HomeAssistant {
let is_muted = attributes["is_volume_muted"].as_bool().unwrap_or(false); let is_muted = attributes["is_volume_muted"].as_bool().unwrap_or(false);
let volume = if is_muted { let volume = if is_muted {
String::from("[ \u{fc5d} ]") String::from("[ \u{f466} ] ")
} else if volume_raw >= 0. { } else if volume_raw >= 0. {
format!("[{:3.0}%] ", volume_raw * 100.) format!("[{:3.0}%] ", volume_raw * 100.)
} else { } else {