From c4515f9b6a1b7d4ca802c6ebedebcb20e8cf5ef4 Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Tue, 12 Apr 2022 23:10:14 +0200 Subject: [PATCH 1/2] Flush stdout after print, get initial state --- src/homeassistant.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/homeassistant.rs b/src/homeassistant.rs index da4577c..ec230a3 100644 --- a/src/homeassistant.rs +++ b/src/homeassistant.rs @@ -1,4 +1,4 @@ -use std::fmt::Display; +use std::{fmt::Display, io::Write}; use async_tungstenite::{tokio::connect_async, tungstenite}; use color_eyre::{eyre::bail, Result}; @@ -324,12 +324,30 @@ impl HomeAssistant { let output = self .format_state(event.data.new_state.state, event.data.new_state.attributes); println!("{output}"); + std::io::stdout().flush()?; } vec![] } MessageType::Result => { - trace!("{}", message.to_json()); + trace!("Result: {:?}", message); + + if let Some(result) = message.result { + if let Some(items) = result.as_array() { + if let Some(entity) = items.iter().find(|&item| { + item.as_object() + .unwrap() + .get::(&String::from("entity_id")) + .unwrap() + .eq(&self.entity) + }) { + let state: EntityState = + serde_json::from_value(entity.clone()).unwrap(); + println!("{}", self.format_state(state.state, state.attributes)); + std::io::stdout().flush()?; + } + } + } vec![] } From 08b294c9accf75ab3d6af6fca8ceb912eccd7f9f Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Tue, 12 Apr 2022 23:12:00 +0200 Subject: [PATCH 2/2] Bump version to 0.4.1 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e08c6b5..2f8b77a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -421,7 +421,7 @@ dependencies = [ [[package]] name = "ha-now-playing" -version = "0.4.0" +version = "0.4.1" dependencies = [ "anyhow", "async-tungstenite", diff --git a/Cargo.toml b/Cargo.toml index c63cf21..6212d84 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.0" +version = "0.4.1" authors = ["Erwin Boskma "] edition = "2021"