Skip to content

Commit 746eec9

Browse files
authored
feat: added error check and deviceprop functionality for core (#42)
* feat: added update to deviceprop * feat: added time remaining to consumable * feat: added more exception checking * fix: linting * feat: add consumable const
1 parent 5261d37 commit 746eec9

File tree

5 files changed

+79
-11
lines changed

5 files changed

+79
-11
lines changed

roborock/api.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@
3737
UserData,
3838
WashTowelMode,
3939
)
40-
from .exceptions import RoborockException, RoborockTimeout, VacuumError
40+
from .exceptions import (
41+
RoborockAccountDoesNotExist,
42+
RoborockException,
43+
RoborockInvalidCode,
44+
RoborockInvalidEmail,
45+
RoborockTimeout,
46+
RoborockUrlException,
47+
VacuumError,
48+
)
4149
from .roborock_future import RoborockFuture
4250
from .roborock_message import RoborockMessage
4351
from .typing import DeviceProp, DockSummary, RoborockCommand
@@ -345,12 +353,15 @@ async def _get_base_url(self) -> str:
345353
params={"email": self._username, "needtwostepauth": "false"},
346354
)
347355
if response is None:
348-
raise RoborockException("get url by email returned None")
349-
if response.get("code") != 200:
350-
raise RoborockException(response.get("error"))
356+
raise RoborockUrlException("get url by email returned None")
357+
response_code = response.get("code")
358+
if response_code != 200:
359+
if response_code == 2003:
360+
raise RoborockInvalidEmail("Your email was incorrectly formatted.")
361+
raise RoborockUrlException(response.get("error"))
351362
response_data = response.get("data")
352363
if response_data is None:
353-
raise RoborockException("response does not have 'data'")
364+
raise RoborockUrlException("response does not have 'data'")
354365
self.base_url = response_data.get("url")
355366
return self.base_url
356367

@@ -375,8 +386,12 @@ async def request_code(self) -> None:
375386
)
376387
if code_response is None:
377388
raise RoborockException("Failed to get a response from send email code")
378-
if code_response.get("code") != 200:
379-
raise RoborockException(code_response.get("msg"))
389+
response_code = code_response.get("code")
390+
if response_code != 200:
391+
if response_code == 2008:
392+
raise RoborockAccountDoesNotExist("Account does not exist - check your login and try again.")
393+
else:
394+
raise RoborockException(f"{code_response.get('msg')} - response code: {code_response.get('code')}")
380395

381396
async def pass_login(self, password: str) -> UserData:
382397
base_url = await self._get_base_url()
@@ -395,7 +410,7 @@ async def pass_login(self, password: str) -> UserData:
395410
if login_response is None:
396411
raise RoborockException("Login response is none")
397412
if login_response.get("code") != 200:
398-
raise RoborockException(login_response.get("msg"))
413+
raise RoborockException(f"{login_response.get('msg')} - response code: {login_response.get('code')}")
399414
user_data = login_response.get("data")
400415
if not isinstance(user_data, dict):
401416
raise RoborockException("Got unexpected data type for user_data")
@@ -417,8 +432,11 @@ async def code_login(self, code) -> UserData:
417432
)
418433
if login_response is None:
419434
raise RoborockException("Login request response is None")
420-
if login_response.get("code") != 200:
421-
raise RoborockException(login_response.get("msg"))
435+
response_code = login_response.get("code")
436+
if response_code != 200:
437+
if response_code == 2018:
438+
raise RoborockInvalidCode("Invalid code - check your code and try again.")
439+
raise RoborockException(f"{login_response.get('msg')} - response code: {response_code}")
422440
user_data = login_response.get("data")
423441
if not isinstance(user_data, dict):
424442
raise RoborockException("Got unexpected data type for user_data")
@@ -439,7 +457,7 @@ async def get_home_data(self, user_data: UserData) -> HomeData:
439457
if home_id_response is None:
440458
raise RoborockException("home_id_response is None")
441459
if home_id_response.get("code") != 200:
442-
raise RoborockException(home_id_response.get("msg"))
460+
raise RoborockException(f"{home_id_response.get('msg')} - response code: {home_id_response.get('code')}")
443461

