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

Reformat code according to @make-all recommendation.
Remove auto protocol version and set default to 3.3 (tinytuya default is 3.1)
Update test_config_flow.py according to new changes

Nicolas 2 лет назад
Родитель
Сommit
f741cc904b

+ 9 - 5
custom_components/tuya_local/config_flow.py

@@ -35,7 +35,7 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
         devid_opts = {}
         host_opts = {"default": "Auto"}
         key_opts = {}
-        proto_opts = {"default": "auto"}
+        proto_opts = {"default": 3.3}
         polling_opts = {"default": False}
         devcid_opts = {}
 
@@ -67,7 +67,7 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
                     vol.Required(
                         CONF_PROTOCOL_VERSION,
                         **proto_opts,
-                    ): vol.In(["auto"] + API_PROTOCOL_VERSIONS),
+                    ): vol.In(API_PROTOCOL_VERSIONS),
                     vol.Required(CONF_POLL_ONLY, **polling_opts): bool,
                     vol.Optional(CONF_DEVICE_CID, **devcid_opts): str,
                 }
@@ -169,11 +169,15 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
             vol.Required(CONF_HOST, default=config.get(CONF_HOST, "")): str,
             vol.Required(
                 CONF_PROTOCOL_VERSION,
-                default=config.get(CONF_PROTOCOL_VERSION, "auto"),
-            ): vol.In(["auto"] + API_PROTOCOL_VERSIONS),
+                default=config.get(CONF_PROTOCOL_VERSION, 3.3),
+            ): vol.In(API_PROTOCOL_VERSIONS),
             vol.Required(
                 CONF_POLL_ONLY, default=config.get(CONF_POLL_ONLY, False)
             ): bool,
+            vol.Optional(
+                CONF_DEVICE_CID,
+                default=config.get(CONF_DEVICE_CID, ""),
+            ): str,
         }
         cfg = get_config(config[CONF_TYPE])
         if cfg is None:
@@ -194,7 +198,7 @@ async def async_test_connection(config: dict, hass: HomeAssistant):
         await asyncio.sleep(5)
 
     try:
-        subdevice_id = config[CONF_DEVICE_CID] if CONF_DEVICE_CID in config else None
+        subdevice_id = config.get(CONF_DEVICE_CID)
         device = TuyaLocalDevice(
             "Test",
             config[CONF_DEVICE_ID],

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

@@ -9,7 +9,7 @@
                     "host": "IP address or hostname",
                     "device_id": "Device ID (uuid)",
                     "local_key": "Local key",
-                    "protocol_version": "Protocol version (try auto if not known)",
+                    "protocol_version": "Protocol version",
                     "poll_only": "Poll only (try this if your device does not work fully)",
                     "device_cid": "Sub device ID (for devices connected via gateway)"
                 }
@@ -45,7 +45,7 @@
                 "data": {
                     "host": "IP address or hostname",
                     "local_key": "Local key",
-                    "protocol_version": "Protocol version (try auto if not known)",
+                    "protocol_version": "Protocol version",
                     "poll_only": "Poll only (try this if your device does not work fully)",
                     "device_cid": "Sub device ID (for devices connected via gateway)"
                 }

+ 6 - 4
tests/test_config_flow.py

@@ -300,7 +300,7 @@ async def test_flow_user_init_invalid_config(mock_test, hass):
             CONF_DEVICE_ID: "deviceid",
             CONF_HOST: "hostname",
             CONF_LOCAL_KEY: "badkey",
-            CONF_PROTOCOL_VERSION: "auto",
+            CONF_PROTOCOL_VERSION: 3.3,
             CONF_POLL_ONLY: False,
         },
     )
@@ -499,7 +499,7 @@ async def test_options_flow_init(hass):
             CONF_POLL_ONLY: False,
             CONF_PROTOCOL_VERSION: 3.4,
             CONF_TYPE: "smartplugv1",
-            CONF_DEVICE_CID: None,
+            CONF_DEVICE_CID: "",
         },
     )
     config_entry.add_to_hass(hass)
@@ -538,7 +538,7 @@ async def test_options_flow_modifies_config(mock_test, hass):
             CONF_POLL_ONLY: False,
             CONF_PROTOCOL_VERSION: 3.3,
             CONF_TYPE: "kogan_kahtp_heater",
-            CONF_DEVICE_CID: None,
+            CONF_DEVICE_CID: "subdeviceid",
         },
     )
     config_entry.add_to_hass(hass)
@@ -555,6 +555,7 @@ async def test_options_flow_modifies_config(mock_test, hass):
             CONF_LOCAL_KEY: "new_key",
             CONF_POLL_ONLY: False,
             CONF_PROTOCOL_VERSION: 3.3,
+            CONF_DEVICE_CID: "subdeviceid",
         },
     )
     expected = {
@@ -562,6 +563,7 @@ async def test_options_flow_modifies_config(mock_test, hass):
         CONF_LOCAL_KEY: "new_key",
         CONF_POLL_ONLY: False,
         CONF_PROTOCOL_VERSION: 3.3,
+        CONF_DEVICE_CID: "subdeviceid",
     }
     assert "create_entry" == result["type"]
     assert "" == result["title"]
@@ -586,7 +588,7 @@ async def test_options_flow_fails_when_connection_fails(mock_test, hass):
             CONF_POLL_ONLY: False,
             CONF_PROTOCOL_VERSION: 3.5,
             CONF_TYPE: "smartplugv1",
-            CONF_DEVICE_CID: None,
+            CONF_DEVICE_CID: "",
         },
     )
     config_entry.add_to_hass(hass)