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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
use crate::values::{ColorFormat, EnumValue, Value, ValueError};
use std::collections::HashMap;
use std::fmt::{self, Debug, Display, Formatter};
use std::ops::RangeInclusive;
use std::str::FromStr;
use std::time::Duration;
use thiserror::Error;

/// The state of a Homie device according to the Homie
/// [device lifecycle](https://homieiot.github.io/specification/#device-lifecycle).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum State {
    /// The state of the device is not yet known to the controller because device discovery is still
    /// underway.
    Unknown,
    /// The device is connected to the MQTT broker but is not yet ready to operate.
    Init,
    /// The device is connected and operational.
    Ready,
    /// The device has cleanly disconnected from the MQTT broker.
    Disconnected,
    /// The device is currently sleeping.
    Sleeping,
    /// The device was uncleanly disconnected from the MQTT broker. This could happen due to a
    /// network issue, power failure or some other unexpected failure.
    Lost,
    /// The device is connected to the MQTT broker but something is wrong and it may require human
    /// intervention.
    Alert,
}

impl State {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Unknown => "unknown",
            Self::Init => "init",
            Self::Ready => "ready",
            Self::Disconnected => "disconnected",
            Self::Sleeping => "sleeping",
            Self::Lost => "lost",
            Self::Alert => "alert",
        }
    }
}

/// An error which can be returned when parsing a `State` from a string, if the string does not
/// match a valid Homie
/// [device lifecycle](https://homieiot.github.io/specification/#device-lifecycle) state.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
#[error("Invalid state '{0}'")]
pub struct ParseStateError(String);

impl FromStr for State {
    type Err = ParseStateError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "init" => Ok(Self::Init),
            "ready" => Ok(Self::Ready),
            "disconnected" => Ok(Self::Disconnected),
            "sleeping" => Ok(Self::Sleeping),
            "lost" => Ok(Self::Lost),
            "alert" => Ok(Self::Alert),
            _ => Err(ParseStateError(s.to_owned())),
        }
    }
}

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

/// The data type of a Homie property.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Datatype {
    /// A [64-bit signed integer](https://homieiot.github.io/specification/#integer).
    Integer,
    /// A [64-bit floating-point number](https://homieiot.github.io/specification/#float).
    Float,
    /// A [boolean value](https://homieiot.github.io/specification/#boolean).
    Boolean,
    /// A [UTF-8 encoded string](https://homieiot.github.io/specification/#string).
    String,
    /// An [enum value](https://homieiot.github.io/specification/#enum) from a set of possible
    /// values specified by the property format.
    Enum,
    /// An [RGB](enum.ColorFormat.html#variant.Rgb) or [HSV](enum.ColorFormat.html#variant.Hsv)
    /// [color](https://homieiot.github.io/specification/#color), depending on the property
    /// [format](struct.Property.html#method.color_format).
    Color,
}

impl Datatype {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Integer => "integer",
            Self::Float => "float",
            Self::Boolean => "boolean",
            Self::String => "string",
            Self::Enum => "enum",
            Self::Color => "color",
        }
    }
}

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

/// An error which can be returned when parsing a `Datatype` from a string, if the string does not
/// match a valid Homie `$datatype` attribute.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
#[error("Invalid datatype '{0}'")]
pub struct ParseDatatypeError(String);

impl FromStr for Datatype {
    type Err = ParseDatatypeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "integer" => Ok(Self::Integer),
            "float" => Ok(Self::Float),
            "boolean" => Ok(Self::Boolean),
            "string" => Ok(Self::String),
            "enum" => Ok(Self::Enum),
            "color" => Ok(Self::Color),
            _ => Err(ParseDatatypeError(s.to_owned())),
        }
    }
}

