denki 0.1.2

CLI for controlling TP-Link smart home devices — denki (電気) means electricity in Japanese
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
use crate::devices::{self, DeviceKind};
use crate::hosts;
use crate::ops;
use crate::output::terminal_text;
use crate::resolve::{Resolved, resolve};
use crate::strip;
use anyhow::Result;
use clap::ValueEnum;
use colored::Colorize;
use futures_util::{StreamExt, stream};

use super::shared::{
    StripOutletTarget, print_outlet_power_state, print_outlet_toggle_state, print_power_state,
    resolve_power_target, tapo_session,
};

fn toggle_target(_kind: &DeviceKind, json: &serde_json::Value) -> Result<bool> {
    Ok(!ops::kasa_power_state(json)?)
}

async fn kasa_set_power(ip: &str, kind: &DeviceKind, on: bool) -> Result<()> {
    devices::can_control_power(kind)?;
    match (kind, on) {
        (DeviceKind::Bulb, true) => ops::bulb_on(ip).await?,
        (DeviceKind::Bulb, false) => ops::bulb_off(ip).await?,
        (DeviceKind::LightStrip, state) => ops::lightstrip_set_power(ip, state).await?,
        (_, true) => ops::relay_on(ip).await?,
        (_, false) => ops::relay_off(ip).await?,
    }
    Ok(())
}

async fn set_device_power(r: &Resolved, on: bool) -> Result<()> {
    match r.protocol {
        hosts::Protocol::Tapo => {
            crate::tapo_client::set_power(&r.ip, Some(on)).await?;
        }
        hosts::Protocol::Klap => {
            let mut s = tapo_session(&r.ip).await?;
            if on {
                ops::tapo_on(&mut s).await?;
            } else {
                ops::tapo_off(&mut s).await?;
            }
        }
        hosts::Protocol::Kasa => {
            let json = ops::sysinfo(&r.ip).await?;
            kasa_set_power(&r.ip, &devices::detect_kind(&json), on).await?;
        }
    }
    Ok(())
}

trait DeviceTransport: Sync {
    async fn apply_power(&self, target: &Resolved, action: GroupAction) -> Result<bool>;
}

struct LiveDeviceTransport;

impl DeviceTransport for LiveDeviceTransport {
    async fn apply_power(&self, target: &Resolved, action: GroupAction) -> Result<bool> {
        match action {
            GroupAction::On => {
                set_device_power(target, true).await?;
                Ok(true)
            }
            GroupAction::Off => {
                set_device_power(target, false).await?;
                Ok(false)
            }
            GroupAction::Toggle => match target.protocol {
                hosts::Protocol::Tapo => crate::tapo_client::set_power(&target.ip, None).await,
                hosts::Protocol::Klap => {
                    let mut session = tapo_session(&target.ip).await?;
                    ops::tapo_toggle(&mut session).await
                }
                hosts::Protocol::Kasa => {
                    let json = ops::sysinfo(&target.ip).await?;
                    let kind = devices::detect_kind(&json);
                    let on = toggle_target(&kind, &json)?;
                    kasa_set_power(&target.ip, &kind, on).await?;
                    Ok(on)
                }
            },
        }
    }
}

pub async fn handle_on(host: &str, outlet: Option<u8>) -> Result<()> {
    let (r, target) = resolve_power_target(host, outlet, "on <outlet>").await?;
    if let Some(outlet_num) = outlet {
        let StripOutletTarget {
            child_id,
            child_alias,
            ..
        } = target.expect("outlet target should be present here");
        ops::strip_outlet_on(&r.ip, &child_id).await?;
        print_outlet_power_state(outlet_num, &child_alias, true);
    } else {
        set_device_power(&r, true).await?;
        print_power_state(&r.ip, true);
    }
    Ok(())
}

