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

Migrate legacy types to config filename based types.

It is more efficient to load config files based on the type, rather than having
to iterate through all configs looking for the matching legacy_type.

Change legacy type in smartplugv2 to kogan_switch2 to tie it back to the old
config name.  kogan_switch has special handling in the migration to handle
mapping to the old shared legacy_type.
Jason Rumney 4 лет назад
Родитель
Сommit
4e7a2e98a1

+ 22 - 0
custom_components/tuya_local/__init__.py

@@ -25,6 +25,7 @@ from .const import (
     DOMAIN,
 )
 from .device import setup_device, delete_device
+from .helpers.device_config import get_config
 
 _LOGGER = logging.getLogger(__name__)
 
@@ -83,6 +84,27 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
         entry.options = {**opts}
         entry.version = 3
 
+    if entry.version == 3:
+        # Migrate to filename based config_type, to avoid needing to
+        # parse config files to find the right one.
+        config = {**entry.data, **entry.options, "name": entry.title}
+        config_type = get_config(config[CONF_TYPE]).config_type
+
+        # Special case for kogan_switch.  Consider also v2.
+        if config_type == "smartplugv1":
+            device = setup_device(hass, config)
+            config_type = await device.async_inferred_type()
+            if config_type != "smartplugv2":
+                config_type = "smartplugv1"
+
+        entry.data = {
+            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
+            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
+            CONF_HOST: config[CONF_HOST],
+            CONF_TYPE: config_type,
+        }
+        entry.version = 4
+
     return True
 
 

+ 1 - 1
custom_components/tuya_local/config_flow.py

@@ -14,7 +14,7 @@ _LOGGER = logging.getLogger(__name__)
 
 
 class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
-    VERSION = 3
+    VERSION = 4
     CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
     device = None
     data = {}

+ 1 - 1
custom_components/tuya_local/devices/smartplugv2.yaml

@@ -1,5 +1,5 @@
 name: Energy Monitoring Smart Plug
-legacy_type: kogan_switch
+legacy_type: kogan_switch2
 primary_entity:
   entity: switch
   class: outlet