greghesp / ha-bambulab

A Home Assistant Integration for Bambu Lab Printers
761 stars 63 forks source link

[Bug] Trying to add 2nd printer #270

Closed jphoke closed 8 months ago

jphoke commented 9 months ago

Describe the bug

Just moved over from the MQTT integration to the HACS path for ease of management ...

I have two printers, installed the HACS integration, all the other req's and have one printer all set and configured without issue. I am trying to add a printer via the Integration's Add Entry flow, and I enter the 2nd printer's Serial#, IP Address and LAN Code. It errors out as below every time

Screenshot 2023-11-16 at 5 46 42 PM

I have tried again with debug logging enabled (attached) home-assistant_bambu_lab_2023-11-16T22-46-47.793Z.log

To Reproduce

Navigate Settings > Devices & Services > Bambu Lab On Integration Entries Page Click "Add Entry" to add a second printer Follow prompts and input Serial Number, IP Address and LAN Code Hit Next Errors Out

Expected Behaviour

To configure and include 2nd printer into HA

What device are you using?

X1C

Diagnostic Output

{
  "home_assistant": {
    "installation_type": "Home Assistant OS",
    "version": "2023.11.2",
    "dev": false,
    "hassio": true,
    "virtualenv": false,
    "python_version": "3.11.6",
    "docker": true,
    "arch": "aarch64",
    "timezone": "America/New_York",
    "os_name": "Linux",
    "os_version": "6.1.21-v8",
    "supervisor": "2023.11.3",
    "host_os": "Home Assistant OS 11.1",
    "docker_version": "24.0.6",
    "chassis": "embedded",
    "run_as_root": true
  },
  "custom_components": {
    "bambu_lab": {
      "version": "1.8.0",
      "requirements": []
    },
    "readme": {
      "version": "0.5.0",
      "requirements": []
    },
    "openplantbook": {
      "version": "1.2.0",
      "requirements": [
        "pyopenplantbook==0.0.4"
      ]
    },
    "fontawesome": {
      "version": "2.2.1",
      "requirements": []
    },
    "dreo": {
      "version": "0.0.2",
      "requirements": [
        "websockets"
      ]
    },
    "pyscript": {
      "version": "1.5.0",
      "requirements": [
        "croniter==1.3.8",
        "watchdog==2.3.1"
      ]
    },
    "ha_blueair": {
      "version": "1.7.4",
      "requirements": [
        "blueair-api==1.8.7"
      ]
    },
    "plant": {
      "version": "2023.11.0",
      "requirements": [
        "async-timeout>=4.0.2"
      ]
    },
    "adaptive_lighting": {
      "version": "1.19.0",
      "requirements": [
        "ulid-transform"
      ]
    },
    "huesyncbox": {
      "version": "2.0.0",
      "requirements": [
        "aiohuesyncbox==0.0.26"
      ]
    },
    "hacs": {
      "version": "1.33.0",
      "requirements": [
        "aiogithubapi>=22.10.1"
      ]
    }
  },
  "integration_manifest": {
    "domain": "bambu_lab",
    "name": "Bambu Lab",
    "codeowners": [
      "@greghesp",
      "@AdrianGarside"
    ],
    "config_flow": true,
    "dependencies": [
      "device_automation",
      "mqtt",
      "ffmpeg"
    ],
    "documentation": "https://github.com/greghesp/ha-bambulab",
    "iot_class": "local_push",
    "issue_tracker": "https://github.com/greghesp/ha-bambulab/issues",
    "ssdp": [
      {
        "st": "urn:bambulab-com:device:3dprinter:1"
      }
    ],
    "version": "1.8.0",
    "is_built_in": false
  },
  "data": {
    "push_all": {
      "ams": {
        "ams": [
          {
            "humidity": "5",
            "id": "0",
            "temp": "23.9",
            "tray": [
              {
                "bed_temp": "0",
                "bed_temp_type": "0",
                "cali_idx": -1,
                "cols": [
                  "161616FF"
                ],
                "ctype": 0,
                "drying_temp": "0",
                "drying_time": "0",
                "id": "0",
                "nozzle_temp_max": "240",
                "nozzle_temp_min": "190",
                "remain": -1,
                "tag_uid": "0000000000000000",
                "tray_color": "161616FF",
                "tray_diameter": "0.00",
                "tray_id_name": "",
                "tray_info_idx": "GFL01",
                "tray_sub_brands": "",
                "tray_type": "PLA",
                "tray_uuid": "00000000000000000000000000000000",
                "tray_weight": "0",
                "xcam_info": "000000000000000000000000"
              },
              {
                "bed_temp": "0",
                "bed_temp_type": "0",
                "cali_idx": 8854,
                "cols": [
                  "E8E6D0FE"
                ],
                "ctype": 0,
                "drying_temp": "0",
                "drying_time": "0",
                "id": "1",
                "nozzle_temp_max": "240",
                "nozzle_temp_min": "190",
                "remain": -1,
                "tag_uid": "0000000000000000",
                "tray_color": "E8E6D0FE",
                "tray_diameter": "0.00",
                "tray_id_name": "",
                "tray_info_idx": "GFL00",
                "tray_sub_brands": "",
                "tray_type": "PLA",
                "tray_uuid": "00000000000000000000000000000000",
                "tray_weight": "0",
                "xcam_info": "000000000000000000000000"
              },
              {
                "bed_temp": "0",
                "bed_temp_type": "0",
                "cali_idx": -1,
                "cols": [
                  "898989FF"
                ],
                "ctype": 0,
                "drying_temp": "0",
                "drying_time": "0",
                "id": "2",
                "nozzle_temp_max": "240",
                "nozzle_temp_min": "190",
                "remain": -1,
                "tag_uid": "0000000000000000",
                "tray_color": "898989FF",
                "tray_diameter": "0.00",
                "tray_id_name": "",
                "tray_info_idx": "GFL01",
                "tray_sub_brands": "",
                "tray_type": "PLA",
                "tray_uuid": "00000000000000000000000000000000",
                "tray_weight": "0",
                "xcam_info": "000000000000000000000000"
              },
              {
                "bed_temp": "0",
                "bed_temp_type": "0",
                "cali_idx": 8854,
                "cols": [
                  "FFFFFFFF"
                ],
                "ctype": 0,
                "drying_temp": "0",
                "drying_time": "0",
                "id": "3",
                "nozzle_temp_max": "240",
                "nozzle_temp_min": "190",
                "remain": -1,
                "tag_uid": "0000000000000000",
                "tray_color": "FFFFFFFF",
                "tray_diameter": "0.00",
                "tray_id_name": "",
                "tray_info_idx": "GFL00",
                "tray_sub_brands": "",
                "tray_type": "PLA",
                "tray_uuid": "00000000000000000000000000000000",
                "tray_weight": "0",
                "xcam_info": "000000000000000000000000"
              }
            ]
          },
          {
            "humidity": "4",
            "id": "1",
            "temp": "22.7",
            "tray": [
              {
                "bed_temp": "0",
                "bed_temp_type": "0",
                "cali_idx": -1,
                "cols": [
                  "F930F3FF"
                ],
                "ctype": 0,
                "drying_temp": "0",
                "drying_time": "0",
                "id": "0",
                "nozzle_temp_max": "240",
                "nozzle_temp_min": "190",
                "remain": -1,
                "tag_uid": "0000000000000000",
                "tray_color": "F930F3FF",
                "tray_diameter": "0.00",
                "tray_id_name": "",
                "tray_info_idx": "GFL99",
                "tray_sub_brands": "",
                "tray_type": "PLA",
                "tray_uuid": "00000000000000000000000000000000",
                "tray_weight": "0",
                "xcam_info": "000000000000000000000000"
              },
              {
                "id": "1"
              },
              {
                "id": "2"
              },
              {
                "bed_temp": "40",
                "bed_temp_type": "1",
                "cali_idx": -1,
                "cols": [
                  "FFFFFFFF"
                ],
                "ctype": 0,
                "drying_temp": "55",
                "drying_time": "8",
                "id": "3",
                "nozzle_temp_max": "230",
                "nozzle_temp_min": "190",
                "remain": 8,
                "tag_uid": "3D4BDC9400000100",
                "tray_color": "FFFFFFFF",
                "tray_diameter": "1.75",
                "tray_id_name": "S02-W0",
                "tray_info_idx": "GFS02",
                "tray_sub_brands": "Support for PLA",
                "tray_type": "PLA-S",
                "tray_uuid": "5E25C2CE903241A0822D0C1CB2477B54",
                "tray_weight": "250",
                "xcam_info": "3421D007E803E8039A99193F"
              }
            ]
          }
        ],
        "ams_exist_bits": "3",
        "insert_flag": true,
        "power_on_flag": true,
        "tray_exist_bits": "bf",
        "tray_is_bbl_bits": "9f",
        "tray_now": "255",
        "tray_pre": "255",
        "tray_read_done_bits": "bf",
        "tray_reading_bits": "0",
        "tray_tar": "255",
        "version": 140
      },
      "ams_rfid_status": 0,
      "ams_status": 0,
      "aux_part_fan": true,
      "bed_target_temper": 0.0,
      "bed_temper": 17.0,
      "big_fan1_speed": "0",
      "big_fan2_speed": "0",
      "cali_version": 0,
      "chamber_temper": 22.0,
      "command": "push_status",
      "cooling_fan_speed": "0",
      "fail_reason": "0",
      "fan_gear": 0,
      "filam_bak": [],
      "force_upgrade": false,
      "gcode_file": "",
      "gcode_file_prepare_percent": "0",
      "gcode_start_time": "0",
      "gcode_state": "IDLE",
      "heatbreak_fan_speed": "0",
      "hms": [],
      "home_flag": 118144,
      "hw_switch_state": 0,
      "ipcam": {
        "ipcam_dev": "1",
        "ipcam_record": "enable",
        "mode_bits": 2,
        "resolution": "720p",
        "rtsp_url": "**REDACTED**",
        "timelapse": "disable",
        "tutk_server": "disable"
      },
      "layer_num": 0,
      "lifecycle": "product",
      "lights_report": [
        {
          "mode": "on",
          "node": "chamber_light"
        },
        {
          "mode": "flashing",
          "node": "work_light"
        }
      ],
      "maintain": 131075,
      "mc_percent": 0,
      "mc_print_error_code": "0",
      "mc_print_stage": "1",
      "mc_print_sub_stage": 0,
      "mc_remaining_time": 0,
      "mess_production_state": "active",
      "msg": 0,
      "nozzle_diameter": "0.4",
      "nozzle_target_temper": 0.0,
      "nozzle_temper": 23.0,
      "nozzle_type": "hardened_steel",
      "online": {
        "ahb": true,
        "ext": false,
        "version": 13
      },
      "print_error": 0,
      "print_gcode_action": 0,
      "print_real_action": 0,
      "print_type": "",
      "profile_id": "",
      "project_id": "",
      "queue_est": 0,
      "queue_number": 0,
      "queue_sts": 0,
      "queue_total": 0,
      "s_obj": [],
      "sdcard": true,
      "sequence_id": "238",
      "spd_lvl": 2,
      "spd_mag": 100,
      "stg": [],
      "stg_cur": -1,
      "subtask_id": "",
      "subtask_name": "",
      "task_id": "",
      "total_layer_num": 0,
      "upgrade_state": {
        "ahb_new_version_number": "",
        "ams_new_version_number": "",
        "consistency_request": false,
        "dis_state": 1,
        "err_code": 0,
        "ext_new_version_number": "",
        "force_upgrade": false,
        "idx": 13,
        "message": "",
        "module": "null",
        "new_version_state": 1,
        "ota_new_version_number": "01.06.06.00",
        "progress": "0",
        "sequence_id": 0,
        "sn": "**REDACTED**",
        "status": "IDLE"
      },
      "upload": {
        "file_size": 0,
        "finish_size": 0,
        "message": "Good",
        "oss_url": "",
        "progress": 0,
        "sequence_id": "0903",
        "speed": 0,
        "status": "idle",
        "task_id": "",
        "time_remaining": 0,
        "trouble_id": ""
      },
      "vt_tray": {
        "bed_temp": "0",
        "bed_temp_type": "0",
        "cali_idx": 4121,
        "cols": [
          "FFFFFFFF"
        ],
        "ctype": 0,
        "drying_temp": "0",
        "drying_time": "0",
        "id": "254",
        "nozzle_temp_max": "240",
        "nozzle_temp_min": "190",
        "remain": 0,
        "tag_uid": "0000000000000000",
        "tray_color": "FFFFFFFF",
        "tray_diameter": "0.00",
        "tray_id_name": "",
        "tray_info_idx": "GFA00",
        "tray_sub_brands": "",
        "tray_type": "PLA",
        "tray_uuid": "00000000000000000000000000000000",
        "tray_weight": "0",
        "xcam_info": "000000000000000000000000"
      },
      "wifi_signal": "-41dBm",
      "xcam": {
        "allow_skip_parts": false,
        "buildplate_marker_detector": true,
        "first_layer_inspector": true,
        "halt_print_sensitivity": "medium",
        "print_halt": true,
        "printing_monitor": true,
        "spaghetti_detector": true
      },
      "xcam_status": "0"
    },
    "get_version": {
      "command": "get_version",
      "module": [
        {
          "hw_ver": "",
          "name": "ota",
          "sn": "",
          "sw_ver": "01.06.05.01"
        },
        {
          "hw_ver": "AP05",
          "name": "rv1126",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.24.13"
        },
        {
          "hw_ver": "TH09",
          "name": "th",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.05.83"
        },
        {
          "hw_ver": "MC07",
          "name": "mc",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.16.68/00.00.16.68"
        },
        {
          "hw_ver": "",
          "name": "xm",
          "sn": "",
          "sw_ver": "00.01.02.02"
        },
        {
          "hw_ver": "AMS08",
          "name": "ams/0",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.06.40"
        },
        {
          "hw_ver": "AMS08",
          "name": "ams/1",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.06.40"
        },
        {
          "hw_ver": "AHB01",
          "name": "ahb",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.00.42"
        }
      ],
      "sequence_id": "0"
    }
  }
}

