Procházet zdrojové kódy

Catit pixi: set water level device class to enum.

Since the return values are from a set of fixed strings, this is an enum.

- modify sensor.options to only return string options.  HA expects all
sensors that define options to be enum device class, and actually
abort the setup if this is not the case.  For some other devices, we
are using mappings to convert to a percentage scale for other device
classes, which we do not want to be an enum type.

Add a test to catch missing enum device classes or enum device classes
without distinct options.

Issue #455
Jason Rumney před 3 roky
rodič
revize
a7e181a039

+ 1 - 0
custom_components/tuya_local/devices/catit_pixi_smart_fountain.yaml

@@ -56,6 +56,7 @@ secondary_entities:
 
 
   - entity: sensor
   - entity: sensor
     name: Water level
     name: Water level
+    class: enum
     category: diagnostic
     category: diagnostic
     dps:
     dps:
       - id: 12
       - id: 12

+ 13 - 7
custom_components/tuya_local/sensor.py

@@ -49,12 +49,13 @@ class TuyaLocalSensor(TuyaLocalEntity, SensorEntity):
     def device_class(self):
     def device_class(self):
         """Return the class of this device"""
         """Return the class of this device"""
         dclass = self._config.device_class
         dclass = self._config.device_class
-        try:
-            return SensorDeviceClass(dclass)
-        except ValueError:
-            if dclass:
-                _LOGGER.warning(f"Unrecognized sensor device class of {dclass} ignored")
-            return None
+        if dclass:
+            try:
+                return SensorDeviceClass(dclass)
+            except ValueError:
+                _LOGGER.warning(
+                    "Unrecognized sensor device class of %s ignored", dclass
+                )
 
 
     @property
     @property
     def state_class(self):
     def state_class(self):
@@ -88,4 +89,9 @@ class TuyaLocalSensor(TuyaLocalEntity, SensorEntity):
     @property
     @property
     def options(self):
     def options(self):
         """Return a set of possible options."""
         """Return a set of possible options."""
-        return self._sensor_dps.values(self._device)
+        # if mappings are all integers,  they are not options to HA
+        values = self._sensor_dps.values(self._device)
+        if values:
+            for val in values:
+                if isinstance(val, str):
+                    return values

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 449
tests/test_device_config.py


Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů