Skip to content

Commit 52ba3ff

Browse files
committed
0.1.8rc12
update pybambu module from upstream HA project
1 parent 98ab94b commit 52ba3ff

File tree

8 files changed

+456
-1316
lines changed

8 files changed

+456
-1316
lines changed

octoprint_bambu_printer/printer/bambu_virtual_printer.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,19 +259,20 @@ def _create_client_connection(self):
259259
f"connecting via local mqtt: {self._settings.get_boolean(['local_mqtt'])}"
260260
)
261261
bambu_client = BambuClient(
262-
device_type=self._settings.get(["device_type"]),
263-
serial=self._settings.get(["serial"]),
264-
host=self._settings.get(["host"]),
265-
username=(
262+
{"device_type": self._settings.get(["device_type"]),
263+
"serial": self._settings.get(["serial"]),
264+
"host": self._settings.get(["host"]),
265+
"username": (
266266
"bblp"
267267
if self._settings.get_boolean(["local_mqtt"])
268268
else self._settings.get(["username"])
269269
),
270-
access_code=self._settings.get(["access_code"]),
271-
local_mqtt=self._settings.get_boolean(["local_mqtt"]),
272-
region=self._settings.get(["region"]),
273-
email=self._settings.get(["email"]),
274-
auth_token=self._settings.get(["auth_token"]) if self._settings.get_boolean(["local_mqtt"]) is False else "",
270+
"access_code": self._settings.get(["access_code"]),
271+
"local_mqtt": self._settings.get_boolean(["local_mqtt"]),
272+
"region": self._settings.get(["region"]),
273+
"email": self._settings.get(["email"]),
274+
"auth_token": self._settings.get(["auth_token"]) if self._settings.get_boolean(["local_mqtt"]) is False else "",
275+
}
275276
)
276277
bambu_client.on_disconnect = self.on_disconnect(bambu_client.on_disconnect)
277278
bambu_client.on_connect = self.on_connect(bambu_client.on_connect)

octoprint_bambu_printer/printer/pybambu/bambu_client.py

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def run(self):
181181

182182
# Reset buffer
183183
img = None
184-
# else:
184+
# else:
185185
# Otherwise we need to continue looping without reseting the buffer to receive the remaining data
186186
# and without delaying.
187187

@@ -237,7 +237,7 @@ def run(self):
237237
exceptionSeen = ""
238238
while True:
239239
try:
240-
host = self._client.host if self._client.local_mqtt else self._client.bambu_cloud.cloud_mqtt_host
240+
host = self._client.host if self._client._local_mqtt else self._client.bambu_cloud.cloud_mqtt_host
241241
LOGGER.debug(f"Connect: Attempting Connection to {host}")
242242
self._client.client.connect(host, self._client._port, keepalive=5)
243243

@@ -285,25 +285,32 @@ class BambuClient:
285285
_camera = None
286286
_usage_hours: float
287287

288-
def __init__(self, device_type: str, serial: str, host: str, local_mqtt: bool, region: str, email: str,
289-
username: str, auth_token: str, access_code: str, usage_hours: float = 0, manual_refresh_mode: bool = False, chamber_image: bool = True):
290-
self.callback = None
291-
self.host = host
292-
self.local_mqtt = local_mqtt
293-
self._serial = serial
294-
self._auth_token = auth_token
295-
self._access_code = access_code
296-
self._username = username
288+
def __init__(self, config):
289+
self.host = config['host']
290+
self._callback = None
291+
292+
self._access_code = config.get('access_code', '')
293+
self._auth_token = config.get('auth_token', '')
294+
self._device_type = config.get('device_type', 'unknown')
295+
self._local_mqtt = config.get('local_mqtt', False)
296+
self._manual_refresh_mode = config.get('manual_refresh_mode', False)
297+
self._serial = config.get('serial', '')
298+
self._usage_hours = config.get('usage_hours', 0)
299+
self._username = config.get('username', '')
300+
self._enable_camera = config.get('enable_camera', True)
301+
297302
self._connected = False
298-
self._device_type = device_type
299-
self._usage_hours = usage_hours
300303
self._port = 1883
301304
self._refreshed = False
302-
self._manual_refresh_mode = manual_refresh_mode
305+
303306
self._device = Device(self)
304-
self.bambu_cloud = BambuCloud(region, email, username, auth_token)
307+
self.bambu_cloud = BambuCloud(
308+
config.get('region', ''),
309+
config.get('email', ''),
310+
config.get('username', ''),
311+
config.get('auth_token', '')
312+
)
305313
self.slicer_settings = SlicerSettings(self)
306-
self.use_chamber_image = chamber_image
307314

308315
@property
309316
def connected(self):
@@ -322,7 +329,22 @@ async def set_manual_refresh_mode(self, on):
322329
self.disconnect()
323330
else:
324331
# Reconnect normally
325-
self.connect(self.callback)
332+
self.connect(self._callback)
333+
334+
@property
335+
def camera_enabled(self):
336+
return self._enable_camera
337+
338+
def callback(self, event: str):
339+
if self._callback is not None:
340+
self._callback(event)
341+
342+
def set_camera_enabled(self, enable):
343+
self._enable_camera = enable
344+
if self._enable_camera:
345+
self._start_camera()
346+
else:
347+
self._stop_camera()
326348

327349
def setup_tls(self):
328350
self.client.tls_set(tls_version=ssl.PROTOCOL_TLS, cert_reqs=ssl.CERT_NONE)
@@ -331,7 +353,7 @@ def setup_tls(self):
331353
def connect(self, callback):
332354
"""Connect to the MQTT Broker"""
333355
self.client = mqtt.Client()
334-
self.callback = callback
356+
self._callback = callback
335357
self.client.on_connect = self.on_connect
336358
self.client.on_disconnect = self.on_disconnect
337359
self.client.on_message = self.on_message
@@ -342,7 +364,7 @@ def connect(self, callback):
342364
self.setup_tls()
343365

344366
self._port = 8883
345-
if self.local_mqtt:
367+
if self._local_mqtt:
346368
self.client.username_pw_set("bblp", password=self._access_code)
347369
else:
348370
self.client.username_pw_set(self._username, password=self._auth_token)
@@ -371,6 +393,22 @@ def on_connect(self,
371393
LOGGER.info("On Connect: Connected to printer")
372394
self._on_connect()
373395

396+
def _start_camera(self):
397+
if not self._device.supports_feature(Features.CAMERA_RTSP):
398+
if self._device.supports_feature(Features.CAMERA_IMAGE):
399+
if self._enable_camera:
400+
LOGGER.debug("Starting Chamber Image thread")
401+
self._camera = ChamberImageThread(self)
402+
self._camera.start()
403+
elif (self.host == "") or (self._access_code == ""):
404+
LOGGER.debug("Skipping camera setup as local access details not provided.")
405+
406+
def _stop_camera(self):
407+
if self._camera is not None:
408+
LOGGER.debug("Stopping camera thread")
409+
self._camera.stop()
410+
self._camera.join()
411+
374412
def _on_connect(self):
375413
self._connected = True
376414
self.subscribe_and_request_info()
@@ -379,14 +417,7 @@ def _on_connect(self):
379417
self._watchdog = WatchdogThread(self)
380418
self._watchdog.start()
381419

382-
if not self._device.supports_feature(Features.CAMERA_RTSP):
383-
if self._device.supports_feature(Features.CAMERA_IMAGE):
384-
if self.use_chamber_image:
385-
LOGGER.debug("Starting Chamber Image thread")
386-
self._camera = ChamberImageThread(self)
387-
self._camera.start()
388-
elif (self.host == "") or (self._access_code == ""):
389-
LOGGER.debug("Skipping camera setup as local access details not provided.")
420+
self._start_camera()
390421

391422
def try_on_connect(self,
392423
client_: mqtt.Client,
@@ -410,7 +441,7 @@ def on_disconnect(self,
410441
"""Called when MQTT Disconnects"""
411442
LOGGER.warn(f"On Disconnect: Printer disconnected with error code: {result_code}")
412443
self._on_disconnect()
413-
444+
414445
def _on_disconnect(self):
415446
LOGGER.debug("_on_disconnect: Lost connection to the printer")
416447
self._connected = False
@@ -419,10 +450,7 @@ def _on_disconnect(self):
419450
LOGGER.debug("Stopping watchdog thread")
420451
self._watchdog.stop()
421452
self._watchdog.join()
422-
if self._camera is not None:
423-
LOGGER.debug("Stopping camera thread")
424-
self._camera.stop()
425-
self._camera.join()
453+
self._stop_camera()
426454

427455
def _on_watchdog_fired(self):
428456
LOGGER.info("Watch dog fired")
@@ -487,7 +515,7 @@ async def refresh(self):
487515
"""Force refresh data"""
488516

489517
if self._manual_refresh_mode:
490-
self.connect(self.callback)
518+
self.connect(self._callback)
491519
else:
492520
LOGGER.debug("Force Refresh: Getting Version Info")
493521
self._refreshed = True
@@ -531,8 +559,8 @@ def on_message(client, userdata, message):
531559
# Run the blocking tls_set method in a separate thread
532560
loop = asyncio.get_event_loop()
533561
await loop.run_in_executor(None, self.setup_tls)
534-
535-
if self.local_mqtt:
562+
563+
if self._local_mqtt:
536564
self.client.username_pw_set("bblp", password=self._access_code)
537565
else:
538566
self.client.username_pw_set(self._username, password=self._auth_token)

0 commit comments

Comments
 (0)