8
8
import json
9
9
import logging
10
10
11
- from karcher .auth import Session
12
11
from karcher .exception import KarcherHomeException
13
12
from karcher .karcher import KarcherHome
14
13
from karcher .consts import Region
18
17
except ImportError :
19
18
echo = click .echo
20
19
20
+
21
21
class EnhancedJSONEncoder (json .JSONEncoder ):
22
22
def default (self , o ):
23
23
if dataclasses .is_dataclass (o ):
24
24
return dataclasses .asdict (o )
25
25
return super ().default (o )
26
26
27
+
27
28
class GlobalContextObject :
28
29
def __init__ (self ,
29
- debug : int = 0 ,
30
- output : str = " json" ,
31
- region : Region = Region .EU
32
- ):
30
+ debug : int = 0 ,
31
+ output : str = ' json' ,
32
+ region : Region = Region .EU
33
+ ):
33
34
self .debug = debug
34
35
self .output = output
35
36
self .region = region
36
37
37
38
def print (self , result ):
38
- data_variable = getattr (result , " data" , None )
39
+ data_variable = getattr (result , ' data' , None )
39
40
if data_variable is not None :
40
41
result = data_variable
41
- if self .output == " json_pretty" :
42
+ if self .output == ' json_pretty' :
42
43
echo (json .dumps (result , cls = EnhancedJSONEncoder , indent = 4 ))
43
44
else :
44
45
echo (json .dumps (result , cls = EnhancedJSONEncoder ))
45
46
47
+
46
48
@click .group ()
47
- @click .option ("-d" , " --debug" , is_flag = True )
49
+ @click .option ('-d' , ' --debug' , is_flag = True )
48
50
@click .option (
49
- "-o" ,
50
- " --output" ,
51
- type = click .Choice ([" json" , " json_pretty" ]),
52
- default = " json" ,
53
- help = " Output format. Default: ' json'" )
51
+ '-o' ,
52
+ ' --output' ,
53
+ type = click .Choice ([' json' , ' json_pretty' ]),
54
+ default = ' json' ,
55
+ help = ' Output format. Default: " json"' )
54
56
@click .option (
55
- "-r" ,
56
- " --region" ,
57
+ '-r' ,
58
+ ' --region' ,
57
59
type = click .Choice ([Region .EU , Region .US , Region .CN ]),
58
60
default = Region .EU ,
59
- help = " Region of the server to query. Default: 'eu'" )
61
+ help = ' Region of the server to query. Default: "eu"' )
60
62
@click .pass_context
61
63
def cli (ctx : click .Context , debug : int , output : str , region : Region ):
62
64
"""Tool for connectiong and getting information from Kärcher Home Robots."""
@@ -68,6 +70,7 @@ def cli(ctx: click.Context, debug: int, output: str, region: Region):
68
70
69
71
ctx .obj = GlobalContextObject (debug = debug , output = output , region = region )
70
72
73
+
71
74
def safe_cli ():
72
75
try :
73
76
cli ()
@@ -78,6 +81,7 @@ def safe_cli():
78
81
}))
79
82
return
80
83
84
+
81
85
@cli .command ()
82
86
@click .pass_context
83
87
def get_urls (ctx : click .Context ):
@@ -88,6 +92,7 @@ def get_urls(ctx: click.Context):
88
92
89
93
ctx .obj .print (d )
90
94
95
+
91
96
@cli .command ()
92
97
@click .option ('--username' , '-u' , help = 'Username to login with.' )
93
98
@click .option ('--password' , '-p' , help = 'Password to login with.' )
@@ -98,6 +103,7 @@ def login(ctx: click.Context, username: str, password: str):
98
103
kh = KarcherHome (region = ctx .obj .region )
99
104
ctx .obj .print (kh .login (username , password ))
100
105
106
+
101
107
@cli .command ()
102
108
@click .option ('--username' , '-u' , default = None , help = 'Username to login with.' )
103
109
@click .option ('--password' , '-p' , default = None , help = 'Password to login with.' )
@@ -107,18 +113,18 @@ def devices(ctx: click.Context, username: str, password: str, token: str):
107
113
"""List all devices."""
108
114
109
115
kh = KarcherHome (region = ctx .obj .region )
110
- sess = None
111
116
if token is not None :
112
- sess = Session . from_token (token , '' )
117
+ kh . login_token (token , '' )
113
118
elif username is not None and password is not None :
114
- sess = kh .login (username , password )
119
+ kh .login (username , password )
115
120
else :
116
- raise click .BadParameter ('Must provide either token or username and password.' )
121
+ raise click .BadParameter (
122
+ 'Must provide either token or username and password.' )
117
123
118
- devices = kh .get_devices (sess )
124
+ devices = kh .get_devices ()
119
125
120
126
# Logout if we used a username and password
121
127
if token is None :
122
- kh .logout (sess )
128
+ kh .logout ()
123
129
124
130
ctx .obj .print (devices )
0 commit comments