Skip to content

Commit 58e8343

Browse files
authored
Implement new API methods to get devices info (issue #651) (#662)
1 parent c1a0023 commit 58e8343

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

custom_components/smartthinq_sensors/wideq/core_async.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@
107107
_COMMON_LANG_URI_ID = "langPackCommonUri"
108108
_LOCAL_LANG_FILE = "local_lang_pack.json"
109109

110+
_HOME_ID = "homeId"
111+
_HOME_NAME = "homeName"
112+
_HOME_CURRENT = "currentHomeYn"
113+
110114
_LOGGER = logging.getLogger(__name__)
111115

112116

@@ -999,6 +1003,7 @@ def __init__(self, auth: Auth, session_id=0) -> None:
9991003
"""Initialize session object."""
10001004
self._auth = auth
10011005
self.session_id = session_id
1006+
self._homes: dict | None = None
10021007
self._common_lang_pack_url = None
10031008

10041009
@property
@@ -1064,11 +1069,75 @@ async def get2(self, path: str) -> dict:
10641069
self._auth.user_number,
10651070
)
10661071

1072+
async def _get_homes(self) -> dict | None:
1073+
"""Get a dict of homes associated with the user's account."""
1074+
if self._homes is not None:
1075+
return self._homes
1076+
1077+
homes = await self.get2("service/homes")
1078+
if not isinstance(homes, dict):
1079+
_LOGGER.warning("LG API return invalid homes information: '%s'", homes)
1080+
return None
1081+
1082+
_LOGGER.debug("Received homes: %s", homes)
1083+
loaded_homes = {}
1084+
homes_list = as_list(homes.get("item", []))
1085+
for home in homes_list:
1086+
if home_id := home.get(_HOME_ID):
1087+
loaded_homes[home_id] = {
1088+
_HOME_NAME: home.get(_HOME_NAME, "unamed home"),
1089+
_HOME_CURRENT: home.get(_HOME_CURRENT, "N"),
1090+
}
1091+
1092+
if loaded_homes:
1093+
self._homes = loaded_homes
1094+
return loaded_homes
1095+
1096+
async def _get_home_devices(self, home_id: str) -> list[dict] | None:
1097+
"""
1098+
Get a list of devices associated with the user's home_id.
1099+
Return information about the devices.
1100+
"""
1101+
dashboard = await self.get2(f"service/homes/{home_id}")
1102+
if not isinstance(dashboard, dict):
1103+
_LOGGER.warning(
1104+
"LG API return invalid devices information for home_id %s: '%s'",
1105+
home_id,
1106+
dashboard,
1107+
)
1108+
return None
1109+
1110+
if self._common_lang_pack_url is None:
1111+
if _COMMON_LANG_URI_ID in dashboard:
1112+
self._common_lang_pack_url = dashboard[_COMMON_LANG_URI_ID]
1113+
else:
1114+
self._common_lang_pack_url = self._auth.gateway.core.lang_pack_url
1115+
return as_list(dashboard.get("devices", []))
1116+
10671117
async def get_devices(self) -> list[dict] | None:
10681118
"""
10691119
Get a list of devices associated with the user's account.
10701120
Return information about the devices.
10711121
"""
1122+
if not (homes := await self._get_homes()):
1123+
_LOGGER.warning("Not possible to determinate a valid home_id")
1124+
return None
1125+
1126+
valid_home = False
1127+
devices_list = []
1128+
for home_id in homes:
1129+
if (devices := await self._get_home_devices(home_id)) is None:
1130+
continue
1131+
valid_home = True
1132+
devices_list.extend(devices)
1133+
1134+
return devices_list if valid_home else None
1135+
1136+
async def get_devices_dashboard(self) -> list[dict] | None:
1137+
"""
1138+
Get a list of devices associated with the user's account.
1139+
Return information about the devices based on old API call
1140+
"""
10721141
dashboard = await self.get2("service/application/dashboard")
10731142
if not isinstance(dashboard, dict):
10741143
_LOGGER.warning(

0 commit comments

Comments
 (0)