hues 0.1.3

A Rust client for the Philips Hue API v2
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
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
use crate::service::{
    AlertEffectType, CIEColor, ColorFeatureBasic, EffectType, GradientMode, GroupDimmingState,
    OnState, ParseColorError, PowerupOnState, PowerupPresetType, ProductArchetype,
    ResourceIdentifier, SceneAction, ScenePalette, SceneStatus, Schedule, SignalType,
    TimedEffectType, ZigbeeChannel, ZoneArchetype,
};
use json_patch::merge;
use serde::{ser::SerializeMap, Serialize};
use serde_json::json;

/// A helper function to merge types serializeable to a JSON object.
pub fn merge_commands<S: Serialize>(commands: &[S]) -> serde_json::Value {
    let mut map = json!({});
    for cmd in commands {
        merge(&mut map, &serde_json::to_value(cmd).unwrap());
    }
    map
}

pub enum CommandType {
    BehaviorInstance(BehaviorInstanceCommand),
    Bridge(BridgeCommand),
    Button(ButtonCommand),
    CameraMotion(CameraMotionCommand),
    Contact(BasicCommand),
    Device(DeviceCommand),
    DevicePower(DevicePowerCommand),
    EntertainmentConfiguration(EntertainmentConfigurationCommand),
    GeofenceClient(GeofenceClientCommand),
    Geolocation(GeolocationCommand),
    GroupedLight(GroupCommand),
    HomeKit(HomeKitCommand),
    Light(String, LightCommand),
    LightLevel(BasicCommand),
    Matter(MatterCommand),
    MatterFabric(MatterFabricCommand),
    Motion(MotionCommand),
    RelativeRotary(RelativeRotaryCommand),
    Room(ZoneCommand),
    Scene(SceneCommand),
    SmartScene(SmartSceneCommand),
    Tamper(TamperCommand),
    Temperature(BasicCommand),
    ZigbeeConnectivity(ZigbeeConnectivityCommand),
    ZigbeeDeviceDiscovery(ZigbeeDeviceDiscoveryCommand),
    Zone(ZoneCommand),
}

/// Command representing the enabled state of a simple device.
#[derive(Serialize)]
#[serde(rename_all = "snake_case")]
pub enum BasicCommand {
    Enabled(bool),
}

/// Commands for a [BehaviorInstance](crate::service::BehaviorInstance).
#[derive(Serialize)]
#[serde(rename_all = "snake_case")]
pub enum BehaviorInstanceCommand {
    /// Indicates whether a scripts is enabled.
    Enabled(bool),
    /// Script configuration.
    /// This property is validated using ScriptDefinition.configuration_schema JSON schema.
    Configuration(serde_json::Value),
    /// Action that needs to be taken by this script instance.
    /// This property is validated using ScriptDefinition.trigger_schema JSON schema.
    Trigger(serde_json::Value),
    Metadata {
        name: String,
    },
}

pub struct BridgeCommand;

pub struct ButtonCommand;

pub struct CameraMotionCommand;

/// Commands for a [Device](crate::service::Device).
#[derive(Debug)]
pub enum DeviceCommand {
    /// Triggers a visual identification sequence, currently implemented as
    /// (which can change in the future): Bridge performs Zigbee LED
    /// identification cycles for 5 seconds Lights perform one breathe cycle
    /// Sensors perform LED identification cycles for 15 seconds.
    Identify,
    Metadata {
        name: Option<String>,
        archetype: Option<ProductArchetype>,
    },
    /// Activates or extends user usertest mode of device for 120 seconds.
    /// `false` deactivates usertest mode. In usertest mode, devices report
    /// changes in state faster and indicate state changes on device LED.
    UserTest(bool),
}

impl Serialize for DeviceCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Identify => {
                map.serialize_entry("identify", &json!({ "action": "identify" }))?;
            }
            Self::Metadata { name, archetype } => {
                map.serialize_entry("metadata", &json!({ "name": name, "archetype": archetype }))?;
            }
            Self::UserTest(u) => {
                map.serialize_entry("usertest", &json!({ "usertest": u }))?;
            }
        }
        map.end()
    }
}

