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

Make unique ids really unique.

Unique ids were no longer unique if multiple entities of the same type were added.
Use the entity name from the config to decorate the unique id.
Jason Rumney 4 лет назад
Родитель
Сommit
628488695a

+ 1 - 1
custom_components/tuya_local/generic/climate.py

@@ -117,7 +117,7 @@ class TuyaLocalClimate(ClimateEntity):
     @property
     def unique_id(self):
         """Return the unique id for this climate device."""
-        return self._device.unique_id
+        return self._config.unique_id(self._device)
 
     @property
     def device_info(self):

+ 2 - 2
custom_components/tuya_local/generic/fan.py

@@ -68,7 +68,7 @@ class TuyaLocalFan(FanEntity):
     @property
     def name(self):
         """Return the name of the climate device."""
-        return self._device.name
+        return f"{self._device.name}"
 
     @property
     def friendly_name(self):
@@ -78,7 +78,7 @@ class TuyaLocalFan(FanEntity):
     @property
     def unique_id(self):
         """Return the unique id for this climate device."""
-        return self._device.unique_id
+        return self._config.unique_id(self._device)
 
     @property
     def device_info(self):

+ 1 - 1
custom_components/tuya_local/generic/humidifier.py

@@ -69,7 +69,7 @@ class TuyaLocalHumidifier(HumidifierEntity):
     @property
     def unique_id(self):
         """Return the unique id for this climate device."""
-        return self._device.unique_id
+        return self._config.unique_id(self._device)
 
     @property
     def device_info(self):

+ 1 - 1
custom_components/tuya_local/generic/light.py

@@ -56,7 +56,7 @@ class TuyaLocalLight(LightEntity):
     @property
     def unique_id(self):
         """Return the unique id for this heater LED display."""
-        return self._device.unique_id
+        return self._config.unique_id(self._device)
 
     @property
     def device_info(self):

+ 1 - 1
custom_components/tuya_local/generic/lock.py

@@ -47,7 +47,7 @@ class TuyaLocalLock(LockEntity):
     @property
     def unique_id(self):
         """Return the device unique ID."""
-        return self._device.unique_id
+        return self._config.unique_id(self._device)
 
     @property
     def device_info(self):

+ 1 - 1
custom_components/tuya_local/generic/switch.py

@@ -54,7 +54,7 @@ class TuyaLocalSwitch(SwitchEntity):
     @property
     def unique_id(self):
         """Return the unique id of the device."""
-        return self._device.unique_id
+        return self._config.unique_id(self._device)
 
     @property
     def device_info(self):

+ 8 - 0
custom_components/tuya_local/helpers/device_config.py

@@ -152,6 +152,14 @@ class TuyaEntityConfig:
         else:
             return self._device.name + " " + own_name
 
+    def unique_id(self, device):
+        """Return a suitable unique_id for this entity."""
+        own_name = self._config.get("name")
+        if own_name:
+            return f"{device.unique_id}-{slugify(own_name)}"
+        else:
+            return device.unique_id
+
     @property
     def legacy_class(self):
         """Return the legacy device corresponding to this config."""

+ 12 - 2
tests/devices/base_device_tests.py

@@ -1,5 +1,6 @@
 from unittest import IsolatedAsyncioTestCase
 from unittest.mock import AsyncMock, patch
+from uuid import uuid4
 
 from custom_components.tuya_local.generic.climate import TuyaLocalClimate
 from custom_components.tuya_local.generic.fan import TuyaLocalFan
@@ -37,6 +38,7 @@ class TuyaDeviceTestCase(IsolatedAsyncioTestCase):
         self.conf_type = cfg.legacy_type
 
         self.mock_device.name = cfg.name
+        self.mock_device.unique_id = str(uuid4())
 
         self.entities = {}
         self.entities[cfg.primary_entity.config_id] = self.create_entity(
@@ -74,9 +76,17 @@ class TuyaDeviceTestCase(IsolatedAsyncioTestCase):
         for e in self.entities:
             self.assertEqual(self.entities[e].friendly_name, self.names[e])
 
-    def test_unique_id_returns_device_unique_id(self):
+    def test_unique_id_contains_device_unique_id(self):
+        entities = {}
         for e in self.entities.values():
-            self.assertEqual(e.unique_id, self.mock_device.unique_id)
+            self.assertIn(self.mock_device.unique_id, e.unique_id)
+            if type(e) not in entities:
+                entities[type(e)] = []
+
+            entities[type(e)].append(e.unique_id)
+
+        for e in entities.values():
+            self.assertCountEqual(e, set(e))
 
     def test_device_info_returns_device_info_from_device(self):
         for e in self.entities.values():