Skip to content

Commit ca9b1b3

Browse files
authored
Refactor session handling and option to subscribe to device property updates (#1)
1 parent 4d1ac55 commit ca9b1b3

File tree

8 files changed

+559
-189
lines changed

8 files changed

+559
-189
lines changed

karcher/cli.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import json
99
import logging
1010

11-
from karcher.auth import Session
1211
from karcher.exception import KarcherHomeException
1312
from karcher.karcher import KarcherHome
1413
from karcher.consts import Region
@@ -18,45 +17,48 @@
1817
except ImportError:
1918
echo = click.echo
2019

20+
2121
class EnhancedJSONEncoder(json.JSONEncoder):
2222
def default(self, o):
2323
if dataclasses.is_dataclass(o):
2424
return dataclasses.asdict(o)
2525
return super().default(o)
2626

27+
2728
class GlobalContextObject:
2829
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+
):
3334
self.debug = debug
3435
self.output = output
3536
self.region = region
3637

3738
def print(self, result):
38-
data_variable = getattr(result, "data", None)
39+
data_variable = getattr(result, 'data', None)
3940
if data_variable is not None:
4041
result = data_variable
41-
if self.output == "json_pretty":
42+
if self.output == 'json_pretty':
4243
echo(json.dumps(result, cls=EnhancedJSONEncoder, indent=4))
4344
else:
4445
echo(json.dumps(result, cls=EnhancedJSONEncoder))
4546

47+
4648
@click.group()
47-
@click.option("-d", "--debug", is_flag=True)
49+
@click.option('-d', '--debug', is_flag=True)
4850
@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"')
5456
@click.option(
55-
"-r",
56-
"--region",
57+
'-r',
58+
'--region',
5759
type=click.Choice([Region.EU, Region.US, Region.CN]),
5860
default=Region.EU,
59-
help="Region of the server to query. Default: 'eu'")
61+
help='Region of the server to query. Default: "eu"')
6062
@click.pass_context
6163
def cli(ctx: click.Context, debug: int, output: str, region: Region):
6264
"""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):
6870

6971
ctx.obj = GlobalContextObject(debug=debug, output=output, region=region)
7072

73+
7174
def safe_cli():
7275
try:
7376
cli()
@@ -78,6 +81,7 @@ def safe_cli():
7881
}))
7982
return
8083

84+
8185
@cli.command()
8286
@click.pass_context
8387
def get_urls(ctx: click.Context):
@@ -88,6 +92,7 @@ def get_urls(ctx: click.Context):
8892

8993
ctx.obj.print(d)
9094

95+
9196
@cli.command()
9297
@click.option('--username', '-u', help='Username to login with.')
9398
@click.option('--password', '-p', help='Password to login with.')
@@ -98,6 +103,7 @@ def login(ctx: click.Context, username: str, password: str):
98103
kh = KarcherHome(region=ctx.obj.region)
99104
ctx.obj.print(kh.login(username, password))
100105

106+
101107
@cli.command()
102108
@click.option('--username', '-u', default=None, help='Username to login with.')
103109
@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):
107113
"""List all devices."""
108114

109115
kh = KarcherHome(region=ctx.obj.region)
110-
sess = None
111116
if token is not None:
112-
sess = Session.from_token(token, '')
117+
kh.login_token(token, '')
113118
elif username is not None and password is not None:
114-
sess = kh.login(username, password)
119+
kh.login(username, password)
115120
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.')
117123

118-
devices = kh.get_devices(sess)
124+
devices = kh.get_devices()
119125

120126
# Logout if we used a username and password
121127
if token is None:
122-
kh.logout(sess)
128+
kh.logout()
123129

124130
ctx.obj.print(devices)

karcher/consts.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ class Product(str, Enum):
6969

7070

7171
REGION_URLS = {
72-
Region.EU: "https://eu-appaiot.3irobotix.net",
73-
Region.US: "https://us-appaiot.3irobotix.net",
74-
Region.CN: "https://cn-appaiot.3irobotix.net",
72+
Region.EU: 'https://eu-appaiot.3irobotix.net',
73+
Region.US: 'https://us-appaiot.3irobotix.net',
74+
Region.CN: 'https://cn-appaiot.3irobotix.net',
7575
}
7676

7777
REGION_INDEX = {
@@ -80,8 +80,39 @@ class Product(str, Enum):
8080
Region.CN: 0,
8181
}
8282

83-
TENANT_ID = "1528983614213726208"
84-
PROJECT_TYPE = "android_iot.karcher"
85-
PROTOCOL_VERSION = "v1"
83+
ROBOT_PROPERTIES = [
84+
'status',
85+
'firmware_code',
86+
'firmware',
87+
'fault',
88+
'mode',
89+
'wind',
90+
'water',
91+
'repeat_state',
92+
'charge_state',
93+
'quantity',
94+
'work_mode',
95+
'sweep_type',
96+
'build_map',
97+
'cleaning_area',
98+
'cleaning_time',
99+
'current_map_id',
100+
'custom_type',
101+
'privacy',
102+
'alarm',
103+
'volume',
104+
'tank_state',
105+
'cloth_state',
106+
'mop_route',
107+
'map_num',
108+
'language',
109+
'voice_type',
110+
'quiet_status',
111+
'quiet_is_open'
112+
]
113+
114+
TENANT_ID = '1528983614213726208'
115+
PROJECT_TYPE = 'android_iot.karcher'
116+
PROTOCOL_VERSION = 'v1'
86117
APP_VERSION_CODE = 10004
87118
APP_VERSION_NAME = '1.0.4'

0 commit comments

Comments
 (0)