Skip to content

Commit 88f6109

Browse files
authored
Improve test_properties output (#1024)
* test_properties: show which property got removed, improve handling for empties * output which properties are being tested * pad the name of the property to test for easier reading * consider all but Nones as non-empties, use repr for printing the value * Assign only non-nones to the valid properties * handle empty lists as nones for validation
1 parent 619f03a commit 88f6109

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

miio/device.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -304,20 +304,28 @@ def fail(x):
304304

305305
click.echo(f"Testing properties {properties} for {model}")
306306
valid_properties = {}
307+
max_property_len = max([len(p) for p in properties])
307308
for property in properties:
308309
try:
309-
click.echo(f"Testing {property}.. ", nl=False)
310-
resp = self.get_properties([property])
311-
# Handle responses with one-element lists
312-
if isinstance(resp, list) and len(resp) == 1:
313-
resp = resp.pop()
314-
value = valid_properties[property] = resp
310+
click.echo(f"Testing {property:{max_property_len+2}} ", nl=False)
311+
value = self.get_properties([property])
312+
# Handle list responses
313+
if isinstance(value, list):
314+
# unwrap single-element lists
315+
if len(value) == 1:
316+
value = value.pop()
317+
# report on unexpected multi-element lists
318+
elif len(value) > 1:
319+
_LOGGER.error("Got an array as response: %s", value)
320+
# otherwise we received an empty list, which we consider here as None
321+
else:
322+
value = None
323+
315324
if value is None:
316325
fail("None")
317-
elif not value:
318-
fail("Empty response")
319326
else:
320-
ok(f"{value} {type(value)}")
327+
valid_properties[property] = value
328+
ok(f"{repr(value)} {type(value)}")
321329
except Exception as ex:
322330
_LOGGER.warning("Unable to request %s: %s", property, ex)
323331

@@ -330,21 +338,26 @@ def fail(x):
330338
while len(props_to_test) > 1:
331339
try:
332340
click.echo(
333-
f"Testing {len(props_to_test)} properties at once.. ", nl=False
341+
f"Testing {len(props_to_test)} properties at once ({' '.join(props_to_test)}): ",
342+
nl=False,
334343
)
335344
resp = self.get_properties(props_to_test)
345+
336346
if len(resp) == len(props_to_test):
337347
max_properties = len(props_to_test)
338348
ok(f"OK for {max_properties} properties")
339349
break
340350
else:
341-
fail("Got different amount of properties than requested")
351+
removed_property = props_to_test.pop()
352+
fail(
353+
f"Got different amount of properties ({len(props_to_test)}) than requested ({len(resp)}), removing {removed_property}"
354+
)
342355

343-
props_to_test.pop()
344356
except Exception as ex:
345-
_LOGGER.warning("Unable to request properties: %s", ex)
357+
removed_property = props_to_test.pop()
358+
msg = f"Unable to request properties: {ex} - removing {removed_property} for next try"
359+
_LOGGER.warning(msg)
346360
fail(ex)
347-
props_to_test.pop()
348361

349362
non_empty_properties = {
350363
k: v for k, v in valid_properties.items() if v is not None

0 commit comments

Comments
 (0)