Log Extracts

2023-11-16 17:46:28.641 DEBUG (MainThread) [custom_components.bambu_lab] Config Flow: Trying Lan Mode Connection
2023-11-16 17:46:28.642 DEBUG (MainThread) [custom_components.bambu_lab.pybambu] Try Connection
2023-11-16 17:46:28.688 DEBUG (MainThread) [custom_components.bambu_lab.pybambu] Try Connection: Connecting to 192.168.1.176 for connection test
2023-11-16 17:46:28.787 INFO (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] On Connect: Connected to Broker
2023-11-16 17:46:28.788 DEBUG (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] Now Subscribing...
2023-11-16 17:46:28.788 DEBUG (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] Subscribing: device/00M00A342400066/report
2023-11-16 17:46:28.789 DEBUG (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] On Connect: Getting Version Info
2023-11-16 17:46:28.790 DEBUG (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] Sent {'info': {'sequence_id': '0', 'command': 'get_version'}} to topic device/00M00A342400066/request
2023-11-16 17:46:28.790 DEBUG (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] On Connect: Request Push All
2023-11-16 17:46:28.792 DEBUG (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] Sent {'pushing': {'sequence_id': '0', 'command': 'pushall'}} to topic device/00M00A342400066/request
2023-11-16 17:46:28.792 DEBUG (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] Starting watchdog thread
2023-11-16 17:46:28.793 DEBUG (Thread-15) [custom_components.bambu_lab.pybambu] Watchdog thread started.
2023-11-16 17:46:28.824 DEBUG (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] Try Connection: Got '<paho.mqtt.client.MQTTMessage object at 0x7f59386ff0>'
2023-11-16 17:46:28.825 DEBUG (Thread-14 (_thread_main)) [custom_components.bambu_lab.pybambu] Got Version Command Data
2023-11-16 17:46:28.825 ERROR (Thread-14 (_thread_main)) [root] Uncaught thread exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/threading.py", line 1045, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.11/threading.py", line 982, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
         ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 1164, in _loop
    rc = self.loop_read()
         ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 1556, in loop_read
    rc = self._packet_read()
         ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 2439, in _packet_read
    rc = self._packet_handle()
         ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3033, in _packet_handle
    return self._handle_publish()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3327, in _handle_publish
    self._handle_on_message(message)
  File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3570, in _handle_on_message
    on_message(self, self._userdata, message)
  File "/config/custom_components/bambu_lab/pybambu/bambu_client.py", line 249, in on_message
    self._device.info_update(data=json_data.get("info"))
  File "/config/custom_components/bambu_lab/pybambu/models.py", line 59, in info_update
    self.ams.info_update(data)
  File "/config/custom_components/bambu_lab/pybambu/models.py", line 510, in info_update
    if self.data[index].serial != module['sn']:
       ~~~~~~~~~^^^^^^^
