pymammotion 0.5.33__py3-none-any.whl → 0.5.40__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pymammotion might be problematic. Click here for more details.
- pymammotion/__init__.py +3 -3
- pymammotion/aliyun/cloud_gateway.py +106 -18
- pymammotion/aliyun/model/dev_by_account_response.py +198 -20
- pymammotion/data/model/device.py +1 -0
- pymammotion/data/model/device_config.py +1 -1
- pymammotion/data/model/enums.py +3 -1
- pymammotion/data/model/generate_route_information.py +2 -2
- pymammotion/data/model/hash_list.py +105 -33
- pymammotion/data/model/region_data.py +4 -4
- pymammotion/data/{state_manager.py → mower_state_manager.py} +17 -7
- pymammotion/homeassistant/__init__.py +3 -0
- pymammotion/homeassistant/mower_api.py +446 -0
- pymammotion/homeassistant/rtk_api.py +54 -0
- pymammotion/http/http.py +118 -7
- pymammotion/http/model/http.py +77 -2
- pymammotion/http/model/response_factory.py +10 -4
- pymammotion/mammotion/commands/mammotion_command.py +6 -0
- pymammotion/mammotion/commands/messages/navigation.py +10 -6
- pymammotion/mammotion/devices/__init__.py +27 -3
- pymammotion/mammotion/devices/base.py +16 -138
- pymammotion/mammotion/devices/mammotion.py +361 -204
- pymammotion/mammotion/devices/mammotion_bluetooth.py +7 -5
- pymammotion/mammotion/devices/mammotion_cloud.py +22 -74
- pymammotion/mammotion/devices/mammotion_mower_ble.py +49 -0
- pymammotion/mammotion/devices/mammotion_mower_cloud.py +39 -0
- pymammotion/mammotion/devices/managers/managers.py +81 -0
- pymammotion/mammotion/devices/mower_device.py +121 -0
- pymammotion/mammotion/devices/mower_manager.py +107 -0
- pymammotion/mammotion/devices/rtk_ble.py +89 -0
- pymammotion/mammotion/devices/rtk_cloud.py +113 -0
- pymammotion/mammotion/devices/rtk_device.py +50 -0
- pymammotion/mammotion/devices/rtk_manager.py +122 -0
- pymammotion/mqtt/__init__.py +2 -1
- pymammotion/mqtt/aliyun_mqtt.py +232 -0
- pymammotion/mqtt/mammotion_mqtt.py +132 -194
- pymammotion/mqtt/mqtt_models.py +66 -0
- pymammotion/proto/__init__.py +1 -1
- pymammotion/proto/mctrl_nav.proto +1 -1
- pymammotion/proto/mctrl_nav_pb2.py +1 -1
- pymammotion/proto/mctrl_nav_pb2.pyi +4 -4
- pymammotion/proto/mctrl_sys.proto +1 -1
- pymammotion/utility/device_type.py +88 -3
- pymammotion/utility/mur_mur_hash.py +132 -87
- {pymammotion-0.5.33.dist-info → pymammotion-0.5.40.dist-info}/METADATA +25 -31
- {pymammotion-0.5.33.dist-info → pymammotion-0.5.40.dist-info}/RECORD +54 -40
- {pymammotion-0.5.33.dist-info → pymammotion-0.5.40.dist-info}/WHEEL +1 -1
- {pymammotion-0.5.33.dist-info → pymammotion-0.5.40.dist-info/licenses}/LICENSE +0 -0
|
@@ -4,165 +4,142 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import asyncio
|
|
6
6
|
import logging
|
|
7
|
-
from typing import Any
|
|
7
|
+
from typing import TYPE_CHECKING, Any
|
|
8
8
|
|
|
9
|
-
from bleak
|
|
9
|
+
from bleak import BLEDevice
|
|
10
10
|
|
|
11
|
+
from pymammotion import MammotionMQTT
|
|
11
12
|
from pymammotion.aliyun.cloud_gateway import CloudIOTGateway
|
|
12
13
|
from pymammotion.aliyun.model.dev_by_account_response import Device
|
|
13
14
|
from pymammotion.data.model.device import MowingDevice
|
|
14
15
|
from pymammotion.data.model.enums import ConnectionPreference
|
|
15
|
-
from pymammotion.data.state_manager import StateManager
|
|
16
16
|
from pymammotion.http.http import MammotionHTTP
|
|
17
17
|
from pymammotion.http.model.camera_stream import StreamSubscriptionResponse, VideoResourceResponse
|
|
18
|
-
from pymammotion.http.model.http import Response
|
|
19
|
-
from pymammotion.mammotion.devices.
|
|
20
|
-
from pymammotion.mammotion.devices.
|
|
21
|
-
from pymammotion.
|
|
18
|
+
from pymammotion.http.model.http import DeviceRecord, Response
|
|
19
|
+
from pymammotion.mammotion.devices.mammotion_cloud import MammotionCloud
|
|
20
|
+
from pymammotion.mammotion.devices.mammotion_mower_ble import MammotionMowerBLEDevice
|
|
21
|
+
from pymammotion.mammotion.devices.managers.managers import AbstractDeviceManager
|
|
22
|
+
from pymammotion.mammotion.devices.mower_manager import MammotionMowerDeviceManager
|
|
23
|
+
from pymammotion.mammotion.devices.rtk_manager import MammotionRTKDeviceManager
|
|
24
|
+
from pymammotion.mqtt import AliyunMQTT
|
|
22
25
|
from pymammotion.utility.device_type import DeviceType
|
|
23
26
|
|
|
27
|
+
# RTK imports - imported here for type hints, full import in add_cloud_devices
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from pymammotion.mammotion.devices.rtk_ble import MammotionRTKBLEDevice
|
|
30
|
+
|
|
24
31
|
TIMEOUT_CLOUD_RESPONSE = 10
|
|
25
32
|
|
|
26
33
|
_LOGGER = logging.getLogger(__name__)
|
|
27
34
|
|
|
28
35
|
|
|
29
|
-
class
|
|
30
|
-
|
|
31
|
-
self,
|
|
32
|
-
name: str,
|
|
33
|
-
iot_id: str,
|
|
34
|
-
cloud_client: CloudIOTGateway,
|
|
35
|
-
cloud_device: Device,
|
|
36
|
-
ble_device: BLEDevice | None = None,
|
|
37
|
-
mqtt: MammotionCloud | None = None,
|
|
38
|
-
preference: ConnectionPreference = ConnectionPreference.BLUETOOTH,
|
|
39
|
-
) -> None:
|
|
40
|
-
self._ble_device: MammotionBaseBLEDevice | None = None
|
|
41
|
-
self._cloud_device: MammotionBaseCloudDevice | None = None
|
|
42
|
-
self.name = name
|
|
43
|
-
self.iot_id = iot_id
|
|
44
|
-
self.cloud_client = cloud_client
|
|
45
|
-
self._state_manager = StateManager(MowingDevice())
|
|
46
|
-
self._state_manager.get_device().name = name
|
|
47
|
-
self._device: Device = cloud_device
|
|
48
|
-
self.add_ble(ble_device) if ble_device else None
|
|
49
|
-
self.add_cloud(mqtt) if mqtt else None
|
|
50
|
-
self.mammotion_http = cloud_client.mammotion_http
|
|
51
|
-
self.preference = preference
|
|
52
|
-
self._state_manager.preference = preference
|
|
53
|
-
|
|
54
|
-
@property
|
|
55
|
-
def state_manager(self) -> StateManager:
|
|
56
|
-
"""Return the state manager."""
|
|
57
|
-
return self._state_manager
|
|
58
|
-
|
|
59
|
-
@property
|
|
60
|
-
def state(self):
|
|
61
|
-
"""Return the state of the device."""
|
|
62
|
-
return self._state_manager.get_device()
|
|
63
|
-
|
|
64
|
-
@state.setter
|
|
65
|
-
def state(self, value: MowingDevice) -> None:
|
|
66
|
-
self._state_manager.set_device(value)
|
|
67
|
-
|
|
68
|
-
def ble(self) -> MammotionBaseBLEDevice | None:
|
|
69
|
-
return self._ble_device
|
|
70
|
-
|
|
71
|
-
def cloud(self) -> MammotionBaseCloudDevice | None:
|
|
72
|
-
return self._cloud_device
|
|
73
|
-
|
|
74
|
-
def has_queued_commands(self) -> bool:
|
|
75
|
-
if self.has_cloud() and self.preference == ConnectionPreference.WIFI:
|
|
76
|
-
return not self.cloud().mqtt.command_queue.empty()
|
|
77
|
-
else:
|
|
78
|
-
return not self.ble().command_queue.empty()
|
|
79
|
-
|
|
80
|
-
def add_ble(self, ble_device: BLEDevice) -> MammotionBaseBLEDevice:
|
|
81
|
-
self._ble_device = MammotionBaseBLEDevice(
|
|
82
|
-
state_manager=self._state_manager, cloud_device=self._device, device=ble_device
|
|
83
|
-
)
|
|
84
|
-
return self._ble_device
|
|
36
|
+
class MammotionDeviceManager:
|
|
37
|
+
"""Manage devices - both mowers and RTK."""
|
|
85
38
|
|
|
86
|
-
def
|
|
87
|
-
self.
|
|
88
|
-
|
|
39
|
+
def __init__(self) -> None:
|
|
40
|
+
self.devices: dict[str, MammotionMowerDeviceManager] = {}
|
|
41
|
+
self.rtk_devices: dict[str, MammotionRTKDeviceManager] = {}
|
|
42
|
+
|
|
43
|
+
def _should_disconnect_mqtt(self, device_for_removal: AbstractDeviceManager) -> bool:
|
|
44
|
+
"""Check if MQTT connection should be disconnected.
|
|
45
|
+
|
|
46
|
+
Returns True if no other devices share the same MQTT connection.
|
|
47
|
+
"""
|
|
48
|
+
if not device_for_removal.cloud:
|
|
49
|
+
return False
|
|
50
|
+
|
|
51
|
+
mqtt_to_check = device_for_removal.cloud.mqtt
|
|
52
|
+
|
|
53
|
+
# Check if any mower device shares this MQTT connection
|
|
54
|
+
shared_devices: set[AbstractDeviceManager] = {
|
|
55
|
+
device
|
|
56
|
+
for device in self.devices.values()
|
|
57
|
+
if device.cloud is not None and device.cloud.mqtt == mqtt_to_check
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
# Also check RTK devices for shared MQTT
|
|
61
|
+
shared_devices.update(
|
|
62
|
+
{
|
|
63
|
+
device
|
|
64
|
+
for device in self.rtk_devices.values()
|
|
65
|
+
if device.cloud is not None and device.cloud.mqtt == mqtt_to_check
|
|
66
|
+
}
|
|
89
67
|
)
|
|
90
|
-
return self._cloud_device
|
|
91
|
-
|
|
92
|
-
def replace_cloud(self, cloud_device: MammotionBaseCloudDevice) -> None:
|
|
93
|
-
self._cloud_device = cloud_device
|
|
94
|
-
|
|
95
|
-
def remove_cloud(self) -> None:
|
|
96
|
-
self._state_manager.cloud_get_commondata_ack_callback = None
|
|
97
|
-
self._state_manager.cloud_get_hashlist_ack_callback = None
|
|
98
|
-
self._state_manager.cloud_get_plan_callback = None
|
|
99
|
-
self._state_manager.cloud_on_notification_callback = None
|
|
100
|
-
self._state_manager.cloud_gethash_ack_callback = None
|
|
101
|
-
self._cloud_device = None
|
|
102
68
|
|
|
103
|
-
|
|
104
|
-
self._ble_device = ble_device
|
|
69
|
+
return len(shared_devices) == 0
|
|
105
70
|
|
|
106
|
-
def
|
|
107
|
-
|
|
108
|
-
self.
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
self._cloud_device = MammotionBaseCloudDevice(mqtt, cloud_device=device, state_manager=self._state_manager)
|
|
117
|
-
|
|
118
|
-
def has_cloud(self) -> bool:
|
|
119
|
-
return self._cloud_device is not None
|
|
120
|
-
|
|
121
|
-
def has_ble(self) -> bool:
|
|
122
|
-
return self._ble_device is not None
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
class MammotionDeviceManager:
|
|
126
|
-
"""Manage devices."""
|
|
71
|
+
def add_device(self, mammotion_device: MammotionMowerDeviceManager) -> None:
|
|
72
|
+
"""Add a mower device."""
|
|
73
|
+
exists: MammotionMowerDeviceManager | None = self.devices.get(mammotion_device.name)
|
|
74
|
+
if exists is None:
|
|
75
|
+
self.devices[mammotion_device.name] = mammotion_device
|
|
76
|
+
return
|
|
77
|
+
if mammotion_device.cloud is not None:
|
|
78
|
+
exists.replace_cloud(mammotion_device.cloud)
|
|
79
|
+
if mammotion_device.ble:
|
|
80
|
+
exists.replace_ble(mammotion_device.ble)
|
|
127
81
|
|
|
128
|
-
def
|
|
129
|
-
|
|
82
|
+
def add_rtk_device(self, rtk_device: MammotionRTKDeviceManager) -> None:
|
|
83
|
+
"""Add an RTK device."""
|
|
130
84
|
|
|
131
|
-
|
|
132
|
-
"""Add a device."""
|
|
133
|
-
exists: MammotionMixedDeviceManager | None = self.devices.get(mammotion_device.name)
|
|
85
|
+
exists: MammotionRTKDeviceManager | None = self.rtk_devices.get(rtk_device.name)
|
|
134
86
|
if exists is None:
|
|
135
|
-
self.
|
|
87
|
+
self.rtk_devices[rtk_device.name] = rtk_device
|
|
136
88
|
return
|
|
137
|
-
if
|
|
138
|
-
exists.replace_cloud(
|
|
139
|
-
if
|
|
140
|
-
exists.replace_ble(
|
|
89
|
+
if rtk_device.cloud:
|
|
90
|
+
exists.replace_cloud(rtk_device.cloud)
|
|
91
|
+
if rtk_device.ble:
|
|
92
|
+
exists.replace_ble(rtk_device.ble)
|
|
141
93
|
|
|
142
94
|
def has_device(self, mammotion_device_name: str) -> bool:
|
|
95
|
+
"""Check if a mower device exists."""
|
|
143
96
|
if self.devices.get(mammotion_device_name, None) is not None:
|
|
144
97
|
return True
|
|
145
98
|
return False
|
|
146
99
|
|
|
147
|
-
def
|
|
100
|
+
def has_rtk_device(self, rtk_device_name: str) -> bool:
|
|
101
|
+
"""Check if an RTK device exists."""
|
|
102
|
+
if self.rtk_devices.get(rtk_device_name, None) is not None:
|
|
103
|
+
return True
|
|
104
|
+
return False
|
|
105
|
+
|
|
106
|
+
def get_device(self, mammotion_device_name: str) -> MammotionMowerDeviceManager:
|
|
107
|
+
"""Get a mower device."""
|
|
148
108
|
return self.devices[mammotion_device_name]
|
|
149
109
|
|
|
110
|
+
def get_rtk_device(self, rtk_device_name: str) -> MammotionRTKDeviceManager:
|
|
111
|
+
"""Get an RTK device."""
|
|
112
|
+
return self.rtk_devices[rtk_device_name]
|
|
113
|
+
|
|
150
114
|
async def remove_device(self, name: str) -> None:
|
|
151
|
-
"""Remove a device."""
|
|
115
|
+
"""Remove a mower device."""
|
|
152
116
|
if self.devices.get(name):
|
|
153
117
|
device_for_removal = self.devices.pop(name)
|
|
154
118
|
loop = asyncio.get_running_loop()
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
119
|
+
|
|
120
|
+
if device_for_removal.cloud:
|
|
121
|
+
if self._should_disconnect_mqtt(device_for_removal):
|
|
122
|
+
await loop.run_in_executor(None, device_for_removal.cloud.mqtt.disconnect)
|
|
123
|
+
await device_for_removal.cloud.stop()
|
|
124
|
+
|
|
125
|
+
if device_for_removal.ble:
|
|
126
|
+
await device_for_removal.ble.stop()
|
|
127
|
+
|
|
128
|
+
del device_for_removal
|
|
129
|
+
|
|
130
|
+
async def remove_rtk_device(self, name: str) -> None:
|
|
131
|
+
"""Remove an RTK device."""
|
|
132
|
+
if self.rtk_devices.get(name):
|
|
133
|
+
device_for_removal = self.rtk_devices.pop(name)
|
|
134
|
+
loop = asyncio.get_running_loop()
|
|
135
|
+
|
|
136
|
+
if device_for_removal.cloud:
|
|
137
|
+
if self._should_disconnect_mqtt(device_for_removal):
|
|
138
|
+
await loop.run_in_executor(None, device_for_removal.cloud.mqtt.disconnect)
|
|
139
|
+
await device_for_removal.cloud.stop()
|
|
140
|
+
|
|
141
|
+
if device_for_removal.ble:
|
|
142
|
+
await device_for_removal.ble.stop()
|
|
166
143
|
|
|
167
144
|
del device_for_removal
|
|
168
145
|
|
|
@@ -187,90 +164,262 @@ class Mammotion:
|
|
|
187
164
|
|
|
188
165
|
async def login_and_initiate_cloud(self, account, password, force: bool = False) -> None:
|
|
189
166
|
async with self._login_lock:
|
|
190
|
-
|
|
191
|
-
|
|
167
|
+
exists_aliyun: MammotionCloud | None = self.mqtt_list.get(f"{account}_aliyun")
|
|
168
|
+
exists_mammotion: MammotionCloud | None = self.mqtt_list.get(f"{account}_mammotion")
|
|
169
|
+
if (not exists_aliyun and not exists_mammotion) or force:
|
|
192
170
|
cloud_client = await self.login(account, password)
|
|
193
171
|
await self.initiate_cloud_connection(account, cloud_client)
|
|
194
172
|
|
|
195
|
-
async def refresh_login(self, account: str
|
|
173
|
+
async def refresh_login(self, account: str) -> None:
|
|
196
174
|
"""Refresh login."""
|
|
197
175
|
async with self._login_lock:
|
|
198
|
-
|
|
199
|
-
|
|
176
|
+
exists_aliyun: MammotionCloud | None = self.mqtt_list.get(f"{account}_aliyun")
|
|
177
|
+
exists_mammotion: MammotionCloud | None = self.mqtt_list.get(f"{account}_mammotion")
|
|
178
|
+
if not exists_aliyun and not exists_mammotion:
|
|
200
179
|
return
|
|
201
|
-
mammotion_http =
|
|
180
|
+
mammotion_http = (
|
|
181
|
+
exists_aliyun.cloud_client.mammotion_http
|
|
182
|
+
if exists_aliyun
|
|
183
|
+
else exists_mammotion.cloud_client.mammotion_http
|
|
184
|
+
)
|
|
202
185
|
await mammotion_http.refresh_login()
|
|
203
|
-
|
|
186
|
+
if len(mammotion_http.device_info) != 0:
|
|
187
|
+
await self.connect_iot(exists_aliyun.cloud_client)
|
|
188
|
+
if len(mammotion_http.device_records.records) != 0:
|
|
189
|
+
await mammotion_http.get_mqtt_credentials()
|
|
204
190
|
|
|
205
|
-
if not
|
|
191
|
+
if exists_aliyun and not exists_aliyun.is_connected():
|
|
206
192
|
loop = asyncio.get_running_loop()
|
|
207
|
-
await loop.run_in_executor(None,
|
|
193
|
+
await loop.run_in_executor(None, exists_aliyun.connect_async)
|
|
194
|
+
if exists_mammotion and not exists_mammotion.is_connected():
|
|
195
|
+
loop = asyncio.get_running_loop()
|
|
196
|
+
await loop.run_in_executor(None, exists_mammotion.connect_async)
|
|
197
|
+
|
|
198
|
+
@staticmethod
|
|
199
|
+
def shim_cloud_devices(devices: list[DeviceRecord]) -> list[Device]:
|
|
200
|
+
device_list: list[Device] = []
|
|
201
|
+
for device in devices:
|
|
202
|
+
device_list.append(
|
|
203
|
+
Device(
|
|
204
|
+
gmt_modified=0,
|
|
205
|
+
product_name="",
|
|
206
|
+
status=0,
|
|
207
|
+
net_type="NET_WIFI",
|
|
208
|
+
is_edge_gateway=False,
|
|
209
|
+
category_name="",
|
|
210
|
+
owned=1,
|
|
211
|
+
identity_alias="UNKNOW",
|
|
212
|
+
thing_type="DEVICE",
|
|
213
|
+
identity_id=device.identity_id,
|
|
214
|
+
device_name=device.device_name,
|
|
215
|
+
product_key=device.product_key,
|
|
216
|
+
iot_id=device.iot_id,
|
|
217
|
+
bind_time=device.bind_time,
|
|
218
|
+
node_type="DEVICE",
|
|
219
|
+
category_key="LawnMower",
|
|
220
|
+
)
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
return device_list
|
|
224
|
+
|
|
225
|
+
async def initiate_ble_connection(self, devices: dict[str, BLEDevice], cloud_devices: list[Device]) -> None:
|
|
226
|
+
"""Initiate BLE connection."""
|
|
227
|
+
for device in cloud_devices:
|
|
228
|
+
if ble_device := devices.get(device.device_name):
|
|
229
|
+
if device.device_name.startswith(("Luba-", "Yuka-")):
|
|
230
|
+
if not self.device_manager.has_device(device.device_name):
|
|
231
|
+
self.device_manager.add_device(
|
|
232
|
+
MammotionMowerDeviceManager(
|
|
233
|
+
name=device.device_name,
|
|
234
|
+
iot_id=device.iot_id,
|
|
235
|
+
cloud_device=device,
|
|
236
|
+
ble_device=ble_device,
|
|
237
|
+
preference=ConnectionPreference.BLUETOOTH,
|
|
238
|
+
cloud_client=CloudIOTGateway(MammotionHTTP()),
|
|
239
|
+
)
|
|
240
|
+
)
|
|
241
|
+
else:
|
|
242
|
+
self.device_manager.get_device(device.device_name).add_ble(ble_device)
|
|
243
|
+
if device.device_name.startswith(("RTK", "RBS")):
|
|
244
|
+
if not self.device_manager.has_rtk_device(device.device_name):
|
|
245
|
+
self.device_manager.add_rtk_device(
|
|
246
|
+
MammotionRTKDeviceManager(
|
|
247
|
+
name=device.device_name,
|
|
248
|
+
iot_id=device.iot_id,
|
|
249
|
+
cloud_device=device,
|
|
250
|
+
ble_device=ble_device,
|
|
251
|
+
preference=ConnectionPreference.BLUETOOTH,
|
|
252
|
+
cloud_client=CloudIOTGateway(MammotionHTTP()),
|
|
253
|
+
)
|
|
254
|
+
)
|
|
255
|
+
else:
|
|
256
|
+
self.device_manager.get_rtk_device(device.device_name).add_ble(ble_device)
|
|
208
257
|
|
|
209
258
|
async def initiate_cloud_connection(self, account: str, cloud_client: CloudIOTGateway) -> None:
|
|
210
259
|
"""Initiate cloud connection."""
|
|
211
260
|
loop = asyncio.get_running_loop()
|
|
212
|
-
|
|
261
|
+
|
|
262
|
+
mammotion_http = cloud_client.mammotion_http
|
|
263
|
+
|
|
264
|
+
if mqtt := self.mqtt_list.get(f"{account}_aliyun"):
|
|
213
265
|
if mqtt.is_connected():
|
|
214
266
|
await loop.run_in_executor(None, mqtt.disconnect)
|
|
215
267
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
product_key=cloud_client.aep_response.data.productKey,
|
|
220
|
-
device_name=cloud_client.aep_response.data.deviceName,
|
|
221
|
-
device_secret=cloud_client.aep_response.data.deviceSecret,
|
|
222
|
-
iot_token=cloud_client.session_by_authcode_response.data.iotToken,
|
|
223
|
-
client_id=cloud_client.client_id,
|
|
224
|
-
cloud_client=cloud_client,
|
|
225
|
-
),
|
|
226
|
-
cloud_client,
|
|
227
|
-
)
|
|
228
|
-
self.mqtt_list[account] = mammotion_cloud
|
|
229
|
-
self.add_cloud_devices(mammotion_cloud)
|
|
268
|
+
if mqtt := self.mqtt_list.get(f"{account}_mammotion"):
|
|
269
|
+
if mqtt.is_connected():
|
|
270
|
+
await loop.run_in_executor(None, mqtt.disconnect)
|
|
230
271
|
|
|
231
|
-
|
|
272
|
+
if len(mammotion_http.device_info) != 0:
|
|
273
|
+
mammotion_cloud = MammotionCloud(
|
|
274
|
+
AliyunMQTT(
|
|
275
|
+
region_id=cloud_client.region_response.data.regionId,
|
|
276
|
+
product_key=cloud_client.aep_response.data.productKey,
|
|
277
|
+
device_name=cloud_client.aep_response.data.deviceName,
|
|
278
|
+
device_secret=cloud_client.aep_response.data.deviceSecret,
|
|
279
|
+
iot_token=cloud_client.session_by_authcode_response.data.iotToken,
|
|
280
|
+
client_id=cloud_client.client_id,
|
|
281
|
+
cloud_client=cloud_client,
|
|
282
|
+
),
|
|
283
|
+
cloud_client,
|
|
284
|
+
)
|
|
285
|
+
self.mqtt_list[f"{account}_aliyun"] = mammotion_cloud
|
|
286
|
+
self.add_cloud_devices(mammotion_cloud)
|
|
287
|
+
|
|
288
|
+
await loop.run_in_executor(None, self.mqtt_list[f"{account}_aliyun"].connect_async)
|
|
289
|
+
if len(mammotion_http.device_records.records) != 0:
|
|
290
|
+
mammotion_cloud = MammotionCloud(
|
|
291
|
+
MammotionMQTT(
|
|
292
|
+
product_key=cloud_client.aep_response.data.productKey,
|
|
293
|
+
device_name=cloud_client.aep_response.data.deviceName,
|
|
294
|
+
mammotion_http=mammotion_http,
|
|
295
|
+
mqtt_connection=mammotion_http.mqtt_credentials,
|
|
296
|
+
),
|
|
297
|
+
cloud_client,
|
|
298
|
+
)
|
|
299
|
+
self.mqtt_list[f"{account}_mammotion"] = mammotion_cloud
|
|
300
|
+
self.add_mammotion_devices(mammotion_cloud, mammotion_http.device_records.records)
|
|
301
|
+
|
|
302
|
+
await loop.run_in_executor(None, self.mqtt_list[f"{account}_mammotion"].connect_async)
|
|
303
|
+
|
|
304
|
+
def add_mammotion_devices(self, mqtt_client: MammotionCloud, devices: list[DeviceRecord]) -> None:
|
|
305
|
+
"""Add devices from mammotion cloud."""
|
|
306
|
+
for device in devices:
|
|
307
|
+
if device.device_name.startswith(("Luba-", "Yuka-")):
|
|
308
|
+
has_device = self.device_manager.has_device(device.device_name)
|
|
309
|
+
if has_device:
|
|
310
|
+
mower_device = self.device_manager.get_device(device.device_name)
|
|
311
|
+
if mower_device.cloud is None:
|
|
312
|
+
mower_device.add_cloud(mqtt=mqtt_client)
|
|
313
|
+
else:
|
|
314
|
+
mower_device.replace_mqtt(mqtt_client)
|
|
315
|
+
|
|
316
|
+
else:
|
|
317
|
+
cloud_device_shim = Device(
|
|
318
|
+
gmt_modified=0,
|
|
319
|
+
product_name="",
|
|
320
|
+
status=0,
|
|
321
|
+
net_type="NET_WIFI",
|
|
322
|
+
is_edge_gateway=False,
|
|
323
|
+
category_name="",
|
|
324
|
+
owned=1,
|
|
325
|
+
identity_alias="UNKNOW",
|
|
326
|
+
thing_type="DEVICE",
|
|
327
|
+
identity_id=device.identity_id,
|
|
328
|
+
device_name=device.device_name,
|
|
329
|
+
product_key=device.product_key,
|
|
330
|
+
iot_id=device.iot_id,
|
|
331
|
+
bind_time=device.bind_time,
|
|
332
|
+
node_type="DEVICE",
|
|
333
|
+
category_key="LawnMower",
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
mixed_device = MammotionMowerDeviceManager(
|
|
337
|
+
name=device.device_name,
|
|
338
|
+
iot_id=device.iot_id,
|
|
339
|
+
cloud_client=mqtt_client.cloud_client,
|
|
340
|
+
cloud_device=cloud_device_shim,
|
|
341
|
+
mqtt=mqtt_client,
|
|
342
|
+
preference=ConnectionPreference.WIFI,
|
|
343
|
+
)
|
|
344
|
+
mixed_device.state.mower_state.product_key = device.product_key
|
|
345
|
+
self.device_manager.add_device(mixed_device)
|
|
232
346
|
|
|
233
347
|
def add_cloud_devices(self, mqtt_client: MammotionCloud) -> None:
|
|
234
|
-
"""Add devices from cloud."""
|
|
348
|
+
"""Add devices from cloud - both mowers and RTK."""
|
|
349
|
+
from pymammotion.mammotion.devices.rtk_manager import MammotionRTKDeviceManager
|
|
350
|
+
|
|
235
351
|
for device in mqtt_client.cloud_client.devices_by_account_response.data.data:
|
|
236
|
-
|
|
237
|
-
if device.
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
mower_device.
|
|
352
|
+
# Handle mower devices (Luba, Yuka)
|
|
353
|
+
if device.device_name.startswith(("Luba-", "Yuka-")):
|
|
354
|
+
has_device = self.device_manager.has_device(device.device_name)
|
|
355
|
+
if not has_device:
|
|
356
|
+
mixed_device = MammotionMowerDeviceManager(
|
|
357
|
+
name=device.device_name,
|
|
358
|
+
iot_id=device.iot_id,
|
|
359
|
+
cloud_client=mqtt_client.cloud_client,
|
|
360
|
+
cloud_device=device,
|
|
361
|
+
mqtt=mqtt_client,
|
|
362
|
+
preference=ConnectionPreference.WIFI,
|
|
363
|
+
)
|
|
364
|
+
mixed_device.state.mower_state.product_key = device.product_key
|
|
365
|
+
mixed_device.state.mower_state.model = (
|
|
366
|
+
device.product_name if device.product_model is None else device.product_model
|
|
367
|
+
)
|
|
368
|
+
self.device_manager.add_device(mixed_device)
|
|
369
|
+
else:
|
|
370
|
+
mower_device = self.device_manager.get_device(device.device_name)
|
|
371
|
+
if mower_device.cloud is None:
|
|
372
|
+
mower_device.add_cloud(mqtt=mqtt_client)
|
|
373
|
+
else:
|
|
374
|
+
mower_device.replace_mqtt(mqtt_client)
|
|
375
|
+
|
|
376
|
+
# Handle RTK devices
|
|
377
|
+
elif device.device_name.startswith(("RTK", "RBS")):
|
|
378
|
+
has_rtk_device = self.device_manager.has_rtk_device(device.device_name)
|
|
379
|
+
if not has_rtk_device:
|
|
380
|
+
rtk_device = MammotionRTKDeviceManager(
|
|
381
|
+
name=device.device_name,
|
|
382
|
+
iot_id=device.iot_id,
|
|
383
|
+
cloud_client=mqtt_client.cloud_client,
|
|
384
|
+
cloud_device=device,
|
|
385
|
+
mqtt=mqtt_client,
|
|
386
|
+
preference=ConnectionPreference.WIFI,
|
|
387
|
+
)
|
|
388
|
+
self.device_manager.add_rtk_device(rtk_device)
|
|
255
389
|
else:
|
|
256
|
-
|
|
390
|
+
rtk_device = self.device_manager.get_rtk_device(device.device_name)
|
|
391
|
+
if rtk_device.cloud is None:
|
|
392
|
+
rtk_device.add_cloud(mqtt=mqtt_client)
|
|
393
|
+
else:
|
|
394
|
+
rtk_device.replace_mqtt(mqtt_client)
|
|
257
395
|
|
|
258
396
|
def set_disconnect_strategy(self, *, disconnect: bool) -> None:
|
|
397
|
+
"""Set disconnect strategy for all BLE devices (mowers and RTK)."""
|
|
259
398
|
for device in self.device_manager.devices.values():
|
|
260
|
-
if device.ble
|
|
261
|
-
ble_device:
|
|
399
|
+
if device.ble is not None:
|
|
400
|
+
ble_device: MammotionMowerBLEDevice = device.ble
|
|
262
401
|
ble_device.set_disconnect_strategy(disconnect=disconnect)
|
|
263
402
|
|
|
403
|
+
for rtk_device in self.device_manager.rtk_devices.values():
|
|
404
|
+
if rtk_device.ble is not None:
|
|
405
|
+
ble_rtk_device: MammotionRTKBLEDevice = rtk_device.ble
|
|
406
|
+
ble_rtk_device.set_disconnect_strategy(disconnect=disconnect)
|
|
407
|
+
|
|
264
408
|
async def login(self, account: str, password: str) -> CloudIOTGateway:
|
|
265
409
|
"""Login to mammotion cloud."""
|
|
266
410
|
mammotion_http = MammotionHTTP()
|
|
267
|
-
cloud_client = CloudIOTGateway(mammotion_http)
|
|
268
411
|
await mammotion_http.login(account, password)
|
|
269
|
-
await
|
|
412
|
+
await mammotion_http.get_user_device_page()
|
|
413
|
+
device_list = await mammotion_http.get_user_device_list()
|
|
414
|
+
await mammotion_http.get_mqtt_credentials()
|
|
415
|
+
cloud_client = CloudIOTGateway(mammotion_http)
|
|
416
|
+
if len(device_list.data or []) != 0:
|
|
417
|
+
await self.connect_iot(cloud_client)
|
|
270
418
|
return cloud_client
|
|
271
419
|
|
|
272
420
|
@staticmethod
|
|
273
421
|
async def connect_iot(cloud_client: CloudIOTGateway) -> None:
|
|
422
|
+
"""Connect to aliyun cloud and fetch device info."""
|
|
274
423
|
mammotion_http = cloud_client.mammotion_http
|
|
275
424
|
country_code = mammotion_http.login_info.userInformation.domainAbbreviation
|
|
276
425
|
if cloud_client.region_response is None:
|
|
@@ -282,21 +431,35 @@ class Mammotion:
|
|
|
282
431
|
await cloud_client.list_binding_by_account()
|
|
283
432
|
|
|
284
433
|
async def remove_device(self, name: str) -> None:
|
|
434
|
+
"""Remove a mower device."""
|
|
285
435
|
await self.device_manager.remove_device(name)
|
|
286
436
|
|
|
287
|
-
def
|
|
437
|
+
async def remove_rtk_device(self, name: str) -> None:
|
|
438
|
+
"""Remove an RTK device."""
|
|
439
|
+
await self.device_manager.remove_rtk_device(name)
|
|
440
|
+
|
|
441
|
+
def get_device_by_name(self, name: str) -> MammotionMowerDeviceManager:
|
|
442
|
+
"""Get a mower device by name."""
|
|
288
443
|
return self.device_manager.get_device(name)
|
|
289
444
|
|
|
290
|
-
def
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
445
|
+
def get_rtk_device_by_name(self, name: str) -> MammotionRTKDeviceManager:
|
|
446
|
+
"""Get an RTK device by name."""
|
|
447
|
+
return self.device_manager.get_rtk_device(name)
|
|
448
|
+
|
|
449
|
+
def get_or_create_device_by_name(
|
|
450
|
+
self, device: Device, mqtt_client: MammotionCloud | None, ble_device: BLEDevice | None
|
|
451
|
+
) -> MammotionMowerDeviceManager:
|
|
452
|
+
"""Get or create a mower device by name."""
|
|
453
|
+
if self.device_manager.has_device(device.device_name):
|
|
454
|
+
return self.device_manager.get_device(device.device_name)
|
|
455
|
+
mow_device = MammotionMowerDeviceManager(
|
|
456
|
+
name=device.device_name,
|
|
457
|
+
iot_id=device.iot_id,
|
|
458
|
+
cloud_client=mqtt_client.cloud_client if mqtt_client else CloudIOTGateway(MammotionHTTP()),
|
|
297
459
|
mqtt=mqtt_client,
|
|
298
460
|
cloud_device=device,
|
|
299
|
-
ble_device=
|
|
461
|
+
ble_device=ble_device,
|
|
462
|
+
preference=ConnectionPreference.WIFI if mqtt_client else ConnectionPreference.BLUETOOTH,
|
|
300
463
|
)
|
|
301
464
|
self.device_manager.add_device(mow_device)
|
|
302
465
|
return mow_device
|
|
@@ -305,10 +468,10 @@ class Mammotion:
|
|
|
305
468
|
"""Send a command to the device."""
|
|
306
469
|
device = self.get_device_by_name(name)
|
|
307
470
|
if device:
|
|
308
|
-
if device.preference is ConnectionPreference.BLUETOOTH and device.
|
|
309
|
-
return await device.ble
|
|
310
|
-
if device.preference is ConnectionPreference.WIFI:
|
|
311
|
-
return await device.cloud
|
|
471
|
+
if device.preference is ConnectionPreference.BLUETOOTH and device.ble:
|
|
472
|
+
return await device.ble.command(key)
|
|
473
|
+
if device.preference is ConnectionPreference.WIFI and device.cloud:
|
|
474
|
+
return await device.cloud.command(key)
|
|
312
475
|
# TODO work with both with EITHER
|
|
313
476
|
return None
|
|
314
477
|
|
|
@@ -316,34 +479,26 @@ class Mammotion:
|
|
|
316
479
|
"""Send a command with args to the device."""
|
|
317
480
|
device = self.get_device_by_name(name)
|
|
318
481
|
if device:
|
|
319
|
-
if device.preference is ConnectionPreference.BLUETOOTH and device.
|
|
320
|
-
return await device.ble
|
|
321
|
-
if device.preference is ConnectionPreference.WIFI:
|
|
322
|
-
return await device.cloud().command(key, **kwargs)
|
|
323
|
-
# TODO work with both with EITHER
|
|
324
|
-
return None
|
|
325
|
-
|
|
326
|
-
async def start_sync(self, name: str, retry: int):
|
|
327
|
-
device = self.get_device_by_name(name)
|
|
328
|
-
if device:
|
|
329
|
-
if device.preference is ConnectionPreference.BLUETOOTH and device.has_ble():
|
|
330
|
-
return await device.ble().start_sync(retry)
|
|
482
|
+
if device.preference is ConnectionPreference.BLUETOOTH and device.ble:
|
|
483
|
+
return await device.ble.command(key, **kwargs)
|
|
331
484
|
if device.preference is ConnectionPreference.WIFI:
|
|
332
|
-
return await device.cloud
|
|
485
|
+
return await device.cloud.command(key, **kwargs)
|
|
333
486
|
# TODO work with both with EITHER
|
|
334
487
|
return None
|
|
335
488
|
|
|
336
489
|
async def start_map_sync(self, name: str):
|
|
490
|
+
"""Start map sync."""
|
|
337
491
|
device = self.get_device_by_name(name)
|
|
338
492
|
if device:
|
|
339
|
-
if device.preference is ConnectionPreference.BLUETOOTH and device.
|
|
340
|
-
return await device.ble
|
|
341
|
-
if device.preference is ConnectionPreference.WIFI:
|
|
342
|
-
return await device.cloud
|
|
493
|
+
if device.preference is ConnectionPreference.BLUETOOTH and device.ble:
|
|
494
|
+
return await device.ble.start_map_sync()
|
|
495
|
+
if device.preference is ConnectionPreference.WIFI and device.cloud:
|
|
496
|
+
return await device.cloud.start_map_sync()
|
|
343
497
|
# TODO work with both with EITHER
|
|
344
498
|
return None
|
|
345
499
|
|
|
346
500
|
async def get_stream_subscription(self, name: str, iot_id: str) -> Response[StreamSubscriptionResponse] | Any:
|
|
501
|
+
"""Get stream subscription."""
|
|
347
502
|
device = self.get_device_by_name(name)
|
|
348
503
|
if DeviceType.is_mini_or_x_series(name):
|
|
349
504
|
_stream_response = await device.mammotion_http.get_stream_subscription_mini_or_x_series(
|
|
@@ -357,6 +512,7 @@ class Mammotion:
|
|
|
357
512
|
return _stream_response
|
|
358
513
|
|
|
359
514
|
async def get_video_resource(self, name: str, iot_id: str) -> Response[VideoResourceResponse] | None:
|
|
515
|
+
"""Get video resource."""
|
|
360
516
|
device = self.get_device_by_name(name)
|
|
361
517
|
|
|
362
518
|
if DeviceType.is_mini_or_x_series(name):
|
|
@@ -366,6 +522,7 @@ class Mammotion:
|
|
|
366
522
|
return None
|
|
367
523
|
|
|
368
524
|
def mower(self, name: str) -> MowingDevice | None:
|
|
525
|
+
"""Get a mower device by name."""
|
|
369
526
|
device = self.get_device_by_name(name)
|
|
370
527
|
if device:
|
|
371
528
|
return device.state
|