|
25 | 25 | FailedRequestException, |
26 | 26 | GatewayTimeoutException, |
27 | 27 | NoConnectionException, |
| 28 | + SetupException, |
28 | 29 | ) |
29 | 30 | from pymammotion.aliyun.model.dev_by_account_response import Device |
30 | 31 | from pymammotion.data.model import GenerateRouteInformation, HashList |
31 | | -from pymammotion.data.model.device import MowerInfo, MowingDevice |
| 32 | +from pymammotion.data.model.device import MowerInfo, MowingDevice, RTKDevice |
32 | 33 | from pymammotion.data.model.device_config import OperationSettings, create_path_order |
33 | 34 | from pymammotion.data.model.report_info import Maintain |
34 | 35 | from pymammotion.data.mqtt.event import DeviceNotificationEventParams, ThingEventMessage |
|
37 | 38 | from pymammotion.http.model.camera_stream import ( |
38 | 39 | StreamSubscriptionResponse, |
39 | 40 | ) |
40 | | -from pymammotion.http.model.http import ErrorInfo, Response |
| 41 | +from pymammotion.http.model.http import CheckDeviceVersion, ErrorInfo, Response |
| 42 | +from pymammotion.http.model.rtk import RTK |
41 | 43 | from pymammotion.mammotion.commands.mammotion_command import MammotionCommand |
42 | 44 | from pymammotion.mammotion.devices.mammotion import ( |
43 | 45 | ConnectionPreference, |
44 | 46 | Mammotion, |
45 | 47 | MammotionMixedDeviceManager, |
46 | 48 | ) |
| 49 | +from pymammotion.mammotion.devices.mammotion_cloud import MammotionCloud |
47 | 50 | from pymammotion.proto import RptAct, RptInfoType, SystemUpdateBufMsg |
48 | 51 | from pymammotion.utility.constant import WorkMode |
49 | 52 | from pymammotion.utility.device_type import DeviceType |
|
75 | 78 | REPORT_INTERVAL = timedelta(minutes=1) |
76 | 79 | DEVICE_VERSION_INTERVAL = timedelta(days=1) |
77 | 80 | MAP_INTERVAL = timedelta(minutes=30) |
| 81 | +RTK_INTERVAL = timedelta(hours=5) |
78 | 82 |
|
79 | 83 |
|
80 | 84 | class MammotionBaseUpdateCoordinator[DataT](DataUpdateCoordinator[DataT]): |
@@ -170,8 +174,8 @@ async def async_refresh_login(self) -> None: |
170 | 174 |
|
171 | 175 | async def device_offline(self, device: MammotionMixedDeviceManager) -> None: |
172 | 176 | device.state.online = False |
173 | | - if cloud := device.cloud(): |
174 | | - await cloud.stop() |
| 177 | + # if cloud := device.cloud(): |
| 178 | + # await cloud.stop() |
175 | 179 |
|
176 | 180 | loop = asyncio.get_running_loop() |
177 | 181 | loop.call_later(900, lambda: asyncio.create_task(self.clear_update_failures())) |
@@ -233,7 +237,7 @@ async def async_send_command(self, command: str, **kwargs: Any) -> bool | None: |
233 | 237 | try: |
234 | 238 | if ble := device.ble(): |
235 | 239 | # if we don't do this it will stay connected and no longer update over wifi |
236 | | - ble.set_disconnect_strategy(True) |
| 240 | + ble.set_disconnect_strategy(disconnect=True) |
237 | 241 | await ble.queue_command(command, **kwargs) |
238 | 242 |
|
239 | 243 | return True |
@@ -407,6 +411,23 @@ async def async_get_area_list(self) -> None: |
407 | 411 | """Mowing area List.""" |
408 | 412 | await self.async_send_command("get_area_name_list", device_id=self.device.iotId) |
409 | 413 |
|
| 414 | + async def async_relocate_charging_station(self): |
| 415 | + """Reset charging station.""" |
| 416 | + await self.async_send_command("delete_charge_point") |
| 417 | + # fetch charging location? |
| 418 | + """ |
| 419 | + nav { |
| 420 | + todev_get_commondata { |
| 421 | + pver: 1 |
| 422 | + subCmd: 2 |
| 423 | + action: 6 |
| 424 | + type: 5 |
| 425 | + totalFrame: 1 |
| 426 | + currentFrame: 1 |
| 427 | + } |
| 428 | + } |
| 429 | + """ |
| 430 | + |
410 | 431 | async def send_command_and_update(self, command_str: str, **kwargs: Any) -> None: |
411 | 432 | """Send command and update.""" |
412 | 433 | await self.async_send_command(command_str, **kwargs) |
@@ -963,7 +984,6 @@ async def _async_update_data(self): |
963 | 984 | if ( |
964 | 985 | len(device.state.map.hashlist) == 0 |
965 | 986 | or len(device.state.map.missing_hashlist()) > 0 |
966 | | - or len(device.state.map.plan) == 0 |
967 | 987 | ): |
968 | 988 | await self.manager.start_map_sync(self.device_name) |
969 | 989 |
|
@@ -1136,3 +1156,101 @@ async def _async_setup(self) -> None: |
1136 | 1156 | ) |
1137 | 1157 | except DeviceOfflineException: |
1138 | 1158 | """Device is offline bluetooth has been attempted.""" |
| 1159 | + |
| 1160 | + |
| 1161 | +class MammotionRTKCoordinator(DataUpdateCoordinator[RTKDevice]): |
| 1162 | + """Mammotion DataUpdateCoordinator.""" |
| 1163 | + |
| 1164 | + def __init__( |
| 1165 | + self, |
| 1166 | + hass: HomeAssistant, |
| 1167 | + config_entry: MammotionConfigEntry, |
| 1168 | + device: Device, |
| 1169 | + cloud: MammotionCloud, |
| 1170 | + ) -> None: |
| 1171 | + """Initialize global mammotion data updater.""" |
| 1172 | + super().__init__( |
| 1173 | + hass=hass, |
| 1174 | + logger=LOGGER, |
| 1175 | + name=DOMAIN, |
| 1176 | + update_interval=RTK_INTERVAL, |
| 1177 | + config_entry=config_entry, |
| 1178 | + ) |
| 1179 | + assert config_entry.unique_id |
| 1180 | + self.account = self.config_entry.data[CONF_ACCOUNTNAME] |
| 1181 | + self.password = self.config_entry.data[CONF_PASSWORD] |
| 1182 | + self.device: Device = device |
| 1183 | + self.device_name = device.deviceName |
| 1184 | + self.cloud: MammotionCloud = cloud |
| 1185 | + self.data: RTKDevice = RTKDevice( |
| 1186 | + name=self.device_name, |
| 1187 | + iot_id=self.device.iotId, |
| 1188 | + product_key=self.device.productKey, |
| 1189 | + ) |
| 1190 | + |
| 1191 | + async def _async_update_data(self): |
| 1192 | + """Update RTK data.""" |
| 1193 | + try: |
| 1194 | + response = await self.cloud.cloud_client.get_device_properties( |
| 1195 | + self.device.iotId |
| 1196 | + ) |
| 1197 | + if response.code == 200: |
| 1198 | + data = response.data |
| 1199 | + if ota_progress := data.otaProgress: |
| 1200 | + self.data.update_check = CheckDeviceVersion.from_dict( |
| 1201 | + ota_progress.value |
| 1202 | + ) |
| 1203 | + if network_info := data.networkInfo: |
| 1204 | + network = json.loads(network_info.value) |
| 1205 | + self.data.wifi_rssi = network["wifi_rssi"] |
| 1206 | + self.data.wifi_sta_mac = network["wifi_sta_mac"] |
| 1207 | + self.data.bt_mac = network["bt_mac"] |
| 1208 | + if coordinate := data.coordinate: |
| 1209 | + coord_val = json.loads(coordinate.value) |
| 1210 | + self.data.lat = coord_val["lat"] |
| 1211 | + self.data.lon = coord_val["lon"] |
| 1212 | + if device_version := data.deviceVersion: |
| 1213 | + self.data.device_version = device_version.value |
| 1214 | + self.data.online = True |
| 1215 | + |
| 1216 | + ota_info = ( |
| 1217 | + await self.cloud.cloud_client.mammotion_http.get_device_ota_firmware( |
| 1218 | + [self.data.iot_id] |
| 1219 | + ) |
| 1220 | + ) |
| 1221 | + if check_versions := ota_info.data: |
| 1222 | + for check_version in check_versions: |
| 1223 | + if check_version.device_id == self.data.iot_id: |
| 1224 | + self.data.update_check = check_version |
| 1225 | + return self.data |
| 1226 | + except SetupException: |
| 1227 | + """Cloud IOT Gateway is not setup.""" |
| 1228 | + return self.data |
| 1229 | + except DeviceOfflineException: |
| 1230 | + self.data.online = False |
| 1231 | + except GatewayTimeoutException: |
| 1232 | + """Gateway is timing out again.""" |
| 1233 | + return self.data |
| 1234 | + |
| 1235 | + async def _async_setup(self) -> None: |
| 1236 | + """Setup RTK data.""" |
| 1237 | + |
| 1238 | + rtk_response = await self.cloud.cloud_client.mammotion_http.get_rtk_devices() |
| 1239 | + if rtk_response.code == 0: |
| 1240 | + rtk_list = [ |
| 1241 | + rtk |
| 1242 | + for rtk in rtk_response.data |
| 1243 | + if self.device.deviceName == rtk.device_name |
| 1244 | + ] |
| 1245 | + try: |
| 1246 | + rtk_device: RTK = next(iter(rtk_list)) |
| 1247 | + self.data.lora_version = rtk_device.lora |
| 1248 | + except StopIteration: |
| 1249 | + """Failed to get RTK device.""" |
| 1250 | + return |
| 1251 | + |
| 1252 | + async def update_firmware(self, version: str) -> None: |
| 1253 | + """Update firmware.""" |
| 1254 | + await self.cloud.cloud_client.mammotion_http.start_ota_upgrade( |
| 1255 | + self.device.iotId, version |
| 1256 | + ) |
0 commit comments