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
use crate::commands::GroupAction;
use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
#[derive(Parser)]
#[command(
name = "denki",
about = "Control TP-Link Kasa and Tapo devices from the terminal",
version,
after_help = "Start here:\n denki scan\n denki aliases\n denki info \"desk plug\"\n denki doctor \"desk plug\"\n\nTapo P125: add an alias with --tapo, then run denki login EMAIL.\nUse --json for one result; energy watch streams JSONL with --json.\nUse energy watch --format csv for spreadsheets; do not combine it with --json.\nOnly commands supported by the selected device are available.\nGuide: https://kahwee.github.io/denki/"
)]
pub struct Cli {
/// Emit versioned JSON (energy watch streams JSONL); progress goes to stderr
#[arg(long, global = true)]
pub json: bool,
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand, Debug)]
pub enum Command {
/// Scan Kasa devices and saved Tapo aliases; save new names without replacing existing aliases
Scan {
#[arg(short, long, default_value = "5")]
timeout: u64,
/// Probe a Tapo IP using its saved protocol, or auto TPAP/KLAP for a new IP
#[arg(long, value_name = "IP")]
tapo_target: Vec<std::net::IpAddr>,
},
/// Show detailed info about a device
Info {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
},
/// Check reachability, protocol, parsing, and advertised capabilities
Doctor {
/// Device name, saved alias, or IP address
#[arg(value_name = "DEVICE")]
host: String,
},
/// Turn a device on
On {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
/// Outlet number, 1-based (strips only)
#[arg(value_parser = clap::value_parser!(u8).range(1..))]
outlet: Option<u8>,
},
/// Turn a device off
Off {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
/// Outlet number, 1-based (strips only)
#[arg(value_parser = clap::value_parser!(u8).range(1..))]
outlet: Option<u8>,
},
/// Toggle a device on/off
Toggle {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
/// Outlet number, 1-based (strips only)
#[arg(value_parser = clap::value_parser!(u8).range(1..))]
outlet: Option<u8>,
},
/// Apply on/off/toggle to all matching aliases
Group {
#[arg(value_enum)]
action: GroupAction,
/// Alias pattern used for group matching (normalized, case- and punctuation-insensitive)
#[arg(value_name = "PATTERN")]
pattern: String,
/// Show matched aliases without changing device state
#[arg(long)]
dry_run: bool,
/// Maximum number of devices controlled at once
#[arg(long, default_value_t = 4, value_parser = clap::value_parser!(u8).range(1..=32))]
concurrency: u8,
},
/// Set brightness 0-100 (bulbs and HS220 dimmers)
Dim {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
#[arg(value_parser = clap::value_parser!(u8).range(0..=100))]
level: u8,
},
/// Set color temperature in Kelvin 2500-9000 (bulbs only)
ColorTemp {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
#[arg(value_parser = clap::value_parser!(u16).range(2500..=9000))]
kelvin: u16,
},
/// Set HSV color (bulbs only)
Color {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
/// Hue 0-360°
#[arg(long, short = 'H', value_parser = clap::value_parser!(u16).range(0..=360))]
hue: u16,
/// Saturation 0-100%
#[arg(long, short = 's', value_parser = clap::value_parser!(u8).range(0..=100))]
saturation: u8,
/// Value (brightness) 0-100%
#[arg(long, short = 'v', value_parser = clap::value_parser!(u8).range(0..=100))]
value: u8,
},
/// Cycle a color light through the rainbow until interrupted with Ctrl-C
Rainbow {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
/// Delay between hue steps, in milliseconds
#[arg(long, default_value_t = 1_000, value_parser = clap::value_parser!(u64).range(20..=60_000))]
step_delay_ms: u64,
},
/// Show real-time energy usage (bulbs, light strips, and ENE-capable plugs/strips)
#[command(
subcommand_precedence_over_arg = true,
args_conflicts_with_subcommands = true
)]
Energy {
#[command(subcommand)]
command: Option<EnergyCommand>,
/// Device name, saved alias, or IP; use `energy watch` for streaming
#[arg(value_name = "DEVICE")]
host: Option<String>,
/// Outlet number, 1-based (strips only)
#[arg(value_parser = clap::value_parser!(u8).range(1..))]
outlet: Option<u8>,
},
/// Show daily energy usage for a month (YYYY-MM) on bulbs, light strips, and ENE-capable plugs/strips
EnergyDaily {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
/// Month in YYYY-MM format (defaults to current month)
#[arg(value_name = "YYYY-MM")]
month: Option<String>,
/// Outlet number, 1-based (strips only)
#[arg(long, short = 'o', value_parser = clap::value_parser!(u8).range(1..))]
outlet: Option<u8>,
},
/// Show monthly energy usage for a year on bulbs, light strips, and ENE-capable plugs/strips
EnergyMonthly {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
#[arg(value_name = "YYYY")]
year: Option<u16>,
/// Outlet number, 1-based (strips only)
#[arg(long, short = 'o', value_parser = clap::value_parser!(u8).range(1..))]
outlet: Option<u8>,
},
/// Show bulb hardware specs — lumens, wattage, CRI (bulbs only)
Specs {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
},
/// Show saved light presets (bulbs only)
Presets {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
},
/// List built-in light strip effects and the current active effect (light strips only)
Effects {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
},
/// Activate a built-in light strip effect by name (light strips only)
Effect {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
/// Effect name, e.g. Aurora, Rainbow, Off
name: String,
},
/// Show scheduled rules (plugs, dimmers, and power strips)
Schedules {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
},
/// Control the LED indicator (plugs, dimmers, and power strips)
Led {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
#[arg(value_enum)]
state: LedAction,
},
/// Show device clock (plugs, dimmers, and power strips)
Clock {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
},
/// Rename a Kasa device
Rename {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
name: String,
},
/// Reboot a Kasa device
Restart {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
},
/// List all outlets on a power strip, showing 1-based outlet numbers, names, and state (strips only)
Outlets {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
},
/// Rename one outlet on a power strip (1-based outlet number)
OutletRename {
/// Device name from scan output, a saved alias, or an IP address
#[arg(value_name = "DEVICE")]
host: String,
/// Outlet number, 1-based
#[arg(value_parser = clap::value_parser!(u8).range(1..))]
outlet: u8,
/// New name for the outlet
name: String,
},
/// Save a friendly name for a device IP (e.g. `denki alias "floor lamp" 192.0.2.42`)
Alias {
/// Friendly name to save
name: String,
/// IP address of the device
ip: String,
/// Mark as a Tapo device (uses KLAP protocol on port 80)
#[arg(long)]
klap: bool,
/// Auto-negotiate TPAP/KLAP with the upstream plug client (P125 power support)
#[arg(long, visible_alias = "tpap", conflicts_with = "klap")]
tapo: bool,
},
/// Remove a saved device alias
Unalias {
/// Friendly name to remove
name: String,
},
/// List all saved device aliases
Aliases,
/// Print shell completions to stdout (pipe to your shell's completions dir)
#[command(hide = true)]
Completions {
#[arg(value_enum)]
shell: Shell,
},
/// Save Tapo account credentials to avoid setting env vars each session
Login {
/// Tapo account email address
email: String,
/// Tapo account password (omit to be prompted; never pass on command line in scripts)
password: Option<String>,
},
}
#[derive(ValueEnum, Clone, Debug)]
pub enum LedAction {
On,
Off,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tapo_alias_options_are_exclusive_and_support_tpap_spelling() {
for flag in ["--tapo", "--tpap"] {
let cli =
Cli::try_parse_from(["denki", "alias", "Test Plug", "192.0.2.1", flag]).unwrap();
assert!(matches!(
cli.command,
Command::Alias {
tapo: true,
klap: false,
..
}
));
assert!(
Cli::try_parse_from(["denki", "alias", "Test Plug", "192.0.2.1", flag, "--klap"])
.is_err()
);
}
let cli =
Cli::try_parse_from(["denki", "alias", "Test Plug", "192.0.2.1", "--klap"]).unwrap();
assert!(matches!(
cli.command,
Command::Alias {
tapo: false,
klap: true,
..
}
));
}
#[test]
fn group_safety_options_parse() {
let cli = Cli::try_parse_from([
"denki",
"group",
"off",
"office",
"--dry-run",
"--concurrency",
"8",
])
.unwrap();
match cli.command {
Command::Group {
dry_run,
concurrency,
..
} => {
assert!(dry_run);
assert_eq!(concurrency, 8);
}
_ => panic!("expected group command"),
}
}
#[test]
fn doctor_json_option_parses() {
let cli = Cli::try_parse_from(["denki", "doctor", "office", "--json"]).unwrap();
assert!(cli.json);
}
#[test]
fn info_json_option_parses() {
let cli = Cli::try_parse_from(["denki", "info", "office", "--json"]).unwrap();
assert!(cli.json);
}
}
impl Command {
pub fn name(&self) -> &'static str {
match self {
Self::Scan { .. } => "scan",
Self::Info { .. } => "info",
Self::Doctor { .. } => "doctor",
Self::On { .. } => "on",
Self::Off { .. } => "off",
Self::Toggle { .. } => "toggle",
Self::Group { .. } => "group",
Self::Dim { .. } => "dim",
Self::ColorTemp { .. } => "color-temp",
Self::Color { .. } => "color",
Self::Rainbow { .. } => "rainbow",
Self::Energy { .. } => "energy",
Self::EnergyDaily { .. } => "energy-daily",
Self::EnergyMonthly { .. } => "energy-monthly",
Self::Specs { .. } => "specs",
Self::Presets { .. } => "presets",
Self::Effects { .. } => "effects",
Self::Effect { .. } => "effect",
Self::Schedules { .. } => "schedules",
Self::Led { .. } => "led",
Self::Clock { .. } => "clock",
Self::Rename { .. } => "rename",
Self::Restart { .. } => "restart",
Self::Outlets { .. } => "outlets",
Self::OutletRename { .. } => "outlet-rename",
Self::Alias { .. } => "alias",
Self::Unalias { .. } => "unalias",
Self::Aliases => "aliases",
Self::Completions { .. } => "completions",
Self::Login { .. } => "login",
}
}
}
#[derive(Subcommand, Debug)]
pub enum EnergyCommand {
/// Poll read-only measurements; export with --format jsonl or csv
Watch {
#[arg(value_name = "DEVICE")]
host: String,
#[arg(long, short = 'o', value_parser = clap::value_parser!(u8).range(1..))]
outlet: Option<u8>,
/// Delay in seconds after each completed sample (no overlapping requests)
#[arg(long, default_value_t = 5, value_parser = clap::value_parser!(u64).range(1..=86400))]
interval: u64,
/// Stop after this many samples; omit to run until Ctrl-C
#[arg(long, value_parser = clap::value_parser!(u64).range(1..))]
count: Option<u64>,
#[arg(long, value_enum, default_value_t = WatchFormat::Table)]
format: WatchFormat,
},
}
#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq)]
pub enum WatchFormat {
Table,
Jsonl,
Csv,
}