pub async fn handle_off(host: &str, outlet: Option<u8>) -> Result<()> {
    let (r, target) = resolve_power_target(host, outlet, "off <outlet>").await?;
    if let Some(outlet_num) = outlet {
        let StripOutletTarget {
            child_id,
            child_alias,
            ..
        } = target.expect("outlet target should be present here");
        ops::strip_outlet_off(&r.ip, &child_id).await?;
        print_outlet_power_state(outlet_num, &child_alias, false);
    } else {
        set_device_power(&r, false).await?;
        print_power_state(&r.ip, false);
    }
    Ok(())
}

pub async fn handle_toggle(host: &str, outlet: Option<u8>) -> Result<()> {
    let (r, target) = resolve_power_target(host, outlet, "toggle <outlet>").await?;
    if let Some(outlet_num) = outlet {
        let StripOutletTarget {
            child_id,
            child_alias,
            was_on,
        } = target.expect("outlet target should be present here");
        let now_on = if was_on {
            ops::strip_outlet_off(&r.ip, &child_id).await?;
            false
        } else {
            ops::strip_outlet_on(&r.ip, &child_id).await?;
            true
        };
        print_outlet_toggle_state(outlet_num, &child_alias, now_on);
    } else {
        let now_on = match r.protocol {
            hosts::Protocol::Tapo => crate::tapo_client::set_power(&r.ip, None).await?,
            hosts::Protocol::Klap => {
                let mut s = tapo_session(&r.ip).await?;
                ops::tapo_toggle(&mut s).await?
            }
            hosts::Protocol::Kasa => {
                let json = ops::sysinfo(&r.ip).await?;
                let kind = devices::detect_kind(&json);
                let on = toggle_target(&kind, &json)?;
                kasa_set_power(&r.ip, &kind, on).await?;
                on
            }
        };
        print_power_state(&r.ip, now_on);
    }
    Ok(())
}

#[derive(ValueEnum, Clone, Copy, Debug)]
pub enum GroupAction {
    /// Turn every matched alias on.
    On,
    /// Turn every matched alias off.
    Off,
    /// Toggle every matched alias.
    Toggle,
}

impl GroupAction {
    fn as_verb(&self) -> &'static str {
        match self {
            Self::On => "on",
            Self::Off => "off",
            Self::Toggle => "toggle",
        }
    }
}

#[derive(Debug)]
struct GroupResult {
    alias: String,
    result: Result<bool>,
}

async fn execute_group<T: DeviceTransport>(
    matches: Vec<(String, hosts::HostEntry)>,
    action: GroupAction,
    concurrency: usize,
    transport: &T,
) -> Vec<GroupResult> {
    stream::iter(matches.into_iter().map(|(alias, entry)| async move {
        let target = Resolved {
            ip: entry.ip,
            protocol: entry.protocol,
            saved_name: Some(alias.clone()),
        };
        GroupResult {
            alias,
            result: transport.apply_power(&target, action).await,
        }
    }))
    .buffer_unordered(concurrency)
    .collect()
    .await
}

