@@ -44,26 +44,32 @@ <h1 class="title">Module <code>pyControl4.websocket</code></h1>
4444 self.token = kwargs.pop("token")
4545 self.callback = kwargs.pop("callback")
4646 self.session = kwargs.pop("session")
47+ self.connect_callback = kwargs.pop("connect_callback")
48+ self.disconnect_callback = kwargs.pop("disconnect_callback")
4749 super().__init__(*args, **kwargs)
4850 self.uri = "/api/v1/items/datatoui"
4951 self.subscriptionId = None
5052 self.connected = False
5153
52- def on_connect(self):
54+ async def on_connect(self):
5355 _LOGGER.debug("Control4 Director socket.io connection established!")
56+ if self.connect_callback is not None:
57+ await self.connect_callback()
5458
55- def on_disconnect(self):
59+ async def on_disconnect(self):
5660 self.connected = False
5761 self.subscriptionId = None
5862 _LOGGER.debug("Control4 Director socket.io disconnected.")
63+ if self.disconnect_callback is not None:
64+ await self.disconnect_callback()
5965
6066 async def trigger_event(self, event, *args):
6167 if event == "subscribe":
6268 await self.on_subscribe(*args)
6369 elif event == "connect":
64- self.on_connect()
70+ await self.on_connect()
6571 elif event == "disconnect":
66- self.on_disconnect()
72+ await self.on_disconnect()
6773 elif event == "clientId":
6874 await self.on_clientId(*args)
6975 elif event == self.subscriptionId:
@@ -113,22 +119,27 @@ <h1 class="title">Module <code>pyControl4.websocket</code></h1>
113119 self,
114120 ip,
115121 session_no_verify_ssl: aiohttp.ClientSession = None,
122+ connect_callback=None,
123+ disconnect_callback=None,
116124 ):
117125 """Creates a Control4 Websocket object.
118126
119127 Parameters:
120128 `ip` - The IP address of the Control4 Director/Controller.
121-
122129 `session` - (Optional) Allows the use of an
123130 `aiohttp.ClientSession` object
124131 for all network requests. This
125132 session will not be closed by the library.
126133 If not provided, the library will open and
127134 close its own `ClientSession`s as needed.
135+ `connect_callback` - (Optional) A callback to be called when the Websocket connection is opened or reconnected after a network error.
136+ `disconnect_callback` - (Optional) A callback to be called when the Websocket connection is lost due to a network error.
128137 """
129138 self.base_url = "https://{}".format(ip)
130139 self.wss_url = "wss://{}".format(ip)
131140 self.session = session_no_verify_ssl
141+ self.connect_callback = connect_callback
142+ self.disconnect_callback = disconnect_callback
132143
133144 # Keep track of the callbacks registered for each item id
134145 self._item_callbacks = dict()
@@ -145,13 +156,19 @@ <h1 class="title">Module <code>pyControl4.websocket</code></h1>
145156 else:
146157 await self._process_message(message)
147158
159+ @property
160+ def item_callbacks(self):
161+ """Returns a dictionary of registered item ids (key) and their callbacks (value).
162+
163+ item_callbacks cannot be modified directly. Use add_item_callback() and remove_item_callback() instead."""
164+ return self._item_callbacks
165+
148166 def add_item_callback(self, item_id, callback):
149167 """Register a callback to receive updates about an item.
150168 If a callback is already registered for the item, it will be overwritten with the provided callback.
151169
152170 Parameters:
153171 `item_id` - The Control4 item ID.
154-
155172 `callback` - The callback to be called when an update is received for the provided item id.
156173 """
157174
@@ -186,6 +203,8 @@ <h1 class="title">Module <code>pyControl4.websocket</code></h1>
186203 url=self.base_url,
187204 callback=self.callback,
188205 session=self.session,
206+ connect_callback=self.connect_callback,
207+ disconnect_callback=self.disconnect_callback,
189208 )
190209 )
191210 await self._sio.connect(
@@ -256,26 +275,32 @@ <h2 class="section-title" id="header-classes">Classes</h2>
256275 self.token = kwargs.pop("token")
257276 self.callback = kwargs.pop("callback")
258277 self.session = kwargs.pop("session")
278+ self.connect_callback = kwargs.pop("connect_callback")
279+ self.disconnect_callback = kwargs.pop("disconnect_callback")
259280 super().__init__(*args, **kwargs)
260281 self.uri = "/api/v1/items/datatoui"
261282 self.subscriptionId = None
262283 self.connected = False
263284
264- def on_connect(self):
285+ async def on_connect(self):
265286 _LOGGER.debug("Control4 Director socket.io connection established!")
287+ if self.connect_callback is not None:
288+ await self.connect_callback()
266289
267- def on_disconnect(self):
290+ async def on_disconnect(self):
268291 self.connected = False
269292 self.subscriptionId = None
270293 _LOGGER.debug("Control4 Director socket.io disconnected.")
294+ if self.disconnect_callback is not None:
295+ await self.disconnect_callback()
271296
272297 async def trigger_event(self, event, *args):
273298 if event == "subscribe":
274299 await self.on_subscribe(*args)
275300 elif event == "connect":
276- self.on_connect()
301+ await self.on_connect()
277302 elif event == "disconnect":
278- self.on_disconnect()
303+ await self.on_disconnect()
279304 elif event == "clientId":
280305 await self.on_clientId(*args)
281306 elif event == self.subscriptionId:
@@ -368,31 +393,35 @@ <h3>Methods</h3>
368393</ details >
369394</ dd >
370395< dt id ="pyControl4.websocket.C4DirectorNamespace.on_connect "> < code class ="name flex ">
371- < span > def < span class ="ident "> on_connect</ span > </ span > (< span > self)</ span >
396+ < span > async def < span class ="ident "> on_connect</ span > </ span > (< span > self)</ span >
372397</ code > </ dt >
373398< dd >
374399< div class ="desc "> </ div >
375400< details class ="source ">
376401< summary >
377402< span > Expand source code</ span >
378403</ summary >
379- < pre > < code class ="python "> def on_connect(self):
380- _LOGGER.debug("Control4 Director socket.io connection established!")</ code > </ pre >
404+ < pre > < code class ="python "> async def on_connect(self):
405+ _LOGGER.debug("Control4 Director socket.io connection established!")
406+ if self.connect_callback is not None:
407+ await self.connect_callback()</ code > </ pre >
381408</ details >
382409</ dd >
383410< dt id ="pyControl4.websocket.C4DirectorNamespace.on_disconnect "> < code class ="name flex ">
384- < span > def < span class ="ident "> on_disconnect</ span > </ span > (< span > self)</ span >
411+ < span > async def < span class ="ident "> on_disconnect</ span > </ span > (< span > self)</ span >
385412</ code > </ dt >
386413< dd >
387414< div class ="desc "> </ div >
388415< details class ="source ">
389416< summary >
390417< span > Expand source code</ span >
391418</ summary >
392- < pre > < code class ="python "> def on_disconnect(self):
419+ < pre > < code class ="python "> async def on_disconnect(self):
393420 self.connected = False
394421 self.subscriptionId = None
395- _LOGGER.debug("Control4 Director socket.io disconnected.")</ code > </ pre >
422+ _LOGGER.debug("Control4 Director socket.io disconnected.")
423+ if self.disconnect_callback is not None:
424+ await self.disconnect_callback()</ code > </ pre >
396425</ details >
397426</ dd >
398427< dt id ="pyControl4.websocket.C4DirectorNamespace.on_subscribe "> < code class ="name flex ">
@@ -426,9 +455,9 @@ <h3>Methods</h3>
426455 if event == "subscribe":
427456 await self.on_subscribe(*args)
428457 elif event == "connect":
429- self.on_connect()
458+ await self.on_connect()
430459 elif event == "disconnect":
431- self.on_disconnect()
460+ await self.on_disconnect()
432461 elif event == "clientId":
433462 await self.on_clientId(*args)
434463 elif event == self.subscriptionId:
@@ -444,18 +473,20 @@ <h3>Methods</h3>
444473</ dd >
445474< dt id ="pyControl4.websocket.C4Websocket "> < code class ="flex name class ">
446475< span > class < span class ="ident "> C4Websocket</ span > </ span >
447- < span > (</ span > < span > ip, session_no_verify_ssl: aiohttp.client.ClientSession = None)</ span >
476+ < span > (</ span > < span > ip, session_no_verify_ssl: aiohttp.client.ClientSession = None, connect_callback=None, disconnect_callback=None )</ span >
448477</ code > </ dt >
449478< dd >
450479< div class ="desc "> < p > Creates a Control4 Websocket object.</ p >
451480< h2 id ="parameters "> Parameters</ h2 >
452- < p > < code > ip</ code > - The IP address of the Control4 Director/Controller.</ p >
453- < p > < code > session</ code > - (Optional) Allows the use of an
481+ < p > < code > ip</ code > - The IP address of the Control4 Director/Controller.
482+ < code > session</ code > - (Optional) Allows the use of an
454483< code > aiohttp.ClientSession</ code > object
455484for all network requests. This
456485session will not be closed by the library.
457486If not provided, the library will open and
458- close its own < code > ClientSession</ code > s as needed.</ p > </ div >
487+ close its own < code > ClientSession</ code > s as needed.
488+ < code > connect_callback</ code > - (Optional) A callback to be called when the Websocket connection is opened or reconnected after a network error.
489+ < code > disconnect_callback</ code > - (Optional) A callback to be called when the Websocket connection is lost due to a network error.</ p > </ div >
459490< details class ="source ">
460491< summary >
461492< span > Expand source code</ span >
@@ -465,22 +496,27 @@ <h2 id="parameters">Parameters</h2>
465496 self,
466497 ip,
467498 session_no_verify_ssl: aiohttp.ClientSession = None,
499+ connect_callback=None,
500+ disconnect_callback=None,
468501 ):
469502 """Creates a Control4 Websocket object.
470503
471504 Parameters:
472505 `ip` - The IP address of the Control4 Director/Controller.
473-
474506 `session` - (Optional) Allows the use of an
475507 `aiohttp.ClientSession` object
476508 for all network requests. This
477509 session will not be closed by the library.
478510 If not provided, the library will open and
479511 close its own `ClientSession`s as needed.
512+ `connect_callback` - (Optional) A callback to be called when the Websocket connection is opened or reconnected after a network error.
513+ `disconnect_callback` - (Optional) A callback to be called when the Websocket connection is lost due to a network error.
480514 """
481515 self.base_url = "https://{}".format(ip)
482516 self.wss_url = "wss://{}".format(ip)
483517 self.session = session_no_verify_ssl
518+ self.connect_callback = connect_callback
519+ self.disconnect_callback = disconnect_callback
484520
485521 # Keep track of the callbacks registered for each item id
486522 self._item_callbacks = dict()
@@ -497,13 +533,19 @@ <h2 id="parameters">Parameters</h2>
497533 else:
498534 await self._process_message(message)
499535
536+ @property
537+ def item_callbacks(self):
538+ """Returns a dictionary of registered item ids (key) and their callbacks (value).
539+
540+ item_callbacks cannot be modified directly. Use add_item_callback() and remove_item_callback() instead."""
541+ return self._item_callbacks
542+
500543 def add_item_callback(self, item_id, callback):
501544 """Register a callback to receive updates about an item.
502545 If a callback is already registered for the item, it will be overwritten with the provided callback.
503546
504547 Parameters:
505548 `item_id` - The Control4 item ID.
506-
507549 `callback` - The callback to be called when an update is received for the provided item id.
508550 """
509551
@@ -538,6 +580,8 @@ <h2 id="parameters">Parameters</h2>
538580 url=self.base_url,
539581 callback=self.callback,
540582 session=self.session,
583+ connect_callback=self.connect_callback,
584+ disconnect_callback=self.disconnect_callback,
541585 )
542586 )
543587 await self._sio.connect(
@@ -574,6 +618,25 @@ <h2 id="parameters">Parameters</h2>
574618 except Exception as exc:
575619 _LOGGER.warning("Captured exception during callback: {}".format(str(exc)))</ code > </ pre >
576620</ details >
621+ < h3 > Instance variables</ h3 >
622+ < dl >
623+ < dt id ="pyControl4.websocket.C4Websocket.item_callbacks "> < code class ="name "> var < span class ="ident "> item_callbacks</ span > </ code > </ dt >
624+ < dd >
625+ < div class ="desc "> < p > Returns a dictionary of registered item ids (key) and their callbacks (value).</ p >
626+ < p > item_callbacks cannot be modified directly. Use add_item_callback() and remove_item_callback() instead.</ p > </ div >
627+ < details class ="source ">
628+ < summary >
629+ < span > Expand source code</ span >
630+ </ summary >
631+ < pre > < code class ="python "> @property
632+ def item_callbacks(self):
633+ """Returns a dictionary of registered item ids (key) and their callbacks (value).
634+
635+ item_callbacks cannot be modified directly. Use add_item_callback() and remove_item_callback() instead."""
636+ return self._item_callbacks</ code > </ pre >
637+ </ details >
638+ </ dd >
639+ </ dl >
577640< h3 > Methods</ h3 >
578641< dl >
579642< dt id ="pyControl4.websocket.C4Websocket.add_item_callback "> < code class ="name flex ">
@@ -583,8 +646,8 @@ <h3>Methods</h3>
583646< div class ="desc "> < p > Register a callback to receive updates about an item.
584647If a callback is already registered for the item, it will be overwritten with the provided callback.</ p >
585648< h2 id ="parameters "> Parameters</ h2 >
586- < p > < code > item_id</ code > - The Control4 item ID.</ p >
587- < p > < code > callback</ code > - The callback to be called when an update is received for the provided item id.</ p > </ div >
649+ < p > < code > item_id</ code > - The Control4 item ID.
650+ < code > callback</ code > - The callback to be called when an update is received for the provided item id.</ p > </ div >
588651< details class ="source ">
589652< summary >
590653< span > Expand source code</ span >
@@ -595,7 +658,6 @@ <h2 id="parameters">Parameters</h2>
595658
596659 Parameters:
597660 `item_id` - The Control4 item ID.
598-
599661 `callback` - The callback to be called when an update is received for the provided item id.
600662 """
601663
@@ -676,6 +738,8 @@ <h2 id="parameters">Parameters</h2>
676738 url=self.base_url,
677739 callback=self.callback,
678740 session=self.session,
741+ connect_callback=self.connect_callback,
742+ disconnect_callback=self.disconnect_callback,
679743 )
680744 )
681745 await self._sio.connect(
@@ -733,6 +797,7 @@ <h4><code><a title="pyControl4.websocket.C4Websocket" href="#pyControl4.websocke
733797< ul class ="">
734798< li > < code > < a title ="pyControl4.websocket.C4Websocket.add_item_callback " href ="#pyControl4.websocket.C4Websocket.add_item_callback "> add_item_callback</ a > </ code > </ li >
735799< li > < code > < a title ="pyControl4.websocket.C4Websocket.callback " href ="#pyControl4.websocket.C4Websocket.callback "> callback</ a > </ code > </ li >
800+ < li > < code > < a title ="pyControl4.websocket.C4Websocket.item_callbacks " href ="#pyControl4.websocket.C4Websocket.item_callbacks "> item_callbacks</ a > </ code > </ li >
736801< li > < code > < a title ="pyControl4.websocket.C4Websocket.remove_item_callback " href ="#pyControl4.websocket.C4Websocket.remove_item_callback "> remove_item_callback</ a > </ code > </ li >
737802< li > < code > < a title ="pyControl4.websocket.C4Websocket.sio_connect " href ="#pyControl4.websocket.C4Websocket.sio_connect "> sio_connect</ a > </ code > </ li >
738803< li > < code > < a title ="pyControl4.websocket.C4Websocket.sio_disconnect " href ="#pyControl4.websocket.C4Websocket.sio_disconnect "> sio_disconnect</ a > </ code > </ li >
0 commit comments