pub struct DevicePowerCommand;

/// Commands for an [EntertainmentConfiguration](crate::service::EntertainmentConfiguration).
#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EntertainmentConfigurationCommand {
    Action(EntertainmentAction),
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EntertainmentAction {
    Start,
    Stop,
}

/// Commands for a [GeofenceClient](crate::service::GeofenceClient).
#[derive(Serialize)]
#[serde(rename_all = "snake_case")]
pub enum GeofenceClientCommand {
    /// Indicates if Geofence Client is at home.
    IsAtHome(bool),
    /// Renames the Geofence Client.
    Name(String),
}

/// Commands for a [Geolocation](crate::service::Geolocation).
#[derive(Serialize)]
#[serde(untagged)]
pub enum GeolocationCommand {
    /// Places the Geolocation at the given coordinates.
    Coordinates {
        #[serde(rename = "latitude")]
        lat: f32,
        #[serde(rename = "longitude")]
        lon: f32,
    },
}

/// Commands for a [Group](crate::service::Group).
#[derive(Debug)]
pub enum GroupCommand {
    /// Sets the alert effect for all members.
    Alert(AlertEffectType),
    /// CIE XY gamut position
    Color {
        /// X position in color gamut (`0.0`, `1.0`)
        x: f32,
        /// Y position in color gamut (`0.0`, `1.0`)
        y: f32,
    },
    /// Color temperature in absolute mirek \[`153`, `500`\] or null when the light color
    /// is not in the ct spectrum.
    ColorTemp(u16),
    /// Color temperature change in mirek.
    ColorTempDelta {
        action: DeltaAction,
        /// Mirek delta to current mirek \[`0`, `347`\]. Clip at
        /// [mirek_minimum](crate::service::MirekSchema::mirek_minimum) and
        /// [mirek_maximum](crate::service::MirekSchema::mirek_maximum) of mirek_schema.
        mirek_delta: Option<u16>,
    },
    /// Joined dimming control, sets absolute brightness of turned-on lights
    /// in this group.
    Dim(f32),
    /// Joined dimming control, sets relative brightness change of turned-on
    /// lights in this group.
    DimDelta {
        action: DeltaAction,
        /// Brightness percentage of absolute delta to current
        /// [brightness](crate::service::DimmingState::brightness).
        brightness_delta: Option<f32>,
    },
    /// Modifies properties of trasitions and timed effects in this group.
    Dynamics {
        /// Duration of a light transition or timed effects in ms.
        duration: Option<usize>,
    },
    /// Joined power state of this group.
    On(bool),
    /// Feature containing signaling properties.
    Signaling {
        signal: SignalType,
        /// Duration in seconds.
        ///
        /// Has a max of 65,534,000ms and a stepsize of 1,000ms.
        /// Values in between steps will be rounded. Duration is ignored for [SignalType::NoSignal].
        duration: usize,
        /// List of colors (1 or 2) to apply to the signal (not supported by all signals).
        colors: Option<SignalColor>,
    },
}

impl GroupCommand {
    pub fn color_from_rgb(rgb: [u8; 3]) -> GroupCommand {
        let cie = CIEColor::from_rgb(rgb);
        GroupCommand::Color { x: cie.x, y: cie.y }
    }

    pub fn color_from_hex(hex: impl Into<String>) -> Result<GroupCommand, ParseColorError> {
        match CIEColor::from_hex(hex) {
            Ok(cie) => Ok(GroupCommand::Color { x: cie.x, y: cie.y }),
            Err(e) => Err(e),
        }
    }
}

