Skip to content

Commit

Permalink
Merge pull request #422 from greghesp/develop
Browse files Browse the repository at this point in the history
Merge develop->main
  • Loading branch information
AdrianGarside authored Jan 10, 2024
2 parents 0a0f0fe + 778fe8e commit d9e20ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
17 changes: 13 additions & 4 deletions custom_components/bambu_lab/pybambu/bambu_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def run(self):
LOGGER.error(f"{self._client._device.info.device_type}: Socket error: {status}")
except socket.error as e:
LOGGER.error(f"{self._client._device.info.device_type}: Socket error: {e}")
# Sleep to allow printer to stabilize during boot when it may fail these connection attempts repeatedly.
time.sleep(1)
continue

sslSock.setblocking(False)
while not self._stop_event.is_set():
Expand Down Expand Up @@ -323,6 +326,9 @@ def on_connect(self,
properties: mqtt.Properties | None = None, ):
"""Handle connection"""
LOGGER.info("On Connect: Connected to Broker")
self._on_connect()

def _on_connect(self):
self._connected = True
self.subscribe_and_request_info()

Expand Down Expand Up @@ -356,6 +362,9 @@ def on_disconnect(self,
result_code: int):
"""Called when MQTT Disconnects"""
LOGGER.warn(f"On Disconnect: Disconnected from Broker: {result_code}")
self._on_disconnect()

def _on_disconnect(self):
self._connected = False
self._device.info.set_online(False)
if self._watchdog is not None:
Expand Down Expand Up @@ -387,14 +396,14 @@ def on_message(self, client, userdata, message):
if json_data.get("event"):
# These are events from the bambu cloud mqtt feed and allow us to detect when a local
# device has connected/disconnected (e.g. turned on/off)
LOGGER.debug(f"EVENT DATA: {message}")
if json_data.get("event").get("event") == "client.connected":
LOGGER.debug("Client connected event received.")
self._device.info.set_online(True)
self.subscribe_and_request_info()
self._watchdog.received_data()
self._on_disconnect() # We aren't guaranteed to recieve a client.disconnected event.
self._on_connect()
elif json_data.get("event").get("event") == "client.disconnected":
LOGGER.debug("Client disconnected event received.")
self._device.info.set_online(False)
self._on_disconnect()
else:
self._device.info.set_online(True)
self._watchdog.received_data()
Expand Down
3 changes: 3 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### V2.0.9
- Fix mishandling of printer power off/on when connected to bambu cloud mqtt.

### V2.0.8
- Fix missing chamber image on A1 Mini

Expand Down

0 comments on commit d9e20ea

Please sign in to comment.