Skip to content

Commit

Permalink
[bug] reconnection problem fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
glaliberte committed Jan 27, 2025
1 parent c609184 commit 6e95fbb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
35 changes: 33 additions & 2 deletions custom_components/connectedroom/connectedroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def login(hass: HomeAssistant, api_key):
"websocket_key": json_data["websocket_key"],
}

async def stop(self):
def stop(self):
self.do_not_reconnect = True

if self.pusher:
Expand Down Expand Up @@ -102,13 +102,44 @@ async def connectedroom_websocket_connect(
custom_host=WSS_HOST,
auth_endpoint=API_URL + "/auth/websockets",
auth_endpoint_headers={"x-websocket-key": login["websocket_key"]},
reconnect_interval=15,
)

def connect_handler(data):
ConnectedRoomEvents(self, self.pusher, login["unique_id"])

self.pusher.connection.ping_interval = 15
def error_handler(data):
if "code" in data:
try:
error_code = int(data["code"])
except ValueError:
error_code = None

if error_code is not None:
self.pusher.connection.logger.error(
"Connection: Received error %s" % error_code
)

if (error_code >= 4200) and (error_code <= 4299):
# The connection SHOULD be re-established immediately
self.pusher.connection.reconnect(5)
else:
self.pusher.connection.reconnect()
else:
self.pusher.connection.logger.error(
"Connection: Unknown error code"
)
else:
self.pusher.connection.logger.error(
"Connection: No error code supplied"
)

self.pusher.connection.bind("pusher:connection_established", connect_handler)

self.pusher.connection.event_callbacks.pop("pusher:error")

self.pusher.connection.bind("pusher:error", error_handler)

self.pusher.connect()

return self.pusher
Expand Down
2 changes: 1 addition & 1 deletion custom_components/connectedroom/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
WSS_HOST = "ws.connectedroom.io"
WSS_KEY = "RiWn4MQFEc3yEEdbWYRFu8mV7HvkBW"

VERSION = "0.4.4"
VERSION = "0.5.0"
4 changes: 2 additions & 2 deletions custom_components/connectedroom/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ async def listen() -> None:
self.hass, listen(), "connectedroom-listen"
)

async def stop(self):
def stop(self):
"""Close WebSocket connection."""
if self.connectedroom is not None:
await self.connectedroom.stop()
self.connectedroom.stop()

async def _async_update_data(self):
"""Fetch data from WLED."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/connectedroom/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"iot_class": "cloud_push",
"issue_tracker": "https://github.com/glaliberte/connected-room-hass/issues",
"requirements": ["pysher==1.0.8"],
"version": "0.4.4"
"version": "0.5.0"
}

0 comments on commit 6e95fbb

Please sign in to comment.