IndexError: list index out of range
2023-11-16 17:46:38.778 DEBUG (MainThread) [custom_components.bambu_lab.pybambu] Disconnect: Client Disconnecting

Other Information

No response

AdrianGarside commented 9 months ago

Well that's odd. The crashing code is here:

    module_list = data.get("module", [])
    for module in module_list:
        name = module["name"]
        if name.startswith("ams/"):
            index = int(name[4])
            # Sometimes we get incomplete version data. We have to skip if that occurs since the serial number is
            # requires as part of the home assistant device identity.
            if not module['sn'] == '':
                # May get data before info so create entry if necessary
                if len(self.data) <= index:
                    received_ams_info = True
                    self.data.append(AMSInstance())
                if self.data[index].serial != module['sn']:   // FAILS HERE
                    received_ams_info = True
                    self.data[index].serial = module['sn']

But from the config data you provided has: { "hw_ver": "AMS08", "name": "ams/0", "sn": "REDACTED", "sw_ver": "00.00.06.40" }, { "hw_ver": "AMS08", "name": "ams/1", "sn": "REDACTED", "sw_ver": "00.00.06.40" },

So we'd get index = 0 initially while the length of the ams instance list is zero (empty). So we'd add an entry. At which point the failing line shouldn't get an index out of range error. And on the second loop through we'd have index == 1 and since 1 <= 1 we'd add another AMSInstance and then the failing line should succeed again. I don't see how it could be failing the way it clearly is.

