Skip to content

Commit

Permalink
Merge pull request #499 from alsuren/packetid
Browse files Browse the repository at this point in the history
Fix panic displaying BTHome V2 reading with packet ID.
  • Loading branch information
qwandor authored May 17, 2024
2 parents efa0fd3 + f52785e commit 3439e5c
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions btsensor/src/bthome/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl Element {
/// Returns `None` if it is an event, floating-point or boolean property.
pub fn value_int(&self) -> Option<i64> {
match *self {
Self::PacketId(value) => Some(value.into()),
Self::Battery(value) => Some(value.into()),
Self::Co2(value) => Some(value.into()),
Self::Count8(value) => Some(value.into()),
Expand Down Expand Up @@ -471,6 +472,23 @@ mod tests {
);
}

#[test]
fn decode_mho() {
assert_eq!(
BtHomeV2::decode(&[64, 0, 168, 1, 100, 2, 203, 9, 3, 38, 17]).unwrap(),
BtHomeV2 {
encrypted: false,
trigger_based: false,
elements: vec![
Element::PacketId(168),
Element::Battery(100),
Element::TemperatureSmall(2507),
Element::Humidity(4390)
]
}
);
}

#[test]
fn format() {
assert_eq!(
Expand All @@ -489,5 +507,19 @@ mod tests {
.to_string(),
"(unencrypted) acceleration: 22.151m/s², temperature: 25.06°C, battery charging: true, button: none, button: long double press, dimmer: rotate left 3 steps"
);
assert_eq!(
BtHomeV2 {
encrypted: false,
trigger_based: false,
elements: vec![
Element::PacketId(168),
Element::Battery(100),
Element::TemperatureSmall(2507),
Element::Humidity(4390),
],
}
.to_string(),
"(unencrypted) packet ID: 168, battery: 100%, temperature: 25.07°C, humidity: 43.9%"
);
}
}

0 comments on commit 3439e5c

Please sign in to comment.