/// A [property](https://homieiot.github.io/specification/#properties) of a Homie node.
///
/// The `id`, `name` and `datatype` are required, but might not be available immediately when the
/// property is first discovered. The other attributes are optional.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Property {
    /// The subtopic ID of the property. This is unique per node, and should follow the Homie
    /// [ID format](https://homieiot.github.io/specification/#topic-ids).
    pub id: String,

    /// The human-readable name of the property. This is a required attribute, but might not be
    /// available as soon as the property is first discovered.
    pub name: Option<String>,

    /// The data type of the property. This is a required attribute, but might not be available as
    /// soon as the property is first discovered.
    pub datatype: Option<Datatype>,

    /// Whether the property can be set by the Homie controller. This should be true for properties
    /// like the brightness or power state of a light, and false for things like the temperature
    /// reading of a sensor. It is false by default.
    pub settable: bool,

    /// Whether the property value is retained by the MQTT broker. This is true by default.
    pub retained: bool,

    /// The unit of the property, if any. This may be one of the
    /// [recommended units](https://homieiot.github.io/specification/#property-attributes), or any
    /// other custom unit.
    pub unit: Option<String>,

    /// The format of the property, if any. This should be specified if the datatype is `Enum` or
    /// `Color`, and may be specified if the datatype is `Integer` or `Float`.
    ///
    /// This field holds the raw string received from the device. Use
    /// [color_format](#method.color_format), [enum_values](#method.enum_values) or
    /// [range](#method.range) to parse it according to the datatype of the property.
    pub format: Option<String>,

    /// The current value of the property, if known. This may change frequently.
    ///
    /// This field holds the raw string received from the device. Use [value](#method.value) to
    /// parse it according to the datatype of the property.
    pub value: Option<String>,
}

impl Property {
    /// Create a new property with the given ID.
    ///
    /// # Arguments
    /// * `id`: The subtopic ID for the property. This must be unique per device, and follow the
    ///   Homie [ID format](https://homieiot.github.io/specification/#topic-ids).
    pub(crate) fn new(id: &str) -> Property {
        Property {
            id: id.to_owned(),
            name: None,
            datatype: None,
            settable: false,
            retained: true,
            unit: None,
            format: None,
            value: None,
        }
    }

    /// Returns whether all the required
    /// [attributes](https://homieiot.github.io/specification/#property-attributes) of the property
    /// are filled in.
    pub fn has_required_attributes(&self) -> bool {
        self.name.is_some() && self.datatype.is_some()
    }

    /// The value of the property, parsed as the appropriate Homie `Value` type. This will return
    /// `WrongDatatype` if you try to parse it as a type which doesn't match the datatype declared
    /// by the property.
    pub fn value<T: Value>(&self) -> Result<T, ValueError> {
        T::valid_for(self.datatype, &self.format)?;

        match self.value {
            None => Err(ValueError::Unknown),
            Some(ref value) => value.parse().map_err(|_| ValueError::ParseFailed {
                value: value.to_owned(),
                datatype: T::datatype(),
            }),
        }
    }

    /// If the datatype of the property is `Color`, returns the color format.
    pub fn color_format(&self) -> Result<ColorFormat, ValueError> {
        // If the datatype is known and it isn't color, that's an error. If it's not known, maybe
        // parsing the format will succeed, so try anyway.
        if let Some(actual) = self.datatype {
            if actual != Datatype::Color {
                return Err(ValueError::WrongDatatype {
                    expected: Datatype::Color,
                    actual,
                });
            }
        }

        match self.format {
            None => Err(ValueError::Unknown),
            Some(ref format) => format.parse(),
        }
    }

    /// If the datatype of the property is `Enum`, gets the possible values of the enum.
    pub fn enum_values(&self) -> Result<Vec<&str>, ValueError> {
        EnumValue::valid_for(self.datatype, &self.format)?;

        match self.format {
            None => Err(ValueError::Unknown),
            Some(ref format) => {
                if format.is_empty() {
                    Err(ValueError::WrongFormat {
                        format: "".to_owned(),
                    })
                } else {
                    Ok(format.split(',').collect())
                }
            }
        }
    }

