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

Standardize naming of outlets to simplify translations.

- Use "Outlet 1" and "Outlet 2" for double outlet devices consistently.
- Use "Master" rather than "Main switch" to commonize with older devices.
- Add translation strings for switch_outlet_1 and switch_outlet_2 so they show in the UI.
- Add a migration to patch up existing configs
- Update unit tests for these changes
Jason Rumney 4 лет назад
Родитель
Сommit
103397bfd9

+ 19 - 0
custom_components/tuya_local/__init__.py

@@ -163,6 +163,25 @@ async def async_migrate_entry(hass, entry: ConfigEntry):
         await async_migrate_entries(hass, entry.entry_id, update_unique_id)
         entry.version = 6
 
+    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)
+        if master is not 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
+        if outlet1 is not None:
+            newopts["switch_outlet_1"] = outlet1
+        if outlet2 is not None:
+            newopts["switch_outlet_2"] = outlet2
+
+        entry.options = {**newopts}
+        entry.version = 7
+        
     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 = 6
+    VERSION = 7
     CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
     device = None
     data = {}

+ 2 - 2
custom_components/tuya_local/devices/grid_connect_double_switch.yaml

@@ -1,7 +1,7 @@
 name: Grid Connect double wall switch
 primary_entity:
   entity: switch
-  name: Wall switch 1
+  name: Outlet 1
   class: switch
   dps:
     - id: 1
@@ -18,7 +18,7 @@ primary_entity:
       name: unknown_105
 secondary_entities:
   - entity: switch
-    name: Wall switch 2
+    name: Outlet 2
     class: switch
     dps:
       - id: 2

+ 3 - 3
custom_components/tuya_local/devices/grid_connect_usb_double_power_point.yaml

@@ -7,7 +7,7 @@
 name: Grid Connnect power metered double outlet with USB
 primary_entity:
   entity: switch
-  name: Main Switch
+  name: Master
   class: outlet
   dps:
     - id: 17
@@ -73,7 +73,7 @@ secondary_entities:
             icon: "mdi:account"
   - entity: switch
     class: outlet
-    name: Left Outlet
+    name: Outlet 1
     dps:
       - id: 1
         name: switch
@@ -92,7 +92,7 @@ secondary_entities:
         hidden: true
   - entity: switch
     class: outlet
-    name: Right Outlet
+    name: Outlet 2
     dps:
       - id: 2
         name: switch

+ 1 - 1
custom_components/tuya_local/manifest.json

@@ -2,7 +2,7 @@
     "domain": "tuya_local",
     "iot_class": "local_polling",
     "name": "Tuya Local",
-    "version": "0.12.2",
+    "version": "0.12.3",
     "documentation": "https://github.com/make-all/tuya-local",
     "issue_tracker": "https://github.com/make-all/tuya-local/issues",
     "dependencies": [],

+ 3 - 3
tests/devices/test_grid_connect_double_power_point.py

@@ -32,9 +32,9 @@ class TestGridConnectDoubleSwitch(TuyaDeviceTestCase):
             "grid_connect_usb_double_power_point.yaml",
             GRIDCONNECT_2SOCKET_PAYLOAD,
         )
-        self.subject = self.entities.get("switch_main_switch")
-        self.switch1 = self.entities.get("switch_left_outlet")
-        self.switch2 = self.entities.get("switch_right_outlet")
+        self.subject = self.entities.get("switch_master")
+        self.switch1 = self.entities.get("switch_outlet_1")
+        self.switch2 = self.entities.get("switch_outlet_2")
         self.lock = self.entities.get("lock_child_lock")
 
     def test_device_class_is_outlet(self):

+ 28 - 7
tests/test_config_flow.py

@@ -173,6 +173,27 @@ async def test_migrate_entry(mock_setup, hass):
     )
     assert await async_migrate_entry(hass, entry)
 
+    mock_device.async_inferred_type = AsyncMock(
+        return_value="grid_connect_usb_double_power_point"
+    )
+    mock_device.reset_mock()
+
+    entry = MockConfigEntry(
+        domain=DOMAIN,
+        version=6,
+        title="test7",
+        data={
+            CONF_DEVICE_ID: "deviceid",
+            CONF_HOST: "hostname",
+            CONF_LOCAL_KEY: "localkey",
+            CONF_TYPE: "grid_connect_usb_double_power_point",
+            "switch_main_switch": True,
+            "switch_left_outlet": True,
+            "switch_right_outlet": True,
+        },
+    )
+    assert await async_migrate_entry(hass, entry)
+
 
 async def test_flow_user_init(hass):
     """Test the initialisation of the form in the first step of the config flow."""
@@ -402,7 +423,7 @@ async def test_flow_choose_entities_creates_config_entry(hass, bypass_setup):
             },
         )
         expected = {
-            "version": 6,
+            "version": 7,
             "type": "create_entry",
             "flow_id": ANY,
             "handler": DOMAIN,
@@ -427,7 +448,7 @@ async def test_options_flow_init(hass):
     """Test config flow options."""
     config_entry = MockConfigEntry(
         domain=DOMAIN,
-        version=6,
+        version=7,
         unique_id="uniqueid",
         data={
             CONF_DEVICE_ID: "deviceid",
@@ -464,7 +485,7 @@ async def test_options_flow_modifies_config(mock_test, hass):
 
     config_entry = MockConfigEntry(
         domain=DOMAIN,
-        version=6,
+        version=7,
         unique_id="uniqueid",
         data={
             CONF_CLIMATE: True,
@@ -510,7 +531,7 @@ async def test_options_flow_fails_when_connection_fails(mock_test, hass):
 
     config_entry = MockConfigEntry(
         domain=DOMAIN,
-        version=6,
+        version=7,
         unique_id="uniqueid",
         data={
             CONF_DEVICE_ID: "deviceid",
@@ -548,7 +569,7 @@ async def test_options_flow_fails_when_config_is_missing(mock_test, hass):
 
     config_entry = MockConfigEntry(
         domain=DOMAIN,
-        version=6,
+        version=7,
         unique_id="uniqueid",
         data={
             CONF_DEVICE_ID: "deviceid",
@@ -575,7 +596,7 @@ async def test_async_setup_entry_for_dehumidifier(mock_setup, hass):
     """Test setting up based on a config entry.  Repeats test_init_entry."""
     config_entry = MockConfigEntry(
         domain=DOMAIN,
-        version=6,
+        version=7,
         unique_id="uniqueid",
         data={
             CONF_CLIMATE: False,
@@ -598,7 +619,7 @@ async def test_async_setup_entry_for_switch(mock_device, hass):
     """Test setting up based on a config entry.  Repeats test_init_entry."""
     config_entry = MockConfigEntry(
         domain=DOMAIN,
-        version=6,
+        version=7,
         unique_id="uniqueid",
         data={
             CONF_DEVICE_ID: "deviceid",