Skip to content

Commit

Permalink
refactor new client initialization process to non-blocking call
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed Jul 5, 2024
1 parent cb9539b commit b21984f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v1.0.16

- Refactor new client initialization process to non-blocking call
- Improved log messages of status changes
- Removed vacuum actions
- Turn on - not supported
Expand Down
63 changes: 35 additions & 28 deletions custom_components/mydolphin_plus/managers/aws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,36 +199,10 @@ async def initialize(self):

self._topic_data = TopicData(motor_unit_serial)

credentials_provider = auth.AwsCredentialsProvider.new_static(
aws_key, aws_secret, aws_token
)

ca_content = await self._get_certificate()

client = mqtt_connection_builder.websockets_with_default_aws_signing(
endpoint=AWS_IOT_URL,
port=AWS_IOT_PORT,
region=AWS_REGION,
ca_bytes=ca_content,
credentials_provider=credentials_provider,
client_id=self._awsiot_id,
clean_session=False,
keep_alive_secs=30,
on_connection_success=self._connection_callbacks.get(
ConnectionCallbacks.SUCCESS
),
on_connection_failure=self._connection_callbacks.get(
ConnectionCallbacks.FAILURE
),
on_connection_closed=self._connection_callbacks.get(
ConnectionCallbacks.CLOSED
),
on_connection_interrupted=self._connection_callbacks.get(
ConnectionCallbacks.INTERRUPTED
),
on_connection_resumed=self._connection_callbacks.get(
ConnectionCallbacks.RESUMED
),
client = await self._hass.async_add_executor_job(
self._get_client, aws_key, aws_secret, aws_token, ca_content
)

def _on_connect_future_completed(future):
Expand All @@ -248,6 +222,39 @@ def _on_connect_future_completed(future):

self._set_status(ConnectivityStatus.Failed, message)

def _get_client(self, aws_key, aws_secret, aws_token, ca_content):
credentials_provider = auth.AwsCredentialsProvider.new_static(
aws_key, aws_secret, aws_token
)

client = mqtt_connection_builder.websockets_with_default_aws_signing(
endpoint=AWS_IOT_URL,
port=AWS_IOT_PORT,
region=AWS_REGION,
ca_bytes=ca_content,
credentials_provider=credentials_provider,
client_id=self._awsiot_id,
clean_session=False,
keep_alive_secs=30,
on_connection_success=self._connection_callbacks.get(
ConnectionCallbacks.SUCCESS
),
on_connection_failure=self._connection_callbacks.get(
ConnectionCallbacks.FAILURE
),
on_connection_closed=self._connection_callbacks.get(
ConnectionCallbacks.CLOSED
),
on_connection_interrupted=self._connection_callbacks.get(
ConnectionCallbacks.INTERRUPTED
),
on_connection_resumed=self._connection_callbacks.get(
ConnectionCallbacks.RESUMED
),
)

return client

def _subscribe(self):
_LOGGER.debug(f"Subscribing topics: {self._topic_data.subscribe}")

Expand Down

0 comments on commit b21984f

Please sign in to comment.