Skip to content

Commit

Permalink
Merge pull request #14 from tradel/little-socket-fixes
Browse files Browse the repository at this point in the history
Little socket fixes
  • Loading branch information
jaraco authored Jan 5, 2023
2 parents 2e30b1f + 3f9d75a commit 3186b29
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v3.1.2
======

#14: Fixed three minor issues with sockets/events.

v3.1.1
======

Expand Down
19 changes: 14 additions & 5 deletions jaraco/abode/event_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Abode cloud push events."""
import collections
import logging
import http.cookiejar

from jaraco.itertools import always_iterable

Expand All @@ -16,6 +17,17 @@
_LOGGER = logging.getLogger(__name__)


def _cookie_string(cookies: http.cookiejar.CookieJar):
"""
>>> jar = http.cookiejar.CookieJar()
>>> from test.test_http_cookiejar import interact_netscape
>>> _ = interact_netscape(jar, 'http://any/', 'foo=bar', 'bing=baz')
>>> _cookie_string(jar) in ['foo=bar; bing=baz', 'bing=baz; foo=bar']
True
"""
return "; ".join(f"{cookie.name}={cookie.value}" for cookie in cookies)


class EventController:
"""Class for subscribing to abode events."""

Expand Down Expand Up @@ -169,10 +181,7 @@ def socketio(self):

def _on_socket_started(self):
"""Socket IO startup callback."""
cookies = self._client._get_session().cookies.get_dict()
cookie_string = "; ".join([str(x) + "=" + str(y) for x, y in cookies.items()])

self._socketio.set_cookie(cookie_string)
self._socketio.set_cookie(_cookie_string(self._client._get_session().cookies))

def _on_socket_connected(self):
"""Socket IO connected callback."""
Expand All @@ -186,7 +195,7 @@ def _on_socket_connected(self):
# Callbacks should still execute even if refresh fails (Abode
# server issues) so that the entity availability in Home Assistant
# is updated since we are in fact connected to the web socket.
for callbacks in self._connection_status_callbacks.values:
for callbacks in self._connection_status_callbacks.values():
for callback in callbacks:
_execute_callback(callback)

Expand Down
6 changes: 4 additions & 2 deletions jaraco/abode/socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from datetime import datetime

from contextlib import suppress
from lomond import WebSocket
from lomond import events
from lomond.persist import persist
Expand Down Expand Up @@ -202,8 +203,9 @@ def _step(self, intervals):
intervals.reset()

name = event.__class__.__name__.lower()
method = getattr(self, f'_on_websocket_{name}')
method(event)
with suppress(AttributeError):
method = getattr(self, f'_on_websocket_{name}')
method(event)

if self._running is False:
self._websocket.close()
Expand Down

0 comments on commit 3186b29

Please sign in to comment.