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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
#![doc = include_str!("../README.md")]

use std::collections::HashMap;
use std::error::Error;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use serde::{Deserialize, Serialize};

use tokio::net::{tcp::OwnedReadHalf, TcpListener, TcpStream};
use tokio::sync::{mpsc, Mutex};
use tokio::task::spawn;

#[cfg(feature = "from-str")]
use itertools::Itertools;

mod reader;
mod writer;

#[cfg(feature = "discover")]
pub mod discover;

pub use reader::{BulbError, Notification, Response};

use reader::{NotifyChan, Reader};
use writer::Writer;

#[derive(Debug)]
struct Message(u64, String);

/// Bulb connection
pub struct Bulb {
    notify_chan: NotifyChan,
    writer: writer::Writer,
}

/// Error generated when parsing value from string.
#[cfg(feature = "from-str")]
#[derive(Debug)]
pub struct ParseError(String);

impl Bulb {
    /// Connect to bulb at the specified address and port.
    ///
    /// If `port` is 0, the default value (55443) is used.
    ///
    /// # Example
    /// ```
    /// # async fn test() {
    /// # use yeelight::Bulb;
    /// let my_bulb_ip = "192.168.1.204";
    /// let mut bulb = Bulb::connect(my_bulb_ip, 55443).await
    ///     .expect("Connection failed");
    /// bulb.toggle().await.unwrap();
    /// # }
    /// ```
    pub async fn connect(addr: &str, mut port: u16) -> Result<Self, Box<dyn Error>> {
        if port == 0 {
            port = 55443
        }

        let stream = TcpStream::connect(format!("{}:{}", addr, port)).await?;

        let (reader, writer, reader_half, notify_chan) = Self::build_rw(stream);

        spawn(reader.start(reader_half));

        Ok(Self {
            notify_chan,
            writer,
        })
    }

    /// Attach to existing `std::net::TcpStream`.
    ///
    /// # Example
    /// ```
    /// # async fn test() {
    /// # use yeelight::Bulb;
    /// let stream = std::net::TcpStream::connect("192.168.1.204:55443")
    ///     .expect("Connection failed");
    /// let mut bulb = Bulb::attach(stream).unwrap();
    /// bulb.toggle().await.unwrap();
    /// # }
    /// ```
    pub fn attach(stream: ::std::net::TcpStream) -> Result<Self, Box<dyn Error>> {
        let stream = TcpStream::from_std(stream)?;

        Ok(Self::attach_tokio(stream))
    }

    /// Same as `attach(stream: std::net::TcpStream)` but for `tokio::net::TcpStream`;
    pub fn attach_tokio(stream: TcpStream) -> Self {
        let (reader, writer, reader_half, notify_chan) = Self::build_rw(stream);

        spawn(reader.start(reader_half));

        Self {
            notify_chan,
            writer,
        }
    }

    fn build_rw(stream: TcpStream) -> (Reader, Writer, OwnedReadHalf, NotifyChan) {
        let (reader_half, writer_half) = stream.into_split();

        let resp_chan = HashMap::new();
        let resp_chan = Arc::new(Mutex::new(resp_chan));
        let notify_chan = Arc::new(Mutex::new(None));

        let reader = Reader::new(resp_chan.clone(), notify_chan.clone());
        let writer = Writer::new(writer_half, resp_chan);

        (reader, writer, reader_half, notify_chan)
    }

    /// Set the [Bulb] connection so that it does not wait for response from the bulb
    ///
    /// If this is used, all the methods will return `None` even if they fail.
    /// It is not recommended unless you know the bulb will not respond (Poor connection,
    /// music mode or firewall). However it is often better to wrap the calls in
    /// a tokio timeout.
    ///
    /// # Example
    /// ```
    /// # async fn test() {
    /// # use yeelight::Bulb;
    /// let my_bulb_ip = "192.168.1.204";
    /// let mut bulb = Bulb::connect(my_bulb_ip, 55443).await
    ///     .expect("Connection failed").no_response();
    /// let response = bulb.toggle().await.unwrap(); // response will be `None`
    /// # }
    /// ```
    pub fn no_response(mut self) -> Self {
        self.writer.set_get_response(false);
        self
    }

