Skip to content

Commit

Permalink
Use homes API only when dashboard API fail
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 committed Jan 5, 2024
1 parent 8a69a74 commit cfb2859
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions custom_components/smartthinq_sensors/wideq/core_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ def __init__(self, auth: Auth, session_id=0) -> None:
self._auth = auth
self.session_id = session_id
self._homes: dict | None = None
self._use_homes = False
self._common_lang_pack_url = None

@property
Expand Down Expand Up @@ -1114,10 +1115,10 @@ async def _get_home_devices(self, home_id: str) -> list[dict] | None:
self._common_lang_pack_url = self._auth.gateway.core.lang_pack_url
return as_list(dashboard.get("devices", []))

async def get_devices(self) -> list[dict] | None:
async def get_devices_homes(self) -> list[dict] | None:
"""
Get a list of devices associated with the user's account.
Return information about the devices.
Return information about the devices based on homes API call.
"""
if not (homes := await self._get_homes()):
_LOGGER.warning("Not possible to determinate a valid home_id")
Expand All @@ -1136,12 +1137,12 @@ async def get_devices(self) -> list[dict] | None:
async def get_devices_dashboard(self) -> list[dict] | None:
"""
Get a list of devices associated with the user's account.
Return information about the devices based on old API call
Return information about the devices based on dashboard API call.
"""
dashboard = await self.get2("service/application/dashboard")
if not isinstance(dashboard, dict):
_LOGGER.warning(
"LG API return invalid devices information: '%s'", dashboard
"LG dashboard API return invalid devices information: '%s'", dashboard
)
return None
if self._common_lang_pack_url is None:
Expand All @@ -1151,6 +1152,18 @@ async def get_devices_dashboard(self) -> list[dict] | None:
self._common_lang_pack_url = self._auth.gateway.core.lang_pack_url
return as_list(dashboard.get("item", []))

async def get_devices(self) -> list[dict] | None:
"""
Get a list of devices associated with the user's account.
Return information about the devices.
"""
if not self._use_homes:
if (devices := await self.get_devices_dashboard()) is not None:
return devices
_LOGGER.info("Switching to homes method to retrieve devices")
self._use_homes = True
return await self.get_devices_homes()

async def monitor_start(self, device_id):
"""
Begin monitoring a device's status.
Expand Down

0 comments on commit cfb2859

Please sign in to comment.