diff --git a/sucks/__init__.py b/sucks/__init__.py index 1c01489..36ac356 100644 --- a/sucks/__init__.py +++ b/sucks/__init__.py @@ -461,11 +461,29 @@ def _ctl_to_dict(self, xml): result = xml.attrib.copy() if 'td' not in result: # This happens for commands with no response data, such as PlaySound - return - - result['event'] = result.pop('td') - if xml: - result.update(xml[0].attrib) + # Handle response data with no 'td' + + if 'type' in result: # single element with type and val + result['event'] = "LifeSpan" # seems to always be LifeSpan type + # don't need to update attrib since there's no child + + else: # non-'type' result with child element that has attribs + if len(xml) > 0: # case where there is child element + if 'clean' in xml[0].tag: + result['event'] = "CleanReport" + elif 'charge' in xml[0].tag: + result['event'] = "ChargeState" + elif 'battery' in xml[0].tag: + result['event'] = "BatteryInfo" + else: + return + result.update(xml[0].attrib) + else: # for non-'type' result with no child element, e.g., result of PlaySound + return + else: # response includes 'td' + result['event'] = result.pop('td') + if xml: + result.update(xml[0].attrib) for key in result: result[key] = stringcase.snakecase(result[key]) diff --git a/tests/test_ecovacs_xmpp.py b/tests/test_ecovacs_xmpp.py index ba79d4c..8e6b243 100644 --- a/tests/test_ecovacs_xmpp.py +++ b/tests/test_ecovacs_xmpp.py @@ -50,7 +50,22 @@ def test_xml_to_dict(): assert_dict_equal( x._ctl_to_dict(make_ctl('# ')), {'event': 'life_span', 'type': 'brush', 'val': '099', 'total': '365'}) + # set of xml responses without the td attrib + assert_dict_equal( + x._ctl_to_dict(make_ctl('')), + {'event': 'life_span', 'type': 'dust_case_heap', 'val': '050', 'total': '365'}) + + assert_dict_equal( + x._ctl_to_dict(make_ctl('')), + {'event': 'battery_info', 'power': '099', 'ret': 'ok', 'errno': ''}) + assert_dict_equal( + x._ctl_to_dict(make_ctl('')), + {'event': 'clean_report', 'type': 'stop', 'speed': 'strong', 'st':'h', 't': '', 'a': '', 'ret':'ok', 'errno': ''}) + + assert_dict_equal( + x._ctl_to_dict(make_ctl('')), + {'event': 'charge_state', 'type': 'slot_charging', 'ret': 'ok', 'errno': ''}) def make_ecovacs_xmpp(): return EcoVacsXMPP('20170101abcdefabcdefa', 'ecouser.net', 'abcdef12', 'A1b2C3d4efghijklmNOPQrstuvwxyz12', 'na')