pub async fn handle_group(
    pattern: &str,
    action: GroupAction,
    dry_run: bool,
    concurrency: usize,
) -> Result<()> {
    let matches = hosts::lookup_many(pattern)?;
    if matches.is_empty() {
        anyhow::bail!(
            "No aliases matched pattern \"{pattern}\".\n\
             Check current aliases with: denki aliases"
        );
    }

    if dry_run {
        crate::output::record(
            serde_json::json!({"dry_run":true,"action":action.as_verb(),"targets":matches.iter().map(|(alias,entry)| serde_json::json!({"alias":alias,"ip":entry.ip,"protocol":entry.protocol,"group_approved":entry.protocol != hosts::Protocol::Kasa || entry.group_approved})).collect::<Vec<_>>()}),
        );
        crate::output::println!(
            "Would turn {} {} alias(es) matching \"{}\":",
            action.as_verb(),
            matches.len(),
            terminal_text(pattern)
        );
        for (alias, entry) in matches {
            crate::output::println!(
                "  {} [{}; {}]",
                terminal_text(&alias).bold(),
                terminal_text(&entry.ip),
                entry.protocol
            );
        }
        return Ok(());
    }

    hosts::require_group_approval(&matches)?;

    crate::output::println!(
        "Turning {} {} alias(es) matching \"{}\" (up to {} at once):",
        action.as_verb(),
        matches.len(),
        terminal_text(pattern),
        concurrency
    );

    let results = execute_group(matches, action, concurrency, &LiveDeviceTransport).await;
    let total = results.len();
    let records: Vec<_> = results.iter().map(|r| match &r.result {
        Ok(on) => serde_json::json!({"alias":r.alias,"status":"ok","power_on":on,"error":null}),
        Err(error) => serde_json::json!({"alias":r.alias,"status":"error","power_on":null,"error":crate::output::failure(error)})
    }).collect();
    let failed = records.iter().filter(|r| r["status"] == "error").count();
    crate::output::record(
        serde_json::json!({"dry_run":false,"action":action.as_verb(),"results":records,"succeeded":total-failed,"failed":failed}),
    );
    let mut failures = Vec::new();
    for outcome in results {
        match outcome.result {
            Ok(on) => crate::output::println!(
                "  {} {} -> {}",
                "OK".green().bold(),
                terminal_text(&outcome.alias),
                if on { "on" } else { "off" }
            ),
            Err(error) => {
                eprintln!(
                    "  {} {}: {}",
                    "FAILED".red().bold(),
                    terminal_text(&outcome.alias),
                    terminal_text(&format!("{error:#}"))
                );
                failures.push(outcome.alias);
            }
        }
    }

    let succeeded = total - failures.len();
    crate::output::println!(
        "Completed: {} succeeded, {} failed.",
        succeeded.to_string().green(),
        failures.len().to_string().red()
    );
    if failures.is_empty() {
        Ok(())
    } else {
        Err(crate::error::error(
            "partial_failure",
            format!(
                "Group action failed for {} alias(es): {}",
                failures.len(),
                failures.join(", ")
            ),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_support;
    use std::sync::Mutex;

    struct FakeTransport {
        fail_alias: Option<&'static str>,
        calls: Mutex<Vec<String>>,
    }

    impl DeviceTransport for FakeTransport {
        async fn apply_power(&self, target: &Resolved, action: GroupAction) -> Result<bool> {
            let alias = target.saved_name.clone().unwrap_or_default();
            self.calls.lock().unwrap().push(alias.clone());
            if self.fail_alias == Some(alias.as_str()) {
                anyhow::bail!("simulated transport failure");
            }
            Ok(!matches!(action, GroupAction::Off))
        }
    }

    fn group_target(alias: &str, ip: &str) -> (String, hosts::HostEntry) {
        group_target_with_protocol(alias, ip, hosts::Protocol::Kasa)
    }

    fn group_target_with_protocol(
        alias: &str,
        ip: &str,
        protocol: hosts::Protocol,
    ) -> (String, hosts::HostEntry) {
        (
            alias.to_string(),
            hosts::HostEntry {
                ip: ip.to_string(),
                protocol,
                device_id: None,
                group_approved: false,
            },
        )
    }

    #[test]
    fn toggle_target_bulb_on_returns_false() {
        let json = serde_json::json!({"system": {"get_sysinfo": {"light_state": {"on_off": 1}}}});
        assert!(!toggle_target(&DeviceKind::Bulb, &json).unwrap());
    }

    #[test]
    fn toggle_target_bulb_off_returns_true() {
        let json = serde_json::json!({"system": {"get_sysinfo": {"light_state": {"on_off": 0}}}});
        assert!(toggle_target(&DeviceKind::Bulb, &json).unwrap());
    }

    #[test]
    fn toggle_target_plug_on_returns_false() {
        let json = serde_json::json!({"system": {"get_sysinfo": {"relay_state": 1}}});
        assert!(!toggle_target(&DeviceKind::Plug, &json).unwrap());
    }

    #[test]
    fn toggle_target_plug_off_returns_true() {
        let json = serde_json::json!({"system": {"get_sysinfo": {"relay_state": 0}}});
        assert!(toggle_target(&DeviceKind::Plug, &json).unwrap());
    }

    #[test]
    fn toggle_target_dimmer_on_returns_false() {
        let json = serde_json::json!({"system": {"get_sysinfo": {"relay_state": 1}}});
        assert!(!toggle_target(&DeviceKind::Dimmer, &json).unwrap());
    }

    #[test]
    fn toggle_target_dimmer_off_returns_true() {
        let json = serde_json::json!({"system": {"get_sysinfo": {"relay_state": 0}}});
        assert!(toggle_target(&DeviceKind::Dimmer, &json).unwrap());
    }

    #[test]
    fn toggle_target_strip_any_on_returns_false() {
        let json = test_support::strip_sysinfo(
            "Strip",
            "HS300(US)",
            "TIM",
            vec![
                test_support::strip_child("A1", 1, "Outlet 1", 0),
                test_support::strip_child("A2", 0, "Outlet 2", 0),
            ],
        );
        assert!(!toggle_target(&DeviceKind::Strip, &json).unwrap());
    }

    #[test]
    fn toggle_target_strip_all_off_returns_true() {
        let json = test_support::strip_sysinfo(
            "Strip",
            "HS300(US)",
            "TIM",
            vec![
                test_support::strip_child("A1", 0, "Outlet 1", 0),
                test_support::strip_child("A2", 0, "Outlet 2", 0),
            ],
        );
        assert!(toggle_target(&DeviceKind::Strip, &json).unwrap());
    }

    #[tokio::test]
    async fn group_executor_continues_after_transport_failure() {
        let transport = FakeTransport {
            fail_alias: Some("broken lamp"),
            calls: Mutex::new(Vec::new()),
        };
        let results = execute_group(
            vec![
                group_target("first lamp", "192.0.2.1"),
                group_target("broken lamp", "192.0.2.2"),
                group_target("last lamp", "192.0.2.3"),
            ],
            GroupAction::Off,
            2,
            &transport,
        )
        .await;

        assert_eq!(results.len(), 3);
        assert_eq!(results.iter().filter(|r| r.result.is_ok()).count(), 2);
        assert_eq!(results.iter().filter(|r| r.result.is_err()).count(), 1);
        let calls = transport.calls.lock().unwrap();
        assert!(calls.contains(&"last lamp".to_string()));
    }

    #[tokio::test]
    async fn group_executor_reports_resulting_power_state() {
        let transport = FakeTransport {
            fail_alias: None,
            calls: Mutex::new(Vec::new()),
        };
        let results = execute_group(
            vec![group_target("office", "192.0.2.4")],
            GroupAction::On,
            1,
            &transport,
        )
        .await;

        assert_eq!(results[0].result.as_ref().unwrap(), &true);
    }

    struct FailureMatrixTransport;

    impl DeviceTransport for FailureMatrixTransport {
        async fn apply_power(&self, target: &Resolved, _: GroupAction) -> Result<bool> {
            match target.saved_name.as_deref() {
                Some("timeout") => anyhow::bail!("simulated timeout"),
                Some("malformed") => anyhow::bail!("simulated malformed response"),
                _ => Ok(target.protocol == hosts::Protocol::Klap),
            }
        }
    }

    #[tokio::test]
    async fn group_executor_handles_mixed_protocols_and_distinct_failures() {
        let results = execute_group(
            vec![
                group_target("kasa", "192.0.2.10"),
                group_target_with_protocol("tapo", "192.0.2.11", hosts::Protocol::Klap),
                group_target("timeout", "192.0.2.12"),
                group_target("malformed", "192.0.2.13"),
            ],
            GroupAction::Toggle,
            3,
            &FailureMatrixTransport,
        )
        .await;

        assert_eq!(results.len(), 4);
        assert_eq!(
            results
                .iter()
                .filter(|result| result.result.is_ok())
                .count(),
            2
        );
        let errors = results
            .iter()
            .filter_map(|result| result.result.as_ref().err())
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join(" ");
        assert!(errors.contains("timeout"));
        assert!(errors.contains("malformed"));
        assert_eq!(
            results
                .iter()
                .find(|result| result.alias == "tapo")
                .unwrap()
                .result
                .as_ref()
                .unwrap(),
            &true
        );
    }
}