    /// Set the [Bulb] connection so that it does wait for response from the bulb
    ///
    /// This reverses the changes made with [Bulb::no_response]
    pub fn get_response(mut self) -> Self {
        self.writer.set_get_response(true);
        self
    }

    /// Get a new notification reciever from the Bulb
    ///
    /// This method creates a new channel and replaces the old one.
    ///
    /// **NOTE:** The channel has 10 message buffer. If more are needed
    /// manually create a [mpsc::channel] and use [Bulb::set_notify]
    pub async fn get_notify(&mut self) -> mpsc::Receiver<Notification> {
        let (sender, receiver) = mpsc::channel(10);
        self.set_notify(sender).await;
        receiver
    }

    /// Attach the [Bulb] notification channel to the provided one
    ///
    /// This replaces the current channel
    ///
    /// **See also:** [Bulb::get_notify]
    pub async fn set_notify(&mut self, chan: mpsc::Sender<Notification>) {
        self.notify_chan.lock().await.replace(chan);
    }

    /// Establishes a Music mode connection with bulb.
    ///
    /// This method returns another `Bulb` object to send commands to the bulb in music mode. Note
    /// that all commands send to the bulb get no response and produce no notification message, so
    /// there is no way to know if the command was executed successfully by the bulb.
    pub async fn start_music(&mut self, host: &str) -> Result<Self, Box<dyn Error>> {
        let addr = format!("0.0.0.0:{}", 0).parse::<SocketAddr>()?;
        let listener = TcpListener::bind(&addr).await?;

        let port = listener.local_addr()?.port();

        self.set_music(MusicAction::On, host, port).await?;

        let (socket, _) = listener.accept().await?;
        Ok(Self::attach_tokio(socket).no_response())
    }
}

#[cfg(feature = "from-str")]
impl ToString for ParseError {
    fn to_string(&self) -> String {
        self.0.to_string()
    }
}

#[cfg(feature = "from-str")]
impl From<::std::num::ParseIntError> for ParseError {
    fn from(e: ::std::num::ParseIntError) -> Self {
        ParseError(e.to_string())
    }
}

trait Stringify {
    fn stringify(&self) -> String;
}

impl Stringify for str {
    fn stringify(&self) -> String {
        format!("\"{}\"", self)
    }
}

macro_rules! stringify_nums {
    ($($type:ty),*) => {
        $(
        impl Stringify for $type {
            fn stringify(&self) -> String {
                self.to_string()
            }
        }
        )*
    };
}

stringify_nums!(u8, u16, u32, u64, i8);

// Create enum and its ToString implementation using stringify (quoted strings)
macro_rules! enum_str {
    ($(#[$comment:meta])* $name:ident: $($variant:ident -> $val:literal),* $(,)?) => {

        $(#[$comment])*
        #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
        pub enum $name {
            $($variant),*
        }

        impl ::std::fmt::Display for $name {
            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                match *self {
                    $($name::$variant => write!(f, stringify!($val)),)+
                }
            }
        }

        impl Stringify for $name {
            fn stringify(&self) -> String {
                self.to_string()
            }
        }

        #[cfg(feature="from-str")]
        impl ::std::str::FromStr for $name {
            type Err = ParseError;

            fn from_str(s: &str) -> Result<Self,Self::Err> {
                match s {
                    $(stringify!($variant) |
                    _ if s.eq_ignore_ascii_case(stringify!($variant)) => Ok($name::$variant),)+
                    _                => Err(ParseError({
                                            let v = vec![
                                                $(stringify!($variant),)+
                                            ];
                                            format!("Could not parse {} \n Valid values:{}", s,
                                                v.iter().fold(String::new(), |a, i| {
                                                    a + &format!(" {}", i)[..]
                                                }))
                                        }))
                }
            }
        }

        #[cfg(feature="from-str")]
        impl $name {
            #[allow(dead_code)]
            pub fn variants() -> Vec<&'static str> {
                vec![
                    $(stringify!($variant),)+
                ]
            }
        }

    };
}

