feat(printers): airduct mode + status badges + force refresh on printer card

Surface four Home Assistant-style controls on the Printers page card:

  - SD Card badge in the top status row (green / red, icon-only).
  - Enclosure Door badge in the top status row (green / yellow, icon-only).
    Detection per printer family — X1/X1C/X1E read home_flag bit 23, all
    others read top-level `stat` (hex string) bit 23 — so X1 firmware that
    does not flip stat bit 23 stops false-triggering "open". WebSocket
    status-change dedup key now includes door_open so toggling the door
    alone publishes a push, no 30s REST-poll wait.
  - Airduct Mode badge beside the speed control (cooling / heating)
    for P2S/H2D/H2C/H2S; one-click dropdown calls the existing
    set_airduct MQTT command via a new POST /printers/{id}/airduct-mode
    route.
  - Force Refresh entry in the kebab menu — calls the existing
    /printers/{id}/refresh-status endpoint to request a pushall snapshot
    without forcing a reconnect.

  Tests: door-open parsing (X1 home_flag, non-X1 stat, ignore mismatched
  source, invalid hex) and airduct route (validation, not-connected,
  success, failure).
This commit is contained in:
maziggy
2026-04-13 12:50:17 +02:00
parent eec7793955
commit 8af0966e68
23 changed files with 862 additions and 446 deletions
@@ -3520,3 +3520,57 @@ class TestStaleReconnect:
assert state_changes == [False]
assert mqtt_client.state.connected is False
class TestDoorOpenParsing:
"""Tests for enclosure door state parsing (X1 home_flag bit 23 vs others stat bit 23)."""
def _make_client(self, model: str):
from backend.app.services.bambu_mqtt import BambuMQTTClient
return BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST",
access_code="12345678",
model=model,
)
def test_x1c_door_open_from_home_flag(self):
client = self._make_client("X1C")
# bit 23 set
client._update_state({"home_flag": 0xC0E5CD98})
assert client.state.door_open is True
def test_x1c_door_closed_from_home_flag(self):
client = self._make_client("X1C")
client.state.door_open = True # start "open"
client._update_state({"home_flag": 0xC065CD98})
assert client.state.door_open is False
def test_x1c_ignores_stat_field(self):
# X1C must NOT use stat (bit 23 in stat is unrelated for X1)
client = self._make_client("X1C")
client._update_state({"home_flag": 0xC065CD98, "stat": "47A58000"})
assert client.state.door_open is False # home_flag wins
def test_h2d_door_open_from_stat(self):
client = self._make_client("H2D")
client._update_state({"stat": "640A58000"}) # bit 23 set
assert client.state.door_open is True
def test_h2d_door_closed_from_stat(self):
client = self._make_client("H2D")
client.state.door_open = True
client._update_state({"stat": "640258000"}) # bit 23 cleared
assert client.state.door_open is False
def test_h2d_ignores_home_flag(self):
# Non-X1 must NOT consume home_flag for door state
client = self._make_client("H2D")
client._update_state({"home_flag": 0xC0E5CD98, "stat": "640258000"})
assert client.state.door_open is False # stat wins
def test_invalid_stat_does_not_raise(self):
client = self._make_client("H2D")
client._update_state({"stat": "not-hex"})
assert client.state.door_open is False