From 1812a212e17965dbf4944731357695bb3570f256 Mon Sep 17 00:00:00 2001 From: Brian Martin Date: Sun, 20 Jan 2019 22:25:59 -0500 Subject: [PATCH] Add mqtt tests Add mqtt tests --- sucks/__init__.py | 4 +- tests/test_ecovacs_iot.py | 5 +-- tests/test_ecovacs_mqtt.py | 85 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 tests/test_ecovacs_mqtt.py diff --git a/sucks/__init__.py b/sucks/__init__.py index 3ce82ca..3fe9d17 100644 --- a/sucks/__init__.py +++ b/sucks/__init__.py @@ -814,7 +814,9 @@ class EcoVacsMQTT(ClientMQTT): result.update(xml[0].attrib) for key in result: - if not RepresentsInt(result[key]): #Fix to handle negative int values + if ',' in result[key]: #Seen in position updates + print(result[key]) + elif not RepresentsInt(result[key]): #Fix to handle negative int values result[key] = stringcase.snakecase(result[key]) return result diff --git a/tests/test_ecovacs_iot.py b/tests/test_ecovacs_iot.py index 04c2604..fbd5979 100644 --- a/tests/test_ecovacs_iot.py +++ b/tests/test_ecovacs_iot.py @@ -115,7 +115,4 @@ def make_ecovacs_iot(): eiotvacuum = d[0] eiotvacuum['resource'] = device_resource - return EcoVacsIOT('20170101abcdefabcdefa', 'ecouser.net', 'abcdef12', 'base64base64base64base64base64ba', 'na', eiotvacuum) - -def make_ctl(string): - return ET.fromstring('' + string + '')[0] + return EcoVacsIOT('20170101abcdefabcdefa', 'ecouser.net', 'abcdef12', 'base64base64base64base64base64ba', 'na', eiotvacuum) \ No newline at end of file diff --git a/tests/test_ecovacs_mqtt.py b/tests/test_ecovacs_mqtt.py new file mode 100644 index 0000000..8c567a4 --- /dev/null +++ b/tests/test_ecovacs_mqtt.py @@ -0,0 +1,85 @@ +from re import search + +from nose.tools import * + +from sucks import * +import paho.mqtt + +# There are few tests for the MQTT stuff here because it's relatively complicated to test given +# the library's design and its multithreaded nature and lack of explicit testing support. + +def test_subscribe_to_ctls(): + response = None + + def save_response(value): + nonlocal response + response = value + + x = make_ecovacs_mqtt() + + x.subscribe_to_ctls(save_response) + test_message = paho.mqtt.client.MQTTMessage + test_message.topic = 'iot/atr/CleanReport/%s/%s/%s/x'.format(x.vacuum['did'], x.vacuum['class'], x.vacuum['resource']) + test_message.payload = b"" + x._handle_ctl('','',test_message) + + assert_dict_equal(response, {'event': 'clean_report', 'ts':'1547824270099','type': 'auto','speed':'standard', 'st':'h','rsn':'a', 'a':'', 'l':'', 'sts':''}) + + +def test_xml_to_dict(): + x = make_ecovacs_mqtt() + + test_topic = 'iot/atr/CleanReport/%s/%s/%s/x'.format(x.vacuum['did'], x.vacuum['class'], x.vacuum['resource']) + assert_dict_equal( + x._ctl_to_dict(test_topic, ""), + {'event': 'clean_report', 'ts':'1547824270099','type': 'auto','speed':'standard', 'st':'h','rsn':'a', 'a':'', 'l':'', 'sts':''}) + + assert_dict_equal( + x._ctl_to_dict(test_topic, ""), + {'event': 'clean_report', 'ts':'1547824270099','type': 'auto','speed':'strong', 'st':'h','rsn':'a', 'a':'', 'l':'', 'sts':''}) + + test_topic = 'iot/atr/BatteryInfo/%s/%s/%s/x'.format(x.vacuum['did'], x.vacuum['class'], x.vacuum['resource']) + assert_dict_equal( + x._ctl_to_dict(test_topic, ""), + {'event': 'battery_info', 'ts':'1547823289924', 'power': '64'}) + + test_topic = 'iot/atr/SleepStatus/%s/%s/%s/x'.format(x.vacuum['did'], x.vacuum['class'], x.vacuum['resource']) + assert_dict_equal( + x._ctl_to_dict(test_topic, ""), + {'event': 'sleep_status', 'ts':'1547823129670', 'st': '1'}) + + test_topic = 'iot/atr/errors/%s/%s/%s/x'.format(x.vacuum['did'], x.vacuum['class'], x.vacuum['resource']) + assert_dict_equal( + x._ctl_to_dict(test_topic, ""), + {'event': 'errors', 'ts':'1547822982581','old':'','new':'102'}) + assert_dict_equal( + x._ctl_to_dict(test_topic, ""), + {'event': 'errors', 'ts':'1547822982581','old':'102','new':''}) + + test_topic = 'iot/atr/Pos/%s/%s/%s/x'.format(x.vacuum['did'], x.vacuum['class'], x.vacuum['resource']) + assert_dict_equal( + x._ctl_to_dict(test_topic, ""), + {'event': 'pos', 't':'p', 'p':'7,-10', 'a':'-42','valid':'0'}) + + test_topic = 'iot/atr/DustCaseST/%s/%s/%s/x'.format(x.vacuum['did'], x.vacuum['class'], x.vacuum['resource']) + assert_dict_equal( + x._ctl_to_dict(test_topic, ""), + {'event': 'dust_case_s_t', 'ts':'1547822871328','st':'1'}) + + test_topic = 'iot/atr/MapSt/%s/%s/%s/x'.format(x.vacuum['did'], x.vacuum['class'], x.vacuum['resource']) + assert_dict_equal( + x._ctl_to_dict(test_topic, ""), + {'event': 'map_st', 'ts':'1547823592934', 'st':'reloc_go_chg_start', 'method':'', 'info':''}) + + # #TODO: Find a way to check if string is b64 encoded + # test_topic = 'iot/atr/trace/%s/%s/%s/x'.format(x.vacuum['did'], x.vacuum['class'], x.vacuum['resource']) + # assert_dict_equal( + # x._ctl_to_dict(test_topic, ""), + # {'event': 'trace', 'trid':'227975', 'tf':'4', 'tr':'XQAABAAKAAAAAB4AMGAQCdAAAAA='}) + + + +def make_ecovacs_mqtt(bot=None): + if bot is None: + bot = bot = {"did": "E0000000001234567890", "class": "126","resource":"test_resource", "nick": "bob", "iot": True} + return EcoVacsMQTT('20170101abcdefabcdefa', 'ecouser.net', 'abcdef12', 'A1b2C3d4efghijklmNOPQrstuvwxyz12', 'na', bot)