jphoke commented 9 months ago

Adrian -- hhmmmmm

This printer had 2 AMS connected, one is Dead waiting on RMA from BL ... It looks like it still sees it SHOULD be there but it is not physically attached... I'll run some more tests when I get back from work tonight ... maybe attaching it even if it is error coding will get it along....

That said -- if that is the case ... that would be odd that you cannot remove/add/swap AMS units but one thing at a time :)

AdrianGarside commented 9 months ago

I would expect that if the AMS isn't physically attached (vs attached but dead/misbehaving) that we'd see just a single AMS in the version data. We definitely should handle this without throwing an exception. Both AMSs are still listed in both the version data and the general state data that you provided.

I did, however, make a mistake early on and chose unique IDs for AMSs that mean we can't reliably handle AMSs being added/removed as they may change IDs when you do that. And I haven't yet worked out how to get HA to let me fix up device/entity naming mistakes without leaving everyone with dead devices/entities they have to manually delete.

jphoke commented 9 months ago

Thanks -- Im working and away from the house until tonight (after 7p NY time) I'll see if I can run a few variations and update with the logs ...

I did look back at the pics I took of the X1C screen ... the 2nd AMS is listed on the device but not connected in any fashion -- may do a hard reset and see if its a BL bug too

AdrianGarside commented 8 months ago

I just got my X1C and had no issues adding it. I think what must have happened is that your AMS was in bad state such that the first one (ams/0) was missing from the version info list while the second one (ams/1) was still present. I can make a code change to be resilient to that. It's the only way I can see that particular code hitting that particular error. This is definitely not the case in your later logs though.