diff --git a/deebot_client/messages/json/__init__.py b/deebot_client/messages/json/__init__.py index 6a916460f..a7e321b83 100644 --- a/deebot_client/messages/json/__init__.py +++ b/deebot_client/messages/json/__init__.py @@ -8,7 +8,7 @@ from .battery import OnBattery from .map import OnMapSetV2 from .station_state import OnStationState -from .stats import ReportStats +from .stats import OnStats, ReportStats if TYPE_CHECKING: from deebot_client.message import Message @@ -16,6 +16,7 @@ __all__ = [ "OnBattery", "OnMapSetV2", + "OnStats", "ReportStats", ] @@ -30,6 +31,7 @@ OnStationState, + OnStats, ReportStats, ] # fmt: on diff --git a/deebot_client/messages/json/stats.py b/deebot_client/messages/json/stats.py index a0c5e9829..e3566f683 100644 --- a/deebot_client/messages/json/stats.py +++ b/deebot_client/messages/json/stats.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Any -from deebot_client.events import CleanJobStatus, ReportStatsEvent +from deebot_client.events import CleanJobStatus, ReportStatsEvent, StatsEvent from deebot_client.message import HandlingResult, MessageBodyDataDict if TYPE_CHECKING: @@ -40,3 +40,22 @@ def _handle_body_data_dict( ) event_bus.notify(stats_event) return HandlingResult.success() + + +class OnStats(MessageBodyDataDict): + """Get stats command.""" + + NAME = "onStats" + + @classmethod + def _handle_body_data_dict( + cls, event_bus: EventBus, data: dict[str, Any] + ) -> HandlingResult: + """Handle message->body->data and notify the correct event subscribers. + + :return: A message response + """ + event_bus.notify( + StatsEvent(area=data["area"], time=data["time"], type=data["type"]) + ) + return HandlingResult.success() diff --git a/tests/messages/json/test_stats.py b/tests/messages/json/test_stats.py index 13c234b53..95609fdae 100644 --- a/tests/messages/json/test_stats.py +++ b/tests/messages/json/test_stats.py @@ -4,8 +4,13 @@ import pytest -from deebot_client.events import CleanJobStatus, FirmwareEvent, ReportStatsEvent -from deebot_client.messages.json import ReportStats +from deebot_client.events import ( + CleanJobStatus, + FirmwareEvent, + ReportStatsEvent, + StatsEvent, +) +from deebot_client.messages.json import OnStats, ReportStats from tests.messages import assert_message @@ -61,3 +66,62 @@ def test_ReportStats(data: dict[str, Any], expected: ReportStatsEvent) -> None: } assert_message(ReportStats, data, (FirmwareEvent("1.8.2"), expected)) + + +@pytest.mark.parametrize( + ("data", "expected"), + [ + ( + { + "area": 2, + "time": 89, + "cid": "2002066096", + "start": "1744009746", + "type": "auto", + "enablePowerMop": 1, + "powerMopType": 2, + "aiopen": 1, + "aitypes": [9], + "avoidCount": 1, + }, + StatsEvent( + area=2, + time=89, + type="auto", + ), + ), + ( + { + "area": 50, + "time": 56289, + "cid": "2002066096", + "start": "1744009746", + "type": "auto", + "enablePowerMop": 1, + "powerMopType": 2, + "aiopen": 1, + "aitypes": [9], + "avoidCount": 1, + }, + StatsEvent( + area=50, + time=56289, + type="auto", + ), + ), + ], +) +def test_onStats(data: dict[str, Any], expected: StatsEvent) -> None: + data = { + "header": { + "pri": 1, + "tzm": 480, + "ts": "1662017348913", + "ver": "0.0.1", + "fwVer": "1.8.2", + "hwVer": "0.1.1", + }, + "body": {"data": data}, + } + + assert_message(OnStats, data, (FirmwareEvent("1.8.2"), expected)) diff --git a/tests/messages/test_get_messages.py b/tests/messages/test_get_messages.py index 5db55670a..2fdf95a7a 100644 --- a/tests/messages/test_get_messages.py +++ b/tests/messages/test_get_messages.py @@ -8,6 +8,7 @@ from deebot_client.const import DataType from deebot_client.messages import get_message from deebot_client.messages.json.battery import OnBattery +from deebot_client.messages.json.stats import OnStats if TYPE_CHECKING: from deebot_client.message import Message @@ -19,6 +20,7 @@ ("onBattery", DataType.JSON, OnBattery), ("onBattery_V2", DataType.JSON, OnBattery), ("onError", DataType.JSON, GetError), + ("onStats", DataType.JSON, OnStats), ("GetCleanLogs", DataType.JSON, None), ("unknown", DataType.JSON, None), ("unknown", DataType.XML, None),