    /// If the dataype of the property is `Integer` or `Float`, gets the allowed range of values (if
    /// any is declared by the device).
    pub fn range<T: Value + Copy>(&self) -> Result<RangeInclusive<T>, ValueError> {
        T::valid_for(self.datatype, &self.format)?;

        match self.format {
            None => Err(ValueError::Unknown),
            Some(ref format) => {
                if let [Ok(start), Ok(end)] = format
                    .splitn(2, ':')
                    .map(|part| part.parse())
                    .collect::<Vec<_>>()
                    .as_slice()
                {
                    Ok(RangeInclusive::new(*start, *end))
                } else {
                    Err(ValueError::WrongFormat {
                        format: format.to_owned(),
                    })
                }
            }
        }
    }
}

/// A [node](https://homieiot.github.io/specification/#nodes) of a Homie device.
///
/// All attributes are required, but might not be available immediately when the node is first
/// discovered.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Node {
    /// The subtopic ID of the node. This is unique per device, and should follow the Homie
    /// [ID format](https://homieiot.github.io/specification/#topic-ids).
    pub id: String,

    /// The human-readable name of the node. This is a required attribute, but might not be
    /// available as soon as the node is first discovered.
    pub name: Option<String>,

    /// The type of the node. This is an arbitrary string. It is a required attribute, but might not
    /// be available as soon as the node is first discovered.
    pub node_type: Option<String>,

    /// The properties of the node, keyed by their IDs. There should be at least one.
    pub properties: HashMap<String, Property>,
}

impl Node {
    /// Create a new node with the given ID.
    ///
    /// # Arguments
    /// * `id`: The subtopic ID for the node. This must be unique per device, and follow the Homie
    ///   [ID format](https://homieiot.github.io/specification/#topic-ids).
    pub(crate) fn new(id: &str) -> Node {
        Node {
            id: id.to_owned(),
            name: None,
            node_type: None,
            properties: HashMap::new(),
        }
    }

    /// Add the given property to the node's set of properties.
    pub(crate) fn add_property(&mut self, property: Property) {
        self.properties.insert(property.id.clone(), property);
    }

    /// Returns whether all the required
    /// [attributes](https://homieiot.github.io/specification/#node-attributes) of the node and its
    /// properties are filled in.
    pub fn has_required_attributes(&self) -> bool {
        self.name.is_some()
            && self.node_type.is_some()
            && !self.properties.is_empty()
            && self
                .properties
                .values()
                .all(|property| property.has_required_attributes())
    }
}

/// A Homie [extension](https://homieiot.github.io/extensions/) supported by a device.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Extension {
    /// The identifier of the extension. This should be a reverse domain name followed by some
    /// suffix.
    pub id: String,
    /// The version of the extension.
    pub version: String,
    /// The versions of the Homie spec which the extension supports.
    pub homie_versions: Vec<String>,
}

/// An error which can be returned when parsing an `Extension` from a string.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
#[error("Invalid extension '{0}'")]
pub struct ParseExtensionError(String);

impl FromStr for Extension {
    type Err = ParseExtensionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<_> = s.split(':').collect();
        if let [id, version, homie_versions] = parts.as_slice() {
            if let Some(homie_versions) = homie_versions.strip_prefix('[') {
                if let Some(homie_versions) = homie_versions.strip_suffix(']') {
                    return Ok(Extension {
                        id: (*id).to_owned(),
                        version: (*version).to_owned(),
                        homie_versions: homie_versions.split(';').map(|p| p.to_owned()).collect(),
                    });
                }
            }
        }
        Err(ParseExtensionError(s.to_owned()))
    }
}

/// A Homie [device](https://homieiot.github.io/specification/#devices) which has been discovered.
///
/// The `id`, `homie_version`, `name` and `state` are required, but might not be available
/// immediately when the device is first discovered. The `implementation` is optional.
#[derive(Clone, Debug, PartialEq)]
pub struct Device {
    /// The subtopic ID of the device. This is unique per Homie base topic, and should follow the
    /// Homie [ID format](https://homieiot.github.io/specification/#topic-ids).
    pub id: String,

    /// The version of the Homie convention which the device implements.
    pub homie_version: String,

    /// The human-readable name of the device. This is a required attribute, but might not be
    /// available as soon as the device is first discovered.
    pub name: Option<String>,

