Skip to content

Commit 28da7c2

Browse files
committed
handle responses without a 'td' attrib - closes wpietri#57
1 parent dde7544 commit 28da7c2

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

sucks/__init__.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ def subscribe_to_ctls(self, function):
452452

453453
def _handle_ctl(self, message):
454454
the_good_part = message.get_payload()[0][0]
455+
_LOGGER.debug('Message payload: ' + ET.tostring(the_good_part, method='xml').decode())
455456
as_dict = self._ctl_to_dict(the_good_part)
456457
if as_dict is not None:
457458
for s in self.ctl_subscribers:
@@ -461,11 +462,29 @@ def _ctl_to_dict(self, xml):
461462
result = xml.attrib.copy()
462463
if 'td' not in result:
463464
# This happens for commands with no response data, such as PlaySound
464-
return
465+
# Handle response data with no 'td'
465466

466-
result['event'] = result.pop('td')
467-
if xml:
468-
result.update(xml[0].attrib)
467+
# return
468+
if 'type' in result: # single element with type and val
469+
result['event'] = "LifeSpan" # seems to always ben LifeSpan type
470+
471+
else:
472+
if len(xml) > 0: # case where there is child element
473+
if 'clean' in xml[0].tag:
474+
result['event'] = "CleanReport"
475+
elif 'charge' in xml[0].tag:
476+
result['event'] = "ChargeState"
477+
elif 'battery' in xml[0].tag:
478+
result['event'] = "BatteryInfo"
479+
else:
480+
return
481+
result.update(xml[0].attrib)
482+
else: # for non-'type' result with no child element, e.g., result of PlaySound
483+
return
484+
else: # response includes 'td'
485+
result['event'] = result.pop('td')
486+
if xml:
487+
result.update(xml[0].attrib)
469488

470489
for key in result:
471490
result[key] = stringcase.snakecase(result[key])

0 commit comments

Comments
 (0)