Skip to content

Fix filament usage tracking on auto-refill/runout (#90)#1

Draft
Claude wants to merge 1 commit intomainfrom
claude/fix-issue-90
Draft

Fix filament usage tracking on auto-refill/runout (#90)#1
Claude wants to merge 1 commit intomainfrom
claude/fix-issue-90

Conversation

@Claude
Copy link
Copy Markdown

@Claude Claude AI commented Apr 19, 2026

Summary

Fixes drndos#90 - Filament usage not correctly tracked on filament runout

When using Bambu Lab's auto-refill feature (where a second spool automatically takes over when the first runs out), OpenSpoolMan was continuing to credit usage to the original spool instead of switching to the new spool. This fix detects tray changes during active printing and invalidates the cached spool mapping to ensure usage is tracked against the correct spool.

Changes

Detection of Tray Changes

  • Added _last_tray_tar tracking in FilamentUsageTracker to detect when the active tray changes
  • Modified on_message to monitor tray_tar changes during RUNNING state
  • Logs tray changes for debugging: [filament-tracker] Detected tray change: 3 -> 0

Handling Tray Changes

  • Added handle_tray_change(tray_tar) method that:
    • Finds which filament index is mapped to the changed tray
    • Clears the cached spool ID for that filament
    • Flushes pending usage to ensure it's applied with the new spool
  • This forces a fresh lookup of the spool assigned to the tray on the next usage event

State Management

  • Reset _last_tray_tar to None when print ends or is aborted
  • Ensures clean state for the next print

Testing

All existing tests pass, including MQTT replay tests that simulate multicolor prints with tray changes. The logs show proper detection:

[filament-tracker] Detected tray change: 3 -> 0
[filament-tracker] Tray 3 changed for filament 0, clearing cached spool 3

Impact

  • Auto-refill: When a spool runs out and auto-refill kicks in, usage will now be tracked against the replacement spool
  • Multi-color prints: No impact - tray changes for color switching continue to work as before
  • Layer tracking: Works with both TRACK_LAYER_USAGE=True and legacy tracking

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com

- Add _last_tray_tar tracking to detect when tray_tar changes
- Add handle_tray_change method to invalidate cached spool mappings
- Clear cached spool ID when tray changes to force re-lookup of spool
- Reset _last_tray_tar on print end/abort

Fixes drndos#90

Agent-Logs-Url: https://github.com/csm10495/openspoolman/sessions/70fcf179-0fb6-4ccc-8862-a54a31d17ea1

Co-authored-by: csm10495 <5749838+csm10495@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Filament usage not correctly tracked on filament runout

2 participants