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

Implement new entity_category property.

For secondary entities, return "diagnostic" for sensor entities, and "config" for others.
Will add a config file option to override this later.
Jason Rumney 4 лет назад
Родитель
Сommit
d01a84fc76

+ 12 - 2
custom_components/tuya_local/helpers/device_config.py

@@ -76,7 +76,7 @@ class TuyaDeviceConfig:
     @property
     def primary_entity(self):
         """Return the primary type of entity for this device."""
-        return TuyaEntityConfig(self, self._config["primary_entity"])
+        return TuyaEntityConfig(self, self._config["primary_entity"], primary=True)
 
     def secondary_entities(self):
         """Iterate through entites for any secondary entites supported."""
@@ -139,9 +139,10 @@ class TuyaDeviceConfig:
 class TuyaEntityConfig:
     """Representation of an entity config for a supported entity."""
 
-    def __init__(self, device, config):
+    def __init__(self, device, config, primary=False):
         self._device = device
         self._config = config
+        self._is_primary = primary
 
     def name(self, base_name):
         """The friendly name for this entity."""
@@ -167,6 +168,15 @@ class TuyaEntityConfig:
             return None
         return locate("custom_components.tuya_local" + name)
 
+    @property
+    def entity_category(self):
+        if self._is_primary:
+            return None
+        elif self.entity in ["binary_sensor", "sensor"]:
+            return "diagnostic"
+        else:
+            return "config"
+
     @property
     def deprecated(self):
         """Return whether this entitiy is deprecated."""

+ 5 - 0
custom_components/tuya_local/helpers/mixin.py

@@ -43,6 +43,11 @@ class TuyaLocalEntity:
         """Return the device's information."""
         return self._device.device_info
 
+    @property
+    def entity_category(self):
+        """Return the entitiy's category."""
+        return self._config.entity_category
+
     @property
     def icon(self):
         """Return the icon to use in the frontend for this device."""

+ 12 - 3
tests/devices/base_device_tests.py

@@ -56,9 +56,8 @@ class TuyaDeviceTestCase(IsolatedAsyncioTestCase):
         self.mock_device.name = cfg.name
 
         self.entities = {}
-        self.entities[cfg.primary_entity.config_id] = self.create_entity(
-            cfg.primary_entity
-        )
+        self.primary_entity = cfg.primary_entity.config_id
+        self.entities[self.primary_entity] = self.create_entity(cfg.primary_entity)
 
         self.names = {}
         self.names[cfg.primary_entity.config_id] = cfg.primary_entity.name(cfg.name)
@@ -87,6 +86,16 @@ class TuyaDeviceTestCase(IsolatedAsyncioTestCase):
         for e in self.entities.values():
             self.assertTrue(e.available)
 
+    def test_entity_category(self):
+        for k in self.entities:
+            e = self.entities[k]
+            if k == self.primary_entity:
+                self.assertIsNone(e.entity_category)
+            elif type(e) in [TuyaLocalBinarySensor, TuyaLocalSensor]:
+                self.assertEqual(e.entity_category, "diagnostic")
+            else:
+                self.assertEqual(e.entity_category, "config")
+
     def test_name_returns_device_name(self):
         for e in self.entities:
             self.assertEqual(self.entities[e].name, self.names[e])