Skip to content

Commit

Permalink
Update x1e detection (#501)
Browse files Browse the repository at this point in the history
* Cleanup printer type detection

* Simplify printer type detection and fix X1E detection
  • Loading branch information
AdrianGarside authored Apr 20, 2024
1 parent 02b6f58 commit d8d721c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
2 changes: 0 additions & 2 deletions custom_components/bambu_lab/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ def _update_external_spool_info(self):
manufacturer=BRAND,
sw_version="",
hw_version="")

# self.hass.async_create_task(self._reinitialize_sensors())

def PublishDeviceTriggerEvent(self, event: str):
dev_reg = device_registry.async_get(self._hass)
Expand Down
8 changes: 2 additions & 6 deletions custom_components/bambu_lab/pybambu/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ def info_update(self, data):
# },
modules = data.get("module", [])
self.device_type = get_printer_type(modules, self.device_type)
LOGGER.debug(f"Device is {self.device_type}")
self.hw_ver = get_hw_version(modules, self.hw_ver)
self.sw_ver = get_sw_version(modules, self.sw_ver)
if self._client.callback is not None:
Expand Down Expand Up @@ -840,7 +841,6 @@ def info_update(self, data):
# "sn": "**REDACTED**"
# }

received_ams_info = False
module_list = data.get("module", [])
for module in module_list:
name = module["name"]
Expand All @@ -859,19 +859,15 @@ def info_update(self, data):
self.data[index] = AMSInstance()

if self.data[index].serial != module['sn']:
received_ams_info = True
self.data[index].serial = module['sn']
if self.data[index].sw_version != module['sw_ver']:
received_ams_info = True
self.data[index].sw_version = module['sw_ver']
if self.data[index].hw_version != module['hw_ver']:
received_ams_info = True
self.data[index].hw_version = module['hw_ver']

data_changed = old_data != f"{self.__dict__}"
LOGGER.debug(f"UPDATED1: {received_ams_info} {data_changed}")

if received_ams_info:
if data_changed:
if self._client.callback is not None:
self._client.callback("event_ams_info_update")

Expand Down
90 changes: 57 additions & 33 deletions custom_components/bambu_lab/pybambu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,44 +117,68 @@ def get_generic_AMS_HMS_error_code(hms_code: str):


def get_printer_type(modules, default):
"""Retrieve printer type"""
esp32 = search(modules, lambda x: x.get('name', "") == "esp32")
rv1126 = search(modules, lambda x: x.get('name', "") == "rv1126")
if len(esp32.keys()) > 1:
if esp32.get("hw_ver") == "AP04":
if esp32.get("project_name") == "C11":
LOGGER.debug("Device is P1P")
return "P1P"
elif esp32.get("project_name") == "C12":
LOGGER.debug("Device is P1S")
return "P1S"
if esp32.get("hw_ver") == "AP05":
if esp32.get("project_name") == "N1":
LOGGER.debug("Device is A1 Mini")
return "A1MINI"
elif esp32.get("project_name") == "N2S":
LOGGER.debug("Device is A1")
return "A1"
LOGGER.debug(f"UNKNOWN DEVICE: esp32 = {esp32.get('hw_ver')}/'{esp32.get('project_name')}'")
elif len(rv1126.keys()) > 1:
if rv1126.get("hw_ver") == "AP05":
LOGGER.debug("Device is X1C")
return "X1C"
elif rv1126.get("hw_ver") == "AP02":
LOGGER.debug("Device is X1E")
return "X1E"
LOGGER.debug(f"UNKNOWN DEVICE: rv1126 = {rv1126.get('hw_ver')}")
# Known possible values:
#
# A1/P1 printers are of the form:
# {
# "name": "esp32",
# "project_name": "C11",
# "sw_ver": "01.07.23.47",
# "hw_ver": "AP04",
# "sn": "**REDACTED**",
# "flag": 0
# },
# P1P = AP04 / C11
# P1S = AP04 / C12
# A1Mini = AP05 / N1
# A1 = AP05 / N2S
#
# X1C printers are of the form:
# {
# "hw_ver": "AP05",
# "name": "rv1126",
# "sn": "**REDACTED**",
# "sw_ver": "00.00.28.55"
# },
# X1C = AP05
#
# X1E printers are of the form:
# {
# "flag": 0,
# "hw_ver": "AP02",
# "name": "ap",
# "sn": "**REDACTED**",
# "sw_ver": "00.00.32.14"
# }
# X1E = AP02

apNode = search(modules, lambda x: x.get('hw_ver', "").find("AP0") == 0)
if len(apNode.keys()) > 1:
hw_ver = apNode['hw_ver']
project_name = apNode.get('project_name', '')
if hw_ver == 'AP02':
return 'X1E'
elif hw_ver == 'AP04':
if project_name == 'C11':
return 'P1P'
if project_name == 'C12':
return 'P1S'
elif hw_ver == 'AP05':
if project_name == 'N1':
return 'A1MINI'
if project_name == 'N2S':
return 'A1'
if project_name == '':
return 'X1C'
LOGGER.debug(f"UNKNOWN DEVICE: hw_ver='{hw_ver}' / project_name='{project_name}'")
return default


def get_hw_version(modules, default):
"""Retrieve hardware version of printer"""
esp32 = search(modules, lambda x: x.get('name', "") == "esp32")
rv1126 = search(modules, lambda x: x.get('name', "") == "rv1126")
if len(esp32.keys()) > 1:
return esp32.get("hw_ver")
elif len(rv1126.keys()) > 1:
return rv1126.get("hw_ver")
apNode = search(modules, lambda x: x.get('hw_ver', "").find("AP0") == 0)
if len(apNode.keys()) > 1:
return apNode.get("hw_ver")
return default


Expand Down

0 comments on commit d8d721c

Please sign in to comment.