impl Serialize for GroupCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Alert(effect) => {
                map.serialize_entry("alert", &json!({ "action": effect }))?;
            }
            Self::Color { x, y } => {
                map.serialize_entry("color", &json!({ "xy": { "x": x, "y": y } }))?;
            }
            Self::ColorTemp(mirek) => {
                map.serialize_entry("color_temperature", &json!({ "mirek": mirek }))?;
            }
            Self::ColorTempDelta {
                action,
                mirek_delta,
            } => {
                map.serialize_entry(
                    "color_temperature_delta",
                    &json!({ "action": action, "mirek_delta": mirek_delta}),
                )?;
            }
            Self::Dim(pct) => {
                map.serialize_entry("dimming", &json!({ "brightness": pct }))?;
            }
            Self::DimDelta {
                action,
                brightness_delta,
            } => {
                map.serialize_entry(
                    "dimming_delta",
                    &json!({ "action": action, "brightness_delta": brightness_delta }),
                )?;
            }
            Self::Dynamics { duration } => {
                map.serialize_entry("dynamics", &json!({ "duration": duration }))?;
            }
            Self::Signaling {
                signal,
                duration,
                colors,
            } => {
                map.serialize_entry(
                    "signaling",
                    &json!({
                        "signal": signal,
                        "duration": duration,
                        "colors": colors,
                    }),
                )?;
            }
            Self::On(on) => {
                map.serialize_entry("on", &OnState { on: *on })?;
            }
        }
        map.end()
    }
}

/// Commands for a [HomeKit](crate::service::HomeKit).
#[derive(Debug)]
pub enum HomeKitCommand {
    Reset,
}

impl Serialize for HomeKitCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(Some(1))?;
        match self {
            Self::Reset => map.serialize_entry("action", "homekit_reset")?,
        }
        map.end()
    }
}

/// Commands for a [Light](crate::service::Light).
#[derive(Debug)]
pub enum LightCommand {
    /// Sets the alert effect for this light.
    Alert(AlertEffectType),
    /// CIE XY gamut position
    Color {
        /// X position in color gamut (`0`, `1`)
        x: f32,
        /// Y position in color gamut (`0`, `1`)
        y: f32,
    },
    /// Color temperature in absolute mirek \[`153`, `500`\] or null when the light color
    /// is not in the ct spectrum.
    ColorTemp(u16),
    /// Color temperature change in mirek.
    ColorTempDelta {
        action: DeltaAction,
        /// Mirek delta to current mirek \[`0`, `347`\]. Clip at
        /// [mirek_minimum](crate::service::MirekSchema::mirek_minimum) and
        /// [mirek_maximum](crate::service::MirekSchema::mirek_maximum) of mirek_schema.
        mirek_delta: Option<u16>,
    },
    /// Brightness percentage \(`0.0`-`100.0`\]. value cannot be `0`, writing
    /// `0` changes it to lowest possible brightness.
    Dim(f32),
    /// Brightness change to this light.
    DimDelta {
        action: Option<DeltaAction>,
        /// Brightness percentage of full-scale increase delta to current
        /// dimlevel. Clip at Max-level or Min-level.
        brightness_delta: Option<f32>,
    },
    /// Modifies properties of trasitions and timed effects of this light.
    Dynamics {
        /// Duration of a light transition or timed effects in ms.
        duration: Option<usize>,
        /// Speed of dynamic palette or effect.
        ///
        /// The speed is valid for the dynamic palette if the status is [DynamicsStatus::DynamicPalette](crate::service::DynamicsStatus::DynamicPalette)
        /// or for the corresponding effect listed in status. In case of status
        /// [None], the speed is not valid.
        speed: Option<f32>,
    },
    /// Basic feature containing gradient properties.
    Gradient {
        /// Collection of gradients points. For control of the gradient points
        /// through a PUT a minimum of 2 points need to be provided.
        points: Vec<CIEColor>,
        mode: Option<GradientMode>,
    },
    /// Basic feature containing effect properties.
    Effect(EffectType),
    /// Triggers a visual identification sequence, performing one breathe cycle.
    Identify,
    Metadata {
        name: Option<String>,
        archetype: Option<ProductArchetype>,
    },
    /// Power state of this light.
    On(bool),
    /// Configures the power-up behavior of this light.
    PowerUp {
        /// When setting the custom preset, other properties can be set.
        /// For all other presets, no other properties can be included.
        preset: PowerupPresetType,
        on: Option<PowerupOnState>,
        dimming: Option<PowerupDimming>,
        color: Option<PowerupColor>,
    },
    /// Feature containing signaling properties.
    Signaling {
        signal: SignalType,
        /// Duration in seconds.
        ///
        /// Has a max of 65,534,000ms and a stepsize of 1000ms.
        /// Values in between steps will be rounded. Duration is ignored for
        /// [SignalType::NoSignal].
        duration: usize,
        /// List of colors (1 or 2) to apply to the signal (not supported by
        /// all signals).
        colors: Option<SignalColor>,
    },
    /// Basic feature containing timed effect properties.
    TimedEffect {
        effect: TimedEffectType,
        /// Duration is mandatory when timed effect is set, except for
        /// [TimedEffectType::NoEffect].
        /// Resolution decreases for a larger duration. e.g Effects with
        /// duration smaller than a minute will be rounded to a resolution of
        /// 1s, while effects with duration larger than an hour will be
        /// rounded up to a resolution of 300,000ms. Duration has a max of
        /// 21,600,000ms.
        duration: Option<usize>,
    },
}

