greghesp / ha-bambulab

A Home Assistant Integration for Bambu Lab Printers
696 stars 58 forks source link

[Bug] Bambu Lab X1E (Firmware: 01.01.00.00) Hardware not detected #495

Closed phoenixswiss closed 2 months ago

phoenixswiss commented 2 months ago

Describe the bug

With the new firmware release 01.01.00.00, the Bambu Lab Printer X1E is no longer correctly recognised by the integration.

To Reproduce

  1. Add a Bambu Lab X1E to HA
  2. Open the device page of the newly added printer
  3. Device HW is UNKNOWN

Expected Behaviour

The device is correctly recognised as a Bambu Lab X1E

What device are you using?

X1E

Diagnostic Output

[...]

    "get_version": {
      "command": "get_version",
      "module": [
        {
          "flag": 3,
          "hw_ver": "N/A",
          "name": "ota",
          "sn": "**REDACTED**",
          "sw_ver": "01.01.00.00"
        },
        {
          "flag": 0,
          "hw_ver": "AMS08",
          "name": "ams/0",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.06.40"
        },
        {
          "flag": 0,
          "hw_ver": "MC01",
          "name": "mc",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.26.96"
        },
        {
          "flag": 0,
          "hw_ver": "SMC01",
          "name": "sm",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.26.96"
        },
        {
          "flag": 0,
          "hw_ver": "TH01",
          "name": "th",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.10.21"
        },
        {
          "flag": 0,
          "hw_ver": "CT01",
          "name": "ctc",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.06.75"
        },
        {
          "flag": 0,
          "hw_ver": "AP02",
          "name": "ap",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.32.14"
        }
      ],
      "sequence_id": "0"
    }
[...]

Log Extracts

No response

Other Information

It seems that with firmware version 01.01.00.00 the parsing strings "esp32" or "rv1126" don't exist any more for X1Es. A new string "ap" has been introduced.

I've modified the corresponding sections in custom_components/bambu_lab/pybambu/utils.py accordingly and it works now:

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")

    #fixed the issue
    ap = search(modules, lambda x: x.get('name', "") == "ap")

    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')}")

    #fixed the issue
    elif len(ap.keys()) > 1:
        if ap.get("hw_ver") == "AP05":
            LOGGER.debug("Device is X1C")
            return "X1C"
        elif ap.get("hw_ver") == "AP02":
            LOGGER.debug("Device is X1E")
            return "X1E"
        LOGGER.debug(f"UNKNOWN DEVICE: ap = {ap.get('hw_ver')}")        

    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")

    #fixed the issue
    ap = search(modules, lambda x: x.get('name', "") == "ap")

    if len(esp32.keys()) > 1:
        return esp32.get("hw_ver")
    elif len(rv1126.keys()) > 1:
        return rv1126.get("hw_ver")

    #fixed the issue
    elif len(ap.keys()) > 1:
        return ap.get("hw_ver")        

    return default
AdrianGarside commented 2 months ago

Yes, that's the correct fix given the way the code currently works. There are alternative ways to identify the model from the serial number but that would be much more opaque. I see you changed X1C lookup to also check for AP05 when the name is 'ap' - was there a specific reason for that? It may be time to key first off the APxx entries since that's the current way is a little inverted and not been revisited with a fresh eye since it was originally written very early on.

phoenixswiss commented 2 months ago

I've just added the AP05 (X1C) as they may have changed the string for this model from rv1126 to ap for this printer too.

AdrianGarside commented 2 months ago

I suspected they might. I’ve rewritten the detection. Logic to be simpler and more forgiving so it will be resilient to that now.

phoenixswiss commented 2 months ago

Thank you for your hard work!

AdrianGarside commented 2 months ago

Yep, just updated my X1C:

    {
      "flag": 0,
      "hw_ver": "AP05",
      "name": "ap",
      "sn": "**REDACTED**",
      "sw_ver": "00.00.32.17"
    }