enum_str!(Property:
    Power -> "power",
    Bright -> "bright",
    CT -> "ct",
    RGB -> "rgb",
    Hue -> "hue",
    Sat -> "sat",
    ColorMode -> "color_mode",
    Flowing -> "flowing",
    DelayOff -> "delayoff",
    FlowParams -> "flow_params",
    MusicOn -> "music_on",
    Name -> "name",
    BgPower -> "bg_power",
    BgFlowing -> "bg_flowing",
    BgFlowParams -> "bg_flow_params",
    BgCT -> "bg_ct",
    BgColorMode -> "bg_lmode",
    BgBright -> "bg_bright",
    BgRGB -> "bg_rgb",
    BgHue -> "bg_hue",
    BgSat -> "bg_sat",
    NightLightBright -> "nl_br",
    ActiveMode -> "active_mode",
);

enum_str!(
    /// Bulb power state (On/Off)
    Power:
    On -> "on",
    Off -> "off",
);
enum_str!(
    /// Specifies how the changes will be applied.
    ///
    /// If [Effect::Sudden], then light will change directly to target value, under this case,
    /// the `Duration` parameter is ignored. If [Effect::Smooth], then the light  will
    /// change to target value in a gradual fashion, under this case, the total time of gradual
    /// change is specified in third parameter "duration".
    Effect:
    Sudden -> "sudden",
    Smooth -> "smooth",
);
enum_str!(Prop:
    Bright -> "bright",
    CT -> "ct",
    Color -> "color",
);
enum_str!(Class:
    Color -> "color",
    HSV -> "hsv",
    CT -> "ct",
    CF -> "cf",
    AutoDelayOff -> "auto_delay_off",
);
enum_str!(Mode:
    Normal -> 0,
    CT -> 1,
    RGB -> 2,
    HSV -> 3,
    CF -> 4,
    NightLight -> 5,
);
enum_str!(CronType:
    Off -> 0,
);
enum_str!(CfAction:
    Recover -> 0,
    Stay -> 1,
    Off -> 2,
);
enum_str!(AdjustAction:
    Increase -> "increase",
    Decrease -> "decrease",
    Circle -> "circle",
);
enum_str!(MusicAction:
    Off -> 0,
    On -> 1,
);
enum_str!(FlowMode:
    Color -> 1,
    CT -> 2,
    Sleep -> 7,
);

/// State Change used to build [`FlowExpresion`](struct.FlowExpresion.html)s
///
/// The state change can be either: color (rgb), color temperature (ct) or sleep.
///
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FlowTuple {
    pub duration: Duration,
    pub mode: FlowMode,
    pub value: u32,
    pub brightness: i8,
}

impl FlowTuple {
    /// Create FlowTuple specifying the mode as a parameter
    /// # Arguments
    ///
    /// * `duration`: duration of change
    /// * `mode`: [`FlowMode`](enum.FlowMode.html) Color / CT / Sleep.
    /// * `value`: RGB color for color mode, CT for ct mode (ignored by sleep)
    /// * `brightness`: percentage (`1` to `100`) `-1` to keep previous value (ignored by sleep)
    ///
    pub fn new(duration: Duration, mode: FlowMode, value: u32, brightness: i8) -> Self {
        Self {
            duration,
            mode,
            value,
            brightness,
        }
    }

    /// Create RGB FlowTuple
    ///
    /// # Arguments
    ///
    /// * `duration`: duration of change
    /// * `rgb`: color in RGB format (`0x00_00_00` to `0xff_ff_ff`)
    /// * `brightness`: percentage (`1` to `100`) `-1` to keep previous value.
    ///
    pub fn rgb(duration: Duration, rgb: u32, brightness: i8) -> Self {
        Self {
            duration,
            mode: FlowMode::Color,
            value: rgb,
            brightness,
        }
    }

    /// Create Color Temperature FlowTuple
    ///
    /// # Arguments
    ///
    /// * `duration`: duration
    /// * `ct`: color temperature (`1700` to `6500`) K (may vary between models).
    /// * `brightness`: percentage (`1` to `100`) or `-1` to keep previous value.
    ///
    pub fn ct(duration: Duration, ct: u32, brightness: i8) -> Self {
        Self {
            duration,
            mode: FlowMode::CT,
            value: ct,
            brightness,
        }
    }

