Skip to content

Commit

Permalink
Merge pull request #78 from elad-bar/web-socket-keep-alive
Browse files Browse the repository at this point in the history
Fix websocket keep alive process
  • Loading branch information
elad-bar authored Dec 8, 2024
2 parents ddca032 + a838a91 commit 6fe9bff
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v3.0.14

- Corrected keep alive process to fix motion and sound detection - [Issue #77](https://github.com/elad-bar/ha-shinobi/issues/77)

## v3.0.13

- Fix missing webrtc initialization led for camera component to fail loading
Expand Down
16 changes: 8 additions & 8 deletions custom_components/shinobi/managers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,41 +137,41 @@ def _load_signal_handlers(self):

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

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

@callback
def on_monitor_discovered(entry_id: str, monitor: MonitorData):
loop.create_task(self._on_monitor_discovered(entry_id, monitor)).__await__()
loop.create_task(self._on_monitor_discovered(entry_id, monitor))

@callback
def on_monitor_updated(entry_id: str, monitor: MonitorData):
loop.create_task(self._on_monitor_updated(entry_id, monitor)).__await__()
loop.create_task(self._on_monitor_updated(entry_id, monitor))

@callback
def on_monitor_triggered(
entry_id: str, monitor_id: str, event_type: str, value
):
loop.create_task(
self._on_monitor_triggered(entry_id, monitor_id, event_type, value)
).__await__()
)

@callback
def on_monitor_status_changed(entry_id: str, monitor_id: str, status_code: int):
loop.create_task(
self._on_monitor_status_changed(entry_id, monitor_id, status_code)
).__await__()
)

@callback
def on_server_discovered(entry_id: str):
loop.create_task(self._on_server_discovered(entry_id)).__await__()
loop.create_task(self._on_server_discovered(entry_id))

@callback
def on_ws_ready(entry_id: str):
loop.create_task(self._on_ws_ready(entry_id)).__await__()
loop.create_task(self._on_ws_ready(entry_id))

signal_handlers = {
SIGNAL_API_STATUS: on_api_status_changed,
Expand Down
20 changes: 14 additions & 6 deletions custom_components/shinobi/managers/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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 @@ -99,7 +100,7 @@ def __init__(

self._messages_handler: dict = {
SHINOBI_WS_CONNECTION_ESTABLISHED_MESSAGE: self._handle_connection_established_message,
SHINOBI_WS_PONG_MESSAGE: self._handle_pong_message,
SHINOBI_WS_PING_MESSAGE: self._handle_ping_message,
SHINOBI_WS_CONNECTION_READY_MESSAGE: self._handle_ready_state_message,
SHINOBI_WS_ACTION_MESSAGE: self._handle_action_message,
}
Expand Down Expand Up @@ -195,8 +196,14 @@ async def initialize(self):
self._set_status(ConnectivityStatus.Connected)

self._ws = ws

await self._listen()

if self.status != ConnectivityStatus.Connected:
_LOGGER.warning(
f"Stopped listening to web socket, Status: {self.status}"
)

except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno
Expand Down Expand Up @@ -320,9 +327,10 @@ async def _handle_connection_established_message(self, prefix, data):
if self.version == 4:
await self._send(SHINOBI_WS_CONNECTION_READY_MESSAGE)

@staticmethod
async def _handle_pong_message(prefix, data):
_LOGGER.debug(f"Pong message received, ID: {prefix}, Payload: {data}")
async def _handle_ping_message(self, prefix, data):
_LOGGER.debug(f"Ping message received, ID: {prefix}, Payload: {data}")

await self._send(SHINOBI_WS_PONG_MESSAGE)

async def _handle_ready_state_message(self, prefix, data):
_LOGGER.debug(
Expand Down Expand Up @@ -628,8 +636,8 @@ def _set_status(self, status: ConnectivityStatus):
status,
)

def set_local_async_dispatcher_send(self, callback):
self._local_async_dispatcher_send = callback
def set_local_async_dispatcher_send(self, cb):
self._local_async_dispatcher_send = cb

def _async_dispatcher_send(self, signal: str, *args: Any) -> None:
if self._hass is None:
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.13"
"version": "3.0.14"
}

0 comments on commit 6fe9bff

Please sign in to comment.