    /// The current state of the device according to the Homie
    /// [device lifecycle](https://homieiot.github.io/specification/#device-lifecycle).
    pub state: State,

    /// An identifier for the Homie implementation which the device uses.
    pub implementation: Option<String>,

    /// The nodes of the device, keyed by their IDs.
    pub nodes: HashMap<String, Node>,

    /// The Homie extensions implemented by the device.
    pub extensions: Vec<Extension>,

    /// The IP address of the device on the local network.
    pub local_ip: Option<String>,

    /// The MAC address of the device's network interface.
    pub mac: Option<String>,

    /// The name of the firmware running on the device.
    pub firmware_name: Option<String>,

    /// The version of the firware running on the device.
    pub firmware_version: Option<String>,

    /// The interval at which the device refreshes its stats.
    pub stats_interval: Option<Duration>,

    /// The amount of time since the device booted.
    pub stats_uptime: Option<Duration>,

    /// The device's signal strength in %.
    pub stats_signal: Option<i64>,

    /// The device's CPU temperature in °C.
    pub stats_cputemp: Option<f64>,

    /// The device's CPU load in %, averaged across all CPUs over the last `stats_interval`.
    pub stats_cpuload: Option<i64>,

    /// The device's battery level in %.
    pub stats_battery: Option<i64>,

    /// The device's free heap space in bytes.
    pub stats_freeheap: Option<u64>,

    /// The device's power supply voltage in volts.
    pub stats_supply: Option<f64>,
}

impl Device {
    /// Create a new device with the given ID.
    ///
    /// # Arguments
    /// * `id`: The subtopic ID for the device. This must be unique per Homie base topic, and follow
    ///   the Homie [ID format](https://homieiot.github.io/specification/#topic-ids).
    /// * `homie_version`: The version of the Homie convention which the device implements.
    pub(crate) fn new(id: &str, homie_version: &str) -> Device {
        Device {
            id: id.to_owned(),
            homie_version: homie_version.to_owned(),
            name: None,
            state: State::Unknown,
            implementation: None,
            nodes: HashMap::new(),
            extensions: Vec::default(),
            local_ip: None,
            mac: None,
            firmware_name: None,
            firmware_version: None,
            stats_interval: None,
            stats_uptime: None,
            stats_signal: None,
            stats_cputemp: None,
            stats_cpuload: None,
            stats_battery: None,
            stats_freeheap: None,
            stats_supply: None,
        }
    }

    /// Add the given node to the devices's set of nodes.
    pub(crate) fn add_node(&mut self, node: Node) {
        self.nodes.insert(node.id.clone(), node);
    }

