Skip to content

Commit

Permalink
Except error on config flow and set timeout for connect.
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou committed Oct 22, 2023
1 parent 1b9c5ff commit f5a1dfe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
26 changes: 14 additions & 12 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Config flow for LocalTuya integration integration."""
import asyncio
import errno
import logging
import time
Expand Down Expand Up @@ -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
Expand All @@ -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 = []
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/localtuya/core/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions custom_components/localtuya/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down

0 comments on commit f5a1dfe

Please sign in to comment.