-
Notifications
You must be signed in to change notification settings - Fork 0
/
viomivacuum.py
443 lines (363 loc) · 12.2 KB
/
viomivacuum.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
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
import logging
import time
from collections import defaultdict
from datetime import timedelta
from enum import Enum
from typing import Dict, List, Optional
import click
from .click_common import EnumType, command, format_output
from .device import Device
from .utils import pretty_seconds
from .vacuumcontainers import ConsumableStatus, DNDStatus
_LOGGER = logging.getLogger(__name__)
ERROR_CODES = {
500: "Radar timed out",
501: "Wheels stuck",
502: "Low battery",
503: "Dust bin missing",
508: "Uneven ground",
509: "Cliff sensor error",
510: "Collision sensor error",
511: "Could not return to dock",
512: "Could not return to dock",
513: "Could not navigate",
514: "Vacuum stuck",
515: "Charging error",
516: "Mop temperature error",
521: "Water tank is not installed",
522: "Mop is not installed",
525: "Insufficient water in water tank",
527: "Remove mop",
528: "Dust bin missing",
529: "Mop and water tank missing",
530: "Mop and water tank missing",
531: "Water tank is not installed",
2101: "Unsufficient battery, continuing cleaning after recharge",
2103: "Charging",
2105: "Fully charged",
}
class ViomiConsumableStatus(ConsumableStatus):
def __init__(self, data: List[int]) -> None:
# [17, 17, 17, 17]
self.data = [d * 60 * 60 for d in data]
self.side_brush_total = timedelta(hours=180)
self.main_brush_total = timedelta(hours=360)
self.filter_total = timedelta(hours=180)
self.mop_total = timedelta(hours=180)
@property
def main_brush(self) -> timedelta:
"""Main brush usage time."""
return pretty_seconds(self.data[0])
@property
def main_brush_left(self) -> timedelta:
"""How long until the main brush should be changed."""
return self.main_brush_total - self.main_brush
@property
def side_brush(self) -> timedelta:
"""Side brush usage time."""
return pretty_seconds(self.data[1])
@property
def side_brush_left(self) -> timedelta:
"""How long until the side brush should be changed."""
return self.side_brush_total - self.side_brush
@property
def filter(self) -> timedelta:
"""Filter usage time."""
return pretty_seconds(self.data[2])
@property
def filter_left(self) -> timedelta:
"""How long until the filter should be changed."""
return self.filter_total - self.filter
@property
def mop(self) -> timedelta:
"""Return ``sensor_dirty_time``"""
return pretty_seconds(self.data[3])
@property
def mop_left(self) -> timedelta:
return self.sensor_dirty_total - self.sensor_dirty
def __repr__(self) -> str:
return (
"<ConsumableStatus main: %s, side: %s, filter: %s, mop: %s>"
% ( # noqa: E501
self.main_brush,
self.side_brush,
self.filter,
self.mop,
)
)
class ViomiVacuumSpeed(Enum):
Silent = 0
Standard = 1
Medium = 2
Turbo = 3
class ViomiVacuumState(Enum):
Unknown = -1
IdleNotDocked = 0
Idle = 1
Idle2 = 2
Cleaning = 3
Returning = 4
Docked = 5
VacuumingAndMopping = 6
class ViomiMode(Enum):
Vacuum = 0 # No Mop, Vacuum only
VacuumAndMop = 1
Mop = 2
CleanZone = 3
CleanSpot = 4
class ViomiLanguage(Enum):
CN = 1 # Chinese (default)
EN = 2 # English
class ViomiLedState(Enum):
Off = 0
On = 1
class ViomiCarpetTurbo(Enum):
Off = 0
Medium = 1
Turbo = 2
class ViomiMovementDirection(Enum):
Forward = 1
Left = 2 # Rotate
Right = 3 # Rotate
Backward = 4
Stop = 5
Unknown = 10
class ViomiBinType(Enum):
Vacuum = 1
Water = 2
VacuumAndWater = 3
NoBin = 0
class ViomiWaterGrade(Enum):
Low = 11
Medium = 12
High = 13
class ViomiMopMode(Enum):
"""Mopping pattern."""
S = 0
Y = 1
class ViomiVacuumStatus:
def __init__(self, data):
# ["run_state","mode","err_state","battary_life","box_type","mop_type","s_time","s_area",
# [ 5, 0, 2103, 85, 3, 1, 0, 0,
# "suction_grade","water_grade","remember_map","has_map","is_mop","has_newmap"]'
# 1, 11, 1, 1, 1, 0 ]
self.data = data
@property
def state(self):
"""State of the vacuum."""
try:
return ViomiVacuumState(self.data["run_state"])
except ValueError:
_LOGGER.warning("Unknown vacuum state: %s", self.data["run_state"])
return ViomiVacuumState.Unknown
@property
def is_on(self) -> bool:
"""True if cleaning."""
cleaning_states = [
ViomiVacuumState.Cleaning,
ViomiVacuumState.VacuumingAndMopping,
]
return self.state in cleaning_states
@property
def mode(self):
"""Active mode.
TODO: is this same as mop_type property?
"""
return ViomiMode(self.data["mode"])
@property
def mop_type(self):
"""Unknown mop_type values."""
return self.data["mop_type"]
@property
def error_code(self) -> int:
"""Error code from vacuum."""
return self.data["err_state"]
@property
def error(self) -> Optional[str]:
"""String presentation for the error code."""
if self.error_code is None:
return None
return ERROR_CODES.get(self.error_code, f"Unknown error {self.error_code}")
@property
def battery(self) -> int:
"""Battery in percentage."""
return self.data["battary_life"]
@property
def bin_type(self) -> ViomiBinType:
"""Type of the inserted bin."""
return ViomiBinType(self.data["box_type"])
@property
def clean_time(self) -> timedelta:
"""Cleaning time."""
return pretty_seconds(self.data["s_time"])
@property
def clean_area(self) -> float:
"""Cleaned area in square meters."""
return self.data["s_area"]
@property
def fanspeed(self) -> ViomiVacuumSpeed:
"""Current fan speed."""
return ViomiVacuumSpeed(self.data["suction_grade"])
@property
def water_grade(self) -> ViomiWaterGrade:
"""Water grade."""
return ViomiWaterGrade(self.data["water_grade"])
@property
def remember_map(self) -> bool:
"""True to remember the map."""
return bool(self.data["remember_map"])
@property
def has_map(self) -> bool:
"""True if device has map?"""
return bool(self.data["has_map"])
@property
def has_new_map(self) -> bool:
"""TODO: unknown"""
return bool(self.data["has_newmap"])
@property
def mop_mode(self) -> ViomiMode:
"""Whether mopping is enabled and if so which mode
TODO: is this really the same as mode?
"""
return ViomiMode(self.data["is_mop"])
class ViomiVacuum(Device):
"""Interface for Viomi vacuums (viomi.vacuum.v7)."""
@command(
default_output=format_output(
"",
"State: {result.state}\n"
"Mode: {result.mode}\n"
"Error: {result.error}\n"
"Battery: {result.battery}\n"
"Fan speed: {result.fanspeed}\n"
"Box type: {result.bin_type}\n"
"Mop type: {result.mop_type}\n"
"Clean time: {result.clean_time}\n"
"Clean area: {result.clean_area}\n"
"Water grade: {result.water_grade}\n"
"Remember map: {result.remember_map}\n"
"Has map: {result.has_map}\n"
"Has new map: {result.has_new_map}\n"
"Mop mode: {result.mop_mode}\n",
)
)
def status(self) -> ViomiVacuumStatus:
"""Retrieve properties."""
properties = [
"run_state",
"mode",
"err_state",
"battary_life",
"box_type",
"mop_type",
"s_time",
"s_area",
"suction_grade",
"water_grade",
"remember_map",
"has_map",
"is_mop",
"has_newmap",
]
values = self.get_properties(properties)
return ViomiVacuumStatus(defaultdict(lambda: None, zip(properties, values)))
@command()
def start(self):
"""Start cleaning."""
# TODO figure out the parameters
self.send("set_mode_withroom", [0, 1, 0])
@command()
def stop(self):
"""Stop cleaning."""
self.send("set_mode", [0])
@command()
def pause(self):
"""Pause cleaning."""
self.send("set_mode_withroom", [0, 2, 0])
@command(click.argument("speed", type=EnumType(ViomiVacuumSpeed)))
def set_fan_speed(self, speed: ViomiVacuumSpeed):
"""Set fanspeed [silent, standard, medium, turbo]."""
self.send("set_suction", [speed.value])
@command(click.argument("watergrade"))
def set_water_grade(self, watergrade: ViomiWaterGrade):
"""Set water grade [low, medium, high]."""
self.send("set_suction", [watergrade.value])
@command()
def home(self):
"""Return to home."""
self.send("set_charge", [1])
@command(
click.argument("direction", type=EnumType(ViomiMovementDirection)),
click.option(
"--duration",
type=float,
default=0.5,
help="number of seconds to perform this movement",
),
)
def move(self, direction: ViomiMovementDirection, duration=0.5):
"""Manual movement."""
start = time.time()
while time.time() - start < duration:
self.send("set_direction", [direction.value])
time.sleep(0.1)
self.send("set_direction", [ViomiMovementDirection.Stop.value])
@command(click.argument("mode", type=EnumType(ViomiMode)))
def clean_mode(self, mode: ViomiMode):
"""Set the cleaning mode."""
self.send("set_mop", [mode.value])
@command(click.argument("mop_mode", type=EnumType(ViomiMopMode)))
def mop_mode(self, mop_mode: ViomiMopMode):
self.send("set_moproute", [mop_mode.value])
@command()
def consumable_status(self) -> ViomiConsumableStatus:
"""Return information about consumables."""
return ViomiConsumableStatus(self.send("get_consumables"))
@command()
def dnd_status(self):
"""Returns do-not-disturb status."""
status = self.send("get_notdisturb")
return DNDStatus(
dict(
enabled=status[0],
start_hour=status[1],
start_minute=status[2],
end_hour=status[3],
end_minute=status[4],
)
)
@command(
click.option("--disable", is_flag=True),
click.argument("start_hr", type=int),
click.argument("start_min", type=int),
click.argument("end_hr", type=int),
click.argument("end_min", type=int),
)
def set_dnd(
self, disable: bool, start_hr: int, start_min: int, end_hr: int, end_min: int
):
"""Set do-not-disturb.
:param int start_hr: Start hour
:param int start_min: Start minute
:param int end_hr: End hour
:param int end_min: End minute"""
return self.send(
"set_notdisturb",
[0 if disable else 1, start_hr, start_min, end_hr, end_min],
)
@command(click.argument("language", type=EnumType(ViomiLanguage)))
def set_language(self, language: ViomiLanguage):
"""Set the device's audio language."""
return self.send("set_language", [language.value])
@command(click.argument("state", type=EnumType(ViomiLedState)))
def led(self, state: ViomiLedState):
"""Switch the button leds on or off."""
return self.send("set_light", [state.value])
@command(click.argument("mode", type=EnumType(ViomiCarpetTurbo)))
def carpet_mode(self, mode: ViomiCarpetTurbo):
"""Set the carpet mode."""
return self.send("set_carpetturbo", [mode.value])
@command()
def fan_speed_presets(self) -> Dict[str, int]:
"""Return dictionary containing supported fanspeeds."""
return {x.name: x.value for x in list(ViomiVacuumSpeed)}