Skip to content

Commit cd7ef7c

Browse files
allenporterCopilot
andauthored
chore: add snapshot tests for device payloads (#676)
* chore: add snapshot tests for device payloads This adds a fixture that captures the requests and responses in the test so they can be snapshotted. I will use this to build similar test functionality for the new API and assert on the same test contents. * chore: Update tests/conftest.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore: Update tests/conftest.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore: fix lint errors * chore: remove unnecessary whitespace --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent f52d220 commit cd7ef7c

File tree

10 files changed

+374
-14
lines changed

10 files changed

+374
-14
lines changed

roborock/protocols/v1_protocol.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import base64
66
import json
77
import logging
8-
import math
98
import secrets
109
import struct
11-
import time
1210
from collections.abc import Callable
1311
from dataclasses import dataclass, field
1412
from enum import StrEnum
@@ -19,7 +17,7 @@
1917
from roborock.protocol import Utils
2018
from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol
2119
from roborock.roborock_typing import RoborockCommand
22-
from roborock.util import get_next_int
20+
from roborock.util import get_next_int, get_timestamp
2321

2422
_LOGGER = logging.getLogger(__name__)
2523

@@ -70,7 +68,7 @@ class RequestMessage:
7068

7169
method: RoborockCommand | str
7270
params: ParamsType
73-
timestamp: int = field(default_factory=lambda: math.floor(time.time()))
71+
timestamp: int = field(default_factory=lambda: get_timestamp())
7472
request_id: int = field(default_factory=lambda: get_next_int(10000, 32767))
7573

7674
def encode_message(

roborock/roborock_message.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from __future__ import annotations
22

3-
import math
4-
import time
53
from dataclasses import dataclass, field
64
from enum import StrEnum
75

86
from roborock import RoborockEnum
9-
from roborock.util import get_next_int
7+
from roborock.util import get_next_int, get_timestamp
108

119

1210
class RoborockMessageProtocol(RoborockEnum):
@@ -245,4 +243,4 @@ class RoborockMessage:
245243
seq: int = field(default_factory=lambda: get_next_int(100000, 999999))
246244
version: bytes = b"1.0"
247245
random: int = field(default_factory=lambda: get_next_int(10000, 99999))
248-
timestamp: int = field(default_factory=lambda: math.floor(time.time()))
246+
timestamp: int = field(default_factory=lambda: get_timestamp())

roborock/util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import asyncio
44
import datetime
55
import logging
6+
import math
7+
import time
68
from asyncio import TimerHandle
79
from collections.abc import Callable, Coroutine, MutableMapping
810
from typing import Any, TypeVar
@@ -97,3 +99,11 @@ def get_next_int(min_val: int, max_val: int) -> int:
9799
counter_map[(min_val, max_val)] = min_val
98100
counter_map[(min_val, max_val)] += 1
99101
return counter_map[(min_val, max_val)] % max_val + min_val
102+
103+
104+
def get_timestamp() -> int:
105+
"""Get the current timestamp in seconds since epoch.
106+
107+
This is separated out to allow for easier mocking in tests.
108+
"""
109+
return math.floor(time.time())
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# serializer version: 1
2+
# name: test_set_value
3+
[mqtt >]
4+
00000000 10 29 00 04 4d 51 54 54 05 c2 00 46 00 00 00 00 |.)..MQTT...F....|
5+
00000010 08 31 39 36 34 38 66 39 34 00 10 32 33 34 36 37 |.19648f94..23467|
6+
00000020 38 65 61 38 35 34 66 31 39 39 65 |8ea854f199e|
7+
[mqtt <]
8+
00000000 20 09 02 00 06 22 00 0a 21 00 14 | ...."..!..|
9+
[mqtt >]
10+
00000000 82 26 00 01 00 00 20 72 72 2f 6d 2f 6f 2f 75 73 |.&.... rr/m/o/us|
11+
00000010 65 72 31 32 33 2f 31 39 36 34 38 66 39 34 2f 7a |er123/19648f94/z|
12+
00000020 65 6f 5f 64 75 69 64 00 |eo_duid.|
13+
[mqtt <]
14+
00000000 90 04 00 01 00 00 |......|
15+
[mqtt >]
16+
00000000 30 6a 00 20 72 72 2f 6d 2f 69 2f 75 73 65 72 31 |0j. rr/m/i/user1|
17+
00000010 32 33 2f 31 39 36 34 38 66 39 34 2f 7a 65 6f 5f |23/19648f94/zeo_|
18+
00000020 64 75 69 64 00 41 30 31 00 00 23 83 00 00 23 84 |duid.A01..#...#.|
19+
00000030 68 a6 a2 24 00 65 00 30 f9 aa 42 d2 e9 30 a6 1f |h..$.e.0..B..0..|
20+
00000040 66 5d 13 70 04 70 52 bd ee 69 ef 0b 80 da 71 27 |f].p.pR..i....q'|
21+
00000050 20 2c 1d d3 a7 eb 86 cf d7 58 3e 79 f6 cc ea 6f | ,.......X>y...o|
22+
00000060 9d ce ef fb cf 18 7a 1d 53 71 af d1 |......z.Sq..|
23+
[mqtt <]
24+
00000000 30 64 00 26 72 72 2f 6d 2f 6f 2f 75 73 65 72 31 |0d.&rr/m/o/user1|
25+
00000010 32 33 2f 31 39 36 34 38 66 39 34 2f 70 72 6f 64 |23/19648f94/prod|
26+
00000020 75 63 74 2d 69 64 2d 31 32 33 00 00 00 00 37 41 |uct-id-123....7A|
27+
00000030 30 31 00 00 07 e4 00 00 23 82 68 a6 a2 23 00 66 |01......#.h..#.f|
28+
00000040 00 20 42 e8 32 ab f4 52 72 fc dd 5f dd 10 38 b3 |. B.2..Rr.._..8.|
29+
00000050 81 9a 09 d3 ec fd 7f f1 d5 b7 67 e5 f2 6c f3 f6 |..........g..l..|
30+
00000060 04 72 d9 1f 7c aa |.r..|.|
31+
# ---
32+
# name: test_update_dyad_values[RoborockCategory.WET_DRY_VAC]
33+
[mqtt >]
34+
00000000 10 29 00 04 4d 51 54 54 05 c2 00 46 00 00 00 00 |.)..MQTT...F....|
35+
00000010 08 31 39 36 34 38 66 39 34 00 10 32 33 34 36 37 |.19648f94..23467|
36+
00000020 38 65 61 38 35 34 66 31 39 39 65 |8ea854f199e|
37+
[mqtt <]
38+
00000000 20 09 02 00 06 22 00 0a 21 00 14 | ...."..!..|
39+
[mqtt >]
40+
00000000 82 26 00 01 00 00 20 72 72 2f 6d 2f 6f 2f 75 73 |.&.... rr/m/o/us|
41+
00000010 65 72 31 32 33 2f 31 39 36 34 38 66 39 34 2f 7a |er123/19648f94/z|
42+
00000020 65 6f 5f 64 75 69 64 00 |eo_duid.|
43+
[mqtt <]
44+
00000000 90 04 00 01 00 00 |......|
45+
[mqtt >]
46+
00000000 30 7a 00 20 72 72 2f 6d 2f 69 2f 75 73 65 72 31 |0z. rr/m/i/user1|
47+
00000010 32 33 2f 31 39 36 34 38 66 39 34 2f 7a 65 6f 5f |23/19648f94/zeo_|
48+
00000020 64 75 69 64 00 41 30 31 00 00 23 83 00 00 23 84 |duid.A01..#...#.|
49+
00000030 68 a6 a2 24 00 65 00 40 98 95 d5 a6 cf 72 88 cd |h..$.e.@.....r..|
50+
00000040 01 b2 ed aa 7d ec f1 18 a8 67 c1 45 ac ae 01 f3 |....}....g.E....|
51+
00000050 50 d6 92 99 26 a0 8b ea 2d 45 5a 20 2d a7 e6 53 |P...&...-EZ -..S|
52+
00000060 49 1e 03 58 85 5f e7 1f 60 0d 59 22 3b 5f b7 1f |I..X._..`.Y";_..|
53+
00000070 8e 79 13 bb 53 ed 04 57 11 ae 14 c8 |.y..S..W....|
54+
[mqtt <]
55+
00000000 30 94 01 00 26 72 72 2f 6d 2f 6f 2f 75 73 65 72 |0...&rr/m/o/user|
56+
00000010 31 32 33 2f 31 39 36 34 38 66 39 34 2f 70 72 6f |123/19648f94/pro|
57+
00000020 64 75 63 74 2d 69 64 2d 31 32 33 00 00 00 00 67 |duct-id-123....g|
58+
00000030 41 30 31 00 00 07 e4 00 00 23 82 68 a6 a2 23 00 |A01......#.h..#.|
59+
00000040 66 00 50 66 89 8b 44 6a aa 4f b5 54 f6 b7 fa 5f |f.Pf..Dj.O.T..._|
60+
00000050 31 ad 89 d9 89 27 ad 88 80 47 58 9e 02 f2 1f 2f |1....'...GX..../|
61+
00000060 1a 4c 17 a2 b8 9f 0a 15 2f 98 1f 43 1f 0b e4 0b |.L....../..C....|
62+
00000070 39 27 0b 3b ba 1b 54 c5 cb 77 64 c4 70 b3 d5 53 |9'.;..T..wd.p..S|
63+
00000080 9e 38 19 ca bb 04 58 50 9f 64 a4 9e aa 25 fa 92 |.8....XP.d...%..|
64+
00000090 79 84 52 3c fc ef a1 |y.R<...|
65+
# ---
66+
# name: test_update_zeo_values
67+
[mqtt >]
68+
00000000 10 29 00 04 4d 51 54 54 05 c2 00 46 00 00 00 00 |.)..MQTT...F....|
69+
00000010 08 31 39 36 34 38 66 39 34 00 10 32 33 34 36 37 |.19648f94..23467|
70+
00000020 38 65 61 38 35 34 66 31 39 39 65 |8ea854f199e|
71+
[mqtt <]
72+
00000000 20 09 02 00 06 22 00 0a 21 00 14 | ...."..!..|
73+
[mqtt >]
74+
00000000 82 26 00 01 00 00 20 72 72 2f 6d 2f 6f 2f 75 73 |.&.... rr/m/o/us|
75+
00000010 65 72 31 32 33 2f 31 39 36 34 38 66 39 34 2f 7a |er123/19648f94/z|
76+
00000020 65 6f 5f 64 75 69 64 00 |eo_duid.|
77+
[mqtt <]
78+
00000000 90 04 00 01 00 00 |......|
79+
[mqtt >]
80+
00000000 30 8a 01 00 20 72 72 2f 6d 2f 69 2f 75 73 65 72 |0... rr/m/i/user|
81+
00000010 31 32 33 2f 31 39 36 34 38 66 39 34 2f 7a 65 6f |123/19648f94/zeo|
82+
00000020 5f 64 75 69 64 00 41 30 31 00 00 23 83 00 00 23 |_duid.A01..#...#|
83+
00000030 84 68 a6 a2 24 00 65 00 50 98 95 d5 a6 cf 72 88 |.h..$.e.P.....r.|
84+
00000040 cd 01 b2 ed aa 7d ec f1 18 d4 e8 96 71 ac f4 b4 |.....}......q...|
85+
00000050 c1 38 a2 33 5e ae 50 95 79 7f 33 cc 48 d0 d0 af |.8.3^.P.y.3.H...|
86+
00000060 d7 cb d9 bd 20 65 23 e9 e6 ef e0 60 f4 a8 2b 7d |.... e#....`..+}|
87+
00000070 45 6d a5 2c 4f d1 87 9a e2 a1 24 be 62 16 f2 87 |Em.,O.....$.b...|
88+
00000080 b3 d1 57 e2 cb 16 bc b9 68 b0 5c 25 82 |..W.....h.\%.|
89+
[mqtt <]
90+
00000000 30 a4 01 00 26 72 72 2f 6d 2f 6f 2f 75 73 65 72 |0...&rr/m/o/user|
91+
00000010 31 32 33 2f 31 39 36 34 38 66 39 34 2f 70 72 6f |123/19648f94/pro|
92+
00000020 64 75 63 74 2d 69 64 2d 31 32 33 00 00 00 00 77 |duct-id-123....w|
93+
00000030 41 30 31 00 00 07 e4 00 00 23 82 68 a6 a2 23 00 |A01......#.h..#.|
94+
00000040 66 00 60 9b a0 34 d0 93 e2 6e 81 e9 1c d4 ee ac |f.`..4...n......|
95+
00000050 6c 11 05 74 d8 2d bc d6 86 35 a5 4f 57 fc 25 3b |l..t.-...5.OW.%;|
96+
00000060 f8 04 91 9f 50 bc 69 bf bc 01 cd 2a cc 8b c3 95 |....P.i....*....|
97+
00000070 f0 05 35 12 24 35 79 d2 6e 66 28 17 92 46 a4 9d |..5.$5y.nf(..F..|
98+
00000080 d5 8d ed 2e 18 81 15 e6 1b 39 ec f2 58 17 42 13 |.........9..X.B.|
99+
00000090 d6 58 cd ee 5f 8b e7 92 2f 65 22 53 b8 53 e3 f6 |.X.._.../e"S.S..|
100+
000000a0 85 f6 1e 50 27 2d 12 |...P'-.|
101+
# ---

tests/__snapshots__/test_api.ambr

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# serializer version: 1
2+
# name: test_get_room_mapping
3+
[mqtt >]
4+
00000000 10 29 00 04 4d 51 54 54 05 c2 00 46 00 00 00 00 |.)..MQTT...F....|
5+
00000010 08 31 39 36 34 38 66 39 34 00 10 32 33 34 36 37 |.19648f94..23467|
6+
00000020 38 65 61 38 35 34 66 31 39 39 65 |8ea854f199e|
7+
[mqtt <]
8+
00000000 20 09 02 00 06 22 00 0a 21 00 14 | ...."..!..|
9+
[mqtt >]
10+
00000000 82 24 00 01 00 00 1e 72 72 2f 6d 2f 6f 2f 75 73 |.$.....rr/m/o/us|
11+
00000010 65 72 31 32 33 2f 31 39 36 34 38 66 39 34 2f 61 |er123/19648f94/a|
12+
00000020 62 63 31 32 33 00 |bc123.|
13+
[mqtt <]
14+
00000000 90 04 00 01 00 00 |......|
15+
[mqtt >]
16+
00000000 30 f8 01 00 1e 72 72 2f 6d 2f 69 2f 75 73 65 72 |0....rr/m/i/user|
17+
00000010 31 32 33 2f 31 39 36 34 38 66 39 34 2f 61 62 63 |123/19648f94/abc|
18+
00000020 31 32 33 00 31 2e 30 00 00 23 83 00 00 23 84 68 |123.1.0..#...#.h|
19+
00000030 a6 a2 24 00 65 00 c0 ac 20 3a 3e 4c 83 e4 f6 a0 |..$.e... :>L....|
20+
00000040 af f7 38 e0 94 5c 4b a5 50 8f a9 16 9c c7 da 10 |..8..\K.P.......|
21+
00000050 eb b8 af 0e 8d c2 a4 d8 bc 39 37 01 8e 6c b3 26 |.........97..l.&|
22+
00000060 79 41 24 51 23 e5 c6 4d 21 09 55 4a c7 66 58 3c |yA$Q#..M!.UJ.fX<|
23+
00000070 88 d2 50 c0 0d e0 37 82 fe 9f e5 9a 09 32 ad 74 |..P...7......2.t|
24+
00000080 14 00 27 91 b2 be 3e ca 42 25 12 05 97 00 d5 f3 |..'...>.B%......|
25+
00000090 0e f1 62 9e fc 73 8a 04 62 f5 bf 18 75 bf c4 df |..b..s..b...u...|
26+
000000a0 70 80 94 ab b1 e4 93 3d 51 33 86 1d 78 78 7b aa |p......=Q3..xx{.|
27+
000000b0 61 66 b7 3e 11 74 1d 7e 45 aa 1f 1b c6 a9 3c 61 |af.>.t.~E.....<a|
28+
000000c0 81 33 6f 25 4e 47 db 9e e0 e7 e8 10 00 be 62 8d |.3o%NG........b.|
29+
000000d0 b9 96 da 92 fd 70 e1 e2 9d 83 a7 2b 7a cc 0d 18 |.....p.....+z...|
30+
000000e0 4b 19 f5 65 5e f0 56 9e f4 3d b3 db 51 85 5f e4 |K..e^.V..=..Q._.|
31+
000000f0 10 71 10 53 16 38 5d 1b 2b 46 e2 |.q.S.8].+F.|
32+
[mqtt <]
33+
00000000 30 a4 01 00 26 72 72 2f 6d 2f 6f 2f 75 73 65 72 |0...&rr/m/o/user|
34+
00000010 31 32 33 2f 31 39 36 34 38 66 39 34 2f 70 72 6f |123/19648f94/pro|
35+
00000020 64 75 63 74 2d 69 64 2d 31 32 33 00 00 00 00 77 |duct-id-123....w|
36+
00000030 31 2e 30 00 00 07 e4 00 00 23 82 68 a6 a2 23 00 |1.0......#.h..#.|
37+
00000040 66 00 60 b2 64 cd 13 2e 13 91 91 c2 b8 20 41 39 |f.`.d........ A9|
38+
00000050 cc c3 d1 ee e6 b6 47 20 29 77 50 74 ec d2 94 18 |......G )wPt....|
39+
00000060 7f cb 69 47 3d e6 2c e9 11 8a 79 27 b3 65 0d 1c |..iG=.,...y'.e..|
40+
00000070 a2 a7 87 de 4a 68 22 aa 6c 52 c4 8e ed 50 c6 00 |....Jh".lR...P..|
41+
00000080 7c d2 9f a7 75 bc 49 6f e5 92 80 42 bb 36 35 8e ||...u.Io...B.65.|
42+
00000090 50 5a 72 fd fa 05 a5 9f c3 b2 32 8f b7 38 7e ba |PZr.......2..8~.|
43+
000000a0 03 fb 69 27 97 b6 cf |..i'...|
44+
# ---
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# serializer version: 1
2+
# name: test_async_connect
3+
[local >]
4+
00000000 00 00 00 15 31 2e 30 00 00 00 01 00 00 23 82 68 |....1.0......#.h|
5+
00000010 a6 a2 25 00 00 e7 7b 4e 54 |..%...{NT|
6+
[local <]
7+
00000000 00 00 00 27 31 2e 30 00 00 00 01 00 00 00 17 68 |...'1.0........h|
8+
00000010 a6 a2 23 00 01 00 10 0b 84 77 ab 60 ab f4 e7 7d |..#......w.`...}|
9+
00000020 ee 8a 24 84 3a 3d 59 92 64 e6 e4 |..$.:=Y.d..|
10+
[local >]
11+
00000000 00 00 00 15 31 2e 30 00 00 23 83 00 00 23 84 68 |....1.0..#...#.h|
12+
00000010 a6 a2 26 00 02 28 c1 21 4d |..&..(.!M|
13+
[local <]
14+
00000000 00 00 00 27 31 2e 30 00 00 00 02 00 00 00 17 68 |...'1.0........h|
15+
00000010 a6 a2 24 00 03 00 10 46 a4 85 f5 ab 51 0d c5 88 |..$....F....Q...|
16+
00000020 38 0b 42 e5 ce 79 20 c4 49 0b 65 |8.B..y .I.e|
17+
# ---
18+
# name: test_get_room_mapping
19+
[local >]
20+
00000000 00 00 00 15 31 2e 30 00 00 00 01 00 00 23 82 68 |....1.0......#.h|
21+
00000010 a6 a2 25 00 00 e7 7b 4e 54 |..%...{NT|
22+
[local <]
23+
00000000 00 00 00 27 31 2e 30 00 00 00 01 00 00 00 17 68 |...'1.0........h|
24+
00000010 a6 a2 23 00 01 00 10 0b 84 77 ab 60 ab f4 e7 7d |..#......w.`...}|
25+
00000020 ee 8a 24 84 3a 3d 59 92 64 e6 e4 |..$.:=Y.d..|
26+
[local >]
27+
00000000 00 00 00 15 31 2e 30 00 00 23 83 00 00 23 84 68 |....1.0..#...#.h|
28+
00000010 a6 a2 26 00 02 28 c1 21 4d |..&..(.!M|
29+
[local <]
30+
00000000 00 00 00 27 31 2e 30 00 00 00 02 00 00 00 17 68 |...'1.0........h|
31+
00000010 a6 a2 24 00 03 00 10 46 a4 85 f5 ab 51 0d c5 88 |..$....F....Q...|
32+
00000020 38 0b 42 e5 ce 79 20 c4 49 0b 65 |8.B..y .I.e|
33+
[local >]
34+
00000000 00 00 00 77 31 2e 30 00 00 23 85 00 00 23 86 68 |...w1.0..#...#.h|
35+
00000010 a6 a2 28 00 04 00 60 11 64 58 61 66 09 7d 21 08 |..(...`.dXaf.}!.|
36+
00000020 64 70 3c 8f 14 d9 1e 22 f2 1b a9 74 07 72 96 08 |dp<...."...t.r..|
37+
00000030 ea ee ea 7e 01 b8 08 9d 96 d5 fd d7 6b 0e e5 d6 |...~........k...|
38+
00000040 97 54 25 79 5d 33 63 3d e1 20 8d 3e fd 70 36 0b |.T%y]3c=. .>.p6.|
39+
00000050 8d 68 3e 60 7a 40 15 c0 01 a6 53 22 b7 5d 4f ec |.h>`z@....S".]O.|
40+
00000060 67 ae dd 7b ad 5d 1a 8a 02 be 1b 41 78 0d 93 a7 |g..{.].....Ax...|
41+
00000070 44 15 87 06 2b 2a 68 c8 08 3e 44 |D...+*h..>D|
42+
[local <]
43+
00000000 00 00 00 77 31 2e 30 00 00 13 ba 00 00 00 17 68 |...w1.0........h|
44+
00000010 a6 a2 27 00 04 00 60 cb 85 58 ea da 93 e1 c2 ea |..'...`..X......|
45+
00000020 30 e9 8d 8e 7b 7d b3 cd eb 9f 54 67 1a b4 d4 98 |0...{}....Tg....|
46+
00000030 ab 9c 6f 25 a9 cd 1f 04 a5 3c 1d 67 54 36 aa f1 |..o%.....<.gT6..|
47+
00000040 a5 ca 9e e7 8b f9 09 f8 ec 2d 57 f3 a6 7e 6e be |.........-W..~n.|
48+
00000050 00 8d 14 c0 60 f7 c9 7f 15 7c 8e 8b d1 5d fb 14 |....`....|...]..|
49+
00000060 b7 4d 61 91 0c e0 1a 24 51 f9 77 04 27 cb d6 67 |.Ma....$Q.w.'..g|
50+
00000070 57 4e 20 7d 9b d9 53 b2 cc 4f 9e |WN }..S..O.|
51+
# ---

0 commit comments

Comments
 (0)