Browse Source

Catit pixi: reduce precision displayed in the UI

- sensor: add ability to set suggested_display_precision
- catit_pixi_smart_fountain: use it for filter and pump sensors to
   display whole days, not fractions down to the minute.
Jason Rumney 3 years ago
parent
commit
788e88d6c5

+ 13 - 0
custom_components/tuya_local/devices/README.md

@@ -168,6 +168,19 @@ explicit request to update them, such plugs will only return monitoring data
 rarely or never.  Devices can misbehave if this is used on dps that do not
 require it.  Use this only where needed, and generally only on read-only dps.
 
+### `precision`
+
+*Optional, default None.*
+
+For integer dps that are sensor values, the suggested precision for
+display in Home Assistant can be specified.  If unspecified, the Home
+Assistant will use the native precision, which is calculated based on
+the scale of the dp so as to provide distinct values with as few
+decimal places as possible. For example a scale of 3 will result in
+one decimal place by default, (values displayed as x.3, x.7 rather
+than x.33333333 and x.666666) but you could override that to 2 or 0
+with by specifying the precision explicitly.
+
 ### `mapping`
 
 *Optional.*

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

@@ -42,6 +42,7 @@ secondary_entities:
       - id: 4
         type: integer
         name: sensor
+        precision: 0
         mapping:
           - scale: 1440 # minutes => days
             invert: true
@@ -54,6 +55,7 @@ secondary_entities:
       - id: 5
         type: integer
         name: sensor
+        precision: 0
         mapping:
           - scale: 1440
             invert: true

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

@@ -465,6 +465,10 @@ class TuyaDpsConfig:
                 precision += 1
             return precision
 
+    @property
+    def suggested_display_precision(self):
+        return self._config.get("precision")
+
     def step(self, device, scaled=True):
         step = 1
         scale = self.scale(device) if scaled else 1
@@ -492,7 +496,7 @@ class TuyaDpsConfig:
             if cond:
                 return cond.get("invalid", False)
         return False
-
+

     @property
     def hidden(self):
         return self._config.get("hidden", False)

+ 5 - 0
custom_components/tuya_local/sensor.py

@@ -86,6 +86,11 @@ class TuyaLocalSensor(TuyaLocalEntity, SensorEntity):
         """Return the precision for the sensor"""
         return self._sensor_dps.precision(self._device)
 
+    @property
+    def suggested_display_precision(self):
+        """Return the suggested display precision for the sensor"""
+        return self._sensor_dps.suggested_display_precision
+
     @property
     def options(self):
         """Return a set of possible options."""

+ 34 - 1
tests/test_sensor.py

@@ -9,7 +9,11 @@ from custom_components.tuya_local.const import (
     CONF_TYPE,
     DOMAIN,
 )
-from custom_components.tuya_local.sensor import async_setup_entry, TuyaLocalSensor
+from custom_components.tuya_local.helpers.device_config import TuyaEntityConfig
+from custom_components.tuya_local.sensor import (
+    async_setup_entry,
+    TuyaLocalSensor,
+)
 
 
 @pytest.mark.asyncio
@@ -86,3 +90,32 @@ async def test_init_entry_fails_if_config_is_missing(hass):
     except ValueError:
         pass
     m_add_entities.assert_not_called()
+
+
+def test_sensor_suggested_display_precision():
+    mock_device = Mock()
+    config = TuyaEntityConfig(
+        mock_device,
+        {
+            "entity": "sensor",
+            "dps": [
+                {
+                    "id": 1,
+                    "name": "sensor",
+                    "type": "integer",
+                    "precision": 1,
+                }
+            ],
+        },
+    )
+    sensor = TuyaLocalSensor(mock_device, config)
+    assert sensor.suggested_display_precision == 1
+    config = TuyaEntityConfig(
+        mock_device,
+        {
+            "entity": "sensor",
+            "dps": [{"id": 1, "name": "sensor", "type": "integer"}],
+        },
+    )
+    sensor = TuyaLocalSensor(mock_device, config)
+    assert sensor.suggested_display_precision is None