impl LightCommand {
    pub fn color_from_rgb(rgb: [u8; 3]) -> LightCommand {
        let cie = CIEColor::from_rgb(rgb);
        LightCommand::Color { x: cie.x, y: cie.y }
    }

    pub fn color_from_hex(hex: impl Into<String>) -> Result<LightCommand, ParseColorError> {
        match CIEColor::from_hex(hex) {
            Ok(cie) => Ok(LightCommand::Color { x: cie.x, y: cie.y }),
            Err(e) => Err(e),
        }
    }
}

#[derive(Debug)]
pub struct PowerupColor {
    /// State to activate after powerup.
    ///
    /// Availability of [PowerupColorMode::ColorTemp] and
    /// [PowerupColorMode::Color] modes depend on the capabilities of the lamp.
    pub mode: PowerupColorMode,
    pub color: Option<CIEColor>,
    pub color_temperature: Option<u16>,
}

impl Serialize for PowerupColor {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        map.serialize_entry("mode", &self.mode)?;
        if let Some(xy) = &self.color {
            map.serialize_entry("color", &ColorFeatureBasic { xy: xy.clone() })?;
        }
        if let Some(temp) = self.color_temperature {
            map.serialize_entry("color_temperature", &json!({ "mirek": temp }))?;
        }
        map.end()
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum PowerupColorMode {
    Color,
    #[serde(rename = "color_temperature")]
    ColorTemp,
    Previous,
}

#[derive(Debug)]
pub struct PowerupDimming {
    pub mode: PowerupDimmingMode,
    pub brightness: Option<f32>,
}

impl Serialize for PowerupDimming {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        map.serialize_entry("mode", &self.mode)?;
        if let Some(bri) = self.brightness {
            map.serialize_entry("dimming", &json!({ "brightness": bri }))?;
        }
        map.end()
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum PowerupDimmingMode {
    Dimming,
    Previous,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DeltaAction {
    /// Increases the target value.
    Up,
    /// Decreases the target value.
    Down,
    /// Halts the target value if it is in the process of animating.
    Stop,
}

#[derive(Debug)]
pub enum SignalColor {
    One(CIEColor),
    Two(CIEColor, CIEColor),
}

impl Serialize for SignalColor {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            SignalColor::One(inner) => {
                serializer.collect_seq([ColorFeatureBasic { xy: inner.clone() }])
            }
            SignalColor::Two(inner_a, inner_b) => serializer.collect_seq([
                ColorFeatureBasic {
                    xy: inner_a.clone(),
                },
                ColorFeatureBasic {
                    xy: inner_b.clone(),
                },
            ]),
        }
    }
}

impl Serialize for LightCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Alert(effect) => {
                map.serialize_entry("alert", &json!({ "action": effect }))?;
            }
            Self::Color { x, y } => {
                map.serialize_entry("color", &json!({ "xy": { "x": x, "y": y } }))?;
            }
            Self::ColorTemp(mirek) => {
                map.serialize_entry("color_temperature", &json!({ "mirek": mirek }))?;
            }
            Self::ColorTempDelta {
                action,
                mirek_delta,
            } => {
                map.serialize_entry(
                    "color_temperature_delta",
                    &json!({ "action": action, "mirek_delta": mirek_delta}),
                )?;
            }
            Self::Dim(pct) => {
                map.serialize_entry("dimming", &json!({ "brightness": pct }))?;
            }
            Self::DimDelta {
                action,
                brightness_delta,
            } => {
                map.serialize_entry(
                    "dimming_delta",
                    &json!({ "action": action, "brightness_delta": brightness_delta }),
                )?;
            }
            Self::Dynamics { duration, speed } => {
                map.serialize_entry("dynamics", &json!({ "duration": duration, "speed": speed }))?;
            }
            Self::Effect(effect) => {
                map.serialize_entry("effects", &json!({ "effect": effect }))?;
            }
            Self::Gradient { points, mode } => {
                let points = points
                    .iter()
                    .map(|xy| ColorFeatureBasic { xy: xy.clone() })
                    .collect::<Vec<ColorFeatureBasic>>();
                map.serialize_entry("gradient", &json!({ "points": points, "mode": mode }))?;
            }
            Self::Identify => {
                map.serialize_entry("identify", &json!({ "action": "identify" }))?;
            }
            Self::Metadata { name, archetype } => {
                map.serialize_entry("metadata", &json!({ "name": name, "archetype": archetype }))?;
            }
            Self::On(on) => {
                map.serialize_entry("on", &OnState { on: *on })?;
            }
            Self::PowerUp {
                preset,
                on,
                dimming,
                color,
            } => {
                map.serialize_entry(
                    "powerup",
                    &json!({
                        "preset": preset,
                        "on": on,
                        "dimming": dimming,
                        "color": color
                    }),
                )?;
            }
            Self::Signaling {
                signal,
                duration,
                colors,
            } => {
                map.serialize_entry(
                    "signaling",
                    &json!({
                        "signal": signal,
                        "duration": duration,
                        "colors": colors,
                    }),
                )?;
            }
            Self::TimedEffect { effect, duration } => {
                map.serialize_entry(
                    "timed_effects",
                    &json!({ "effect": effect, "duration": duration }),
                )?;
            }
        }
        map.end()
    }
}

