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 {
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,9 +265,8 @@ impl HomeAssistant {
loop {
tokio::select! {
msg = ws_stream.next() => match msg {
Some(msg) => {
let msg = msg?;
Some(msg) => match msg {
Ok(msg) => {
match self.handle_message(msg).await {
Ok(response_messages) => {
for response in response_messages {
@ -305,6 +291,11 @@ impl HomeAssistant {
}
}
}
},
Err(e) => {
error!("Error in receiving message: {e}");
break;
}
}
None => break,
},
@ -327,7 +318,7 @@ impl HomeAssistant {
&mut self,
msg: tungstenite::Message,
) -> Result<Vec<tungstenite::Message>> {
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 {