Skip to content

Commit 589a998

Browse files
committed
feat: Allow RoborockModeEnum parsing by either enum name, value name, or code.
This will be used for command line parsing for Q10 commands. Adds the first tests in the tests/data directory since we moved many containers here.
1 parent 7e40857 commit 589a998

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

roborock/data/code_mappings.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ def from_code(cls, code: int):
7070
return member
7171
raise ValueError(f"{code} is not a valid code for {cls.__name__}")
7272

73+
@classmethod
74+
def from_value(cls, value: str):
75+
"""Find enum member by string value (case-insensitive)."""
76+
for member in cls:
77+
if member.value.lower() == value.lower():
78+
return member
79+
raise ValueError(f"{value} is not a valid value for {cls.__name__}")
80+
81+
@classmethod
82+
def from_name(cls, name: str):
83+
"""Find enum member by name (case-insensitive)."""
84+
for member in cls:
85+
if member.name.lower() == name.lower():
86+
return member
87+
raise ValueError(f"{name} is not a valid name for {cls.__name__}")
88+
7389
@classmethod
7490
def keys(cls) -> list[str]:
7591
"""Returns a list of all member values."""

tests/data/test_code_mappings.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Tests for code mappings.
2+
These tests exercise the custom enum methods using arbitrary enum values.
3+
"""
4+
5+
from roborock.data.b01_q10.b01_q10_code_mappings import B01_Q10_DP
6+
7+
8+
def test_from_code():
9+
"""Test from_code method."""
10+
assert B01_Q10_DP.START_CLEAN == B01_Q10_DP.from_code(201)
11+
assert B01_Q10_DP.PAUSE == B01_Q10_DP.from_code(204)
12+
assert B01_Q10_DP.STOP == B01_Q10_DP.from_code(206)
13+
14+
15+
def test_from_name():
16+
"""Test from_name method."""
17+
assert B01_Q10_DP.START_CLEAN == B01_Q10_DP.from_name("START_CLEAN")
18+
assert B01_Q10_DP.PAUSE == B01_Q10_DP.from_name("pause")
19+
assert B01_Q10_DP.STOP == B01_Q10_DP.from_name("Stop")
20+
21+
22+
def test_from_value():
23+
"""Test from_value method."""
24+
assert B01_Q10_DP.START_CLEAN == B01_Q10_DP.from_value("dpStartClean")
25+
assert B01_Q10_DP.PAUSE == B01_Q10_DP.from_value("dpPause")
26+
assert B01_Q10_DP.STOP == B01_Q10_DP.from_value("dpStop")

0 commit comments

Comments
 (0)