btsensor 0.1.2

A library for decoding sensor readings from BLE advertisements, including the BTHome format.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! Support for the [BTHome](https://bthome.io/) v1 format.

use super::DecodeError;
use super::events::{ButtonEventType, DimmerEventType, Event};
use crate::uuid_from_u16;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::fmt::{self, Display, Formatter};
use uuid::Uuid;

/// The service data UUID used for unencrypted BTHome v1 advertisements.
pub const UNENCRYPTED_UUID: Uuid = uuid_from_u16(0x181c);

/// The service data UUID used for encrypted BTHome v1 advertisements.
pub const ENCRYPTED_UUID: Uuid = uuid_from_u16(0x181e);

/// A single element of a BTHome v1 advertisement: either a sensor reading or an event.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Element {
    Sensor(Sensor),
    Event(Event),
}

impl Element {
    /// Constructs a new sensor reading element for the given property, with an unsigned integer
    /// value.
    pub fn new_unsigned(property: Property, value: u32) -> Self {
        Self::Sensor(Sensor {
            property,
            value: Value::UnsignedInt(value),
        })
    }

    /// Constructs a new sensor reading element for the given property, with a signed integer value.
    pub fn new_signed(property: Property, value: i32) -> Self {
        Self::Sensor(Sensor {
            property,
            value: Value::SignedInt(value),
        })
    }

    /// Constructs a new element for the given event.
    pub fn new_event(event: Event) -> Self {
        Self::Event(event)
    }

    fn decode(format: DataType, data: &[u8]) -> Result<Self, DecodeError> {
        let property = Property::try_from(data[0])?;
        let value = &data[1..];
        match property {
            Property::ButtonEvent => {
                let event_type = ButtonEventType::from_bytes(value)?;
                let event = Event::Button(event_type);
                Ok(Self::Event(event))
            }
            Property::DimmerEvent => {
                let event_type = DimmerEventType::from_bytes(value)?;
                let event = Event::Dimmer(event_type);
                Ok(Self::Event(event))
            }
            _ => Ok(Self::Sensor(Sensor::decode(format, property, value)?)),
        }
    }

    /// Attempts to decode the given service data as a BTHome v1 advertisement.
    pub fn decode_all(mut data: &[u8]) -> Result<Vec<Self>, DecodeError> {
        let mut elements = Vec::new();

        while data.len() > 2 {
            let length_format = data[0];
            let length = length_format & 0x1f;
            let element_end = usize::from(length) + 1;
            // length includes the measurement type byte but not the length/format byte.
            if data.len() <= length.into() {
                return Err(DecodeError::PrematureEnd);
            }
            let format = ((length_format & 0xe0) >> 5).try_into()?;
            elements.push(Self::decode(format, &data[1..element_end])?);

            data = &data[element_end..];
        }

        if data.is_empty() {
            Ok(elements)
        } else {
            Err(DecodeError::ExtraData(data.to_owned()))
        }
    }
}

impl Display for Element {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::Sensor(sensor) => sensor.fmt(f),
            Self::Event(event) => event.fmt(f),
        }
    }
}

/// A sensor reading from a device.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Sensor {
    /// The property for which the reading is taken. This implies the unit and scale.
    pub property: Property,
    /// The raw value of the reading as sent by the device, before the scaling factor for the
    /// property is applied.
    value: Value,
}

impl Sensor {
    /// Returns the value of the reading as a floating-point number, properly scaled according to
    /// the property it is for.
    pub fn value_float(&self) -> f64 {
        f64::from(&self.value) / self.property.denominator()
    }

    /// Returns the integer value of the reading, if it is for a property with no scaling factor.
    ///
    /// Returns `None` if the property has a scaling factor meaning that the value may not be an
    /// integer.
    pub fn value_int(&self) -> Option<i64> {
        if self.property.denominator() == 1.0 {
            Some((&self.value).into())
        } else {
            None
        }
    }

