Fix custom commands
Fix for custom commands with multiple inner tags Updated tests
This commit is contained in:
+21
-14
@@ -115,9 +115,9 @@ COMPONENT_FROM_ECOVACS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def str_to_bool(s):
|
def str_to_bool(s):
|
||||||
if s == 'True':
|
if s == 'True' or s == True:
|
||||||
return True
|
return True
|
||||||
elif s == 'False':
|
elif s == 'False' or s == False:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
raise ValueError("Cannot covert {} to a bool".format(s))
|
raise ValueError("Cannot covert {} to a bool".format(s))
|
||||||
@@ -661,6 +661,10 @@ class EcoVacsIOT():
|
|||||||
|
|
||||||
|
|
||||||
def _wrap_command(self, cmd, recipient):
|
def _wrap_command(self, cmd, recipient):
|
||||||
|
#Remove the td from ctl xml for RestAPI
|
||||||
|
payloadxml = cmd.to_xml()
|
||||||
|
payloadxml.attrib.pop("td")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'auth': {
|
'auth': {
|
||||||
'realm': EcoVacsAPI.REALM,
|
'realm': EcoVacsAPI.REALM,
|
||||||
@@ -670,7 +674,8 @@ class EcoVacsIOT():
|
|||||||
'with': 'users',
|
'with': 'users',
|
||||||
},
|
},
|
||||||
"cmdName": cmd.name,
|
"cmdName": cmd.name,
|
||||||
"payload": cmd.args_to_xml(),
|
"payload": ET.tostring(payloadxml).decode(),
|
||||||
|
|
||||||
"payloadType": "x",
|
"payloadType": "x",
|
||||||
"td": "q",
|
"td": "q",
|
||||||
"toId": recipient,
|
"toId": recipient,
|
||||||
@@ -1001,27 +1006,29 @@ class VacBotCommand:
|
|||||||
if type(value) is dict:
|
if type(value) is dict:
|
||||||
inner = ET.Element(key, value)
|
inner = ET.Element(key, value)
|
||||||
ctl.append(inner)
|
ctl.append(inner)
|
||||||
|
elif type(value) is list:
|
||||||
|
for item in value:
|
||||||
|
ixml = self.listobject_to_xml(key, item)
|
||||||
|
ctl.append(ixml)
|
||||||
else:
|
else:
|
||||||
ctl.set(key, value)
|
ctl.set(key, value)
|
||||||
|
|
||||||
return ctl
|
return ctl
|
||||||
|
|
||||||
def args_to_xml(self):
|
|
||||||
ctl = ET.Element('ctl',{})
|
|
||||||
for key, value in self.args.items():
|
|
||||||
if type(value) is dict:
|
|
||||||
inner = ET.Element(key, value)
|
|
||||||
ctl.append(inner)
|
|
||||||
else:
|
|
||||||
ctl.set(key, value)
|
|
||||||
return ET.tostring(ctl).decode()
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self, *args, **kwargs):
|
def __str__(self, *args, **kwargs):
|
||||||
return self.command_name() + " command"
|
return self.command_name() + " command"
|
||||||
|
|
||||||
def command_name(self):
|
def command_name(self):
|
||||||
return self.__class__.__name__.lower()
|
return self.__class__.__name__.lower()
|
||||||
|
|
||||||
|
def listobject_to_xml(self, tag, conv_object):
|
||||||
|
rtnobject = ET.Element(tag)
|
||||||
|
if type(conv_object) is dict:
|
||||||
|
for key, value in conv_object.items():
|
||||||
|
rtnobject.set(key, value)
|
||||||
|
else:
|
||||||
|
rtnobject.set(tag, conv_object)
|
||||||
|
return rtnobject
|
||||||
|
|
||||||
class Clean(VacBotCommand):
|
class Clean(VacBotCommand):
|
||||||
def __init__(self, mode='auto', speed='normal', iot=False, action='start',terminal=False, **kwargs):
|
def __init__(self, mode='auto', speed='normal', iot=False, action='start',terminal=False, **kwargs):
|
||||||
|
|||||||
@@ -19,6 +19,20 @@ def test_custom_command_inner_tag():
|
|||||||
b'<ctl td="CustomCommand"><customtag customvar="customvalue" /></ctl>')
|
b'<ctl td="CustomCommand"><customtag customvar="customvalue" /></ctl>')
|
||||||
|
|
||||||
|
|
||||||
|
def test_custom_command_multiple_inner_tag():
|
||||||
|
# Ensure a custom-built command with multiple inner tags generates the expected XML payload
|
||||||
|
c = VacBotCommand('CustomCommand', {"customtag":[{"customvar":"customvalue1"},{"customvar":"customvalue2"}]})
|
||||||
|
logging.info(ElementTree.tostring(c.to_xml()))
|
||||||
|
assert_equals(ElementTree.tostring(c.to_xml()),
|
||||||
|
b'<ctl td="CustomCommand"><customtag customvar="customvalue1" /><customtag customvar="customvalue2" /></ctl>')
|
||||||
|
|
||||||
|
def test_custom_command_args_multiple_inner_tag():
|
||||||
|
# Ensure a custom-built command with args and multiple inner tags generates the expected XML payload
|
||||||
|
c = VacBotCommand('CustomCommand', {"arg1":"value1","customtag":[{"customvar":"customvalue1"},{"customvar":"customvalue2"}]})
|
||||||
|
assert_equals(ElementTree.tostring(c.to_xml()),
|
||||||
|
b'<ctl arg1="value1" td="CustomCommand"><customtag customvar="customvalue1" /><customtag customvar="customvalue2" /></ctl>')
|
||||||
|
|
||||||
|
|
||||||
def test_custom_command_noargs():
|
def test_custom_command_noargs():
|
||||||
# Ensure a custom-built command with no args generates XML without an args element
|
# Ensure a custom-built command with no args generates XML without an args element
|
||||||
c = VacBotCommand('CustomCommand')
|
c = VacBotCommand('CustomCommand')
|
||||||
|
|||||||
Reference in New Issue
Block a user