Skip to content

Commit cab2f1b

Browse files
authored
Add legacy GetNetInfo JSON command (#893)
1 parent eed7a71 commit cab2f1b

File tree

4 files changed

+99
-10
lines changed

4 files changed

+99
-10
lines changed

deebot_client/commands/json/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
)
3838
from .moveup_warning import GetMoveUpWarning, SetMoveUpWarning
3939
from .multimap_state import GetMultimapState, SetMultimapState
40-
from .network import GetNetInfo
40+
from .network import GetNetInfo, GetNetInfoLegacy
4141
from .ota import GetOta, SetOta
4242
from .play_sound import PlaySound
4343
from .pos import GetPos
@@ -88,6 +88,7 @@
8888
"GetMoveUpWarning",
8989
"GetMultimapState",
9090
"GetNetInfo",
91+
"GetNetInfoLegacy",
9192
"GetOta",
9293
"GetPos",
9394
"GetSafeProtect",
@@ -198,6 +199,7 @@
198199
SetMultimapState,
199200

200201
GetNetInfo,
202+
GetNetInfoLegacy,
201203

202204
GetOta,
203205
SetOta,

deebot_client/commands/json/network.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
from typing import TYPE_CHECKING, Any
66

7+
from deebot_client.command import CommandWithMessageHandling
78
from deebot_client.events import NetworkInfoEvent
8-
from deebot_client.message import HandlingResult, MessageBodyDataDict
9+
from deebot_client.message import (
10+
HandlingResult,
11+
MessageBodyDataDict,
12+
MessageDict,
13+
)
914

10-
from .common import JsonCommandWithMessageHandling
15+
from .common import JsonCommand, JsonCommandWithMessageHandling
1116

1217
if TYPE_CHECKING:
1318
from deebot_client.event_bus import EventBus
@@ -35,3 +40,27 @@ def _handle_body_data_dict(
3540
)
3641
)
3742
return HandlingResult.success()
43+
44+
45+
class GetNetInfoLegacy(JsonCommand, CommandWithMessageHandling, MessageDict):
46+
"""Get network info command."""
47+
48+
NAME = "GetNetInfo"
49+
50+
@classmethod
51+
def _handle_dict(
52+
cls, event_bus: EventBus, message: 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+
NetworkInfoEvent(
60+
ip=message.get("wi", ""),
61+
ssid=message.get("s", ""),
62+
rssi=int(message.get("st", "0")),
63+
mac=message.get("wm", ""),
64+
)
65+
)
66+
return HandlingResult.success()

deebot_client/message.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,42 @@ def _handle(
140140
return super()._handle(event_bus, message)
141141

142142

143-
class MessageBody(Message, ABC):
143+
class MessageDict(Message, ABC):
144+
"""Dict message."""
145+
146+
@classmethod
147+
@abstractmethod
148+
def _handle_dict(
149+
cls, event_bus: EventBus, message: dict[str, Any]
150+
) -> HandlingResult:
151+
"""Handle string message and notify the correct event subscribers.
152+
153+
:return: A message response
154+
"""
155+
156+
@classmethod
157+
@_handle_error_or_analyse
158+
@final
159+
def __handle_dict(
160+
cls, event_bus: EventBus, message: dict[str, Any]
161+
) -> HandlingResult:
162+
return cls._handle_dict(event_bus, message)
163+
164+
@classmethod
165+
def _handle(
166+
cls, event_bus: EventBus, message: dict[str, Any] | str
167+
) -> HandlingResult:
168+
"""Handle message and notify the correct event subscribers.
169+
170+
:return: A message response
171+
"""
172+
if isinstance(message, dict):
173+
return cls.__handle_dict(event_bus, message)
174+
175+
return super()._handle(event_bus, message)
176+
177+
178+
class MessageBody(MessageDict, ABC):
144179
"""Dict message with body attribute."""
145180

146181
@classmethod
@@ -158,17 +193,17 @@ def __handle_body(cls, event_bus: EventBus, body: dict[str, Any]) -> HandlingRes
158193
return cls._handle_body(event_bus, body)
159194

160195
@classmethod
161-
def _handle(
162-
cls, event_bus: EventBus, message: dict[str, Any] | str
196+
def _handle_dict(
197+
cls, event_bus: EventBus, message: dict[str, Any]
163198
) -> HandlingResult:
164199
"""Handle message and notify the correct event subscribers.
165200
166201
:return: A message response
167202
"""
168-
if isinstance(message, dict):
203+
if "body" in message:
169204
return cls.__handle_body(event_bus, message["body"])
170205

171-
return super()._handle(event_bus, message)
206+
return super()._handle_dict(event_bus, message)
172207

173208

174209
class MessageBodyData(MessageBody, ABC):

tests/commands/json/test_network.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from deebot_client.commands.json import GetNetInfo
3+
from deebot_client.commands.json import GetNetInfo, GetNetInfoLegacy
44
from deebot_client.events import NetworkInfoEvent
55
from tests.commands.json import assert_command
66
from tests.helpers import (
@@ -9,7 +9,7 @@
99
)
1010

1111

12-
async def test_GetFanSpeed() -> None:
12+
async def test_GetNetInfo() -> None:
1313
json = get_request_json(
1414
get_success_body(
1515
{
@@ -28,3 +28,26 @@ async def test_GetFanSpeed() -> None:
2828
ip="192.168.1.100", ssid="WLAN", rssi=-61, mac="AA:BB:CC:DD:EE:FF"
2929
),
3030
)
31+
32+
33+
async def test_GetNetInfoLegacy() -> None:
34+
json = {
35+
"ret": "ok",
36+
"resp": {
37+
"ret": "ok",
38+
"s": "WLAN",
39+
"p": "",
40+
"wi": "192.168.1.100",
41+
"wm": "AA:BB:CC:DD:EE:FF",
42+
"st": "-61",
43+
},
44+
"id": "qvHH",
45+
"payloadType": "j",
46+
}
47+
await assert_command(
48+
GetNetInfoLegacy(),
49+
json,
50+
NetworkInfoEvent(
51+
ip="192.168.1.100", ssid="WLAN", rssi=-61, mac="AA:BB:CC:DD:EE:FF"
52+
),
53+
)

0 commit comments

Comments
 (0)