diff --git a/Cargo.lock b/Cargo.lock index a27fcb6..3d2fe83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -421,7 +421,7 @@ dependencies = [ [[package]] name = "ha-now-playing" -version = "0.4.2" +version = "0.4.3" dependencies = [ "anyhow", "async-tungstenite", diff --git a/Cargo.toml b/Cargo.toml index c77c143..3e5c36a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ha-now-playing" description = "Retrieves the state of a media_player entity from Home Assistant" -version = "0.4.2" +version = "0.4.3" authors = ["Erwin Boskma "] edition = "2021" diff --git a/flake.lock b/flake.lock index a8b73b6..bfda706 100644 --- a/flake.lock +++ b/flake.lock @@ -7,11 +7,11 @@ ] }, "locked": { - "lastModified": 1649096192, - "narHash": "sha256-7O8e+eZEYeU+ET98u/zW5epuoN/xYx9G+CIh4DjZVzY=", + "lastModified": 1651574473, + "narHash": "sha256-wQhFORvRjo8LB2hTmETmv6cbyKGDPbfWqvZ/0chnDE4=", "owner": "nmattia", "repo": "naersk", - "rev": "d626f73332a8f587b613b0afe7293dd0777be07d", + "rev": "f21309b38e1da0d61b881b6b6d41b81c1aed4e1d", "type": "github" }, "original": { @@ -23,16 +23,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1648219316, - "narHash": "sha256-Ctij+dOi0ZZIfX5eMhgwugfvB+WZSrvVNAyAuANOsnQ=", + "lastModified": 1652172129, + "narHash": "sha256-8e2JMrswaKe02P8rYfvIMoc59pNuw6h/GYu8DBE1c+0=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "30d3d79b7d3607d56546dd2a6b49e156ba0ec634", + "rev": "f419dc5763c2b3c5580e396dea065b6d8b58ee27", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixpkgs-unstable", + "ref": "nixos-unstable", "repo": "nixpkgs", "type": "github" } @@ -55,11 +55,11 @@ ] }, "locked": { - "lastModified": 1649298784, - "narHash": "sha256-rUwQKjBOZiRpzgkG3h9OM5pUyxRYIV45ohgqZJtSZtE=", + "lastModified": 1652236937, + "narHash": "sha256-anMSkIvaxB3jV74nTvPcE8fH7bZ5LPuc2wXGvt50Oe0=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "c6fe2a14f10cb85659b8848bac87e4ff29a00f9a", + "rev": "18c816d2d10317f4a8816925a12cb4bbdd0821de", "type": "github" }, "original": { @@ -70,11 +70,11 @@ }, "utils": { "locked": { - "lastModified": 1648297722, - "narHash": "sha256-W+qlPsiZd8F3XkzXOzAoR+mpFqzm3ekQkJNa+PIh1BQ=", + "lastModified": 1649676176, + "narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=", "owner": "numtide", "repo": "flake-utils", - "rev": "0f8662f1319ad6abf89b3380dd2722369fc51ade", + "rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index feb106e..7478b2b 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,7 @@ { inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; utils.url = "github:numtide/flake-utils"; naersk = { url = "github:nmattia/naersk/master"; diff --git a/src/homeassistant.rs b/src/homeassistant.rs index 0a72541..3e53759 100644 --- a/src/homeassistant.rs +++ b/src/homeassistant.rs @@ -195,63 +195,50 @@ impl Display for MessageType { impl Message { fn auth() -> Self { - let mut msg = Self::default(); - msg.message_type = MessageType::Auth; - msg + Self { + message_type: MessageType::Auth, + ..Default::default() + } } fn subscribe_events() -> Self { - let mut msg = Self::default(); - msg.message_type = MessageType::SubscribeEvents; - msg + Self { + message_type: MessageType::SubscribeEvents, + ..Default::default() + } } fn get_states() -> Self { - let mut msg = Self::default(); - msg.message_type = MessageType::GetStates; - msg + Self { + message_type: MessageType::GetStates, + ..Default::default() + } } fn ping() -> Self { - let mut msg = Self::default(); - msg.message_type = MessageType::Ping; - msg + Self { + message_type: MessageType::Ping, + ..Default::default() + } } fn pong() -> Self { - let mut msg = Self::default(); - msg.message_type = MessageType::Pong; - msg + Self { + message_type: MessageType::Pong, + ..Default::default() + } } - fn to_json(self) -> String { + fn to_json(&self) -> String { serde_json::to_string(&self).unwrap() } - fn to_message(self) -> tungstenite::Message { + fn to_message(&self) -> tungstenite::Message { tungstenite::Message::from(self.to_json()) } } 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 { HomeAssistantBuilder::new() } @@ -278,32 +265,36 @@ impl HomeAssistant { loop { tokio::select! { msg = ws_stream.next() => match msg { - Some(msg) => { - let msg = msg?; - - match self.handle_message(msg).await { - Ok(response_messages) => { - for response in response_messages { - ws_stream.send(response).await? - } - } - Err(e) => { - if let Some(err) = e.downcast_ref::() { - match err { - HomeAssistantError::EmptyMessage => { - debug!("Received empty message, ignoring...") - } - HomeAssistantError::UnhandledMessage(message_type) => { - debug!( - "Received '{message_type}', we're currently not handling that." - ) - } - HomeAssistantError::JsonError(e) => error!("{e}"), + Some(msg) => match msg { + Ok(msg) => { + match self.handle_message(msg).await { + Ok(response_messages) => { + for response in response_messages { + ws_stream.send(response).await? + } + } + Err(e) => { + if let Some(err) = e.downcast_ref::() { + match err { + HomeAssistantError::EmptyMessage => { + debug!("Received empty message, ignoring...") + } + HomeAssistantError::UnhandledMessage(message_type) => { + debug!( + "Received '{message_type}', we're currently not handling that." + ) + } + HomeAssistantError::JsonError(e) => error!("{e}"), + } + } else { + error!("{e}"); } - } else { - error!("{e}"); } } + }, + Err(e) => { + error!("Error in receiving message: {e}"); + break; } } None => break, @@ -327,7 +318,7 @@ impl HomeAssistant { &mut self, msg: tungstenite::Message, ) -> Result> { - if msg.len() == 0 { + if msg.is_empty() { bail!(HomeAssistantError::EmptyMessage); } @@ -417,7 +408,7 @@ impl HomeAssistant { let is_muted = attributes["is_volume_muted"].as_bool().unwrap_or(false); let volume = if is_muted { - String::from("[ \u{fc5d} ]") + String::from("[ \u{f466} ] ") } else if volume_raw >= 0. { format!("[{:3.0}%] ", volume_raw * 100.) } else {