    /// Returns the boolean value of the reading, if it is a boolean property and has a valid value.
    pub fn value_bool(&self) -> Option<bool> {
        if self.property.is_boolean() {
            match &self.value {
                Value::UnsignedInt(0) => Some(false),
                Value::UnsignedInt(1) => Some(true),
                _ => None,
            }
        } else {
            None
        }
    }

    fn decode(format: DataType, property: Property, value: &[u8]) -> Result<Self, DecodeError> {
        let value = Value::from_bytes(value, format)?;
        Ok(Self { property, value })
    }
}

impl Display for Sensor {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        // TODO: Special handling for timestamp.
        if let Some(value) = self.value_bool() {
            write!(f, "{}: {}", self.property, value)
        } else if let Some(value) = self.value_int() {
            write!(f, "{}: {}{}", self.property, value, self.property.unit())
        } else {
            write!(
                f,
                "{}: {}{}",
                self.property,
                self.value_float(),
                self.property.unit(),
            )
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)]
#[num_enum(error_type(name = DecodeError, constructor = DecodeError::InvalidDataType))]
#[repr(u8)]
pub enum DataType {
    UnsignedInt = 0b000,
    SignedInt = 0b001,
    Float = 0b010,
    String = 0b011,
    Mac = 0b100,
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum Value {
    UnsignedInt(u32),
    SignedInt(i32),
}

impl Value {
    fn from_bytes(bytes: &[u8], format: DataType) -> Result<Self, DecodeError> {
        match (format, bytes.len()) {
            (DataType::UnsignedInt, 4) => Ok(Self::UnsignedInt(u32::from_le_bytes(
                bytes.try_into().unwrap(),
            ))),
            (DataType::UnsignedInt, 3) => Ok(Self::UnsignedInt(
                bytes[0] as u32 | (bytes[1] as u32) << 8 | (bytes[2] as u32) << 16,
            )),
            (DataType::UnsignedInt, 2) => Ok(Self::UnsignedInt(
                u16::from_le_bytes(bytes.try_into().unwrap()).into(),
            )),
            (DataType::UnsignedInt, 1) => Ok(Self::UnsignedInt(bytes[0].into())),
            (DataType::SignedInt, 4) => Ok(Self::SignedInt(i32::from_le_bytes(
                bytes.try_into().unwrap(),
            ))),
            (DataType::SignedInt, 3) => Ok(Self::SignedInt(
                bytes[0] as i32 | (bytes[1] as i32) << 8 | (bytes[2] as i32) << 16,
            )),
            (DataType::SignedInt, 2) => Ok(Self::SignedInt(
                i16::from_le_bytes(bytes.try_into().unwrap()).into(),
            )),
            (DataType::SignedInt, 1) => Ok(Self::SignedInt((bytes[0] as i8).into())),
            _ => Err(DecodeError::UnsupportedFormat(format)),
        }
    }
}

impl From<&Value> for f64 {
    fn from(value: &Value) -> Self {
        match *value {
            Value::SignedInt(v) => v.into(),
            Value::UnsignedInt(v) => v.into(),
        }
    }
}

impl From<&Value> for i64 {
    fn from(value: &Value) -> Self {
        match *value {
            Value::SignedInt(v) => v.into(),
            Value::UnsignedInt(v) => v.into(),
        }
    }
}

/// A BTHome v1 property type.
///
/// Each property type has an associated unit and scale defined by the
/// standard.
#[derive(Copy, Clone, Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)]
#[num_enum(error_type(name = DecodeError, constructor = DecodeError::InvalidProperty))]
#[repr(u8)]
pub enum Property {
    // Misc data.
    PacketId = 0x00,

    // Sensor data.
    Battery = 0x01,
    Temperature = 0x02,
    Humidity = 0x03,
    HumidityShort = 0x2e,
    Pressure = 0x04,
    Illuminance = 0x05,
    MassKg = 0x06,
    MassLb = 0x07,
    Dewpoint = 0x08,
    Count = 0x09,
    Energy = 0x0a,
    Power = 0x0b,
    Voltage = 0x0c,
    Pm2_5 = 0x0d,
    Pm10 = 0x0e,
    Co2 = 0x12,
    Tvoc = 0x13,
    Moisture = 0x14,
    MoistureShort = 0x2f,
    Timestamp = 0x50,
    Acceleration = 0x51,
    Gyroscope = 0x52,

