-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemsctl.py
More file actions
4266 lines (3674 loc) · 142 KB
/
Copy pathemsctl.py
File metadata and controls
4266 lines (3674 loc) · 142 KB
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
#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Safe runtime-state editor for ems-solarflow-api-control."""
import argparse
import subprocess
import getpass
import json
import math
import os
import re
import sys
from datetime import datetime
from dashboard import auth as dashboard_auth
from ems.paths import (
BASE_DIR,
LAYOUT_LEGACY_ROOT_ONLY,
detect_config_layout_state,
resolve_config_path as resolve_ems_config_path,
resolve_runtime_path,
resolve_dashboard_auth_path,
standard_config_path,
)
from ems.diagnostics import (
diagnose_json_file,
diagnose_text,
diagnose_write_support_bundle,
run_control_diagnosis as run_control_diagnosis,
run_control_quality_diagnosis as run_control_quality_diagnosis,
run_deep_diagnosis as run_deep_diagnosis,
run_diagnosis,
run_hardware_diagnosis as run_hardware_diagnosis,
run_install_diagnosis as run_install_diagnosis,
)
from ems import backup as backup_mod
from ems import config as config_mod
from ems import config_init as config_init_mod
from ems.cli_privilege import PrivilegeDropError, maybe_drop_privileges
DEFAULT_RUNTIME_STATE_PATH = os.path.join(BASE_DIR, "runtime-state.json")
OFFGRID_SOCKET_MODES = ("off", "eco", "standard")
TOP_LEVEL_COMMANDS = (
"status",
"system",
"device",
"ha",
"ha-control",
"winter",
"dashboard",
"influx",
"stack",
"diagnose",
"backup",
"config",
"interactive",
"menu",
"examples",
"completion",
"help",
)
BACKUP_ACTIONS = (
"create",
"restore",
"inspect",
"diff",
)
SYSTEM_ACTIONS = (
"enable",
"disable",
"max-power",
"loop-interval",
"min-output-limit",
)
DEVICE_ACTIONS = (
"enable",
"disable",
"max-power",
"offgrid",
"pv-priority-factor",
"ac-mode",
"ac-charge-power",
)
DEVICE_AC_MODE_VALUES = ("output", "input")
DEVICE_AC_MODE_RUNTIME_ROLES = {
"output": "ac_output",
"input": "ac_input",
}
DEVICE_AC_MODE_ROLE_ALIASES = {
"normal_output": "ac_output",
"ac_output": "ac_output",
"ac_input_charge": "ac_input",
"reserved": "ac_input",
"ac_input": "ac_input",
}
DASHBOARD_ACTIONS = (
"set-password",
"change-password",
"disable-auth",
"auth-status",
)
COMPLETION_SHELLS = ("bash", "zsh")
TOP_LEVEL_EPILOG = """\
Command overview:
status Show runtime-state.json
system <action> [value] Enable/disable EMS and tune global limits
device <name> <action> [value] Enable/disable devices and tune device values
ha enable|disable Toggle Home Assistant publishing
ha-control enable|disable Toggle Home Assistant helper control
winter enable|disable|status Toggle or inspect winter mode
dashboard <command> Manage dashboard write-mode authentication
diagnose [--json] Run read-only local diagnostics
backup [create|restore|...] Create/restore manual config backups
interactive Open a menu for common runtime edits
examples Print a longer command cookbook
completion bash|zsh Generate optional shell completion
Common examples:
python3 emsctl.py interactive
python3 emsctl.py status
python3 emsctl.py system disable
python3 emsctl.py system max-power 1200
python3 emsctl.py device WR1 max-power 600
python3 emsctl.py device WR1 offgrid eco
python3 emsctl.py device WR1 ac-mode output
python3 emsctl.py device WR1 ac-charge-power 200
python3 emsctl.py winter enable
python3 emsctl.py dashboard auth-status
python3 emsctl.py diagnose
python3 emsctl.py diagnose --deep
python3 emsctl.py diagnose --hardware
python3 emsctl.py diagnose --control
python3 emsctl.py diagnose --control-quality --sample-seconds 60
python3 emsctl.py diagnose --support-bundle
Tip:
Run `python3 emsctl.py` for a short start screen.
Use `python3 emsctl.py examples` for a longer command cookbook.
Use `python3 emsctl.py completion bash` to generate shell completion.
"""
QUICK_HELP_TEXT = """\
EMS Control CLI
Usage:
python3 emsctl.py <command> [options]
Start here:
python3 emsctl.py status
python3 emsctl.py diagnose
python3 emsctl.py examples
Common commands:
python3 emsctl.py status
python3 emsctl.py interactive
python3 emsctl.py examples
Diagnostics:
python3 emsctl.py diagnose
Run basic installation and runtime checks.
python3 emsctl.py diagnose --deep
Run extended runtime, database, log, and dashboard checks.
python3 emsctl.py diagnose --hardware
Run read-only hardware connectivity checks.
python3 emsctl.py diagnose --control
Explain current EMS control decisions.
python3 emsctl.py diagnose --control-quality --sample-seconds 60
Measure export/import quality, PV usage, and SOC balancing.
python3 emsctl.py diagnose --support-bundle
Create a redacted support bundle for GitHub issues.
Use --json for machine-readable output.
Use --output <path> together with --support-bundle.
Runtime control:
python3 emsctl.py system disable
python3 emsctl.py system max-power 1200
python3 emsctl.py device WR1 max-power 600
python3 emsctl.py device WR1 ac-mode input
python3 emsctl.py device WR1 ac-charge-power 200
python3 emsctl.py device WR1 offgrid eco
Dashboard:
python3 emsctl.py dashboard auth-status
python3 emsctl.py dashboard set-password
Backup / restore (config may contain secrets; database/InfluxDB hold history):
python3 emsctl.py backup
python3 emsctl.py backup create
python3 emsctl.py backup create --type databases
python3 emsctl.py backup restore data/backups/ems-config-manual-....tar.gz
More help:
python3 emsctl.py --help
python3 emsctl.py examples
python3 emsctl.py diagnose --help
"""
EXAMPLES_TEXT = """\
EMS runtime control CLI examples
Interactive mode:
python3 emsctl.py interactive
Status:
python3 emsctl.py status
System runtime control:
python3 emsctl.py system enable
python3 emsctl.py system disable
python3 emsctl.py system max-power 1200
python3 emsctl.py system min-output-limit 35
python3 emsctl.py system loop-interval 5
Device runtime control:
python3 emsctl.py device WR1 enable
python3 emsctl.py device WR1 disable
python3 emsctl.py device WR1 max-power 600
python3 emsctl.py device WR1 ac-mode output
python3 emsctl.py device WR1 ac-mode input
python3 emsctl.py device WR1 ac-charge-power 200
python3 emsctl.py device WR1 ac-charge-power 0
python3 emsctl.py device WR1 pv-priority-factor 1.2
python3 emsctl.py device WR1 offgrid off
python3 emsctl.py device WR1 offgrid eco
python3 emsctl.py device WR1 offgrid standard
HA publishing / helper control:
python3 emsctl.py ha enable
python3 emsctl.py ha disable
python3 emsctl.py ha-control enable
python3 emsctl.py ha-control disable
Winter mode:
python3 emsctl.py winter status
python3 emsctl.py winter enable
python3 emsctl.py winter disable
Dashboard authentication:
python3 emsctl.py dashboard auth-status
python3 emsctl.py dashboard set-password
python3 emsctl.py dashboard change-password
python3 emsctl.py dashboard disable-auth
Diagnostics:
python3 emsctl.py diagnose
python3 emsctl.py diagnose --deep
python3 emsctl.py diagnose --hardware
python3 emsctl.py diagnose --control
python3 emsctl.py diagnose --control --sample-seconds 30
python3 emsctl.py diagnose --control-quality --sample-seconds 60
python3 emsctl.py diagnose --quality --json
python3 emsctl.py diagnose --json
python3 emsctl.py diagnose --support-bundle
python3 emsctl.py diagnose --support-bundle --output /tmp/ems-support.zip
Backup / restore (config may contain secrets; database/InfluxDB hold history):
python3 emsctl.py backup
python3 emsctl.py backup create
python3 emsctl.py backup create --type databases
python3 emsctl.py backup create --compression-level 3
python3 emsctl.py backup inspect data/backups/ems-config-manual-....tar.gz
python3 emsctl.py backup restore data/backups/ems-config-manual-....tar.gz
python3 emsctl.py backup restore data/backups/ems-databases-manual-....tar.gz
python3 emsctl.py backup restore data/backups/...tar.gz --on-conflict keep --no-rollback
python3 emsctl.py backup diff data/backups/...tar.gz --file config.json
Docker diagnostics:
docker compose exec ems python3 emsctl.py diagnose
docker compose exec ems python3 emsctl.py diagnose --control
docker compose exec ems python3 emsctl.py diagnose --control-quality --sample-seconds 60
docker compose exec ems python3 emsctl.py diagnose --support-bundle
Explicit config/runtime paths:
python3 emsctl.py --config /etc/ems/config.json status
python3 emsctl.py --runtime-state /var/lib/ems/runtime-state.json status
python3 emsctl.py --config config.json --runtime-state runtime-state.json device WR1 max-power 600
python3 emsctl.py --dashboard-auth config/dashboard-auth.json dashboard auth-status
Shell completion:
source <(python3 emsctl.py completion bash)
source <(python3 emsctl.py completion zsh)
"""
class EMSHelpFormatter(argparse.RawDescriptionHelpFormatter):
pass
def fail(message, code=1):
print(f"ERROR: {message}", file=sys.stderr)
return code
def build_parser():
parser = argparse.ArgumentParser(
description="EMS runtime control CLI",
epilog=TOP_LEVEL_EPILOG,
formatter_class=EMSHelpFormatter,
)
parser.add_argument(
"--config",
help=(
"Path to config.json. Default discovery: EMS_CONFIG_FILE, "
"config/config.json, then legacy config.json."
)
)
parser.add_argument(
"--runtime-state",
help="Path to runtime-state.json. Overrides config runtime_state_path."
)
parser.add_argument(
"--dashboard-auth",
help="Path to dashboard-auth.json. Overrides dashboard auth_file config."
)
subparsers = parser.add_subparsers(dest="command", required=True)
subparsers.add_parser(
"status",
help="Print current runtime state.",
description="Print the current runtime-state.json payload.",
epilog="""\
Examples:
python3 emsctl.py status
python3 emsctl.py --config config.json status
""",
formatter_class=EMSHelpFormatter,
)
system = subparsers.add_parser(
"system",
help="Edit system runtime state.",
description="Edit global EMS runtime values.",
epilog="""\
Actions:
enable Enable EMS runtime control
disable Disable EMS runtime control
max-power VALUE Set max_total_power in watts
loop-interval SEC Set loop_interval in seconds
min-output-limit W Set min_output_limit in watts
Examples:
python3 emsctl.py system disable
python3 emsctl.py system max-power 1200
python3 emsctl.py system loop-interval 5
""",
formatter_class=EMSHelpFormatter,
)
system.add_argument(
"action",
choices=SYSTEM_ACTIONS,
metavar="action",
help="One of: " + ", ".join(SYSTEM_ACTIONS),
)
system.add_argument("value", nargs="?", help="Required for numeric actions.")
device = subparsers.add_parser(
"device",
help="Edit device runtime state.",
description="Edit per-device runtime values. Device names come from config.json.",
epilog="""\
Actions:
enable Enable a device
disable Disable a device
max-power VALUE Set device max_power in watts
offgrid off|eco|standard Set offgrid socket mode
pv-priority-factor VALUE Set PV-first allocation weight
ac-mode [output|input] Set or show runtime AC mode role
ac-charge-power WATTS Set runtime AC charge inputLimit in watts
Examples:
python3 emsctl.py device WR1 disable
python3 emsctl.py device WR1 max-power 600
python3 emsctl.py device WR1 ac-mode output
python3 emsctl.py device WR1 ac-mode input
python3 emsctl.py device WR1 ac-charge-power 200
python3 emsctl.py device WR1 offgrid eco
python3 emsctl.py device WR1 pv-priority-factor 1.2
""",
formatter_class=EMSHelpFormatter,
)
device.add_argument("name", help="Configured device name, for example WR1.")
device.add_argument(
"action",
choices=DEVICE_ACTIONS,
metavar="action",
help="One of: " + ", ".join(DEVICE_ACTIONS),
)
device.add_argument("value", nargs="?", help="Required for max-power, offgrid, pv-priority-factor, ac-mode writes, and ac-charge-power.")
ha = subparsers.add_parser(
"ha",
help="Edit HA runtime publishing state.",
description="Enable or disable runtime Home Assistant publishing.",
epilog="""\
Examples:
python3 emsctl.py ha enable
python3 emsctl.py ha disable
""",
formatter_class=EMSHelpFormatter,
)
ha.add_argument("action", choices=["enable", "disable"], help="One of: enable, disable.")
ha.add_argument("value", nargs="?", help=argparse.SUPPRESS)
ha_control = subparsers.add_parser(
"ha-control",
help="Edit HA runtime helper-control state.",
description="Enable or disable runtime Home Assistant helper control.",
epilog="""\
Examples:
python3 emsctl.py ha-control enable
python3 emsctl.py ha-control disable
""",
formatter_class=EMSHelpFormatter,
)
ha_control.add_argument("action", choices=["enable", "disable"], help="One of: enable, disable.")
ha_control.add_argument("value", nargs="?", help=argparse.SUPPRESS)
winter = subparsers.add_parser(
"winter",
help="Edit winter runtime state.",
description="Enable, disable, or inspect winter runtime mode.",
epilog="""\
Examples:
python3 emsctl.py winter status
python3 emsctl.py winter enable
python3 emsctl.py winter disable
""",
formatter_class=EMSHelpFormatter,
)
winter.add_argument("action", choices=["enable", "disable", "status"], help="One of: enable, disable, status.")
winter.add_argument("value", nargs="?", help=argparse.SUPPRESS)
dashboard = subparsers.add_parser(
"dashboard",
help="Manage dashboard authentication.",
description="Manage local dashboard write-mode authentication.",
epilog="""\
Examples:
python3 emsctl.py dashboard auth-status
python3 emsctl.py dashboard set-password
python3 emsctl.py dashboard change-password
python3 emsctl.py dashboard disable-auth
Password prompts do not echo input. Hidden automation flags are intentionally
omitted from normal help output.
""",
formatter_class=EMSHelpFormatter,
)
dashboard_subparsers = dashboard.add_subparsers(
dest="dashboard_command",
required=True
)
set_password = dashboard_subparsers.add_parser(
"set-password",
help="Set dashboard admin password."
)
set_password.add_argument("--password", help=argparse.SUPPRESS)
set_password.add_argument("--confirm-password", help=argparse.SUPPRESS)
change_password = dashboard_subparsers.add_parser(
"change-password",
help="Change dashboard admin password."
)
change_password.add_argument("--current-password", help=argparse.SUPPRESS)
change_password.add_argument("--new-password", help=argparse.SUPPRESS)
change_password.add_argument("--confirm-password", help=argparse.SUPPRESS)
dashboard_subparsers.add_parser(
"disable-auth",
help="Disable dashboard write-mode authentication."
)
dashboard_subparsers.add_parser(
"auth-status",
help="Show dashboard authentication status."
)
diagnose = subparsers.add_parser(
"diagnose",
help="Run read-only local diagnostics.",
description=(
"Run read-only diagnostics for config, runtime-state, data paths, "
"and container mounts without contacting external services."
),
epilog="""\
Examples:
python3 emsctl.py diagnose
python3 emsctl.py diagnose --json
python3 emsctl.py diagnose --deep
python3 emsctl.py diagnose --hardware
python3 emsctl.py diagnose --control
python3 emsctl.py diagnose --control --sample-seconds 30
python3 emsctl.py diagnose --control-quality --sample-seconds 60
python3 emsctl.py diagnose --quality --json
python3 emsctl.py diagnose --support-bundle
python3 emsctl.py diagnose --support-bundle --output /tmp/ems-support.zip
""",
formatter_class=EMSHelpFormatter,
)
diagnose.add_argument(
"--json",
action="store_true",
help="Print machine-readable diagnostic output.",
)
diagnose.add_argument(
"--deep",
action="store_true",
help="Run deeper local read-only operational checks.",
)
diagnose.add_argument(
"--hardware",
action="store_true",
help="Run optional short-timeout read-only hardware/network probes.",
)
diagnose.add_argument(
"--support-bundle",
action="store_true",
help="Create a redacted ZIP support bundle.",
)
diagnose.add_argument(
"--control",
action="store_true",
help="Explain current EMS control/regulation behavior from local state.",
)
diagnose.add_argument(
"--control-quality",
action="store_true",
help="Evaluate zero-export, PV usage, and SOC balancing quality.",
)
diagnose.add_argument(
"--quality",
action="store_true",
help="Alias for --control-quality.",
)
diagnose.add_argument(
"--sample-seconds",
type=int,
default=0,
help="Collect local runtime-state meter samples for control diagnostics.",
)
diagnose.add_argument(
"--output",
help="ZIP output path. Only valid with --support-bundle.",
)
grid_meter = subparsers.add_parser(
"grid-meter",
help="Probe the configured grid meter read endpoint.",
description=(
"Read-only grid-meter communication test. Repeatedly reads the "
"configured grid meter and reports success rate and read latency."
),
epilog="""\
Examples:
python3 emsctl.py grid-meter test
python3 emsctl.py grid-meter test --duration 120 --interval 1
""",
formatter_class=EMSHelpFormatter,
)
grid_meter_subparsers = grid_meter.add_subparsers(
dest="action",
required=True,
)
grid_meter_test = grid_meter_subparsers.add_parser(
"test",
help="Repeatedly read the grid meter and report latency/error stats.",
formatter_class=EMSHelpFormatter,
)
grid_meter_test.add_argument(
"--duration",
type=int,
default=30,
help="Total test duration in seconds (default: 30).",
)
grid_meter_test.add_argument(
"--interval",
type=float,
default=1.0,
help="Seconds between reads (default: 1.0).",
)
influx = subparsers.add_parser(
"influx",
help="Set up, inspect or reconcile the InfluxDB history backend.",
description=(
"Manage the optional InfluxDB 2.x history backend. Config is the "
"source of truth: 'init' sets up the bundled docker-compose "
"InfluxDB zero-config (generate local secrets, start it, sync "
"schema); 'sync' reconciles buckets, retention and downsampling "
"tasks to match config.json; 'status' reports the live buckets, "
"tasks and task health."
),
epilog="""\
Examples:
python3 emsctl.py influx init
python3 emsctl.py influx init --no-start
python3 emsctl.py influx init --no-sync --json
python3 emsctl.py influx status
python3 emsctl.py influx status --json
python3 emsctl.py influx sync
python3 emsctl.py influx sync --json
""",
formatter_class=EMSHelpFormatter,
)
influx.add_argument(
"action",
choices=("init", "status", "sync"),
help=(
"init: complete Analytics setup (bundled: secrets, start, wait, "
"sync; external: validate, check connectivity, sync). "
"status: read live schema. sync: reconcile schema to config."
),
)
influx.add_argument(
"--json",
action="store_true",
help="Print machine-readable output.",
)
influx.add_argument(
"--no-start",
action="store_true",
help="init: create/merge secrets only; do not start the container.",
)
influx.add_argument(
"--no-sync",
action="store_true",
help="init: skip the schema sync step after InfluxDB is ready.",
)
influx.add_argument(
"--force-disabled",
action="store_true",
help="init: proceed even when influxdb.enabled is false in config.",
)
stack = subparsers.add_parser(
"stack",
help="Start the EMS docker-compose stack (with bundled InfluxDB).",
description=(
"Beginner-friendly host-side helper that starts the EMS "
"docker-compose stack. With bundled InfluxDB enabled it also "
"creates local secrets, includes the InfluxDB compose files and "
"runs the schema sync, so Analytics works from one command. Runs "
"docker on the host; the EMS controller never manages Docker."
),
epilog="""\
Examples:
python3 emsctl.py stack up
python3 emsctl.py stack up --no-sync
python3 emsctl.py stack up --json
""",
formatter_class=EMSHelpFormatter,
)
stack.add_argument(
"stack_action",
choices=("up",),
help="up: create secrets if needed, start containers, sync schema.",
)
stack.add_argument(
"--json",
action="store_true",
help="Print machine-readable output.",
)
stack.add_argument(
"--no-sync",
action="store_true",
help="Skip the InfluxDB schema sync step.",
)
stack.add_argument(
"--dry-run",
action="store_true",
help="Print the docker compose command without running it.",
)
config_cmd = subparsers.add_parser(
"config",
help="Inspect, initialize, or upgrade config.json.",
description=(
"Inspect, initialize, or upgrade config.json. The init command "
"guides first setup; upgrade fills missing persisted keys from "
"config.template.json."
),
epilog="""\
Examples:
python3 emsctl.py config init
python3 emsctl.py config init --dry-run
python3 emsctl.py config upgrade --dry-run
python3 emsctl.py config upgrade
python3 emsctl.py config upgrade --yes --backup
python3 emsctl.py config upgrade --yes --no-backup
""",
formatter_class=EMSHelpFormatter,
)
config_subparsers = config_cmd.add_subparsers(
dest="config_command",
required=True,
)
config_init = config_subparsers.add_parser(
"init",
help="Run the guided first setup assistant.",
description=(
"Run a local guided setup assistant for beginner-relevant "
"config.json values. Existing edited configs preserve unknown "
"keys and unrelated values."
),
formatter_class=EMSHelpFormatter,
)
config_init.add_argument(
"--dry-run",
action="store_true",
help="Show the generated config preview without writing config.json.",
)
config_init.add_argument(
"--yes",
action="store_true",
help="Run without prompts, accepting existing values and defaults.",
)
config_init.add_argument(
"--analytics",
action="store_true",
help=(
"Enable bundled InfluxDB analytics (Docker-first: secrets in "
"config/influxdb.env)."
),
)
init_backup_policy = config_init.add_mutually_exclusive_group()
init_backup_policy.add_argument(
"--backup",
dest="backup",
action="store_true",
default=None,
help="Create a normal config backup before writing.",
)
init_backup_policy.add_argument(
"--no-backup",
dest="backup",
action="store_false",
help="Write without creating a backup.",
)
config_upgrade = config_subparsers.add_parser(
"upgrade",
help="Fill missing persisted config keys from config.template.json.",
description=(
"Plan or apply a template-based config.json upgrade. User values "
"and unknown keys are preserved."
),
formatter_class=EMSHelpFormatter,
)
config_upgrade.add_argument(
"--dry-run",
action="store_true",
help="Show the upgrade plan without writing config.json.",
)
config_upgrade.add_argument(
"--yes",
action="store_true",
help="Apply without interactive confirmation.",
)
backup_policy = config_upgrade.add_mutually_exclusive_group()
backup_policy.add_argument(
"--backup",
dest="backup",
action="store_true",
default=None,
help="Create a normal config backup before writing.",
)
backup_policy.add_argument(
"--no-backup",
dest="backup",
action="store_false",
help="Write without creating a backup.",
)
config_migrate_zendure = config_subparsers.add_parser(
"migrate-zendure-mqtt",
help="Plan or apply the safe Zendure MQTT control migration.",
description=(
"Resolve legacy Zendure MQTT control devices to a concrete, verified "
"hardware model, or disable control where the model is unknown or "
"conflicting. --dry-run shows the exact before/after diff without "
"writing; without --yes an apply prompts for confirmation."
),
formatter_class=EMSHelpFormatter,
)
config_migrate_zendure.add_argument(
"--dry-run",
action="store_true",
help="Show the migration plan without writing config.json.",
)
config_migrate_zendure.add_argument(
"--yes",
action="store_true",
help="Apply without interactive confirmation.",
)
config_migrate_zendure.add_argument(
"--json",
action="store_true",
help="Emit the migration plan as machine-readable JSON.",
)
migrate_backup_policy = config_migrate_zendure.add_mutually_exclusive_group()
migrate_backup_policy.add_argument(
"--backup",
dest="backup",
action="store_true",
default=None,
help="Create a normal config backup before writing.",
)
migrate_backup_policy.add_argument(
"--no-backup",
dest="backup",
action="store_false",
help="Write without creating a backup.",
)
backup = subparsers.add_parser(
"backup",
help="Create, inspect or restore manual config backups.",
description=(
"Manual config backup/restore. A config backup is a tar.gz archive "
"of config.json, the runtime state and configured dashboard "
"auth/cert files (plus the bundled InfluxDB secret when bundled "
"analytics is enabled). Config backups may contain secrets; "
"optional password protection encrypts the whole archive into a "
".tar.gz.enc file. 'create --type databases' backs up the local "
"SQLite databases (consistent SQLite snapshots). 'create --type "
"influxdb' backs up bundled InfluxDB data via the official influx "
"backup CLI (bundled mode only; external mode is rejected). Run "
"'backup' with no action for an interactive menu."
),
epilog="""\
Examples:
python3 emsctl.py backup
python3 emsctl.py backup create
python3 emsctl.py backup create --type databases
python3 emsctl.py backup create --type influxdb
python3 emsctl.py backup create --output-dir ./my-backups
python3 emsctl.py backup create --compression-level 3
python3 emsctl.py backup inspect data/backups/ems-config-manual-2026-06-18-221500.tar.gz
python3 emsctl.py backup restore data/backups/ems-config-manual-2026-06-18-221500.tar.gz
python3 emsctl.py backup restore data/backups/ems-databases-manual-2026-06-18-221500.tar.gz
python3 emsctl.py backup restore data/backups/ems-influxdb-manual-2026-06-18-221500.tar.gz
python3 emsctl.py backup restore data/backups/...tar.gz --on-conflict keep --no-rollback
python3 emsctl.py backup diff data/backups/...tar.gz --file config.json
""",
formatter_class=EMSHelpFormatter,
)
backup.add_argument(
"action",
nargs="?",
choices=BACKUP_ACTIONS,
help=(
"create: write a new config backup. restore: restore from a "
"backup. inspect: print a backup manifest. diff: compare a backed "
"up file to the current file. Omit for the interactive menu."
),
)
backup.add_argument(
"file",
nargs="?",
help="Backup archive path (required for restore/inspect/diff).",
)
backup.add_argument(
"--file",
dest="diff_file",
help="Config file to diff (used with 'backup diff').",
)
backup.add_argument(
"--type",
dest="type",
choices=("config", "databases", "influxdb"),
default="config",
help=(
"create: backup type — config (default), databases or influxdb "
"(bundled InfluxDB data via the official influx backup CLI)."
),
)
backup.add_argument(
"--compression-level",
type=int,
default=backup_mod.DEFAULT_COMPRESSION_LEVEL,
help="gzip compression level 0-9 (default: 3). Used with 'create'.",
)
backup.add_argument(
"--output-dir",
default=None,
help="create: directory for the created backup archive.",
)
backup.add_argument(
"--password",
action="store_true",
help="create: prompt for a password and encrypt the backup.",
)
backup.add_argument(
"--encryption",
dest="encryption",
choices=backup_mod.SUPPORTED_ENCRYPTION_ALGORITHMS,
default=backup_mod.DEFAULT_ENCRYPTION_ALGORITHM,
help=(
"create: streaming AEAD algorithm for encrypted backups "
f"(default: {backup_mod.DEFAULT_ENCRYPTION_ALGORITHM})."
),
)
backup.add_argument(
"--chunk-size",
dest="chunk_size",
default=None,
help="create: encryption chunk size, e.g. 4M or 1048576 (default: 4M).",
)
backup.add_argument(
"--kdf-iterations",
dest="kdf_iterations",
type=int,
default=None,
help="create: PBKDF2-SHA256 iteration count (default: 300000).",
)
backup.add_argument(
"--verify",
dest="verify",
action="store_true",
help=(
"create: after writing, open the archive and validate its manifest "
"and every member checksum; exit non-zero if verification fails."
),
)
backup.add_argument(
"--on-conflict",
choices=("abort", "keep", "replace"),
default=None,
help="restore: how to handle existing differing files (default: ask).",
)
backup.add_argument(
"--rollback",
dest="rollback",
action="store_true",
default=None,
help="restore: create a rollback backup before restoring.",
)
backup.add_argument(
"--no-rollback",
dest="rollback",
action="store_false",
help="restore: do not create a rollback backup before restoring.",
)
backup.add_argument(
"--dry-run",
action="store_true",
help="restore: show what would change without writing files.",
)
subparsers.add_parser(
"interactive",
help="Open a menu for common runtime edits.",
description="Open a dependency-free interactive menu for common runtime edits.",
epilog="""\
Examples:
python3 emsctl.py interactive
python3 emsctl.py --config config.json interactive
""",
formatter_class=EMSHelpFormatter,
)
subparsers.add_parser(
"menu",
help="Alias for interactive.",
description="Alias for the interactive menu.",
formatter_class=EMSHelpFormatter,
)
subparsers.add_parser(
"examples",
help="Print a longer command cookbook.",
description="Print practical emsctl.py examples without reading or writing runtime-state.",
formatter_class=EMSHelpFormatter,
)
completion = subparsers.add_parser(
"completion",
help="Generate optional shell completion.",
description="Generate shell completion code. Completion is optional and dependency-free.",
epilog="""\
Examples:
python3 emsctl.py completion bash
python3 emsctl.py completion zsh
""",
formatter_class=EMSHelpFormatter,
)
completion.add_argument("shell", choices=COMPLETION_SHELLS, help="Shell completion format to print.")
subparsers.add_parser(
"help",
help="Show top-level help.",
description="Show the same top-level help as --help.",
formatter_class=EMSHelpFormatter,
)