Browse Source

Avoid using legacy_type.

It should only be a backwards compatibility thing, the config file name
is a more efficient type to use when configuring new devices.
Jason Rumney 4 năm trước cách đây
mục cha
commit
733cc4d841

+ 2 - 2
custom_components/tuya_local/config_flow.py

@@ -61,11 +61,11 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
         best_matching_type = None
 
         async for type in self.device.async_possible_types():
-            types.append(type.legacy_type)
+            types.append(type.config_type)
             q = type.match_quality(self.device._get_cached_state())
             if q > best_match:
                 best_match = q
-                best_matching_type = type.legacy_type
+                best_matching_type = type.config_type
 
         if best_match < 100:
             best_match = int(best_match)

+ 1 - 1
custom_components/tuya_local/device.py

@@ -112,7 +112,7 @@ class TuyaLocalDevice(object):
             _LOGGER.warning(f"Detection for {self.name} with dps {cached_state} failed")
             return None
 
-        return best_match.legacy_type
+        return best_match.config_type
 
     async def async_refresh(self):
         cache = self._get_cached_state()

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

@@ -55,10 +55,15 @@ class TuyaDeviceConfig:
         """Return the config file associated with this device."""
         return self._fname
 
+    @property
+    def config_type(self):
+        """Return the config type associated with this device."""
+        return splitext(self._fname)[0]
+
     @property
     def legacy_type(self):
         """Return the legacy conf_type associated with this device."""
-        return self._config.get("legacy_type", splitext(self.config)[0])
+        return self._config.get("legacy_type", self.config_type)
 
     @property
     def primary_entity(self):

+ 1 - 0
tests/test_config_flow.py

@@ -171,6 +171,7 @@ async def test_flow_user_init_invalid_config(mock_test, hass):
 def setup_device_mock(mock, failure=False, type="test"):
     mock_type = MagicMock()
     mock_type.legacy_type = type
+    mock_type.config_type = type
     mock_type.match_quality.return_value = 100
     mock_iter = MagicMock()
     mock_iter.__aiter__.return_value = [mock_type] if not failure else []