greghesp / ha-bambulab

A Home Assistant Integration for Bambu Lab Printers
928 stars 80 forks source link

[Bug] KeyError: 'serial' - printer setup not working #624

Open apfelklaus opened 4 days ago

apfelklaus commented 4 days ago

Describe the bug

After latest bambulab upgrade printer is lost and setting it up again did not work

Bildschirmfoto 2024-10-23 um 10 11 04

LOG

2024-10-23 10:11:00.606 ERROR (MainThread) [aiohttp.server] Error handling request Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/aiohttp/web_protocol.py", line 477, in _handle_request resp = await request_handler(request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/aiohttp/web_app.py", line 559, in _handle return await handler(request) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/aiohttp/web_middlewares.py", line 117, in impl return await handler(request) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 92, in security_filter_middleware return await handler(request) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 77, in forwarded_middleware return await handler(request) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 26, in request_context_middleware return await handler(request) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 85, in ban_middleware return await handler(request) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 242, in auth_middleware return await handler(request) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/http/headers.py", line 32, in headers_middleware response = await handler(request) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/http.py", line 73, in handle result = await handler(request, request.match_info) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/http/decorators.py", line 81, in with_admin return await func(self, request, *args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 222, in post return await super().post(request, flow_id) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 74, in wrapper return await method(view, request, data, args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 122, in post result = await self._flow_mgr.async_configure(flow_id, data) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 370, in async_configure result = await self._async_configure(flow_id, user_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 417, in _async_configure result = await self._async_handle_step( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 520, in _async_handle_step result: _FlowResultT = await getattr(flow, method)(user_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/bambu_lab/config_flow.py", line 154, in async_step_Bambu_Choose_Device self.serial = user_input['serial']


KeyError: 'serial'

### To Reproduce

Adding a new cloud printer in the UI after upgrade because printers are lost 

### Expected Behaviour

printer to be setted up

### What device are you using?

P1S

### Diagnostic Output

```shell
no available
```

### Log Extracts

_No response_

### Other Information

_No response_
Spirituss commented 4 days ago

The same is here, it was previously connected. This is the log:

2024-10-23 18:57:44.742 WARNING (MainThread) [homeassistant.components.fan] Entity None (<class 'custom_components.bambu_lab.fan.BambuLabFan'>) does not set FanEntityFeature.TURN_OFF but implements the turn_off method. Please create a bug report at https://github.com/greghesp/ha-bambulab/issues
2024-10-23 18:57:44.742 WARNING (MainThread) [homeassistant.components.fan] Entity None (<class 'custom_components.bambu_lab.fan.BambuLabFan'>) does not set FanEntityFeature.TURN_ON but implements the turn_on method. Please create a bug report at https://github.com/greghesp/ha-bambulab/issues
2024-10-23 18:57:44.744 WARNING (MainThread) [homeassistant.components.fan] Entity None (<class 'custom_components.bambu_lab.fan.BambuLabFan'>) does not set FanEntityFeature.TURN_OFF but implements the turn_off method. Please create a bug report at https://github.com/greghesp/ha-bambulab/issues
2024-10-23 18:57:44.744 WARNING (MainThread) [homeassistant.components.fan] Entity None (<class 'custom_components.bambu_lab.fan.BambuLabFan'>) does not set FanEntityFeature.TURN_ON but implements the turn_on method. Please create a bug report at https://github.com/greghesp/ha-bambulab/issues
2024-10-23 18:57:44.745 WARNING (MainThread) [homeassistant.components.fan] Entity None (<class 'custom_components.bambu_lab.fan.BambuLabFan'>) does not set FanEntityFeature.TURN_OFF but implements the turn_off method. Please create a bug report at https://github.com/greghesp/ha-bambulab/issues
2024-10-23 18:57:44.745 WARNING (MainThread) [homeassistant.components.fan] Entity None (<class 'custom_components.bambu_lab.fan.BambuLabFan'>) does not set FanEntityFeature.TURN_ON but implements the turn_on method. Please create a bug report at https://github.com/greghesp/ha-bambulab/issues
2024-10-23 18:57:44.837 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to load_default_certs with args (<ssl.SSLContext object at 0x7f6e26c616d0>,) inside the event loop by custom integration 'bambu_lab' at custom_components/bambu_lab/pybambu/bambu_client.py, line 332: self.client.tls_set(tls_version=ssl.PROTOCOL_TLS, cert_reqs=ssl.CERT_NONE) (offender: /usr/local/lib/python3.12/site-packages/paho/mqtt/client.py, line 806: context.load_default_certs()), please create a bug report at https://github.com/greghesp/ha-bambulab/issues
For developers, please see https://developers.home-assistant.io/docs/asyncio_blocking_operations/#load_default_certs
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 223, in <module>
    sys.exit(main())
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 209, in main
    exit_code = runner.run(runtime_conf)
  File "/usr/src/homeassistant/homeassistant/runner.py", line 189, in run
    return loop.run_until_complete(setup_and_run_hass(runtime_config))
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 641, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1990, in _run_once
    handle._run()
  File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/config/custom_components/bambu_lab/coordinator.py", line 131, in listen
    self.client.connect(callback=self.event_handler)
  File "/config/custom_components/bambu_lab/pybambu/bambu_client.py", line 332, in connect
    self.client.tls_set(tls_version=ssl.PROTOCOL_TLS, cert_reqs=ssl.CERT_NONE)
2024-10-23 18:57:46.207 ERROR (P1S-Mqtt-67) [custom_components.bambu_lab.pybambu] A listener loop thread exception occurred:
2024-10-23 18:57:46.207 ERROR (P1S-Mqtt-67) [custom_components.bambu_lab.pybambu] Exception. Type: <class 'ValueError'> Args: 403
2024-10-23 18:57:47.209 WARNING (P1S-Mqtt-67) [custom_components.bambu_lab.pybambu] On Disconnect: Disconnected from Broker: 0
2024-10-23 18:57:47.209 WARNING (P1S-Mqtt-67) [custom_components.bambu_lab.pybambu] _on_disconnect
2024-10-23 18:57:49.099 ERROR (P1S-Mqtt-67) [custom_components.bambu_lab.pybambu] A listener loop thread exception occurred:
2024-10-23 18:57:49.099 ERROR (P1S-Mqtt-67) [custom_components.bambu_lab.pybambu] Exception. Type: <class 'ValueError'> Args: 403
AdrianGarside commented 2 days ago

@Spirituss yours is just the general access denied error from the bambu cloudflare rollout. Are you on the latest version of the integration?

AdrianGarside commented 2 days ago
    if user_input is not None:
        self.serial = user_input['serial']
        return await self.async_step_Bambu_Lan(None)

The user_input variable there is based on the schema we pass to the HA form code - and that schema will always have that field in it unless... LOGGER.debug(f"Printer count = {printer_list.count}") if len(printer_list) == 0: errors['base'] = "no_printers"

    # Build form
    fields: OrderedDict[vol.Marker, Any] = OrderedDict()
    **if len(printer_list) != 0:
        fields[vol.Required('serial')] = printer_selector**

Do debug logs show you're getting back no printers?

AdrianGarside commented 2 days ago

Oh, it's right there in the screen snip: image

I had one report of this a long time ago but without a repro I haven't been able to come up with a solution. Home Assistant still has your printer registered as a device despite having uninstalled the integration previously. So when it searches for available devices to add it filters it out. There's probably a way to fix this manually by editing HA's device list files directly but there may be a safer way to clean up the orphaned entry.

apfelklaus commented 2 days ago

I'm now getting :

2024-10-25 17:25:31.069 ERROR (MainThread) [custom_components.bambu_lab] Failed to connect with error code (403,)
2024-10-25 17:25:35.827 ERROR (MainThread) [custom_components.bambu_lab] Failed to connect with error code (403,)
2024-10-25 17:27:28.151 ERROR (MainThread) [custom_components.bambu_lab] Failed to connect with error code (403,)

Some days ago it was the serial error mentioned above - latest HACS version both errors

AdrianGarside commented 2 days ago

Great, more cloud flare problems then :(

apfelklaus commented 2 days ago

Oh, it's right there in the screen snip: image

I had one report of this a long time ago but without a repro I haven't been able to come up with a solution. Home Assistant still has your printer registered as a device despite having uninstalled the integration previously. So when it searches for available devices to add it filters it out. There's probably a way to fix this manually by editing HA's device list files directly but there may be a safer way to clean up the orphaned entry.

there are no printers anymore that could be deleted:

Bildschirmfoto 2024-10-25 um 17 30 29 Bildschirmfoto 2024-10-25 um 17 30 36
AdrianGarside commented 2 days ago

Grab debug logs. Either it's not getting any printers back from bambu for the account or HA still has the printer registered as a device internally and so we're filtering it out as 'already added'.