    /// Returns whether all the required
    /// [attributes](https://homieiot.github.io/specification/#device-attributes) of the device and
    /// all its nodes and properties are filled in.
    pub fn has_required_attributes(&self) -> bool {
        self.name.is_some()
            && self.state != State::Unknown
            && self
                .nodes
                .values()
                .all(|node| node.has_required_attributes())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::values::{ColorHsv, ColorRgb, EnumValue};

    #[test]
    fn extension_parse_succeeds() {
        let legacy_stats: Extension = "org.homie.legacy-stats:0.1.1:[4.x]".parse().unwrap();
        assert_eq!(legacy_stats.id, "org.homie.legacy-stats");
        assert_eq!(legacy_stats.version, "0.1.1");
        assert_eq!(legacy_stats.homie_versions, &["4.x"]);

        let meta: Extension = "eu.epnw.meta:1.1.0:[3.0.1;4.x]".parse().unwrap();
        assert_eq!(meta.id, "eu.epnw.meta");
        assert_eq!(meta.version, "1.1.0");
        assert_eq!(meta.homie_versions, &["3.0.1", "4.x"]);

        let minimal: Extension = "a:0:[]".parse().unwrap();
        assert_eq!(minimal.id, "a");
        assert_eq!(minimal.version, "0");
        assert_eq!(minimal.homie_versions, &[""]);
    }

    #[test]
    fn extension_parse_fails() {
        assert_eq!(
            "".parse::<Extension>(),
            Err(ParseExtensionError("".to_owned()))
        );
        assert_eq!(
            "test.blah:1.2.3".parse::<Extension>(),
            Err(ParseExtensionError("test.blah:1.2.3".to_owned()))
        );
        assert_eq!(
            "test.blah:1.2.3:4.x".parse::<Extension>(),
            Err(ParseExtensionError("test.blah:1.2.3:4.x".to_owned()))
        );
    }

    #[test]
    fn property_integer_parse() {
        let mut property = Property::new("property_id");

        // With no known value, parsing fails.
        assert_eq!(property.value::<i64>(), Err(ValueError::Unknown));

        // With an invalid value, parsing also fails.
        property.value = Some("-".to_owned());
        assert_eq!(
            property.value::<i64>(),
            Err(ValueError::ParseFailed {
                value: "-".to_owned(),
                datatype: Datatype::Integer,
            })
        );

        // With a valid value but unknown datatype, parsing succeeds.
        property.value = Some("42".to_owned());
        assert_eq!(property.value(), Ok(42));

        // With the correct datatype, parsing still succeeds.
        property.datatype = Some(Datatype::Integer);
        assert_eq!(property.value(), Ok(42));

        // Negative values can be parsed.
        property.value = Some("-66".to_owned());
        assert_eq!(property.value(), Ok(-66));

        // With the wrong datatype, parsing fails.
        property.datatype = Some(Datatype::Float);
        assert_eq!(
            property.value::<i64>(),
            Err(ValueError::WrongDatatype {
                actual: Datatype::Float,
                expected: Datatype::Integer,
            })
        );
    }

    #[test]
    fn property_float_parse() {
        let mut property = Property::new("property_id");

        // With no known value, parsing fails.
        assert_eq!(property.value::<f64>(), Err(ValueError::Unknown));

        // With an invalid value, parsing also fails.
        property.value = Some("-".to_owned());
        assert_eq!(
            property.value::<f64>(),
            Err(ValueError::ParseFailed {
                value: "-".to_owned(),
                datatype: Datatype::Float,
            })
        );

        // With a valid value but unknown datatype, parsing succeeds.
        property.value = Some("42.36".to_owned());
        assert_eq!(property.value(), Ok(42.36));

        // With the correct datatype, parsing still succeeds.
        property.datatype = Some(Datatype::Float);
        assert_eq!(property.value(), Ok(42.36));

        // With the wrong datatype, parsing fails.
        property.datatype = Some(Datatype::Integer);
        assert_eq!(
            property.value::<f64>(),
            Err(ValueError::WrongDatatype {
                actual: Datatype::Integer,
                expected: Datatype::Float,
            })
        );
    }

    #[test]
    fn property_color_parse() {
        let mut property = Property::new("property_id");

        // With no known value, parsing fails.
        assert_eq!(property.value::<ColorRgb>(), Err(ValueError::Unknown));
        assert_eq!(property.value::<ColorHsv>(), Err(ValueError::Unknown));

        // With an invalid value, parsing also fails.
        property.value = Some("".to_owned());
        assert_eq!(
            property.value::<ColorRgb>(),
            Err(ValueError::ParseFailed {
                value: "".to_owned(),
                datatype: Datatype::Color,
            })
        );

        // With a valid value but unknown datatype, parsing succeeds as either kind of colour.
        property.value = Some("12,34,56".to_owned());
        assert_eq!(
            property.value(),
            Ok(ColorRgb {
                r: 12,
                g: 34,
                b: 56
            })
        );
        assert_eq!(
            property.value(),
            Ok(ColorHsv {
                h: 12,
                s: 34,
                v: 56
            })
        );

        // With the correct datatype and no format, parsing succeeds as either kind of colour.
        property.datatype = Some(Datatype::Color);
        assert_eq!(
            property.value(),
            Ok(ColorRgb {
                r: 12,
                g: 34,
                b: 56
            })
        );
        assert_eq!(
            property.value(),
            Ok(ColorHsv {
                h: 12,
                s: 34,
                v: 56
            })
        );

        // With a format set, parsing succeeds only as the correct kind of colour.
        property.format = Some("rgb".to_owned());
        assert_eq!(
            property.value(),
            Ok(ColorRgb {
                r: 12,
                g: 34,
                b: 56
            })
        );
        assert_eq!(
            property.value::<ColorHsv>(),
            Err(ValueError::WrongFormat {
                format: "rgb".to_owned()
            })
        );

        // With the wrong datatype, parsing fails.
        property.datatype = Some(Datatype::Integer);
        assert_eq!(
            property.value::<ColorRgb>(),
            Err(ValueError::WrongDatatype {
                actual: Datatype::Integer,
                expected: Datatype::Color,
            })
        );
        assert_eq!(
            property.value::<ColorHsv>(),
            Err(ValueError::WrongDatatype {
                actual: Datatype::Integer,
                expected: Datatype::Color,
            })
        );
    }

    #[test]
    fn property_enum_parse() {
        let mut property = Property::new("property_id");

        // With no known value, parsing fails.
        assert_eq!(property.value::<EnumValue>(), Err(ValueError::Unknown));

        // With an invalid value, parsing also fails.
        property.value = Some("".to_owned());
        assert_eq!(
            property.value::<EnumValue>(),
            Err(ValueError::ParseFailed {
                value: "".to_owned(),
                datatype: Datatype::Enum,
            })
        );

        // With a valid value but unknown datatype, parsing succeeds.
        property.value = Some("anything".to_owned());
        assert_eq!(property.value(), Ok(EnumValue::new("anything")));

        // With the correct datatype, parsing still succeeds.
        property.datatype = Some(Datatype::Enum);
        assert_eq!(property.value(), Ok(EnumValue::new("anything")));

        // With the wrong datatype, parsing fails.
        property.datatype = Some(Datatype::String);
        assert_eq!(
            property.value::<EnumValue>(),
            Err(ValueError::WrongDatatype {
                actual: Datatype::String,
                expected: Datatype::Enum,
            })
        );
    }

    #[test]
    fn property_color_format() {
        let mut property = Property::new("property_id");

        // With no known format or datatype, format parsing fails.
        assert_eq!(property.color_format(), Err(ValueError::Unknown));

        // Parsing an invalid format fails.
        property.format = Some("".to_owned());
        assert_eq!(
            property.color_format(),
            Err(ValueError::WrongFormat {
                format: "".to_owned()
            })
        );

        // Parsing valid formats works even if datatype is unnkown.
        property.format = Some("rgb".to_owned());
        assert_eq!(property.color_format(), Ok(ColorFormat::Rgb));
        property.format = Some("hsv".to_owned());
        assert_eq!(property.color_format(), Ok(ColorFormat::Hsv));

        // With the wrong datatype, parsing fails.
        property.datatype = Some(Datatype::Integer);
        assert_eq!(
            property.color_format(),
            Err(ValueError::WrongDatatype {
                actual: Datatype::Integer,
                expected: Datatype::Color
            })
        );

        // With the correct datatype, parsing works.
        property.datatype = Some(Datatype::Color);
        assert_eq!(property.color_format(), Ok(ColorFormat::Hsv));
    }

    #[test]
    fn property_enum_format() {
        let mut property = Property::new("property_id");

        // With no known format or datatype, format parsing fails.
        assert_eq!(property.enum_values(), Err(ValueError::Unknown));

        // An empty format string is invalid.
        property.format = Some("".to_owned());
        assert_eq!(
            property.enum_values(),
            Err(ValueError::WrongFormat {
                format: "".to_owned()
            })
        );

        // A single value is valid.
        property.format = Some("one".to_owned());
        assert_eq!(property.enum_values(), Ok(vec!["one"]));

        // Several values are parsed correctly.
        property.format = Some("one,two,three".to_owned());
        assert_eq!(property.enum_values(), Ok(vec!["one", "two", "three"]));

        // With the correct datatype, parsing works.
        property.datatype = Some(Datatype::Enum);
        assert_eq!(property.enum_values(), Ok(vec!["one", "two", "three"]));

        // With the wrong datatype, parsing fails.
        property.datatype = Some(Datatype::Color);
        assert_eq!(
            property.enum_values(),
            Err(ValueError::WrongDatatype {
                actual: Datatype::Color,
                expected: Datatype::Enum
            })
        );
    }

    #[test]
    fn property_numeric_format() {
        let mut property = Property::new("property_id");

        // With no known format or datatype, format parsing fails.
        assert_eq!(property.range::<i64>(), Err(ValueError::Unknown));
        assert_eq!(property.range::<f64>(), Err(ValueError::Unknown));

        // An empty format string is invalid.
        property.format = Some("".to_owned());
        assert_eq!(
            property.range::<i64>(),
            Err(ValueError::WrongFormat {
                format: "".to_owned()
            })
        );
        assert_eq!(
            property.range::<f64>(),
            Err(ValueError::WrongFormat {
                format: "".to_owned()
            })
        );

        // A valid range is parsed correctly.
        property.format = Some("1:10".to_owned());
        assert_eq!(property.range(), Ok(1..=10));
        assert_eq!(property.range(), Ok(1.0..=10.0));

        // A range with a decimal point must be a float.
        property.format = Some("3.6:4.2".to_owned());
        assert_eq!(property.range(), Ok(3.6..=4.2));
        assert_eq!(
            property.range::<i64>(),
            Err(ValueError::WrongFormat {
                format: "3.6:4.2".to_owned()
            })
        );

        // With the correct datatype, parsing works.
        property.datatype = Some(Datatype::Integer);
        property.format = Some("1:10".to_owned());
        assert_eq!(property.range(), Ok(1..=10));

        // For the wrong datatype, parsing fails.
        assert_eq!(
            property.range::<f64>(),
            Err(ValueError::WrongDatatype {
                actual: Datatype::Integer,
                expected: Datatype::Float
            })
        );
    }

    #[test]
    fn property_has_required_attributes() {
        let mut property = Property::new("property_id");
        assert_eq!(property.has_required_attributes(), false);

        property.name = Some("Property name".to_owned());
        assert_eq!(property.has_required_attributes(), false);

        property.datatype = Some(Datatype::Integer);
        assert_eq!(property.has_required_attributes(), true);
    }

    /// Construct a minimal `Property` with all the required attributes.
    fn property_with_required_attributes() -> Property {
        let mut property = Property::new("property_id");
        property.name = Some("Property name".to_owned());
        property.datatype = Some(Datatype::Integer);
        property
    }

    #[test]
    fn node_has_required_attributes() {
        let mut node = Node::new("node_id");
        assert_eq!(node.has_required_attributes(), false);

        node.name = Some("Node name".to_owned());
        assert_eq!(node.has_required_attributes(), false);

        node.node_type = Some("Node type".to_owned());
        assert_eq!(node.has_required_attributes(), false);

        node.add_property(property_with_required_attributes());
        assert_eq!(node.has_required_attributes(), true);

        node.add_property(Property::new("property_without_required_attributes"));
        assert_eq!(node.has_required_attributes(), false);
    }

    /// Construct a minimal `Node` with all the required attributes.
    fn node_with_required_attributes() -> Node {
        let mut node = Node::new("node_id");
        node.name = Some("Node name".to_owned());
        node.node_type = Some("Node type".to_owned());
        node.add_property(property_with_required_attributes());
        node
    }

    #[test]
    fn device_has_required_attributes() {
        let mut device = Device::new("device_id", "123");
        assert_eq!(device.has_required_attributes(), false);

        device.name = Some("Device name".to_owned());
        assert_eq!(device.has_required_attributes(), false);

        device.state = State::Init;
        assert_eq!(device.has_required_attributes(), true);

        device.add_node(node_with_required_attributes());
        assert_eq!(device.has_required_attributes(), true);

        device.add_node(Node::new("node_without_required_attributes"));
        assert_eq!(device.has_required_attributes(), false);
    }
}