Skip to content

Commit

Permalink
Increased default timeout to 10 seconds (also for subdevices)
Browse files Browse the repository at this point in the history
Improved command timeout error logging
  • Loading branch information
albertogeniola committed Aug 10, 2021
1 parent d344719 commit 4bd1c0e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions meross_iot/controller/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ async def _execute_command(self,
method: str,
namespace: Namespace,
payload: dict,
timeout: float = 5,
timeout: float = 10,
skip_rate_limits: bool = False,
drop_on_overquota: bool = True
) -> dict:
Expand Down Expand Up @@ -344,7 +344,7 @@ async def _execute_command(self,
method: str,
namespace: Namespace,
payload: dict,
timeout: float = 5,
timeout: float = 10,
skip_rate_limits: bool = False,
drop_on_overquota: bool = True
) -> dict:
Expand Down
4 changes: 2 additions & 2 deletions meross_iot/controller/known/subdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def _execute_command(self,
method: str,
namespace: Namespace,
payload: dict,
timeout: float = 5,
timeout: float = 10,
skip_rate_limits: bool = False,
drop_on_overquota: bool = True) -> dict:
raise NotImplementedError("This method should never be called directly for subdevices.")
Expand Down Expand Up @@ -151,7 +151,7 @@ async def _execute_command(self,
method: str,
namespace: Namespace,
payload: dict,
timeout: float = 5,
timeout: float = 10,
skip_rate_limits: bool = False,
drop_on_overquota: bool = True) -> dict:
raise NotImplementedError("This method should never be called directly for subdevices.")
Expand Down
18 changes: 14 additions & 4 deletions meross_iot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def __init__(
self._user_topic = build_client_user_topic(user_id=self._cloud_creds.user_id)
self._limiter = rate_limiter

def _get_client_from_domain_port(self, client: mqtt.Client) -> Tuple[Optional[str], Optional[int]]:
for k, v in self._mqtt_clients.items():
if v == client:
domain, port = k.split(":")
return domain, int(port)
return None, None

async def _async_get_create_mqtt_client(self, domain: str, port: int) -> mqtt.Client:
"""
Retrieves the mqtt_client for the given domain/port combination.
Expand Down Expand Up @@ -819,6 +826,8 @@ async def async_execute_cmd_client(self,
namespace=namespace,
payload=payload,
timeout=timeout,
skip_rate_limiting_check=skip_rate_limiting_check,
drop_on_overquota=drop_on_overquota
)
elif rate_limiting_action == RateLimitResultStrategy.DropCall:
raise RateLimitExceeded()
Expand All @@ -838,24 +847,25 @@ async def async_execute_cmd_client(self,
future=fut,
target_device_uuid=destination_device_uuid,
message=message,
timeout=timeout,
timeout=timeout
)
return response.get("payload")

async def _async_send_and_wait_ack(
self, client: mqtt.Client, future: Future, target_device_uuid: str, message: bytes, timeout: float
self, client: mqtt.Client, future: Future, target_device_uuid: str, message: bytes, timeout: float,
):
message_info = client.publish(
topic=build_device_request_topic(target_device_uuid), payload=message
)
try:
return await asyncio.wait_for(future, timeout, loop=self._loop)
except TimeoutError as e:
domain, port = self._get_client_from_domain_port(client=client)
_LOGGER.error(
f"Timeout occurred while waiting a response for message {message} sent to device uuid "
f"{target_device_uuid}. Timeout was: {timeout} seconds."
f"{target_device_uuid}. Timeout was: {timeout} seconds. Mqtt Host: {domain}:{port}."
)
raise CommandTimeoutError()
raise CommandTimeoutError(message=str(message), target_device_uuid=target_device_uuid, timeout=timeout)

async def _notify_connection_drop(self):
for d in self._device_registry.find_all_by():
Expand Down
5 changes: 4 additions & 1 deletion meross_iot/model/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ class UnconnectedError(Exception):


class CommandTimeoutError(Exception):
pass
def __init__(self, message: str, target_device_uuid: str, timeout: float):
self.message = message
self.target_device_uuid = target_device_uuid
self.timeout=timeout


class MqttError(Exception):
Expand Down

0 comments on commit 4bd1c0e

Please sign in to comment.