Skip to content

Commit e363ad0

Browse files
committed
Add more checks to Pos message parsing
1 parent e21500e commit e363ad0

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

deebot_client/commands/xml/pos.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ class PosParser:
2020
"""Support class for producing Pos events."""
2121

2222
@classmethod
23-
def __parse_xml(
23+
def _parse_xml(
2424
cls, position_type: PositionType, event_bus: EventBus, xml: Element
2525
) -> HandlingResult:
2626
"""Handle xml message and notify the correct event subscribers."""
27-
if (p := xml.attrib.get("p")) and (xml.attrib.get("valid", "1")) != "1":
27+
if (p := xml.attrib.get("p")) and (xml.attrib.get("valid", "1")) == "1":
2828
p_x, p_y = p.split(",", 2)
2929
p_a = xml.attrib.get("a", 0)
3030
position = Position(type=position_type, x=int(p_x), y=int(p_y), a=int(p_a))
@@ -48,7 +48,7 @@ def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
4848
if xml.attrib.get("ret") != "ok" or xml.attrib.get("t") != "p":
4949
return HandlingResult.analyse()
5050

51-
return cls.__parse_xml(PositionType.DEEBOT, event_bus, xml)
51+
return cls._parse_xml(PositionType.DEEBOT, event_bus, xml)
5252

5353

5454
class GetChargerPos(XmlCommandWithMessageHandling, PosParser):
@@ -65,4 +65,4 @@ def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
6565
if xml.attrib.get("ret") != "ok":
6666
return HandlingResult.analyse()
6767

68-
return cls.__parse_xml(PositionType.CHARGER, event_bus, xml)
68+
return cls._parse_xml(PositionType.CHARGER, event_bus, xml)

deebot_client/messages/xml/pos.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from typing import TYPE_CHECKING
66

77
from deebot_client.commands.xml.pos import PosParser
8+
from deebot_client.message import HandlingResult
89
from deebot_client.messages.xml.common import XmlMessage
910
from deebot_client.rs.map import PositionType
1011

1112
if TYPE_CHECKING:
1213
from xml.etree.ElementTree import Element
1314

1415
from deebot_client.event_bus import EventBus
15-
from deebot_client.message import HandlingResult
1616

1717

1818
class Pos(XmlMessage, PosParser):
@@ -22,4 +22,7 @@ class Pos(XmlMessage, PosParser):
2222

2323
@classmethod
2424
def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
25-
return cls.__parse_xml(PositionType.DEEBOT, event_bus, xml)
25+
if xml.attrib.get("t") != "p":
26+
return HandlingResult.analyse()
27+
28+
return cls._parse_xml(PositionType.DEEBOT, event_bus, xml)

tests/messages/xml/test_pos.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ def test_Pos(position: tuple[int, int, int]) -> None:
2121

2222

2323
@pytest.mark.parametrize(
24-
"xml_message",
24+
("xml_message", "expected_result_state"),
2525
[
26-
'<ctl td="Pos" t="p" a="89" valid="1" />',
26+
('<ctl td="Pos" t="p" a="89" valid="1" />', HandlingState.ANALYSE_LOGGED),
27+
('<ctl td="Pos" t="??" p="0,0" a="89" valid="1" />', HandlingState.ERROR),
28+
(
29+
'<ctl td="Pos" t="p" p="0,0" a="89" valid="0" />',
30+
HandlingState.ANALYSE_LOGGED,
31+
),
2732
],
2833
)
29-
def test_Pos_error(xml_message: str) -> None:
30-
assert_message_failure(Pos, xml_message, HandlingState.ANALYSE_LOGGED)
34+
def test_Pos_error(xml_message: str, expected_result_state: HandlingState) -> None:
35+
assert_message_failure(Pos, xml_message, expected_result_state)

0 commit comments

Comments
 (0)