added new on_ws_connect callback

This commit is contained in:
Patrick762
2023-04-22 13:57:15 +02:00
parent b5a63bc011
commit abf828a34b

View File

@@ -28,6 +28,7 @@ class StreamDeckApi:
on_button_release: Callable[[str], None] | None = None, on_button_release: Callable[[str], None] | None = None,
on_status_update: Callable[[SDInfo], None] | None = None, on_status_update: Callable[[SDInfo], None] | None = None,
on_ws_message: Callable[[SDWebsocketMessage], None] | None = None, on_ws_message: Callable[[SDWebsocketMessage], None] | None = None,
on_ws_connect: Callable[[], None] | None = None,
) -> None: ) -> None:
"""Init Stream Deck API object.""" """Init Stream Deck API object."""
self._host = host self._host = host
@@ -35,6 +36,7 @@ class StreamDeckApi:
self._on_button_release = on_button_release self._on_button_release = on_button_release
self._on_status_update = on_status_update self._on_status_update = on_status_update
self._on_ws_message = on_ws_message self._on_ws_message = on_ws_message
self._on_ws_connect = on_ws_connect
self._loop = asyncio.get_event_loop() self._loop = asyncio.get_event_loop()
self._running = False self._running = False
self._task: asyncio.Task | None = None self._task: asyncio.Task | None = None
@@ -90,10 +92,10 @@ class StreamDeckApi:
try: try:
res = requests.post(url, data, headers=headers, timeout=5) res = requests.post(url, data, headers=headers, timeout=5)
except requests.RequestException: except requests.RequestException:
_LOGGER.error("Error sending data to Stream Deck Plugin (exception)") _LOGGER.debug("Error sending data to Stream Deck Plugin (exception)")
return None return None
if res.status_code != 200: if res.status_code != 200:
_LOGGER.info( _LOGGER.debug(
"Error sending data to Stream Deck Plugin (%s). Is the button currently visible?", "Error sending data to Stream Deck Plugin (%s). Is the button currently visible?",
res.reason, res.reason,
) )
@@ -114,12 +116,12 @@ class StreamDeckApi:
try: try:
rjson = res.json() rjson = res.json()
except requests.JSONDecodeError: except requests.JSONDecodeError:
_LOGGER.error("Error decoding response from %s", self._info_url) _LOGGER.debug("Error decoding response from %s", self._info_url)
return None return None
try: try:
info = SDInfo(rjson) info = SDInfo(rjson)
except KeyError: except KeyError:
_LOGGER.error("Error parsing response from %s to SDInfo", self._info_url) _LOGGER.debug("Error parsing response from %s to SDInfo", self._info_url)
return None return None
return info return info
@@ -130,7 +132,7 @@ class StreamDeckApi:
if res is None or res.status_code != 200: if res is None or res.status_code != 200:
return None return None
if res.headers.get("Content-Type", "") != "image/svg+xml": if res.headers.get("Content-Type", "") != "image/svg+xml":
_LOGGER.error("Invalid content type received from %s", url) _LOGGER.debug("Invalid content type received from %s", url)
return None return None
return res.text return res.text
@@ -178,12 +180,12 @@ class StreamDeckApi:
try: try:
datajson = json.loads(msg) datajson = json.loads(msg)
except json.JSONDecodeError: except json.JSONDecodeError:
_LOGGER.warning("Method _on_message: Websocket message couldn't get parsed") _LOGGER.debug("Method _on_message: Websocket message couldn't get parsed")
return return
try: try:
data = SDWebsocketMessage(datajson) data = SDWebsocketMessage(datajson)
except KeyError: except KeyError:
_LOGGER.warning( _LOGGER.debug(
"Method _on_message: Websocket message couldn't get parsed to SDWebsocketMessage" "Method _on_message: Websocket message couldn't get parsed to SDWebsocketMessage"
) )
return return
@@ -212,9 +214,11 @@ class StreamDeckApi:
while self._running: while self._running:
info = await self.get_info() info = await self.get_info()
if isinstance(info, SDInfo): if isinstance(info, SDInfo):
_LOGGER.info("Method _websocket_loop: Streamdeck online") _LOGGER.debug("Method _websocket_loop: Streamdeck online")
try: try:
async with connect(self._websocket_url) as websocket: async with connect(self._websocket_url) as websocket:
if self._on_ws_connect is not None:
self._on_ws_connect()
try: try:
while self._running: while self._running:
data = await asyncio.wait_for( data = await asyncio.wait_for(
@@ -222,17 +226,17 @@ class StreamDeckApi:
) )
self._on_message(data) self._on_message(data)
await websocket.close() await websocket.close()
_LOGGER.info("Method _websocket_loop: Websocket closed") _LOGGER.debug("Method _websocket_loop: Websocket closed")
except WebSocketException: except WebSocketException:
_LOGGER.warning( _LOGGER.debug(
"Method _websocket_loop: Websocket client crashed. Restarting it" "Method _websocket_loop: Websocket client crashed. Restarting it"
) )
except asyncio.TimeoutError: except asyncio.TimeoutError:
_LOGGER.warning( _LOGGER.debug(
"Method _websocket_loop: Websocket client timed out. Restarting it" "Method _websocket_loop: Websocket client timed out. Restarting it"
) )
except WebSocketException: except WebSocketException:
_LOGGER.warning( _LOGGER.debug(
"Method _websocket_loop: Websocket client not connecting. Restarting it" "Method _websocket_loop: Websocket client not connecting. Restarting it"
) )