#[derive(Default, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DeviceIdentifyType {
    /// Performs Zigbee LED identification cycles for 5 seconds.
    Bridge,
    /// Perform one breathe cycle.
    #[default]
    Lights,
    /// Perform LED identification cycles for 15 seconds.
    Sensors,
}

/// Commands for a [Matter](crate::service::Matter).
#[derive(Debug)]
pub enum MatterCommand {
    Reset,
}

impl Serialize for MatterCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Reset => map.serialize_entry("action", "matter_reset")?,
        }
        map.end()
    }
}

pub struct MatterFabricCommand;

/// Commands for a [Motion](crate::service::Motion).
#[derive(Debug)]
pub enum MotionCommand {
    /// The enabled state of the Motion device.
    Enabled(bool),
    /// Sensitivity of the sensor. Value in the range [`0`,
    /// [sensitivity_max](crate::service::Sensitivity::sensitivity_max)].
    Sensitivity(usize),
}

impl Serialize for MotionCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Enabled(e) => {
                map.serialize_entry("enabled", e)?;
            }
            Self::Sensitivity(s) => {
                map.serialize_entry("sensitivity", &json!({ "sensitivity": s }))?;
            }
        }
        map.end()
    }
}

pub struct RelativeRotaryCommand;

/// Commands for a [Zone](crate::service::Zone).
#[derive(Debug)]
pub enum ZoneCommand {
    /// Sets the devices/services of this Zone.
    Children(Vec<ResourceIdentifier>),
    Metadata {
        name: Option<String>,
        archetype: Option<ZoneArchetype>,
    },
}

impl Serialize for ZoneCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Children(rids) => {
                map.serialize_entry("children", rids)?;
            }
            Self::Metadata { name, archetype } => {
                map.serialize_entry("metadata", &json!({ "name": name, "archetype": archetype }))?;
            }
        }
        map.end()
    }
}

