Skip to content

Commit

Permalink
Manage wrong device info returned by API (issue #651) (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 authored Jan 1, 2024
1 parent 167e4b2 commit c1a0023
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
13 changes: 12 additions & 1 deletion custom_components/smartthinq_sensors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
raise ConfigEntryNotReady("ThinQ platform not ready") from exc

if discovered_devices is None:
raise ConfigEntryNotReady("ThinQ platform not ready: no devices found.")

# remove device not available anymore
dev_ids = [v for ids in discovered_devices.values() for v in ids]
cleanup_orphan_lge_devices(hass, entry.entry_id, dev_ids)
Expand Down Expand Up @@ -538,6 +541,14 @@ async def lge_devices_setup(

wrapped_devices: dict[DeviceType, list[LGEDevice]] = {}
unsupported_devices: dict[DeviceType, list[ThinQDeviceInfo]] = {}

if not client.has_devices:
await client.refresh_devices()

# if client device is None somenthing is wrong
if (client_devices := client.devices) is None:
return wrapped_devices, unsupported_devices, discovered_devices

new_devices = {}
if discovered_devices is None:
discovered_devices = {}
Expand All @@ -547,7 +558,7 @@ async def lge_devices_setup(
if hass.config.units.temperature_unit != UnitOfTemperature.CELSIUS:
temp_unit = TemperatureUnit.FAHRENHEIT

for device_info in client.devices:
for device_info in client_devices:
device_id = device_info.device_id
if device_id in discovered_devices:
new_devices[device_id] = discovered_devices[device_id]
Expand Down
25 changes: 22 additions & 3 deletions custom_components/smartthinq_sensors/wideq/core_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK"
)

_COMMON_LANG_URI_ID = "langPackCommonUri"
_LOCAL_LANG_FILE = "local_lang_pack.json"

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -170,6 +171,7 @@ def __init__(
self._language = language
self._timeout = aiohttp.ClientTimeout(total=timeout)
self._oauth_url = oauth_url
self._lang_pack_url = None

if session:
self._session = session
Expand All @@ -188,6 +190,11 @@ def language(self):
"""Return the used language."""
return self._language

@property
def lang_pack_url(self):
"""Return the used language."""
return self._lang_pack_url

async def close(self):
"""Close the managed session on exit."""
if self._managed_session and self._session:
Expand Down Expand Up @@ -401,6 +408,8 @@ async def get_oauth_url(self):
gateway_result = self._manage_lge_result(out)
_LOGGER.debug("Gateway info: %s", gateway_result)
self._oauth_url = gateway_result["oauthUri"]
if self._lang_pack_url is None and _COMMON_LANG_URI_ID in gateway_result:
self._lang_pack_url = gateway_result[_COMMON_LANG_URI_ID]
return self._oauth_url

async def gateway_info(self):
Expand Down Expand Up @@ -1055,14 +1064,22 @@ async def get2(self, path: str) -> dict:
self._auth.user_number,
)

async def get_devices(self) -> list[dict]:
async def get_devices(self) -> list[dict] | None:
"""
Get a list of devices associated with the user's account.
Return information about the devices.
"""
dashboard = await self.get2("service/application/dashboard")
if not isinstance(dashboard, dict):
_LOGGER.warning(
"LG API return invalid devices information: '%s'", dashboard
)
return None
if self._common_lang_pack_url is None:
self._common_lang_pack_url = dashboard.get("langPackCommonUri")
if _COMMON_LANG_URI_ID in dashboard:
self._common_lang_pack_url = dashboard[_COMMON_LANG_URI_ID]
else:
self._common_lang_pack_url = self._auth.gateway.core.lang_pack_url
return as_list(dashboard.get("item", []))

async def monitor_start(self, device_id):
Expand Down Expand Up @@ -1280,7 +1297,9 @@ def _load_emul_devices(self):
async def _load_devices(self, force_update: bool = False):
"""Load dict with available devices."""
if self._session and (self._devices is None or force_update):
new_devices = await self._session.get_devices()
if (new_devices := await self._session.get_devices()) is None:
self._devices = None
return
if self.emulation:
# for debug
if emul_device := self._load_emul_devices():
Expand Down

0 comments on commit c1a0023

Please sign in to comment.