|
107 | 107 | _COMMON_LANG_URI_ID = "langPackCommonUri"
|
108 | 108 | _LOCAL_LANG_FILE = "local_lang_pack.json"
|
109 | 109 |
|
| 110 | +_HOME_ID = "homeId" |
| 111 | +_HOME_NAME = "homeName" |
| 112 | +_HOME_CURRENT = "currentHomeYn" |
| 113 | + |
110 | 114 | _LOGGER = logging.getLogger(__name__)
|
111 | 115 |
|
112 | 116 |
|
@@ -999,6 +1003,7 @@ def __init__(self, auth: Auth, session_id=0) -> None:
|
999 | 1003 | """Initialize session object."""
|
1000 | 1004 | self._auth = auth
|
1001 | 1005 | self.session_id = session_id
|
| 1006 | + self._homes: dict | None = None |
1002 | 1007 | self._common_lang_pack_url = None
|
1003 | 1008 |
|
1004 | 1009 | @property
|
@@ -1064,11 +1069,75 @@ async def get2(self, path: str) -> dict:
|
1064 | 1069 | self._auth.user_number,
|
1065 | 1070 | )
|
1066 | 1071 |
|
| 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 | + |
1067 | 1117 | async def get_devices(self) -> list[dict] | None:
|
1068 | 1118 | """
|
1069 | 1119 | Get a list of devices associated with the user's account.
|
1070 | 1120 | Return information about the devices.
|
1071 | 1121 | """
|
| 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 | + """ |
1072 | 1141 | dashboard = await self.get2("service/application/dashboard")
|
1073 | 1142 | if not isinstance(dashboard, dict):
|
1074 | 1143 | _LOGGER.warning(
|
|
0 commit comments