Skip to content

Commit

Permalink
Except errors from pytuya.
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou committed Oct 22, 2023
1 parent 4d557cf commit 1b9c5ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
7 changes: 1 addition & 6 deletions custom_components/localtuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,7 @@ async def setup_entities(devices: dict):
device.async_connect()
for device in hass.data[DOMAIN][entry.entry_id][TUYA_DEVICES].values()
]

try:
await asyncio.wait_for(asyncio.gather(*connect_to_devices), 5)
except:
# If there is device that isn't connected to network it will return failed Initialization.
...
await asyncio.gather(*connect_to_devices)

await setup_entities(entry.data[CONF_DEVICES])

Expand Down
6 changes: 5 additions & 1 deletion custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ async def async_connect(self):
"""Connect to device if not already connected."""
# self.info("async_connect: %d %r %r", self._is_closing, self._connect_task, self._interface)
# if not self._is_closing and self._connect_task is None and not self._interface:
await self._make_connection()
try:
await asyncio.wait_for(self._make_connection(), 5)
except TimeoutError:
...
# self._connect_task = asyncio.create_task(self._make_connection())

async def _make_connection(self):
Expand Down Expand Up @@ -349,6 +352,7 @@ async def abort_connect(self):
if self._interface is not None:
await self._interface.close()
self._interface = None
self._connect_task = None

async def update_local_key(self):
"""Retrieve updated local_key from Cloud API and update the config_entry."""
Expand Down
31 changes: 19 additions & 12 deletions custom_components/localtuya/core/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"""

import asyncio
import errno
import base64
import binascii
import hmac
Expand Down Expand Up @@ -1450,18 +1451,24 @@ async def connect(
"""Connect to a device."""
loop = asyncio.get_running_loop()
on_connected = loop.create_future()
_, protocol = await loop.create_connection(
lambda: TuyaProtocol(
device_id,
local_key,
protocol_version,
enable_debug,
on_connected,
listener or EmptyListener(),
),
address,
port,
)
try:
_, protocol = await loop.create_connection(
lambda: TuyaProtocol(
device_id,
local_key,
protocol_version,
enable_debug,
on_connected,
listener or EmptyListener(),
),
address,
port,
)
except OSError as ex:
if ex.errno == errno.EHOSTUNREACH:
raise ValueError(f"The host is unreachable")
except:
raise ValueError(f"Port is closed for this host?")

await asyncio.wait_for(on_connected, timeout=timeout)
return protocol

0 comments on commit 1b9c5ff

Please sign in to comment.