yeelight 0.4.1

Rust CLI and API bindings for yeelight WiFi Light Inter-Operation
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
mod presets;

use std::{collections::HashSet, net::IpAddr, time::Duration};

use itertools::join;
use structopt::{
    clap::{AppSettings, ArgGroup},
    StructOpt,
};

use tokio::sync::mpsc;

#[derive(Debug, StructOpt)]
#[structopt(
    name = "yeelight",
    about = "A CLI to control your Yeelight smart lights."
)]
#[structopt(global_setting = AppSettings::ColoredHelp)]
struct Options {
    #[structopt(
        env = "YEELIGHT_ADDR",
        default_value = "NULL",
        help = "The IP address or name of the bulb (if 'all', perform command on all bulbs found)"
    )]
    address: String,
    #[structopt(short, long, default_value = "55443", env = "YEELIGHT_PORT")]
    port: u16,
    #[structopt(short, long, default_value = "5000", env = "YEELIGHT_TIMEOUT")]
    timeout: u64,
    #[structopt(subcommand)]
    subcommand: Command,
}

#[derive(Debug, StructOpt, Clone)]
enum Command {
    #[structopt(about = "Get properties")]
    Get {
        #[structopt(possible_values = &yeelight::Property::variants(), case_insensitive = true)]
        #[structopt(required = true)]
        properties: Vec<yeelight::Property>,
        #[structopt(long, help = "Output in JSON format")]
        json: bool,
    },
    #[structopt(about = "Toggle light")]
    #[structopt(group = ArgGroup::with_name("light"))]
    Toggle {
        #[structopt(long, group = "light", help = "Perform action on all lights of device")]
        dev: bool,
        #[structopt(long, group = "light", help = "Perform action on background light")]
        bg: bool,
    },
    #[structopt(about = "Turn on light")]
    On {
        #[structopt(possible_values = &yeelight::Effect::variants(), case_insensitive = true)]
        #[structopt(short, long, default_value = "Smooth")]
        effect: yeelight::Effect,
        #[structopt(short, long, default_value = "500")]
        duration: u64,
        #[structopt(possible_values = &yeelight::Mode::variants(), case_insensitive = true)]
        #[structopt(short, long, default_value = "Normal")]
        mode: yeelight::Mode,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    #[structopt(about = "Turn off light")]
    Off {
        #[structopt(possible_values = &yeelight::Effect::variants(), case_insensitive = true)]
        #[structopt(short, long, default_value = "Smooth")]
        effect: yeelight::Effect,
        #[structopt(short, long, default_value = "500")]
        duration: u64,
        #[structopt(possible_values = &yeelight::Mode::variants(), case_insensitive = true)]
        #[structopt(short, long, default_value = "Normal")]
        mode: yeelight::Mode,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    #[structopt(about = "Start timer")]
    Timer { minutes: u64 },
    #[structopt(about = "Clear current timer")]
    TimerClear,
    #[structopt(about = "Get remaining minutes for timer")]
    TimerGet,
    #[structopt(about = "Set values")]
    Set {
        #[structopt(flatten)]
        property: Prop,
        #[structopt(possible_values = &yeelight::Effect::variants(), case_insensitive = true)]
        #[structopt(short, long, default_value = "Smooth")]
        effect: yeelight::Effect,
        #[structopt(short, long, default_value = "500")]
        duration: u64,
    },
    #[structopt(about = "Start color flow")]
    Flow {
        expression: yeelight::FlowExpresion,
        #[structopt(default_value = "0")]
        count: u8,
        #[structopt(possible_values = &yeelight::CfAction::variants(), case_insensitive = true)]
        #[structopt(default_value = "Recover")]
        action: yeelight::CfAction,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    #[structopt(about = "Stop color flow")]
    FlowStop {
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    #[structopt(about = "Adjust properties (Bright/CT/Color) (increase/decrease/circle)")]
    Adjust {
        #[structopt(possible_values = &yeelight::Prop::variants(), case_insensitive = true)]
        property: yeelight::Prop,
        #[structopt(possible_values = &yeelight::AdjustAction::variants(), case_insensitive = true)]
        action: yeelight::AdjustAction,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    #[structopt(about = "Adjust properties (Bright/CT/Color) with percentage (-100~100)")]
    #[structopt(setting = AppSettings::AllowNegativeNumbers)]
    AdjustPercent {
        #[structopt(possible_values = &yeelight::Prop::variants(), case_insensitive = true)]
        property: yeelight::Prop,
        percent: i8,
        #[structopt(default_value = "500")]
        duration: u64,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    #[structopt(about = "Connect to music TCP stream")]
    MusicConnect { host: String, port: u16 },
    #[structopt(about = "Stop music mode")]
    MusicStop,
    #[structopt(about = "Presets")]
    Preset {
        #[structopt(possible_values = &presets::Preset::variants(), case_insensitive = true)]
        preset: presets::Preset,
    },
    #[structopt(about = "Listen to notifications from lamp")]
    Listen,
    #[structopt(about = "Search for lamps in the network")]
    Discover {
        #[structopt(long, default_value = "5000")]
        duration: u64,
    },
}

#[derive(Debug, StructOpt, Clone)]
enum Prop {
    Power {
        #[structopt(possible_values = &yeelight::Power::variants(), case_insensitive = true)]
        power: yeelight::Power,
        #[structopt(possible_values = &yeelight::Mode::variants(), case_insensitive = true)]
        #[structopt(default_value = "Normal")]
        mode: yeelight::Mode,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    CT {
        color_temperature: u16,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    RGB {
        rgb_value: u32,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    HSV {
        hue: u16,
        #[structopt(default_value = "100")]
        sat: u8,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    Bright {
        brightness: u8,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    Name {
        name: String,
    },
    Scene {
        #[structopt(possible_values = &yeelight::Class::variants(), case_insensitive = true)]
        class: yeelight::Class,
        val1: u64,
        #[structopt(default_value = "100")]
        val2: u64,
        #[structopt(default_value = "100")]
        val3: u64,
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
    Default {
        #[structopt(long, help = "Perform action on background light")]
        bg: bool,
    },
}

macro_rules! sel_bg {
    ($obj:tt.$fn:ident ($($p:expr),*) || $fn_bg:ident if $var:tt ) => (
        if $var {
            $obj.$fn_bg($($p),*).await
        } else {
            $obj.$fn($($p),*).await
        }
    );
}

fn display_dbulb_info(dbulb: &yeelight::discover::DiscoveredBulb) {
    let dash = "-".to_owned();
    let name = dbulb.properties.get("name").unwrap_or(&dash);
    let location = dbulb
        .properties
        .get("Location")
        .unwrap_or(&dash)
        .trim_start_matches("yeelight://");
    eprintln!("{}\t{}", &location, &name);
}

#[tokio::main]
async fn main() {
    let opt = Options::from_args();

    // If discovery is used, we do not try to connect to any bulb
    if let Command::Discover { duration } = opt.subcommand {
        let (tx, mut rx) = mpsc::channel(5);
        tokio::spawn(discover_unique_with_timeout(tx, duration));
        while let Some(dbulb) = rx.recv().await {
            display_dbulb_info(&dbulb);
        }

        return;
    }

    // If the address is ALL or all, we run the command for all the bulbs we find
    if opt.address.to_lowercase() == "all" {
        eprintln!("Discovering bulbs...");
        let (tx, mut rx) = mpsc::channel(5);
        tokio::spawn(discover_unique_with_timeout(tx, opt.timeout));

        let unnamed = "Unnamed".to_owned();
        let mut unnamed_count = 0;

        // Check if the command is get --json
        let is_get_json = if let Command::Get { json, .. } = opt.subcommand.clone() {
            json
        } else {
            false
        };

        if is_get_json {
            println!("{{");
        }

        let mut first = true;
        while let Some(dbulb) = rx.recv().await {
            display_dbulb_info(&dbulb);
            let bulb = dbulb.connect().await.unwrap();
            let response = run_command(opt.subcommand.clone(), bulb).await.unwrap();

            let mut has_name = true;
            let name = dbulb.properties.get("name").unwrap_or_else(|| {
                unnamed_count += 1;
                has_name = false;
                &unnamed
            });

            if let Some(result) = response {
                result.iter().for_each(|x| {
                    if x != "ok" {
                        if !first {
                            println!(",");
                        } else {
                            first = false;
                        }

                        let unnamed_name = format!("{}{}", &unnamed, unnamed_count);
                        let name = if has_name { name } else { &unnamed_name };

                        if is_get_json {
                            print!("\"{}\":{}", &name, x)
                        } else {
                            print!("{}: {}", &name, x)
                        }
                    }
                });
            }
        }

        if is_get_json {
            println!();
            println!("}}");
        }

        return;
    }

    // At this point, if the address is NULL, the user did not specify the address so we error
    if opt.address == "NULL" {
        structopt::clap::Error::with_description(
            "No address specified (use --help for more info)",
            structopt::clap::ErrorKind::MissingRequiredArgument,
        )
        .exit();
    }

    // If the address is valid, try to connect to it
    let bulb = if opt.address.parse::<IpAddr>().is_ok() {
        tokio::time::timeout(Duration::from_secs(opt.timeout), async {
            return yeelight::Bulb::connect(&opt.address, opt.port)
                .await
                .unwrap();
        })
        .await
        .unwrap()
    } else {
        // otherwise, search for bulbs matching the name
        println!("Discovering bulbs...");
        let (tx, mut rx) = mpsc::channel(5);
        tokio::spawn(discover_unique_with_timeout(tx, opt.timeout));
        (async {
            while let Some(dbulb) = rx.recv().await {
                display_dbulb_info(&dbulb);
                let name = dbulb.properties.get("name").unwrap();
                if name == &opt.address {
                    return Some(dbulb.connect().await.unwrap());
                }
            }
            return None;
        })
        .await
        .unwrap_or_else(|| {
            structopt::clap::Error::with_description(
                "Bulb not found",
                structopt::clap::ErrorKind::InvalidValue,
            )
            .exit();
        })
    };

    let response = run_command(opt.subcommand, bulb).await.unwrap();

    if let Some(result) = response {
        result.iter().for_each(|x| {
            if x != "ok" {
                println!("{}", x)
            }
        });
    }
}

async fn run_command(
    command: Command,
    bulb: yeelight::Bulb,
) -> Result<Option<Vec<String>>, yeelight::BulbError> {
    let mut bulb = bulb;
    return match command {
        Command::Toggle { bg, dev } => match (bg, dev) {
            (true, _) => bulb.bg_toggle().await,
            (_, true) => bulb.dev_toggle().await,
            _ => bulb.toggle().await,
        },
        Command::On {
            effect,
            duration,
            mode,
            bg,
        } => {
            sel_bg!(bulb.set_power(yeelight::Power::On, effect, Duration::from_millis(duration), mode) || bg_set_power if bg)
        }
        Command::Off {
            effect,
            duration,
            mode,
            bg,
        } => {
            sel_bg!(bulb.set_power(yeelight::Power::Off, effect, Duration::from_millis(duration), mode) || bg_set_power if bg)
        }
        Command::Get { properties, json } => {
            let states = bulb
                .get_prop(&yeelight::Properties(properties.clone()))
                .await;
            if !json {
                return states;
            }

            if let Ok(Some(states)) = states {
                let states: Vec<String> = states
                    .into_iter()
                    .zip(properties)
                    .map(|(state, prop)| {
                        // if state is a number, we want to display it as a number, not a string
                        if let Ok(num) = state.parse::<f64>() {
                            return format!("{}:{}", prop, num);
                        }
                        format!("{}:\"{}\"", prop, state)
                    })
                    .collect();
                let states = join(states, ", ");

                Ok(Some(vec![format!("{{{}}}", states)]))
            } else {
                states
            }
        }
        Command::Set {
            property,
            effect,
            duration,
        } => match property {
            Prop::Power { power, mode, bg } => {
                sel_bg!(bulb.set_power(power, effect, Duration::from_millis(duration), mode) || bg_set_power if bg)
            }
            Prop::CT {
                color_temperature,
                bg,
            } => {
                sel_bg!(bulb.set_ct_abx(color_temperature, effect, Duration::from_millis(duration)) || bg_set_ct_abx if bg)
            }
            Prop::RGB { rgb_value, bg } => {
                sel_bg!(bulb.set_rgb(rgb_value, effect, Duration::from_millis(duration)) || bg_set_rgb if bg)
            }
            Prop::HSV { hue, sat, bg } => {
                sel_bg!(bulb.set_hsv(hue, sat, effect, Duration::from_millis(duration)) || bg_set_hsv if bg)
            }
            Prop::Bright { brightness, bg } => {
                sel_bg!(bulb.set_bright(brightness, effect, Duration::from_millis(duration)) || bg_set_bright if bg)
            }
            Prop::Name { name } => bulb.set_name(&name).await,
            Prop::Scene {
                class,
                val1,
                val2,
                val3,
                bg,
            } => sel_bg!(bulb.set_scene(class, val1, val2, val3) || bg_set_scene if bg),
            Prop::Default { bg } => sel_bg!(bulb.set_default() || bg_set_default if bg),
        },
        Command::Timer { minutes } => bulb.cron_add(yeelight::CronType::Off, minutes).await,
        Command::TimerClear => bulb.cron_del(yeelight::CronType::Off).await,
        Command::TimerGet => bulb.cron_get(yeelight::CronType::Off).await,
        Command::Flow {
            count,
            action,
            expression,
            bg,
        } => sel_bg!(bulb.start_cf(count, action, expression) || bg_start_cf if bg),
        Command::FlowStop { bg } => sel_bg!(bulb.stop_cf() || bg_stop_cf if bg),
        Command::Adjust {
            action,
            property,
            bg,
        } => sel_bg!(bulb.set_adjust(action, property) || bg_set_adjust if bg),
        Command::AdjustPercent {
            property,
            percent,
            duration,
            bg,
        } => match property {
            yeelight::Prop::Bright => {
                sel_bg!(bulb.adjust_bright(percent, Duration::from_millis(duration)) || bg_adjust_bright if bg)
            }
            yeelight::Prop::Color => {
                sel_bg!(bulb.adjust_color(percent, Duration::from_millis(duration)) || bg_adjust_color if bg)
            }
            yeelight::Prop::CT => {
                sel_bg!(bulb.adjust_ct(percent, Duration::from_millis(duration)) || bg_adjust_ct if bg)
            }
        },
        Command::MusicConnect { host, port } => {
            bulb.set_music(yeelight::MusicAction::On, &host, port).await
        }
        Command::MusicStop => {
            bulb.set_music(yeelight::MusicAction::Off, &"".to_string(), 0)
                .await
        }
        Command::Preset { preset } => presets::apply(bulb, preset).await,
        Command::Listen => {
            let (sender, mut recv) = mpsc::channel(10);

            bulb.set_notify(sender).await;

            while let Some(yeelight::Notification(i)) = recv.recv().await {
                for (k, v) in i.iter() {
                    println!("{} {}", k, v);
                }
            }
            Ok(None)
        }
        Command::Discover { duration: _ } => unreachable!(), // Special command run in main
    };
}

async fn discover_unique_with_timeout(
    rx: mpsc::Sender<yeelight::discover::DiscoveredBulb>,
    timeout: u64,
) {
    let search = async move {
        let mut channel = yeelight::discover::find_bulbs().await.unwrap();
        let mut found = HashSet::new();

        while let Some(dbulb) = channel.recv().await {
            if found.contains(&dbulb.uid) {
                continue;
            }
            found.insert(dbulb.uid);
            if rx.send(dbulb).await.is_err() {
                break;
            }
        }
    };

    // if duration if == 0 do not timeout
    if timeout > 0 {
        let _ = tokio::time::timeout(Duration::from_millis(timeout), search).await;
    } else {
        search.await;
    }
}