فهرست منبع

Ensure cached state is fetched before use.

async_inferred_type was getting the cached_state, then looping over
async_possible_types, relying on that to ensure the cached_state was fetched
from the device.  Then it was using its own copy of cached_state to make
the decision about match quality.  But if that copy was taken before the
state is fetched from the device, it is empty.  Defer getting the copy of
cached state until inside the loop.

Improve migration logic, return the migration status.

Update strings for Options flow, even though we haven't made the changes for that yet.
Jason Rumney 4 سال پیش
والد
کامیت
3c8efd43ee

+ 20 - 17
custom_components/tuya_local/__init__.py

@@ -39,26 +39,29 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
     if entry.version == 1:
         # Removal of Auto detection.
         config = {**entry.data, **entry.options, "name": entry.title}
+        opts = {**entry.options}
         if config[CONF_TYPE] == CONF_TYPE_AUTO:
             device = setup_device(hass, config)
             config[CONF_TYPE] = await device.async_inferred_type()
-            entry.data = {
-                CONF_DEVICE_ID: config[CONF_DEVICE_ID],
-                CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
-                CONF_HOST: config[CONF_HOST],
-            }
-            opts = {**entry.options}
-            opts[CONF_TYPE] = config[CONF_TYPE]
-            if CONF_CHILD_LOCK in config:
-                opts.pop(CONF_CHILD_LOCK)
-                opts[CONF_LOCK] = config[CONF_CHILD_LOCK]
-            if CONF_DISPLAY_LIGHT in config:
-                opts.pop(CONF_DISPLAY_LIGHT)
-                opts[CONF_LIGHT] = config[CONF_DISPLAY_LIGHT]
-
-            entry.options = {**opts}
-            entry.version = 2
-
+            if config[CONF_TYPE] == None
+            return False
+
+        entry.data = {
+            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
+            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
+            CONF_HOST: config[CONF_HOST],
+        }
+        opts[CONF_TYPE] = config[CONF_TYPE]
+        if CONF_CHILD_LOCK in config:
+            opts.pop(CONF_CHILD_LOCK)
+            opts[CONF_LOCK] = config[CONF_CHILD_LOCK]
+        if CONF_DISPLAY_LIGHT in config:
+            opts.pop(CONF_DISPLAY_LIGHT)
+            opts[CONF_LIGHT] = config[CONF_DISPLAY_LIGHT]
+
+        entry.options = {**opts}
+        entry.version = 2
+        return True
 
 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
     _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")

+ 2 - 1
custom_components/tuya_local/device.py

@@ -97,8 +97,9 @@ class TuyaLocalDevice(object):
     async def async_inferred_type(self):
         best_match = None
         best_quality = 0
-        cached_state = self._get_cached_state()
+        cached_state = {}
         async for config in self.async_possible_types():
+            cached_state = self._get_cached_state()
             quality = config.match_quality(cached_state)
             _LOGGER.info(
                 f"{self.name} considering {config.name} with quality {quality}"

+ 6 - 6
custom_components/tuya_local/translations/en.json

@@ -49,12 +49,12 @@
 		"host": "IP address or hostname",
 		"local_key": "Local key",
 		"type": "Device type",
-		"climate": "Include device as a climate entity (unsupported for switches)",
-		"display_light": "Include LED display as light entity (Goldair only)",
-		"child_lock": "Include child lock as lock entity (unsupported on fans and switches)",
-		"switch": "Include device as a switch entity (switches only)",
-		"humidifier": "Include a humidifier entity (humidifiers and dehumidifiers only)",
-		"fan": "Include a fan entitiy (fans, humidifiers and dehumidifiers)"
+		"humidifier": "Include a humidifier entity",
+		"fan": "Include a fan entitiy"
+		"climate": "Include a climate entity",
+		"light": "Include a light entity",
+		"lock": "Include a lock entity",
+		"switch": "Include a switch entity",
             }
 	},
     },