Skip to content

Commit 61360ac

Browse files
gpongelliedenhauspre-commit-ci[bot]
authored
Add onStats message (#922)
Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a7e5dc4 commit 61360ac

4 files changed

Lines changed: 91 additions & 4 deletions

File tree

deebot_client/messages/json/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
from .battery import OnBattery
99
from .map import OnMapSetV2
1010
from .station_state import OnStationState
11-
from .stats import ReportStats
11+
from .stats import OnStats, ReportStats
1212

1313
if TYPE_CHECKING:
1414
from deebot_client.message import Message
1515

1616
__all__ = [
1717
"OnBattery",
1818
"OnMapSetV2",
19+
"OnStats",
1920
"ReportStats",
2021
]
2122

@@ -30,6 +31,7 @@
3031

3132
OnStationState,
3233

34+
OnStats,
3335
ReportStats,
3436
]
3537
# fmt: on

deebot_client/messages/json/stats.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from typing import TYPE_CHECKING, Any
66

7-
from deebot_client.events import CleanJobStatus, ReportStatsEvent
7+
from deebot_client.events import CleanJobStatus, ReportStatsEvent, StatsEvent
88
from deebot_client.message import HandlingResult, MessageBodyDataDict
99

1010
if TYPE_CHECKING:
@@ -40,3 +40,22 @@ def _handle_body_data_dict(
4040
)
4141
event_bus.notify(stats_event)
4242
return HandlingResult.success()
43+
44+
45+
class OnStats(MessageBodyDataDict):
46+
"""Get stats command."""
47+
48+
NAME = "onStats"
49+
50+
@classmethod
51+
def _handle_body_data_dict(
52+
cls, event_bus: EventBus, data: dict[str, Any]
53+
) -> HandlingResult:
54+
"""Handle message->body->data and notify the correct event subscribers.
55+
56+
:return: A message response
57+
"""
58+
event_bus.notify(
59+
StatsEvent(area=data["area"], time=data["time"], type=data["type"])
60+
)
61+
return HandlingResult.success()

tests/messages/json/test_stats.py

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
import pytest
66

7-
from deebot_client.events import CleanJobStatus, FirmwareEvent, ReportStatsEvent
8-
from deebot_client.messages.json import ReportStats
7+
from deebot_client.events import (
8+
CleanJobStatus,
9+
FirmwareEvent,
10+
ReportStatsEvent,
11+
StatsEvent,
12+
)
13+
from deebot_client.messages.json import OnStats, ReportStats
914
from tests.messages import assert_message
1015

1116

@@ -61,3 +66,62 @@ def test_ReportStats(data: dict[str, Any], expected: ReportStatsEvent) -> None:
6166
}
6267

6368
assert_message(ReportStats, data, (FirmwareEvent("1.8.2"), expected))
69+
70+
71+
@pytest.mark.parametrize(
72+
("data", "expected"),
73+
[
74+
(
75+
{
76+
"area": 2,
77+
"time": 89,
78+
"cid": "2002066096",
79+
"start": "1744009746",
80+
"type": "auto",
81+
"enablePowerMop": 1,
82+
"powerMopType": 2,
83+
"aiopen": 1,
84+
"aitypes": [9],
85+
"avoidCount": 1,
86+
},
87+
StatsEvent(
88+
area=2,
89+
time=89,
90+
type="auto",
91+
),
92+
),
93+
(
94+
{
95+
"area": 50,
96+
"time": 56289,
97+
"cid": "2002066096",
98+
"start": "1744009746",
99+
"type": "auto",
100+
"enablePowerMop": 1,
101+
"powerMopType": 2,
102+
"aiopen": 1,
103+
"aitypes": [9],
104+
"avoidCount": 1,
105+
},
106+
StatsEvent(
107+
area=50,
108+
time=56289,
109+
type="auto",
110+
),
111+
),
112+
],
113+
)
114+
def test_onStats(data: dict[str, Any], expected: StatsEvent) -> None:
115+
data = {
116+
"header": {
117+
"pri": 1,
118+
"tzm": 480,
119+
"ts": "1662017348913",
120+
"ver": "0.0.1",
121+
"fwVer": "1.8.2",
122+
"hwVer": "0.1.1",
123+
},
124+
"body": {"data": data},
125+
}
126+
127+
assert_message(OnStats, data, (FirmwareEvent("1.8.2"), expected))

tests/messages/test_get_messages.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from deebot_client.const import DataType
99
from deebot_client.messages import get_message
1010
from deebot_client.messages.json.battery import OnBattery
11+
from deebot_client.messages.json.stats import OnStats
1112

1213
if TYPE_CHECKING:
1314
from deebot_client.message import Message
@@ -19,6 +20,7 @@
1920
("onBattery", DataType.JSON, OnBattery),
2021
("onBattery_V2", DataType.JSON, OnBattery),
2122
("onError", DataType.JSON, GetError),
23+
("onStats", DataType.JSON, OnStats),
2224
("GetCleanLogs", DataType.JSON, None),
2325
("unknown", DataType.JSON, None),
2426
("unknown", DataType.XML, None),

0 commit comments

Comments
 (0)