Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix freeze after publishing message with AWS IoT SDK v2 #173

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## v1.0.10

- Update manifest requirements to use only AWS IoT Device SDK v2
- Fix parameter of callback to the right convention
- Fix AWS IoT MQTT publish message validation and QoS
- Change clean_session parameter to start a new session on every attempt to connect
- Set the AWS Client ID to the entry ID instead of randomize on every attempt to connect
- Set message being sent while publishing empty message to empty JSON instead of empty string

## v1.0.9

- Upgrade AWS IoT Device SDK to v2
Expand Down
28 changes: 15 additions & 13 deletions custom_components/mydolphin_plus/managers/aws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
from time import sleep
from typing import Any
import uuid

from awscrt import auth, mqtt
from awsiot import mqtt_connection_builder
Expand Down Expand Up @@ -95,6 +94,7 @@ def __init__(self, hass: HomeAssistant | None, config_manager: ConfigManager):
try:
self._hass = hass
self._config_manager = config_manager
self._awsiot_id = config_manager.entry_id

self._api_data = {}
self._data = {}
Expand Down Expand Up @@ -165,7 +165,6 @@ async def initialize(self):

self._set_status(ConnectivityStatus.Connecting)

awsiot_id = str(uuid.uuid4())
aws_token = self._api_data.get(API_RESPONSE_DATA_TOKEN)
aws_key = self._api_data.get(API_RESPONSE_DATA_ACCESS_KEY_ID)
aws_secret = self._api_data.get(API_RESPONSE_DATA_SECRET_ACCESS_KEY)
Expand All @@ -192,10 +191,9 @@ async def initialize(self):
region=AWS_REGION,
ca_filepath=ca_file_path,
credentials_provider=credentials_provider,
client_id=awsiot_id,
clean_session=False,
client_id=self._awsiot_id,
clean_session=True,
keep_alive_secs=30,
http_proxy_options=None,
on_connection_success=self._connection_callbacks.get(
ConnectionCallbacks.SUCCESS
),
Expand Down Expand Up @@ -288,7 +286,7 @@ def _on_connection_closed(self, connection, callback_data):

self._set_status(ConnectivityStatus.Disconnected)

def _on_connection_interrupted(self, _connection, error, **_kwargs):
def _on_connection_interrupted(self, connection, error, **_kwargs):
_LOGGER.error(f"AWS IoT connection interrupted, Error: {error}")

self._set_status(ConnectivityStatus.Failed)
Expand Down Expand Up @@ -411,19 +409,23 @@ def _send_dynamic_command(self, description: str, payload: dict | None):
self._publish(self._topic_data.dynamic, payload)

def _publish(self, topic: str, data: dict | None):
payload = "" if data is None else json.dumps(data)
if data is None:
data = {}

payload = json.dumps(data)

if self._status == ConnectivityStatus.Connected:
try:
if self._awsiot_client is not None:
published = self._awsiot_client.publish(
topic, payload, mqtt.QoS.AT_LEAST_ONCE
_LOGGER.debug(f"Trying to publish message {payload} to {topic}")

publish_future, packet_id = self._awsiot_client.publish(
topic, payload, mqtt.QoS.AT_MOST_ONCE
)

if published:
_LOGGER.debug(f"Published message: {data} to {topic}")
else:
_LOGGER.warning(f"Failed to publish message: {data} to {topic}")
publish_future.result()

_LOGGER.debug(f"Published message: {data} to {topic}")

except Exception as ex:
_LOGGER.error(
Expand Down
4 changes: 2 additions & 2 deletions custom_components/mydolphin_plus/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"documentation": "https://github.com/sh00t2kill/dolphin-robot",
"iot_class": "cloud_push",
"issue_tracker": "https://github.com/sh00t2kill/dolphin-robot/issues",
"requirements": ["awscrt~=0.20.2", "awsiotsdk~=1.21.0"],
"version": "1.0.9"
"requirements": ["awsiotsdk"],
"version": "1.0.10"
}
Loading