From c4515f9b6a1b7d4ca802c6ebedebcb20e8cf5ef4 Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Tue, 12 Apr 2022 23:10:14 +0200 Subject: [PATCH] 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![] }