    /// Create Sleep FlowTuple
    ///
    /// # Arguments
    ///
    /// * `duration`: time to sleep
    ///
    pub fn sleep(duration: Duration) -> Self {
        Self {
            duration,
            mode: FlowMode::Sleep,
            value: 0,
            brightness: -1,
        }
    }
}

impl ToString for FlowTuple {
    fn to_string(&self) -> String {
        format!(
            "{},{},{},{}",
            self.duration.as_millis(),
            self.mode.to_string(),
            self.value,
            self.brightness
        )
    }
}

/// FlowExpresion consisting of a series of `FlowTuple`s
///
/// # Example
///```
///# use yeelight::{FlowTuple, FlowExpresion};
///# use std::time::Duration;
/// let duration = Duration::from_secs(1);
/// let brightness = 100; // percentage 1..100 (-1 to keep previous)
///
/// let police = FlowExpresion(vec![
///     FlowTuple::rgb(duration, 0xff_00_00, brightness),
///     FlowTuple::rgb(duration, 0x00_00_ff, brightness),
/// ]);
///
/// let police2 = FlowExpresion(vec![
///     FlowTuple::rgb(duration, 0xff_00_00, brightness),
///     FlowTuple::rgb(duration, 0xff_00_00, 1),
///     FlowTuple::rgb(duration, 0xff_00_00, brightness),
///     FlowTuple::sleep(duration),
///     FlowTuple::rgb(duration, 0x00_00_ff, brightness),
///     FlowTuple::rgb(duration, 0x00_00_ff, 1),
///     FlowTuple::rgb(duration, 0x00_00_ff, brightness),
///     FlowTuple::sleep(duration),
/// ]);
///```
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FlowExpresion(pub Vec<FlowTuple>);

impl Stringify for FlowExpresion {
    fn stringify(&self) -> String {
        let mut s = '"'.to_string();
        for tuple in self.0.iter() {
            s.push_str(&tuple.to_string());
            s.push(',');
        }
        s.pop();
        s.push('"');
        s
    }
}

#[cfg(feature = "from-str")]
impl ::std::str::FromStr for FlowExpresion {
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut v = Vec::new();
        for (duration, mode, value, brightness) in s.split(',').tuples() {
            let duration = Duration::from_millis(duration.parse::<u64>()?);
            let value = value.parse::<u32>()?;
            let mode = match FlowMode::from_str(mode) {
                Ok(m) => Ok(m),
                Err(_) => match mode {
                    "1" => Ok(FlowMode::Color),
                    "2" => Ok(FlowMode::CT),
                    "7" => Ok(FlowMode::Sleep),
                    _ => Err(ParseError(format!(
                        "Could not parse FlowMode: {}\nvalid values: 1 (Color), 2(CT), 7(Sleep)",
                        mode.to_string()
                    ))),
                },
            }?;
            let brightness = brightness.parse::<i8>()?;
            v.push(FlowTuple {
                duration,
                mode,
                value,
                brightness,
            });
        }
        Ok(FlowExpresion(v))
    }
}

/// List of `Property` (used by `get_prop`)
///
/// # Example
///```
///# use yeelight::{Properties, Property};
/// let props = Properties(vec![
///     Property::Name,
///     Property::Power,
///     Property::Bright,
///     Property::CT,
///     Property::RGB,
///     Property::ColorMode,
///     Property::Flowing,
/// ]);
///```
#[derive(Debug, Serialize, Deserialize)]
pub struct Properties(pub Vec<Property>);

impl Stringify for Properties {
    fn stringify(&self) -> String {
        self.0
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<String>>()
            .join(",")
    }
}

impl Stringify for Duration {
    fn stringify(&self) -> String {
        format!("{}", self.as_millis())
    }
}

// Convert function parameters into comma separated string
macro_rules! params {
    ($($v:tt),+) => {
        vec!( $( $v.stringify() ),+ ).join(",")
    };
    () => {""};
}

