Flush stdout after print, get initial state

This commit is contained in:
Erwin Boskma 2022-04-12 23:10:14 +02:00
parent 4f8155c1a6
commit c4515f9b6a
Signed by: erwin
GPG key ID: 270B20D17394F7E5

View file

@ -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>(&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![]
}