Skip to content

Commit 962ffe2

Browse files
authored
feat: Add a device property to determine local connection status (#547)
1 parent 80f9149 commit 962ffe2

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

roborock/devices/channel.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def is_connected(self) -> bool:
2222
"""Return true if the channel is connected."""
2323
...
2424

25+
@property
26+
def is_local_connected(self) -> bool:
27+
"""Return true if the channel is connected locally."""
28+
...
29+
2530
async def subscribe(self, callback: Callable[[RoborockMessage], None]) -> Callable[[], None]:
2631
"""Subscribe to messages from the device."""
2732
...

roborock/devices/device.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ def is_connected(self) -> bool:
8787
"""Return whether the device is connected."""
8888
return self._channel.is_connected
8989

90+
@property
91+
def is_local_connected(self) -> bool:
92+
"""Return whether the device is connected locally.
93+
94+
This can be used to determine if the device is reachable over a local
95+
network connection, as opposed to a cloud connection. This is useful
96+
for adjusting behavior like polling frequency.
97+
"""
98+
return self._channel.is_local_connected
99+
90100
async def connect(self) -> None:
91101
"""Connect to the device using the appropriate protocol channel."""
92102
if self._unsub:

roborock/devices/local_channel.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ def is_connected(self) -> bool:
5656
"""Check if the channel is currently connected."""
5757
return self._is_connected
5858

59+
@property
60+
def is_local_connected(self) -> bool:
61+
"""Check if the channel is currently connected locally."""
62+
return self._is_connected
63+
5964
async def connect(self) -> None:
6065
"""Connect to the device."""
6166
if self._is_connected:

roborock/devices/mqtt_channel.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def is_connected(self) -> bool:
4040
"""
4141
return self._mqtt_session.connected
4242

43+
@property
44+
def is_local_connected(self) -> bool:
45+
"""Return true if the channel is connected locally."""
46+
return False
47+
4348
@property
4449
def _publish_topic(self) -> str:
4550
"""Topic to send commands to the device."""

tests/devices/test_mqtt_channel.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ async def test_publish_success(
134134
assert decoded_message.protocol == RoborockMessageProtocol.RPC_REQUEST
135135

136136

137+
@pytest.mark.parametrize(("connected"), [(True), (False)])
138+
async def test_connection_status(
139+
mqtt_session: Mock,
140+
mqtt_channel: MqttChannel,
141+
connected: bool,
142+
) -> None:
143+
"""Test successful RPC command sending and response handling."""
144+
mqtt_session.connected = connected
145+
assert mqtt_channel.is_connected is connected
146+
assert mqtt_channel.is_local_connected is False
147+
148+
137149
async def test_message_decode_error(
138150
mqtt_channel: MqttChannel,
139151
mqtt_message_handler: Callable[[bytes], None],

tests/devices/test_v1_device.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ async def test_device_connection(device: RoborockDevice, channel: AsyncMock) ->
8080
assert unsub.called
8181

8282

83+
@pytest.mark.parametrize(
84+
("connected", "local_connected"),
85+
[
86+
(True, False),
87+
(False, False),
88+
(True, True),
89+
(False, True),
90+
],
91+
)
92+
async def test_connection_status(
93+
device: RoborockDevice,
94+
channel: AsyncMock,
95+
connected: bool,
96+
local_connected: bool,
97+
) -> None:
98+
"""Test successful RPC command sending and response handling."""
99+
channel.is_connected = connected
100+
channel.is_local_connected = local_connected
101+
assert device.is_connected is connected
102+
assert device.is_local_connected is local_connected
103+
104+
83105
@pytest.fixture(name="setup_rpc_channel")
84106
def setup_rpc_channel_fixture(rpc_channel: AsyncMock, payload: pathlib.Path) -> AsyncMock:
85107
"""Fixture to set up the RPC channel for the tests."""

0 commit comments

Comments
 (0)