From f5a1dfe22e1abda5e87f019d7fdf30acfde5519e Mon Sep 17 00:00:00 2001 From: xZetsubou Date: Sun, 22 Oct 2023 11:53:07 +0300 Subject: [PATCH] Except error on config flow and set timeout for connect. --- custom_components/localtuya/config_flow.py | 26 ++++++++++--------- .../localtuya/core/pytuya/__init__.py | 2 +- .../localtuya/translations/en.json | 6 +++-- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index 23ac90f61..a5d9893c9 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -1,4 +1,5 @@ """Config flow for LocalTuya integration integration.""" +import asyncio import errno import logging import time @@ -356,14 +357,16 @@ async def validate_input(hass: core.HomeAssistant, entry_id, data): for ver in SUPPORTED_PROTOCOL_VERSIONS: try: version = ver if auto_protocol else conf_protocol - interface = await pytuya.connect( - data[CONF_HOST], - data[CONF_DEVICE_ID], - data[CONF_LOCAL_KEY], - float(version), - data[CONF_ENABLE_DEBUG], + interface = await asyncio.wait_for( + pytuya.connect( + data[CONF_HOST], + data[CONF_DEVICE_ID], + data[CONF_LOCAL_KEY], + float(version), + data[CONF_ENABLE_DEBUG], + ), + 5, ) - # Break the loop if input isn't auto. if not auto_protocol: break @@ -375,12 +378,10 @@ async def validate_input(hass: core.HomeAssistant, entry_id, data): conf_protocol = version break # If connection to host is failed raise wrong address. - except OSError as ex: - if ex.errno == errno.EHOSTUNREACH: - raise CannotConnect + except ValueError as ex: + raise ValueError(ex) except: continue - if CONF_RESET_DPIDS in data: reset_ids_str = data[CONF_RESET_DPIDS].split(",") reset_ids = [] @@ -425,7 +426,7 @@ async def validate_input(hass: core.HomeAssistant, entry_id, data): except (ConnectionRefusedError, ConnectionResetError) as ex: raise CannotConnect from ex except ValueError as ex: - raise InvalidAuth from ex + error = ex finally: if interface and close: await interface.close() @@ -833,6 +834,7 @@ async def async_step_configure_device(self, user_input=None): except InvalidAuth: errors["base"] = "invalid_auth" except ValueError as ex: + placeholders["ex"] = str(ex) errors["base"] = "value_error" _LOGGER.debug("Value Error: %s", ex) except EmptyDpsList: diff --git a/custom_components/localtuya/core/pytuya/__init__.py b/custom_components/localtuya/core/pytuya/__init__.py index 8fe0f23cd..e8ea49953 100644 --- a/custom_components/localtuya/core/pytuya/__init__.py +++ b/custom_components/localtuya/core/pytuya/__init__.py @@ -1468,7 +1468,7 @@ async def connect( if ex.errno == errno.EHOSTUNREACH: raise ValueError(f"The host is unreachable") except: - raise ValueError(f"Port is closed for this host?") + raise ValueError(f"Unknown, Maybe port is closed for this host?.") await asyncio.wait_for(on_connected, timeout=timeout) return protocol diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index 240674ee2..9262ffc9e 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -9,7 +9,8 @@ "cannot_connect": "Cannot connect to device. Verify that address is correct and try again.", "device_list_failed": "Failed to retrieve device list.\n{msg}", "invalid_auth": "Failed to authenticate with device. Verify that device id and local key are correct.", - "unknown": "An unknown error occurred. \n{ex}.", + "value_error":"An error occurred: {ex}", + "unknown": "An unknown error occurred. See log for details {ex}", "entity_already_configured": "Entity with this ID has already been configured.", "address_in_use": "Address used for discovery is already in use. Make sure no other application is using it (TCP port 6668).", "discovery_failed": "Something failed when discovering devices. See log for details.", @@ -41,7 +42,8 @@ "cannot_connect": "Cannot connect to device. Verify that address is correct and try again.", "device_list_failed": "Failed to retrieve device list.\n{msg}", "invalid_auth": "Failed to authenticate with device. Verify that device id and local key are correct.", - "unknown": "An unknown error occurred. \n{ex}.", + "value_error":"An error occurred: {ex}", + "unknown": "An unknown error occurred. See log for details \n{ex}.", "entity_already_configured": "Entity with this ID has already been configured.", "address_in_use": "Address used for discovery is already in use. Make sure no other application is using it (TCP port 6668).", "discovery_failed": "Something failed when discovering devices. See log for details.",