|
6 | 6 | import secrets |
7 | 7 | import string |
8 | 8 | import time |
| 9 | +from collections.abc import Callable |
9 | 10 | from dataclasses import dataclass |
10 | 11 |
|
11 | 12 | import aiohttp |
@@ -737,23 +738,46 @@ class UserWebApiClient: |
737 | 738 | to avoid needing to pass UserData around and mock out the web API. |
738 | 739 | """ |
739 | 740 |
|
740 | | - def __init__(self, web_api: RoborockApiClient, user_data: UserData) -> None: |
| 741 | + def __init__( |
| 742 | + self, web_api: RoborockApiClient, user_data: UserData, unauthorized_hook: Callable[[], None] | None = None |
| 743 | + ) -> None: |
741 | 744 | """Initialize the wrapper with the API client and user data.""" |
742 | 745 | self._web_api = web_api |
743 | 746 | self._user_data = user_data |
| 747 | + self._unauthorized_hook = unauthorized_hook |
744 | 748 |
|
745 | 749 | async def get_home_data(self) -> HomeData: |
746 | 750 | """Fetch home data using the API client.""" |
747 | | - return await self._web_api.get_home_data_v3(self._user_data) |
| 751 | + try: |
| 752 | + return await self._web_api.get_home_data_v3(self._user_data) |
| 753 | + except RoborockInvalidCredentials: |
| 754 | + if self._unauthorized_hook: |
| 755 | + self._unauthorized_hook() |
| 756 | + raise |
748 | 757 |
|
749 | 758 | async def get_routines(self, device_id: str) -> list[HomeDataScene]: |
750 | 759 | """Fetch routines (scenes) for a specific device.""" |
751 | | - return await self._web_api.get_scenes(self._user_data, device_id) |
| 760 | + try: |
| 761 | + return await self._web_api.get_scenes(self._user_data, device_id) |
| 762 | + except RoborockInvalidCredentials: |
| 763 | + if self._unauthorized_hook: |
| 764 | + self._unauthorized_hook() |
| 765 | + raise |
752 | 766 |
|
753 | 767 | async def get_rooms(self) -> list[HomeDataRoom]: |
754 | 768 | """Fetch rooms using the API client.""" |
755 | | - return await self._web_api.get_rooms(self._user_data) |
| 769 | + try: |
| 770 | + return await self._web_api.get_rooms(self._user_data) |
| 771 | + except RoborockInvalidCredentials: |
| 772 | + if self._unauthorized_hook: |
| 773 | + self._unauthorized_hook() |
| 774 | + raise |
756 | 775 |
|
757 | 776 | async def execute_routine(self, scene_id: int) -> None: |
758 | 777 | """Execute a specific routine (scene) by its ID.""" |
759 | | - await self._web_api.execute_scene(self._user_data, scene_id) |
| 778 | + try: |
| 779 | + await self._web_api.execute_scene(self._user_data, scene_id) |
| 780 | + except RoborockInvalidCredentials: |
| 781 | + if self._unauthorized_hook: |
| 782 | + self._unauthorized_hook() |
| 783 | + raise |
0 commit comments