/// Commands for a [Scene](crate::service::Scene).
#[derive(Debug)]
pub enum SceneCommand {
    /// List of actions to be executed synchronously on recall.
    Actions(Vec<SceneAction>),
    /// Indicates whether to automatically start the scene dynamically on
    /// [SceneStatus::Active] recall.
    AutoDynamic(bool),
    Metadata {
        name: Option<String>,
        appdata: Option<String>,
    },
    /// Group of colors that describe the palette of colors to be used when
    /// playing dynamics.
    Palette(ScenePalette),
    /// Trigger the scene, optionally overriding some of its properties.
    Recall {
        /// When writing [SceneStatus::Active], the actions in the scene are
        /// executed on the target.
        /// [SceneStatus::DynamicPalette] starts dynamic scene with colors in
        /// the Palette object.
        action: Option<SceneStatus>,
        /// Transition to the scene within the timeframe given by duration in ms.
        duration: Option<usize>,
        /// Override the scene dimming/brightness.
        dimming: Option<GroupDimmingState>,
    },
    /// Speed of dynamic palette for this scene.
    Speed(f32),
}

impl Serialize for SceneCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Actions(actions) => {
                map.serialize_entry("actions", actions)?;
            }
            Self::AutoDynamic(a) => {
                map.serialize_entry("auto_dynamic", a)?;
            }
            Self::Metadata { name, appdata } => {
                map.serialize_entry("metadata", &json!({ "name": name, "appdata": appdata}))?;
            }
            Self::Palette(palette) => {
                map.serialize_entry("palette", palette)?;
            }
            Self::Recall {
                action,
                duration,
                dimming,
            } => {
                map.serialize_entry(
                    "recall",
                    &json!({ "action": action, "duration": duration, "dimming": dimming }),
                )?;
            }
            Self::Speed(s) => {
                map.serialize_entry("speed", s)?;
            }
        }
        map.end()
    }
}

/// Commands for a [SmartScene](crate::service::SmartScene).
#[derive(Debug)]
pub enum SmartSceneCommand {
    /// Enabled state of this SmartScene.
    Enabled(bool),
    Metadata {
        name: Option<String>,
        appdata: Option<String>,
    },
    /// Commits a schedule of timeslots in which scenes should be applied.
    Schedule(Vec<Schedule>),
    /// Sets the duration of the transition betwees scenes, by default 60,000ms.
    TransitionDuration(usize),
}

impl SmartSceneCommand {
    pub fn create_schedule() -> Schedule {
        Schedule::new()
    }
}

impl Serialize for SmartSceneCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Enabled(b) => {
                map.serialize_entry(
                    "recall",
                    &json!({ "action": if *b { "activate" } else { "deactivate"} }),
                )?;
            }
            Self::Metadata { name, appdata } => {
                map.serialize_entry("metadata", &json!({ "name": name, "appdata": appdata}))?;
            }
            Self::Schedule(ts) => {
                map.serialize_entry("week_timeslots", ts)?;
            }
            Self::TransitionDuration(ms) => {
                map.serialize_entry("transition_duration", ms)?;
            }
        }
        map.end()
    }
}

pub struct TamperCommand;

/// Commands for a [ZigbeeConnectivity](crate::service::ZigbeeConnectivity).
#[derive(Debug)]
pub enum ZigbeeConnectivityCommand {
    Channel(ZigbeeChannel),
}

impl Serialize for ZigbeeConnectivityCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Channel(ch) => {
                map.serialize_entry("channel", &json!({ "value": ch }))?;
            }
        }
        map.end()
    }
}

/// Commands for a [ZigbeeDeviceDiscovery](crate::service::ZigbeeDeviceDiscovery).
#[derive(Debug)]
pub enum ZigbeeDeviceDiscoveryCommand {
    Action {
        search_codes: Vec<String>,
        install_codes: Vec<String>,
    },
}

impl Serialize for ZigbeeDeviceDiscoveryCommand {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;
        match self {
            Self::Action {
                search_codes,
                install_codes,
            } => {
                map.serialize_entry(
                    "action",
                    &json!({
                        "action_type": "search",
                        "search_codes": search_codes,
                        "install_codes": install_codes
                    }),
                )?;
            }
        }
        map.end()
    }
}