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

Migrate CONF_TYPE from entry.options to entry.data

When entry options were edited later, the conf_type was lost, since it isn't
editable as an option.
Move it to the main config data to avoid this.
This migration repeats the auto-detect process for users who already lost the
CONF_TYPE in 0.8.0.

Bump version
Jason Rumney 4 лет назад
Родитель
Сommit
1fee9c3d4e

+ 23 - 1
custom_components/tuya_local/__init__.py

@@ -51,7 +51,6 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
             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, False)
             opts[CONF_LOCK] = config[CONF_CHILD_LOCK]
@@ -61,6 +60,29 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
 
         entry.options = {**opts}
         entry.version = 2
+
+    if entry.version == 2:
+        # CONF_TYPE is not configurable, move it from options to the main config.
+        config = {**entry.data, **entry.options, "name": entry.title}
+        opts = {**entry.options}
+        # Ensure type has been migrated.  Some users are reporting errors which
+        # suggest it was removed completely.  But that is probably due to
+        # overwriting options without CONF_TYPE.
+        if config.get(CONF_TYPE, CONF_TYPE_AUTO) == CONF_TYPE_AUTO:
+            device = setup_device(hass, config)
+            config[CONF_TYPE] = await device.async_inferred_type()
+            if config[CONF_TYPE] is None:
+                return False
+        entry.data = {
+            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
+            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
+            CONF_HOST: config[CONF_HOST],
+            CONF_TYPE: config[CONF_TYPE],
+        }
+        opts.pop(CONF_TYPE, None)
+        entry.options = {**opts}
+        entry.version = 3
+
     return True
 
 

+ 1 - 1
custom_components/tuya_local/manifest.json

@@ -2,7 +2,7 @@
     "domain": "tuya_local",
     "iot_class": "local_polling",
     "name": "Tuya based devices local control",
-    "version": "0.8.0", 
+    "version": "0.8.1", 
     "documentation": "https://github.com/make-all/tuya-local",
     "issue_tracker": "https://github.com/make-all/tuya-local/issues",
     "dependencies": [],