Skip to content

Commit

Permalink
Merge pull request #202 from sh00t2kill/develop
Browse files Browse the repository at this point in the history
Develop
elad-bar authored Jun 20, 2024
2 parents 53c7785 + b6cea7c commit d6e7f35
Showing 4 changed files with 48 additions and 29 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v1.0.15

- Remove startup blocking call
- Improve reconnect process (cool-down between attempts)

## v1.0.14

- Fix reset configuration on integration unload (HA restart)
66 changes: 42 additions & 24 deletions custom_components/mydolphin_plus/managers/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from asyncio import sleep
from datetime import datetime, timedelta
import logging
import sys
@@ -6,7 +7,7 @@
from voluptuous import MultipleInvalid

from homeassistant.const import ATTR_ICON, ATTR_MODE, ATTR_STATE, CONF_STATE
from homeassistant.core import Event
from homeassistant.core import Event, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@@ -28,6 +29,7 @@
ACTION_ENTITY_TURN_OFF,
ACTION_ENTITY_TURN_ON,
API_DATA_SERIAL_NUMBER,
API_RECONNECT_INTERVAL,
ATTR_ACTIONS,
ATTR_ATTRIBUTES,
ATTR_CALCULATED_STATUS,
@@ -160,20 +162,6 @@ def __init__(self, hass, config_manager: ConfigManager):
self._api = RestAPI(hass, config_manager)
self._aws_client = AWSClient(hass, config_manager)

entry = config_manager.entry

entry.async_on_unload(
async_dispatcher_connect(
hass, SIGNAL_API_STATUS, self._on_api_status_changed
)
)

entry.async_on_unload(
async_dispatcher_connect(
hass, SIGNAL_AWS_CLIENT_STATUS, self._on_aws_client_status_changed
)
)

self._config_manager = config_manager

self._data_mapping = None
@@ -186,6 +174,8 @@ def __init__(self, hass, config_manager: ConfigManager):
SERVICE_EXIT_NAVIGATION: self._service_exit_navigation,
}

self._load_signal_handlers()

@property
def robot_name(self):
robot_name = self.api_data.get(DATA_ROBOT_NAME)
@@ -239,6 +229,31 @@ async def initialize(self):

await self._api.initialize(self._config_manager.aws_token_encrypted_key)

def _load_signal_handlers(self):
loop = self.hass.loop

@callback
def on_api_status_changed(entry_id: str, status: ConnectivityStatus):
loop.create_task(self._on_api_status_changed(entry_id, status)).__await__()

@callback
def on_aws_client_status_changed(entry_id: str, status: ConnectivityStatus):
loop.create_task(
self._on_aws_client_status_changed(entry_id, status)
).__await__()

self.config_entry.async_on_unload(
async_dispatcher_connect(
self.hass, SIGNAL_API_STATUS, on_api_status_changed
)
)

self.config_entry.async_on_unload(
async_dispatcher_connect(
self.hass, SIGNAL_AWS_CLIENT_STATUS, on_aws_client_status_changed
)
)

def get_device_serial_number(self) -> str:
serial_number = self.api_data.get(API_DATA_SERIAL_NUMBER)

@@ -299,13 +314,11 @@ async def _on_api_status_changed(self, entry_id: str, status: ConnectivityStatus

await self._aws_client.initialize()

elif status == ConnectivityStatus.Failed:
await self._aws_client.terminate()

await self._api.initialize(self._config_manager.aws_token_encrypted_key)

elif status == ConnectivityStatus.InvalidCredentials:
self.update_interval = None
elif status in [
ConnectivityStatus.Failed,
ConnectivityStatus.InvalidCredentials,
]:
await self._handle_connection_failure()

async def _on_aws_client_status_changed(
self, entry_id: str, status: ConnectivityStatus
@@ -317,9 +330,14 @@ async def _on_aws_client_status_changed(
await self._aws_client.update()

if status in [ConnectivityStatus.Failed, ConnectivityStatus.NotConnected]:
await self._aws_client.terminate()
await self._handle_connection_failure()

async def _handle_connection_failure(self):
await self._aws_client.terminate()

await sleep(API_RECONNECT_INTERVAL.total_seconds())

await self._api.initialize(self._config_manager.aws_token_encrypted_key)
await self._api.initialize(self._config_manager.aws_token_encrypted_key)

async def _async_update_data(self):
"""Fetch parameters from API endpoint.
4 changes: 0 additions & 4 deletions custom_components/mydolphin_plus/managers/rest_api.py
Original file line number Diff line number Diff line change
@@ -236,10 +236,6 @@ async def _async_get(self, url, headers: dict):
return result

async def update(self):
if self._status == ConnectivityStatus.Failed:
_LOGGER.debug("Connection failed. Reinitialize")
await self.initialize(self.aws_token_encrypted_key)

if self._status == ConnectivityStatus.Connected:
_LOGGER.debug("Connected. Refresh details")
await self._load_details()
2 changes: 1 addition & 1 deletion custom_components/mydolphin_plus/manifest.json
Original file line number Diff line number Diff line change
@@ -9,5 +9,5 @@
"iot_class": "cloud_push",
"issue_tracker": "https://github.com/sh00t2kill/dolphin-robot/issues",
"requirements": ["awsiotsdk"],
"version": "1.0.14"
"version": "1.0.15"
}

0 comments on commit d6e7f35

Please sign in to comment.