-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowermanager.py
executable file
·95 lines (79 loc) · 2.37 KB
/
powermanager.py
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
#!/usr/bin/env python
import sys
import click
import requests
@click.group()
@click.option("-m", "--module",
type = str,
default = "nullpower",
help = "Name of power manager module")
@click.option("-a", "--address",
type = str,
default = "192.168.1.100",
help = "IP address for power manager",
show_default = True)
@click.option("-u", "--username",
type = str,
default = "admin",
help = "Username for power manager",
show_default = True)
@click.option("-p", "--password",
type = str,
default = "admin",
help = "Password for power manager",
show_default = True)
@click.pass_context
def cli(ctx, module, address, username, password):
"""A command line tool for controlling NP05B power strips, via IP address.
"""
if ctx.obj is None:
ctx.obj = {}
mod = __import__(module)
ctx.obj["manager"] = mod.__dict__[module](address, username, password)
@cli.command()
@click.argument("outlet",
type = click.IntRange(1,16))
@click.pass_context
def power_on(ctx, outlet):
"""Power on specified outlet."""
response = ctx.obj["manager"].power_on(outlet)
sys.exit(response)
@cli.command()
@click.argument("outlet",
type = click.IntRange(1,16))
@click.pass_context
def power_off(ctx, outlet):
"""Power off specified outlet."""
response = ctx.obj["manager"].power_off(outlet)
sys.exit(response)
@cli.command()
@click.pass_context
def all_on(ctx):
"""Power on all outlets."""
response = ctx.obj["manager"].all_on()
sys.exit(response)
@cli.command()
@click.pass_context
def all_off(ctx):
"""Power off all outlets."""
response = ctx.obj["manager"].all_off()
sys.exit(response)
@cli.command()
@click.argument("outlet",
type = click.IntRange(1,16))
@click.pass_context
def is_on(ctx, outlet):
"""Return 1 if outlet is on, 0 otherwise."""
response = ctx.obj["manager"].is_on(outlet)
if response:
click.echo("1")
else:
click.echo("0")
@cli.command()
@click.pass_context
def wake_up(ctx):
"Send wake up signal to power manager"
response = ctx.obj["manager"].wake_up()
sys.exit(response)
if __name__ == "__main__":
cli()