444462
home_id = home_id_response["data"].get("rrHomeId")
445463
timestamp = math.floor(time.time())

roborock/const.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Total time in seconds consumables have before Roborock recommends replacing
2+
MAIN_BRUSH_REPLACE_TIME = 1080000
3+
SIDE_BRUSH_REPLACE_TIME = 720000
4+
FILTER_REPLACE_TIME = 540000
5+
SENSOR_DIRTY_REPLACE_TIME = 108000

roborock/containers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
RoborockFanPowerCode,
2222
RoborockMopModeCode,
2323
)
24+
from .const import FILTER_REPLACE_TIME, MAIN_BRUSH_REPLACE_TIME, SENSOR_DIRTY_REPLACE_TIME, SIDE_BRUSH_REPLACE_TIME
2425

2526

2627
def camelize(s: str):
@@ -279,6 +280,20 @@ class Consumable(RoborockBase):
279280
strainer_work_times: Optional[int] = None
280281
dust_collection_work_times: Optional[int] = None
281282
cleaning_brush_work_times: Optional[int] = None
283+
main_brush_time_left: Optional[int] = None
284+
side_brush_time_left: Optional[int] = None
285+
filter_time_left: Optional[int] = None
286+
sensor_time_left: Optional[int] = None
287+
288+
def __post_init__(self):
289+
self.main_brush_time_left = (
290+
MAIN_BRUSH_REPLACE_TIME - self.main_brush_work_time if self.main_brush_work_time else None
291+
)
292+
self.side_brush_time_left = (
293+
SIDE_BRUSH_REPLACE_TIME - self.side_brush_work_time if self.side_brush_work_time else None
294+
)
295+
self.filter_time_left = FILTER_REPLACE_TIME - self.filter_work_time if self.filter_work_time else None
296+
self.sensor_time_left = SENSOR_DIRTY_REPLACE_TIME - self.sensor_dirty_time if self.sensor_dirty_time else None
282297

283298

284299
@dataclass

roborock/exceptions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,19 @@ class CommandVacuumError(RoborockException):
3636
def __init__(self, command: str, vacuum_error: VacuumError):
3737
self.message = f"{command}: {str(vacuum_error)}"
3838
super().__init__(self.message)
39+
40+
41+
class RoborockAccountDoesNotExist(RoborockException):
42+
"""Class for Roborock account does not exist exceptions."""
43+
44+
45+
class RoborockUrlException(RoborockException):
46+
"""Class for being unable to get the URL for the Roborock account."""
47+
48+
49+
class RoborockInvalidCode(RoborockException):
50+
"""Class for Roborock invalid code exceptions."""
51+
52+
53+
class RoborockInvalidEmail(RoborockException):
54+
"""Class for Roborock invalid formatted email exceptions."""

roborock/typing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,17 @@ class DeviceProp:
221221
consumable: Optional[Consumable] = None
222222
last_clean_record: Optional[CleanRecord] = None
223223
dock_summary: Optional[DockSummary] = None
224+
225+
def update(self, device_prop: DeviceProp) -> None:
226+
if device_prop.status:
227+
self.status = device_prop.status
228+
if device_prop.dnd_timer:
229+
self.dnd_timer = device_prop.dnd_timer
230+
if device_prop.clean_summary:
231+
self.clean_summary = device_prop.clean_summary
232+
if device_prop.consumable:
233+
self.consumable = device_prop.consumable
234+
if device_prop.last_clean_record:
235+
self.last_clean_record = device_prop.last_clean_record
236+
if device_prop.dock_summary:
237+
self.dock_summary = device_prop.dock_summary

0 commit comments

Comments
 (0)