Просмотр исходного кода

Maintain order when deduplicating values.

When returning the list of possible values, we removed duplicates by
the fastest method in Python of converting to a set then back to a
list.  However, the conversion to a set loses the order of items.
Instead construct a new list by iterating through them discarding ones
that have been seen before.

Issue #808
Jason Rumney 2 лет назад
Родитель
Сommit
89c36af8ce
1 измененных файлов с 10 добавлено и 1 удалено
  1. 10 1
      custom_components/tuya_local/helpers/device_config.py

+ 10 - 1
custom_components/tuya_local/helpers/device_config.py

@@ -80,6 +80,15 @@ def _equal_or_in(value1, values2):
         return value1 == values2
 
 
+def _remove_duplicates(seq):
+    """Remove dulicates from seq, maintaining order."""
+    if not seq:
+        return []
+    seen = set()
+    adder = seen.add
+    return [x for x in seq if not (x in seen or adder(x))]
+
+
 class TuyaDeviceConfig:
     """Representation of a device config for Tuya Local devices."""
 
@@ -490,7 +499,7 @@ class TuyaDpsConfig:
                     val = c_val
                     break
         _LOGGER.debug("%s values: %s", self.name, val)
-        return list(set(val)) if val else []
+        return _remove_duplicates(val)
 
     @property
     def default(self):