    // Binary sensor data.
    GenericBoolean = 0x0f,
    PowerOn = 0x10,
    Open = 0x11,
    BatteryLow = 0x15,
    BatteryCharging = 0x16,
    CarbonMonoxideDetected = 0x17,
    Cold = 0x18,
    Connected = 0x19,
    DoorOpen = 0x1a,
    GarageDoorOpen = 0x1b,
    GasDetected = 0x1c,
    HeatAbnormal = 0x1d,
    LightDetected = 0x1e,
    Unlocked = 0x1f,
    Wet = 0x20,
    MotionDetected = 0x21,
    Moving = 0x22,
    OccupancyDetected = 0x23,
    PluggedIn = 0x24,
    Home = 0x25,
    Problem = 0x26,
    Running = 0x27,
    Safe = 0x28,
    SmokeDetected = 0x29,
    Sound = 0x2a,
    Tamper = 0x2b,
    VibrationDetected = 0x2c,
    WindowOpen = 0x2d,

    // Events.
    ButtonEvent = 0x3a,
    DimmerEvent = 0x3c,
}

impl Display for Property {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(self.name())
    }
}

impl Property {
    /// Returns the name of the property.
    pub fn name(self) -> &'static str {
        match self {
            Self::PacketId => "packet ID",
            Self::Battery => "battery",
            Self::Temperature => "temperature",
            Self::Humidity | Self::HumidityShort => "humidity",
            Self::Pressure => "pressure",
            Self::Illuminance => "illuminance",
            Self::MassKg | Self::MassLb => "mass",
            Self::Dewpoint => "dew point",
            Self::Count => "count",
            Self::Energy => "energy",
            Self::Power => "power",
            Self::Voltage => "voltage",
            Self::Pm2_5 => "pm2.5",
            Self::Pm10 => "pm10",
            Self::Co2 => "CO2",
            Self::Tvoc => "tvoc",
            Self::Moisture | Self::MoistureShort => "moisture",
            Self::Timestamp => "timestamp",
            Self::Acceleration => "acceleration",
            Self::Gyroscope => "gyroscope",
            Self::GenericBoolean => "generic boolean",
            Self::PowerOn => "power on",
            Self::Open => "open",
            Self::BatteryLow => "battery low",
            Self::BatteryCharging => "battery charging",
            Self::CarbonMonoxideDetected => "carbon monoxide detected",
            Self::Cold => "cold",
            Self::Connected => "connected",
            Self::DoorOpen => "door open",
            Self::GarageDoorOpen => "garage door open",
            Self::GasDetected => "gas detected",
            Self::HeatAbnormal => "abnormal heat",
            Self::LightDetected => "light detected",
            Self::Unlocked => "unlocked",
            Self::Wet => "wet",
            Self::MotionDetected => "motion detected",
            Self::Moving => "moving",
            Self::OccupancyDetected => "occupancy detected",
            Self::PluggedIn => "plugged in",
            Self::Home => "home",
            Self::Problem => "problem",
            Self::Running => "running",
            Self::Safe => "safe",
            Self::SmokeDetected => "smoke detected",
            Self::Sound => "sound detected",
            Self::Tamper => "tampered",
            Self::VibrationDetected => "vibration detected",
            Self::WindowOpen => "window open",
            Self::ButtonEvent => "button event",
            Self::DimmerEvent => "dimmer event",
        }
    }

    /// Returns the standard unit for the property.
    pub fn unit(self) -> &'static str {
        match self {
            Self::PacketId
            | Self::Count
            | Self::Timestamp
            | Self::GenericBoolean
            | Self::PowerOn
            | Self::Open
            | Self::BatteryLow
            | Self::BatteryCharging
            | Self::CarbonMonoxideDetected
            | Self::Cold
            | Self::Connected
            | Self::DoorOpen
            | Self::GarageDoorOpen
            | Self::GasDetected
            | Self::HeatAbnormal
            | Self::LightDetected
            | Self::Unlocked
            | Self::Wet
            | Self::MotionDetected
            | Self::Moving
            | Self::OccupancyDetected
            | Self::PluggedIn
            | Self::Home
            | Self::Problem
            | Self::Running
            | Self::Safe
            | Self::SmokeDetected
            | Self::Sound
            | Self::Tamper
            | Self::VibrationDetected
            | Self::WindowOpen
            | Self::ButtonEvent
            | Self::DimmerEvent => "",
            Self::Battery
            | Self::Humidity
            | Self::HumidityShort
            | Self::Moisture
            | Self::MoistureShort => "%",
            Self::Temperature => "°C",
            Self::Pressure => "hPa",
            Self::Illuminance => "lux",
            Self::MassKg => "kg",
            Self::MassLb => "lb",
            Self::Dewpoint => "°C",
            Self::Energy => "kWh",
            Self::Power => "W",
            Self::Voltage => "V",
            Self::Pm2_5 | Self::Pm10 | Self::Tvoc => "ug/m3",
            Self::Co2 => "ppm",
            Self::Acceleration => "m/s²",
            Self::Gyroscope => "°/s",
        }
    }

    /// The denominator for fixed-point values.
    ///
    /// In other words, the value stored should be divided by this number to get the actual value.
    fn denominator(self) -> f64 {
        match self {
            Self::PacketId
            | Self::Battery
            | Self::HumidityShort
            | Self::Count
            | Self::Pm2_5
            | Self::Pm10
            | Self::Co2
            | Self::Tvoc
            | Self::MoistureShort
            | Self::Timestamp
            | Self::GenericBoolean
            | Self::PowerOn
            | Self::Open
            | Self::BatteryLow
            | Self::BatteryCharging
            | Self::CarbonMonoxideDetected
            | Self::Cold
            | Self::Connected
            | Self::DoorOpen
            | Self::GarageDoorOpen
            | Self::GasDetected
            | Self::HeatAbnormal
            | Self::LightDetected
            | Self::Unlocked
            | Self::Wet
            | Self::MotionDetected
            | Self::Moving
            | Self::OccupancyDetected
            | Self::PluggedIn
            | Self::Home
            | Self::Problem
            | Self::Running
            | Self::Safe
            | Self::SmokeDetected
            | Self::Sound
            | Self::Tamper
            | Self::VibrationDetected
            | Self::WindowOpen
            | Self::ButtonEvent
            | Self::DimmerEvent => 1.0,
            Self::Temperature
            | Self::Humidity
            | Self::Pressure
            | Self::Illuminance
            | Self::MassKg
            | Self::MassLb
            | Self::Dewpoint
            | Self::Power
            | Self::Moisture => 100.0,
            Self::Energy | Self::Voltage | Self::Acceleration | Self::Gyroscope => 1000.0,
        }
    }

    fn is_boolean(self) -> bool {
        matches!(
            self,
            Self::GenericBoolean
                | Self::PowerOn
                | Self::Open
                | Self::BatteryLow
                | Self::BatteryCharging
                | Self::CarbonMonoxideDetected
                | Self::Cold
                | Self::Connected
                | Self::DoorOpen
                | Self::GarageDoorOpen
                | Self::GasDetected
                | Self::HeatAbnormal
                | Self::LightDetected
                | Self::Unlocked
                | Self::Wet
                | Self::MotionDetected
                | Self::Moving
                | Self::OccupancyDetected
                | Self::PluggedIn
                | Self::Home
                | Self::Problem
                | Self::Running
                | Self::Safe
                | Self::SmokeDetected
                | Self::Sound
                | Self::Tamper
                | Self::VibrationDetected
                | Self::WindowOpen
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn decode_valid() {
        assert_eq!(
            Element::decode_all(&[0x23, 0x02, 0xC4, 0x09, 0x03, 0x03, 0xBF, 0x13]).unwrap(),
            vec![
                Element::Sensor(Sensor {
                    property: Property::Temperature,
                    value: Value::SignedInt(2500),
                }),
                Element::Sensor(Sensor {
                    property: Property::Humidity,
                    value: Value::UnsignedInt(5055),
                }),
            ]
        );
        assert_eq!(
            Element::decode_all(&[2, 0, 140, 35, 2, 203, 8, 3, 3, 171, 20, 2, 1, 100]).unwrap(),
            vec![
                Element::Sensor(Sensor {
                    property: Property::PacketId,
                    value: Value::UnsignedInt(140),
                }),
                Element::Sensor(Sensor {
                    property: Property::Temperature,
                    value: Value::SignedInt(2251),
                }),
                Element::Sensor(Sensor {
                    property: Property::Humidity,
                    value: Value::UnsignedInt(5291),
                }),
                Element::Sensor(Sensor {
                    property: Property::Battery,
                    value: Value::UnsignedInt(100),
                }),
            ]
        );
        assert_eq!(
            Element::decode_all(&[2, 0, 137, 2, 16, 0, 3, 12, 182, 11]).unwrap(),
            vec![
                Element::Sensor(Sensor {
                    property: Property::PacketId,
                    value: Value::UnsignedInt(137),
                }),
                Element::Sensor(Sensor {
                    property: Property::PowerOn,
                    value: Value::UnsignedInt(0),
                }),
                Element::Sensor(Sensor {
                    property: Property::Voltage,
                    value: Value::UnsignedInt(2998),
                }),
            ]
        );
    }

    #[test]
    fn decode_button_events() {
        assert_eq!(
            Element::decode_all(&[0x02, 0x3a, 0x00, 0x02, 0x3a, 0x05]).unwrap(),
            vec![
                Element::Event(Event::Button(None)),
                Element::Event(Event::Button(Some(ButtonEventType::LongDoublePress))),
            ]
        );
    }

    #[test]
    fn decode_dimmer_events() {
        assert_eq!(
            Element::decode_all(&[0x02, 0x3c, 0x00, 0x03, 0x3c, 0x01, 0x03]).unwrap(),
            vec![
                Element::Event(Event::Dimmer(None)),
                Element::Event(Event::Dimmer(Some(DimmerEventType::RotateLeft(3)))),
            ]
        );
    }

    #[test]
    fn format_sensor_element() {
        assert_eq!(
            Element::Sensor(Sensor {
                property: Property::Humidity,
                value: Value::UnsignedInt(5055),
            })
            .to_string(),
            "humidity: 50.55%"
        );
        assert_eq!(
            Element::Sensor(Sensor {
                property: Property::Temperature,
                value: Value::SignedInt(2500),
            })
            .to_string(),
            "temperature: 25°C"
        );
        assert_eq!(
            Element::Sensor(Sensor {
                property: Property::HumidityShort,
                value: Value::UnsignedInt(42),
            })
            .to_string(),
            "humidity: 42%"
        );
    }

    #[test]
    fn format_boolean_element() {
        assert_eq!(
            Element::Sensor(Sensor {
                property: Property::BatteryLow,
                value: Value::UnsignedInt(0),
            })
            .to_string(),
            "battery low: false"
        );
        assert_eq!(
            Element::Sensor(Sensor {
                property: Property::LightDetected,
                value: Value::UnsignedInt(1),
            })
            .to_string(),
            "light detected: true"
        );
    }

    #[test]
    fn format_event_element() {
        assert_eq!(
            Element::Event(Event::Button(None)).to_string(),
            "button: none"
        );
        assert_eq!(
            Element::Event(Event::Button(Some(ButtonEventType::LongDoublePress))).to_string(),
            "button: long double press"
        );
        assert_eq!(
            Element::Event(Event::Dimmer(Some(DimmerEventType::RotateRight(42)))).to_string(),
            "dimmer: rotate right 42 steps"
        );
    }
}