// Generate function
macro_rules! gen_func {
    ($(#[$comment:meta])* $name:ident - $( $p:ident : $t:ty ),* ) => {

            $(#[$comment])*
            pub async fn $name(&mut self, $($p : $t),*) -> Result<Option<Response>, BulbError> {
                self.writer.send(
                    &stringify!($name), &params!($($p),*)
                ).await
            }

    };
    ($(#[$comment:meta])* $fn_default:ident / $(#[$comment_bg:meta])* $fn_bg:ident - $( $p:ident : $t:ty ),* ) => {

        gen_func!($(#[$comment])* $fn_default - $($p : $t),*);
        gen_func!($(#[$comment_bg])* $fn_bg - $($p : $t),*);

    };
    ($(#[$comment:meta])* $name:ident) => { gen_func!($(#[$comment])* $name - ); };
    ($(#[$comment:meta])* $fn_default:ident / $(#[$comment_bg:meta])* $fn_bg:ident) => {
        gen_func!($(#[$comment])* $fn_default / $(#[$comment_bg])* $fn_bg - );
    };
}

/// # Messages
///
/// This are all the methods as by the yeelight API spec.
/// They all take the parameters specified by the spec, build a message, send
/// it to the bulb and wait for the response which is parsed into a
/// [`Response`].
///
/// ## Example
///
/// ```
/// # async fn test() {
/// # use yeelight::*;
/// # use std::time::Duration;
/// let mut bulb = Bulb::connect("192.168.1.204", 0).await.expect("Connection failed");
/// let response = bulb.set_power(Power::On, Effect::Smooth, Duration::from_secs(1), Mode::Normal).await.unwrap();
///
/// match response {
///     Some(vec) => {
///         // In this case, the response should be ["ok"].
///         for v in vec.iter() {
///             println!("{}", v);
///         }
///     },
///     None => eprintln!("This should not happen"),
/// }
/// # }
/// ```
///
/// [`Response`]: enum.Response.html
// #[rustfmt::skip]
impl Bulb {
    gen_func!(
        /// Retrieve current propertes of smart LED.
        ///
        /// Parameters:
        ///
        /// - `properties`: List of properties. The answer will follow the same order.
        get_prop
            - properties: &Properties
    );

    gen_func!(
        /// Switch on or off the smart LED (software managed on/off).
        ///
        /// Parameters:
        ///
        /// - `power`:
        /// - `effect`:
        /// - `duration`: total time of the gradual changing (minimum 30 milliseconds) (Ignored if
        /// `effect` is `Sudden`)
        /// - `mode`: Mode in which the lamp will turn on (`Mode::Normal` to keep the current mode)
        set_power
            / ///
            bg_set_power
            - power: Power,
        effect: Effect,
        duration: Duration,
        mode: Mode
    );
    pub async fn on(&mut self, _cron_type: CronType) -> Result<Option<Response>, BulbError> {
        self.set_power(
            Power::On,
            Effect::Sudden,
            Duration::from_millis(0),
            Mode::Normal,
        )
        .await
    }
    pub async fn off(&mut self, _cron_type: CronType) -> Result<Option<Response>, BulbError> {
        self.set_power(
            Power::Off,
            Effect::Sudden,
            Duration::from_millis(0),
            Mode::Normal,
        )
        .await
    }
    pub async fn bg_on(&mut self, _cron_type: CronType) -> Result<Option<Response>, BulbError> {
        self.bg_set_power(
            Power::On,
            Effect::Sudden,
            Duration::from_millis(0),
            Mode::Normal,
        )
        .await
    }
    pub async fn bg_off(&mut self, _cron_type: CronType) -> Result<Option<Response>, BulbError> {
        self.bg_set_power(
            Power::Off,
            Effect::Sudden,
            Duration::from_millis(0),
            Mode::Normal,
        )
        .await
    }
    gen_func!(
        /// Flip the main light power state
        toggle
            / /// Flip the background light power state
            bg_toggle
    );
    gen_func!(
        /// Flip the both the main light and the background light power state
        dev_toggle
    );

    gen_func!(
        /// Set light color temperature
        set_ct_abx
            / /// Set background light color temperature
            bg_set_ct_abx
            - ct_value: u16,
        effect: Effect,
        duration: Duration
    );
    gen_func!(
        set_rgb / bg_set_rgb - rgb_value: u32,
        effect: Effect,
        duration: Duration
    );
    gen_func!(
        set_hsv / bg_set_hsv - hue: u16,
        sat: u8,
        effect: Effect,
        duration: Duration
    );
    gen_func!(
        set_bright / bg_set_bright - brightness: u8,
        effect: Effect,
        duration: Duration
    );
    gen_func!(
        set_scene / bg_set_scene - class: Class,
        val1: u64,
        val2: u64,
        val3: u64
    );

    gen_func!(
        start_cf / bg_start_cf - count: u8,
        action: CfAction,
        flow_expression: FlowExpresion
    );
    gen_func!(stop_cf / bg_stop_cf);

    gen_func!(
        /// Change brightness, CT or color of a smart LED without knowing the current value.
        ///
        /// # Parameters
        ///
        /// - action: The direction of the adjustment ([AdjustAction::Increase], [AdjustAction::Decrease][AdjustAction::Circle])
        ///
        /// - prop: The property to adjust ([Prop::Bright], [Prop::CT], [Prop::Color])
        ///
        /// **NOTES:** When `prop` is [Prop::Color], the `action` can only be
        /// [AdjustAction::Circle], otherwise the request will be invalid.
        ///
        set_adjust
            / /// Change brightness, CT or color of a **background** smart LED without knowing the
            /// current value.
            ///
            /// **See:** [Bulb::set_adjust]
            bg_set_adjust
            - action: AdjustAction,
        prop: Prop
    );
    gen_func!(
        adjust_bright / bg_adjust_bright - percentage: i8,
        duration: Duration
    );
    gen_func!(
        adjust_ct / bg_adjust_ct - percentage: i8,
        duration: Duration
    );
    gen_func!(
        adjust_color / bg_adjust_color - percentage: i8,
        duration: Duration
    );

    gen_func!(
        /// Save current state of smart LED in persistent memory.
        ///
        /// If user powers off and then powers on the smart LED again (hard power reset), the
        /// smart LED will show last saved state.
        ///
        /// > **NOTE:**  Only accepted if the smart LED is currently in "on" state.
        ///
        set_default
            / /// Save current state of the **background** smart LED in persistent memory.
            ///
            /// **See:** [Bulb::set_default]
            bg_set_default
    );

    gen_func!(
        /// Set the device name.
        ///
        /// The name will be stored on the device and reported in discovering response.
        set_name
            - name: &str
    );

    gen_func!(
        /// Start or stop music mode on a device.
        ///
        /// Under music mode, no property will be reported and no message quota is checked.
        set_music
            - action: MusicAction,
        host: &str,
        port: u16
    );

    gen_func!(
        /// Start a timer job on the smart LED.
        ///
        /// Currently there is only a timer type.
        cron_add
            - cron_type: CronType,
        value: u64
    );

    gen_func!(
        /// Stop the current cron job
        cron_del
            - cron_type: CronType
    );

    // gen_func!(cron_get                          - cron_type: CronType);
    // cron_get response is a dictionary which is difficult to parse,
    // instead use delayoff property which should give the same values.

    /// Get the settings of the current cron job.
    pub async fn cron_get(&mut self, _cron_type: CronType) -> Result<Option<Response>, BulbError> {
        self.get_prop(&Properties(vec![Property::DelayOff])).await
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    use tokio::{
        net::{TcpListener, TcpStream},
        task::JoinHandle,
    };

    async fn fake_bulb(expect: &'static str, response: &'static str) -> (Bulb, JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        let task = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.unwrap();

            loop {
                stream.readable().await.unwrap();

                let mut buf = [0; 4096];

                match stream.try_read(&mut buf) {
                    Ok(n) => {
                        let data = ::std::str::from_utf8(&buf[0..n]).unwrap();
                        stream.try_write(response.as_bytes()).unwrap();

                        assert_eq!(data, expect);
                    }
                    Err(_) => {
                        return;
                    }
                }
            }
        });

        let stream = TcpStream::connect(addr).await.unwrap();
        (Bulb::attach_tokio(stream), task)
    }

    #[tokio::test]
    async fn get_prop() {
        let expect = "{\"id\":1,\"method\":\"get_prop\",\"params\":[\"name\",\"power\"]}\r\n";
        let response = "{\"id\":1, \"result\":[\"bulb_name\",\"on\"]}\r\n";

        let (mut bulb, task) = fake_bulb(expect, response).await;

        let prop = &Properties(vec![Property::Name, Property::Power]);

        let (tres, res) = tokio::join!(task, bulb.get_prop(prop));
        tres.unwrap();

        if let Ok(Some(properties)) = res {
            assert_eq!(properties, vec!["bulb_name", "on"]);
        } else {
            assert!(false, "Unexpected result: {:?}", res);
        }
    }

    #[tokio::test]
    async fn set_power() {
        let expect = "{\"id\":1,\"method\":\"set_power\",\"params\":[\"on\",\"smooth\",500,0]}\r\n";
        let response = "{\"id\":1, \"result\":[\"ok\"]}\r\n";

        let (mut bulb, task) = fake_bulb(expect, response).await;

        let (tres, res) = tokio::join!(
            task,
            bulb.set_power(
                Power::On,
                Effect::Smooth,
                Duration::from_millis(500),
                Mode::Normal
            )
        );
        tres.unwrap();

        if let Ok(Some(properties)) = res {
            assert_eq!(properties, vec!["ok"]);
        } else {
            assert!(false, "Unexpected result: {:?}", res);
        }
    }

    #[tokio::test]
    async fn unsupported() {
        let expect = "{\"id\":1,\"method\":\"set_power\",\"params\":[\"on\",\"smooth\",500,0]}\r\n";
        let response =
            "{\"id\":1, \"error\":{\"code\":-1, \"message\":\"unsupported method\"}}\r\n";

        let (mut bulb, task) = fake_bulb(expect, response).await;

        let (tres, res) = tokio::join!(
            task,
            bulb.set_power(
                Power::On,
                Effect::Smooth,
                Duration::from_millis(500),
                Mode::Normal
            )
        );
        tres.unwrap();

        if let Err(error) = res {
            assert_eq!(
                "Bulb response error: unsupported method (code -1)",
                error.to_string()
            );
            if let BulbError::ErrResponse(code, message) = error {
                assert_eq!(code, -1);
                assert_eq!(message, "unsupported method");
            }
        } else {
            assert!(false, "Unexpected result: {:?}", res);
        }
    }

    #[tokio::test]
    async fn no_response() {
        let expect = "{\"id\":1,\"method\":\"set_power\",\"params\":[\"on\",\"smooth\",500,0]}\r\n";
        let response = "{\"empty\"}";

        let (mut bulb, task) = fake_bulb(expect, response).await;

        bulb = bulb.get_response().no_response();

        let (tres, res) = tokio::join!(
            task,
            bulb.set_power(
                Power::On,
                Effect::Smooth,
                Duration::from_millis(500),
                Mode::Normal
            )
        );
        tres.unwrap();
        assert_eq!(res.unwrap(), None);
    }

    #[tokio::test]
    async fn notify() {
        let expect = "{\"id\":1,\"method\":\"set_power\",\"params\":[\"on\",\"smooth\",500,0]}\r\n";
        let response = "{\"method\":\"props\",\"params\":{\"power\":\"on\", \"bright\":\"10\"}}\r\n{\"id\":1, \"result\":[\"ok\"]}\r\n";

        let (mut bulb, task) = fake_bulb(expect, response).await;
        let mut recv = bulb.get_notify().await;

        let (tres, res) = tokio::join!(
            task,
            bulb.set_power(
                Power::On,
                Effect::Smooth,
                Duration::from_millis(500),
                Mode::Normal
            )
        );
        tres.unwrap();

        if let Ok(Some(properties)) = res {
            assert_eq!(properties, vec!["ok"]);
        } else {
            assert!(false, "Unexpected result: {:?}", res);
        }

        while let Some(Notification(i)) = recv.recv().await {
            println!("Something");
            for (k, v) in i.iter() {
                println!("{} {}", k, v);
            }
            break;
        }
    }
}