Skip to content

Commit

Permalink
Use sub-queries by default (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlarrick authored Feb 26, 2023
1 parent 86fe8ef commit 50d87d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
49 changes: 27 additions & 22 deletions pykumo/py_kumo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def __init__(self, name, addr, cfg_json, timeouts=None, serial=None):
super().__init__(name, addr, cfg_json, timeouts, serial)

def _retrieve_attributes(
self, query_path: list[str], needed: list[str]) -> dict:
self, query_path: list[str], needed: list[str],
do_top_query: bool = False) -> dict:
""" Try to retrieve a base query, but in specific error conditions retrieve specific
needed attributes individually.
"""
Expand All @@ -52,10 +53,13 @@ def _retrieve_attributes(
base_query += '}' * (len(query_path) + 2)
query = base_query.encode('utf-8')
try:
response = self._request(query)
if (response.get('_api_error', "") == 'serializer_error' or
response.get('r') == '__no_memory'):
# Base query response is too long. Get the needed attributes.
response = None
if do_top_query:
response = self._request(query)
if (not response or
response.get('_api_error', "") == 'serializer_error' or
'__no_memory' in str(response)):
# Use individual attribute queries
response = {'r': {}}
for attribute in needed:
attr_query = base_query.replace(
Expand All @@ -65,8 +69,7 @@ def _retrieve_attributes(
response = merge(response, sub_response)
else:
_LOGGER.warning(
"Did not get %s from %s: %s",
attribute, base_query, str(sub_response))
f"{self._name}: Did not get {attribute} from {attr_query}: {sub_response}")
except Exception as e:
_LOGGER.warning(
"Exception fetching %s: %s", base_query, str(e))
Expand All @@ -90,21 +93,24 @@ def update_status(self):
self._status = raw_status['r']['indoorUnit']['status']
self._last_status_update = now
except KeyError as ke:
_LOGGER.warning("Error retrieving status: %s", str(ke))
_LOGGER.warning(f"{self._name}: Error retrieving status from {response}: {str(ke)}")
return False

self._sensors = []
query = ['sensors']
needed = [f'{s}' for s in range(POSSIBLE_SENSORS)]
response = self._retrieve_attributes(query, needed)
for s in range(POSSIBLE_SENSORS):
s_str = f'{s}'
query = ['sensors', s_str]
needed = ['uuid', 'humidity', 'temperature', 'battery', 'rssi', 'txPower']

response = self._retrieve_attributes(query, needed)

try:
for sensor in response['r']['sensors'].values():
try:
sensor = response['r']['sensors'][s_str]
if isinstance(sensor, dict) and sensor.get('uuid'):
self._sensors.append(sensor)
except KeyError as ke:
_LOGGER.warning("Error retrieving sensors: %s", str(ke))
return False
except KeyError as ke:
_LOGGER.warning(f"{self._name}: Error retrieving sensors from {response}: {str(ke)}")
return False

query = ['indoorUnit', 'profile']
needed = ['numberOfFanSpeeds', 'hasFanSpeedAuto', 'hasVaneSwing', 'hasModeDry',
Expand All @@ -116,7 +122,7 @@ def update_status(self):
try:
self._profile = response['r']['indoorUnit']['profile']
except KeyError as ke:
_LOGGER.warning(f"Error retrieving profile: %s", str(ke))
_LOGGER.warning(f"{self._name}: Error retrieving profile from {response}: {str(ke)}")
return False

# Edit profile with settings from adapter
Expand Down Expand Up @@ -144,13 +150,12 @@ def update_status(self):
self._profile['wifiRSSI'] = None
self._profile['runState'] = status.get('runState', "unknown")
except KeyError as ke:
_LOGGER.warning("Error retrieving adapter profile: %s", str(ke))
_LOGGER.warning(f"{self._name}: Error retrieving adapter profile from {response}: {str(ke)}")
return False

# Edit profile with data from MHK2 if present
query = ['mhk2', 'status']
needed = ['indoorHumid']
response = self._retrieve_attributes(query, needed)
query = '{"c":{"mhk2":{"status":{}}}}'.encode('utf-8')
response = self._request(query)
try:
self._mhk2 = response['r']['mhk2']
if isinstance(self._mhk2, dict):
Expand All @@ -169,7 +174,7 @@ def update_status(self):
self._sensors.append(mhk2_sensor_value)
except (KeyError, TypeError) as e:
# We don't bailout here since the MHK2 component is optional.
_LOGGER.info(f"Error retrieving MHK2 status: {e}")
_LOGGER.info(f"{self._name}: Error retrieving MHK2 status from {response}: {e}")
pass
return True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pykumo",
version="0.3.1",
version="0.3.2",
author="Doug Larrick",
author_email="[email protected]",
description="Small library for interfacing with Mitsubishi KumoCloud enabled devices",
Expand Down

0 comments on commit 50d87d1

Please sign in to comment.