Răsfoiți Sursa

Device naming: do not default translation_key to device class

Previously we defaulted translation_key to the device class, as it
seemed necessary to get the class translations to work.

But actually the side effect this caused was to use the super class
name (to inherit translations) instead of returning None when there
was no explicit name.

Rather than getting that side effect by mis-setting the translation
key, it is better to detect when we should use the device class as
the name, by checking the undocumented _default_to_device_class_name
attribute from the superclass.  This is set by the base classes that
properly handle translation when there is a device class with a name.

For base classes that do not set this, we avoid getting None appended
if we adjust our logic to take this into account.

Issue #1856.
Jason Rumney 1 an în urmă
părinte
comite
1c1030da4d

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

@@ -252,7 +252,7 @@ class TuyaEntityConfig:
     @property
     def translation_key(self):
         """The translation key for this entity."""
-        return self._config.get("translation_key", self.device_class)
+        return self._config.get("translation_key")
 
     @property
     def translation_only_key(self):
@@ -307,6 +307,8 @@ class TuyaEntityConfig:
                 else:
                     slug = f"{slug}_{value}"
             return slug
+        elif self.device_class:
+            return f"{self.entity}_{self.device_class}"
         return self.entity
 
     @property

+ 8 - 1
custom_components/tuya_local/helpers/mixin.py

@@ -57,7 +57,14 @@ class TuyaLocalEntity:
     @property
     def use_device_name(self):
         """Return whether to use the device name for the entity name"""
-        own_name = self._config.name or self._config.translation_key
+        own_name = (
+            self._config.name
+            or self._config.translation_key
+            or (
+                self._default_to_device_class_name
+                and not self._config.translation_only_key
+            )
+        )
         return not own_name
 
     @property