Minor changes

- Fix spacing when muted
- Change mute icon
- Improve Message struct
This commit is contained in:
Erwin Boskma 2022-05-11 16:18:10 +02:00
parent a43bb27461
commit 090ffaedf4
Signed by: erwin
GPG key ID: 270B20D17394F7E5

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 {