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

config_flow: catch exceptions when we test connection, and fail

Rather than allowing the exception to propagate all the way up to HA, fail
gracefully, and just log the exception message.

Issue #395 (point 2 in #issuecomment-1427799579)
Jason Rumney 3 лет назад
Родитель
Сommit
c7896e1f1e
1 измененных файлов с 17 добавлено и 11 удалено
  1. 17 11
      custom_components/tuya_local/config_flow.py

+ 17 - 11
custom_components/tuya_local/config_flow.py

@@ -172,17 +172,23 @@ async def async_test_connection(config: dict, hass: HomeAssistant):
     if existing:
         existing["device"].pause()
 
-    device = TuyaLocalDevice(
-        "Test",
-        config[CONF_DEVICE_ID],
-        config[CONF_HOST],
-        config[CONF_LOCAL_KEY],
-        config[CONF_PROTOCOL_VERSION],
-        hass,
-        True,
-    )
-    await device.async_refresh()
+    try:
+        device = TuyaLocalDevice(
+            "Test",
+            config[CONF_DEVICE_ID],
+            config[CONF_HOST],
+            config[CONF_LOCAL_KEY],
+            config[CONF_PROTOCOL_VERSION],
+            hass,
+            True,
+        )
+        await device.async_refresh()
+        retval = device if device.has_returned_state else None
+    except Exception as e:
+        _LOGGER.warning(e)
+        retval = None
+
     if existing:
         existing["device"].resume()
 
-    return device if device.has_returned_state else None
+    return retval