Skip to content

Commit fafef75

Browse files
Add XML command "GetCleanSum" (#561)
Co-authored-by: flubshi <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 863bc2b commit fafef75

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

deebot_client/commands/xml/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
from .error import GetError
1111
from .fan_speed import GetFanSpeed
1212
from .pos import GetPos
13+
from .stats import GetCleanSum
1314

1415
if TYPE_CHECKING:
1516
from .common import XmlCommand
1617

1718
__all__ = [
1819
"GetChargeState",
20+
"GetCleanSum",
1921
"GetError",
2022
"GetFanSpeed",
2123
"GetPos",

deebot_client/commands/xml/stats.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""CleanSum command module."""
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
7+
from deebot_client.events import TotalStatsEvent
8+
from deebot_client.message import HandlingResult
9+
10+
from .common import XmlCommandWithMessageHandling
11+
12+
if TYPE_CHECKING:
13+
from xml.etree.ElementTree import Element
14+
15+
from deebot_client.event_bus import EventBus
16+
17+
18+
class GetCleanSum(XmlCommandWithMessageHandling):
19+
"""GetCleanSum command."""
20+
21+
name = "GetCleanSum"
22+
23+
@classmethod
24+
def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
25+
"""Handle xml message and notify the correct event subscribers.
26+
27+
:return: A message response
28+
"""
29+
if xml.attrib.get("ret") != "ok":
30+
return HandlingResult.analyse()
31+
32+
if (
33+
(area := xml.attrib.get("a"))
34+
and (lifetime := xml.attrib.get("l"))
35+
and (count := xml.attrib.get("c"))
36+
):
37+
event_bus.notify(TotalStatsEvent(int(area), int(lifetime), int(count)))
38+
return HandlingResult.success()
39+
40+
return HandlingResult.analyse()

tests/commands/xml/test_stats.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
7+
from deebot_client.command import CommandResult
8+
from deebot_client.commands.xml import GetCleanSum
9+
from deebot_client.events import TotalStatsEvent
10+
from deebot_client.message import HandlingState
11+
from tests.commands import assert_command
12+
13+
from . import get_request_xml
14+
15+
if TYPE_CHECKING:
16+
from deebot_client.events.base import Event
17+
18+
19+
@pytest.mark.parametrize(
20+
("area", "lifetime", "count", "expected_event"),
21+
[
22+
(1000, 20, 30, TotalStatsEvent(1000, 20, 30)),
23+
],
24+
)
25+
async def test_get_clean_sum(
26+
area: int, lifetime: int, count: int, expected_event: Event
27+
) -> None:
28+
json = get_request_xml(f"<ctl ret='ok' a='{area}' l='{lifetime}' c='{count}' />")
29+
await assert_command(GetCleanSum(), json, expected_event)
30+
31+
32+
@pytest.mark.parametrize(
33+
"xml",
34+
["<ctl ret='error'/>", "<ctl ret='ok' a='34' />"],
35+
ids=["error", "error"],
36+
)
37+
async def test_get_clean_sum_error(xml: str) -> None:
38+
json = get_request_xml(xml)
39+
await assert_command(
40+
GetCleanSum(),
41+
json,
42+
None,
43+
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
44+
)

0 commit comments

Comments
 (0)