Skip to content

Commit

Permalink
Fix stability issues - webrtc initialization, refresh request and web…
Browse files Browse the repository at this point in the history
…socket connection
  • Loading branch information
elad-bar committed Nov 26, 2024
1 parent 679ce13 commit 03bd273
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v3.0.13

- Fix missing webrtc initialization led for camera component to fail loading
- Removed first time loading by outdated function, instead use request to refresh data after loading is done (to avoid waiting for next iteration in 30 seconds)
- Fix stability of WebSocket connection

## v3.0.12

- Fix errors introduced in v3.0.11 while trying to get results from API
Expand Down
4 changes: 3 additions & 1 deletion custom_components/shinobi/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ def __init__(
):
super().__init__(hass, entity_description, coordinator, monitor)

Camera.__init__(self)

self.stream: Stream | None = None
self.stream_options: dict[str, str | bool | float] = {}
self.content_type: str = DEFAULT_CONTENT_TYPE
self.access_tokens: collections.deque = collections.deque([], 2)
self._warned_old_signature = False
self.async_update_token()
self._create_stream_lock: asyncio.Lock | None = None
self._rtsp_to_webrtc = False
self._rtsp_to_webrtc = True

self._stream_source = None

Expand Down
16 changes: 6 additions & 10 deletions custom_components/shinobi/managers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
DATA_KEY_SOUND_DETECTION,
DEFAULT_NAME,
DOMAIN,
HEARTBEAT_INTERVAL,
SIGNAL_API_STATUS,
SIGNAL_MONITOR_ADDED,
SIGNAL_MONITOR_DISCOVERED,
Expand Down Expand Up @@ -90,6 +89,7 @@ def __init__(self, hass, config_manager: ConfigManager):
name=config_manager.entry_title,
update_interval=UPDATE_ENTITIES_INTERVAL,
update_method=self._async_update_data,
config_entry=config_manager.entry,
)

self._api = RestAPI(hass, config_manager)
Expand Down Expand Up @@ -192,16 +192,17 @@ def on_ws_ready(entry_id: str):
)

async def initialize(self):
self._build_data_mapping()

entry = self.config_manager.entry
await self.hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

_LOGGER.info(f"Start loading {DOMAIN} integration, Entry ID: {entry.entry_id}")

self._build_data_mapping()

await self.hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

views_async_setup(self.hass, self._config_manager)

await self.async_config_entry_first_refresh()
await self.async_request_refresh()

await self._api.initialize()

Expand Down Expand Up @@ -379,11 +380,6 @@ async def _async_update_data(self):
if is_ready:
now = datetime.now().timestamp()

if now - self._last_heartbeat >= HEARTBEAT_INTERVAL.total_seconds():
await self._websockets.send_heartbeat()

self._last_heartbeat = now

if now - self._last_update >= UPDATE_API_INTERVAL.total_seconds():
await self._api.update()

Expand Down
21 changes: 3 additions & 18 deletions custom_components/shinobi/managers/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
SHINOBI_WS_CONNECTION_ESTABLISHED_MESSAGE,
SHINOBI_WS_CONNECTION_READY_MESSAGE,
SHINOBI_WS_ENDPOINT,
SHINOBI_WS_PING_MESSAGE,
SHINOBI_WS_PONG_MESSAGE,
SIGNAL_MONITOR_STATUS_CHANGED,
SIGNAL_MONITOR_TRIGGER,
Expand Down Expand Up @@ -247,23 +246,6 @@ async def _initialize_session(self):

self._set_status(ConnectivityStatus.Failed)

async def send_heartbeat(self):
if self._session is None or self._session.closed:
self._set_status(ConnectivityStatus.NotConnected)

if self.status == ConnectivityStatus.Connected:
try:
await self._ws.ping(SHINOBI_WS_PING_MESSAGE)

except ConnectionResetError as crex:
_LOGGER.debug(
f"Gracefully failed to send heartbeat - Restarting connection, Error: {crex}"
)
self._set_status(ConnectivityStatus.NotConnected)

except Exception as ex:
_LOGGER.error(f"Failed to send heartbeat, Error: {ex}")

async def _listen(self):
_LOGGER.info("Starting to listen connected")

Expand Down Expand Up @@ -377,6 +359,7 @@ async def _handle_action_message(self, prefix, data):
_LOGGER.debug(f"Payload ({prefix}) received, Type: {func}")

elif action == WS_EVENT_ACTION_PING:
_LOGGER.debug("Ping received")
await self._send_pong_message(data)

else:
Expand Down Expand Up @@ -499,6 +482,8 @@ async def _send_pong_message(self, data):
json_str = json.dumps(message_data)
message = f"42{json_str}"

_LOGGER.debug("Pong sent")

await self._send(message)

async def send_connect_monitor(self, monitor_id: str):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/shinobi/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/elad-bar/ha-shinobi/issues",
"requirements": [],
"version": "3.0.12"
"version": "3.0.13"
}

0 comments on commit 03bd273

Please sign in to comment.