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

Options can be in data after initial config.

Modify test to set options, but modify code to check both data and options.
Jason Rumney 4 лет назад
Родитель
Сommit
1aa8190ba9
2 измененных файлов с 25 добавлено и 12 удалено
  1. 12 11
      custom_components/tuya_local/__init__.py
  2. 13 1
      tests/test_config_flow.py

+ 12 - 11
custom_components/tuya_local/__init__.py

@@ -165,22 +165,23 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
 
     if entry.version == 6:
         # Migrate some entity names to make them consistent for translations
-        opts = {**entry.options}
-        newopts = {**opts}
-        master = newopts.pop("switch_main_switch", None)
+        opts = {**entry.data, **entry.options}
+        newopts = {**entry.options}
+        master = opts.get("switch_main_switch")
         if master is not None:
+            newopts.pop("switch_main_switch", None)
             newopts["switch_master"] = master
-        outlet1 = newopts.pop("switch_left_outlet", None)
-        outlet2 = newopts.pop("switch_right_outlet", None)
-        outlet1 = (
-            newopts.pop("switch_wall_switch_1", None) if outlet1 is None else outlet1
-        )
-        outlet2 = (
-            newopts.pop("switch_wall_switch_2", None) if outlet2 is None else outlet2
-        )
+        outlet1 = opts.get("switch_left_outlet")
+        outlet2 = opts.get("switch_right_outlet")
+        outlet1 = opts.get("switch_wall_switch_1") if outlet1 is None else outlet1
+        outlet2 = opts.get("switch_wall_switch_2") if outlet2 is None else outlet2
         if outlet1 is not None:
+            newopts.pop("switch_left_outlet", None)
+            newopts.pop("switch_wall_switch_1", None)
             newopts["switch_outlet_1"] = outlet1
         if outlet2 is not None:
+            newopts.pop("switch_right_outlet", None)
+            newopts.pop("switch_wall_switch_2", None)
             newopts["switch_outlet_2"] = outlet2
 
         entry.options = {**newopts}

+ 13 - 1
tests/test_config_flow.py

@@ -45,13 +45,15 @@ async def test_init_entry(hass):
     """Test initialisation of the config flow."""
     entry = MockConfigEntry(
         domain=DOMAIN,
-        version=6,
+        version=7,
         title="test",
         data={
             CONF_DEVICE_ID: "deviceid",
             CONF_HOST: "hostname",
             CONF_LOCAL_KEY: "localkey",
             CONF_TYPE: "kogan_kahtp_heater",
+        },
+        options={
             CONF_CLIMATE: True,
             "lock_child_lock": True,
         },
@@ -113,6 +115,8 @@ async def test_migrate_entry(mock_setup, hass):
             CONF_HOST: "hostname",
             CONF_LOCAL_KEY: "localkey",
             CONF_TYPE: "auto",
+        },
+        options={
             CONF_CLIMATE: False,
         },
     )
@@ -130,6 +134,8 @@ async def test_migrate_entry(mock_setup, hass):
             CONF_HOST: "hostname",
             CONF_LOCAL_KEY: "localkey",
             CONF_TYPE: "smartplugv1",
+        },
+        options={
             CONF_SWITCH: True,
         },
     )
@@ -147,6 +153,8 @@ async def test_migrate_entry(mock_setup, hass):
             CONF_HOST: "hostname",
             CONF_LOCAL_KEY: "localkey",
             CONF_TYPE: "smartplugv1",
+        },
+        options={
             CONF_SWITCH: True,
         },
     )
@@ -164,6 +172,8 @@ async def test_migrate_entry(mock_setup, hass):
             CONF_HOST: "hostname",
             CONF_LOCAL_KEY: "localkey",
             CONF_TYPE: "goldair_dehumidifier",
+        },
+        options={
             CONF_HUMIDIFIER: True,
             CONF_FAN: True,
             CONF_LIGHT: True,
@@ -187,6 +197,8 @@ async def test_migrate_entry(mock_setup, hass):
             CONF_HOST: "hostname",
             CONF_LOCAL_KEY: "localkey",
             CONF_TYPE: "grid_connect_usb_double_power_point",
+        },
+        options={
             "switch_main_switch": True,
             "switch_left_outlet": True,
             "switch_right_outlet": True,