test_p2s_accessory_fans.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. """Tests for the P2S/X2D left auxiliary part cooling fan (#2576).
  2. The "Auxiliary Part Cooling Fan - Left" (also fits X2D) is reported ONLY as
  3. device.airduct part with raw id 160 (decoded id = 160 >> 4 = 10,
  4. AIR_FUN.FAN_REMOTE_COOLING_1 in Bambu Studio) — the firmware does NOT mirror
  5. it into any flat big_fanX_speed field, which is why it was previously dropped.
  6. It is controlled with "M106 P10", exactly like Bambu's official P2S machine-
  7. profile gcode does.
  8. The airduct payloads below are verbatim captures from a live P2S
  9. (fw 01.02.00.00) with the accessory installed.
  10. """
  11. import pytest
  12. @pytest.fixture
  13. def mqtt_client():
  14. from backend.app.services.bambu_mqtt import BambuMQTTClient
  15. return BambuMQTTClient(
  16. ip_address="192.168.1.100",
  17. serial_number="TESTP2S",
  18. access_code="12345678",
  19. )
  20. def _airduct_device(parts):
  21. """Wrap airduct parts in the device envelope as pushed by a P2S."""
  22. return {
  23. "device": {
  24. "airduct": {
  25. "modeCur": 0,
  26. "modeFunc": 0,
  27. "modeList": [
  28. {"ctrl": [16, 32, 160, 48], "modeId": 0, "off": []},
  29. {"ctrl": [16, 32, 48], "modeId": 1, "off": [160]},
  30. ],
  31. "modeVisable": 7,
  32. "parts": parts,
  33. "subFunc": 0,
  34. "subMode": 0,
  35. "subVisable": 7,
  36. "version": 1,
  37. },
  38. "type": 1,
  39. }
  40. }
  41. # Verbatim parts list from a live P2S: part cooling ramping (state 30,
  42. # target 90), right aux at 40%, left aux OFF, chamber at 70%.
  43. P2S_PARTS_LEFT_AUX_OFF = [
  44. {"func": 0, "id": 16, "range": 6553600, "state": 30, "tar_state": 90},
  45. {"func": 6, "id": 32, "range": 6553600, "state": 40, "tar_state": 40},
  46. {"func": 5, "id": 160, "range": 6553600, "state": 0, "tar_state": 0},
  47. {"func": 2, "id": 48, "range": 6553600, "state": 70, "tar_state": 70},
  48. ]
  49. # Same printer later in the print: left aux running at 80%.
  50. P2S_PARTS_LEFT_AUX_80 = [
  51. {"func": 0, "id": 16, "range": 6553600, "state": 60, "tar_state": 60},
  52. {"func": 6, "id": 32, "range": 6553600, "state": 100, "tar_state": 100},
  53. {"func": 5, "id": 160, "range": 6553600, "state": 80, "tar_state": 80},
  54. {"func": 2, "id": 48, "range": 6553600, "state": 80, "tar_state": 80},
  55. ]
  56. class TestLeftAuxFanParsing:
  57. """device.airduct part id 10 (raw 160) -> state.left_aux_fan_speed."""
  58. def test_defaults_to_none(self, mqtt_client):
  59. assert mqtt_client.state.left_aux_fan_speed is None
  60. def test_parses_left_aux_running(self, mqtt_client):
  61. mqtt_client._update_state(_airduct_device(P2S_PARTS_LEFT_AUX_80))
  62. assert mqtt_client.state.left_aux_fan_speed == 80
  63. def test_parses_left_aux_off(self, mqtt_client):
  64. mqtt_client._update_state(_airduct_device(P2S_PARTS_LEFT_AUX_OFF))
  65. assert mqtt_client.state.left_aux_fan_speed == 0
  66. def test_raw_id_is_bit_unpacked(self, mqtt_client):
  67. """Raw id 160 must decode to part id 10 (id >> 4), NOT match on 160."""
  68. # A hypothetical raw id of 10 would decode to part id 0 — must not match.
  69. parts = [{"func": 5, "id": 10, "range": 6553600, "state": 50, "tar_state": 50}]
  70. mqtt_client._update_state(_airduct_device(parts))
  71. assert mqtt_client.state.left_aux_fan_speed is None
  72. def test_parts_without_left_aux_reports_none(self, mqtt_client):
  73. """A full parts list without id 10 means the fan is not installed."""
  74. mqtt_client.state.left_aux_fan_speed = 80 # previously seen
  75. parts = [p for p in P2S_PARTS_LEFT_AUX_80 if p["id"] != 160]
  76. mqtt_client._update_state(_airduct_device(parts))
  77. assert mqtt_client.state.left_aux_fan_speed is None
  78. def test_diff_push_without_device_preserves_value(self, mqtt_client):
  79. """P-series diff pushes omit device.airduct — value must survive."""
  80. mqtt_client._update_state(_airduct_device(P2S_PARTS_LEFT_AUX_80))
  81. mqtt_client._update_state({"nozzle_temper": 250.0})
  82. assert mqtt_client.state.left_aux_fan_speed == 80
  83. def test_state_clamped_to_0_100(self, mqtt_client):
  84. parts = [{"func": 5, "id": 160, "range": 6553600, "state": 250, "tar_state": 0}]
  85. mqtt_client._update_state(_airduct_device(parts))
  86. assert mqtt_client.state.left_aux_fan_speed == 100
  87. def test_malformed_part_entries_ignored(self, mqtt_client):
  88. parts = [
  89. "not-a-dict",
  90. {"func": 5}, # no id/state
  91. {"id": "garbage", "state": 10},
  92. {"func": 5, "id": 160, "range": 6553600, "state": 30, "tar_state": 30},
  93. ]
  94. mqtt_client._update_state(_airduct_device(parts))
  95. assert mqtt_client.state.left_aux_fan_speed == 30
  96. def test_flat_fan_fields_unaffected(self, mqtt_client):
  97. """Regression: flat fields keep coming from the flat MQTT keys."""
  98. payload = {
  99. "cooling_fan_speed": "4",
  100. "big_fan1_speed": "6",
  101. "big_fan2_speed": "10",
  102. "heatbreak_fan_speed": "14",
  103. **_airduct_device(P2S_PARTS_LEFT_AUX_OFF),
  104. }
  105. mqtt_client._update_state(payload)
  106. assert mqtt_client.state.cooling_fan_speed == 27 # 4/15
  107. assert mqtt_client.state.big_fan1_speed == 40 # 6/15
  108. assert mqtt_client.state.big_fan2_speed == 67 # 10/15
  109. assert mqtt_client.state.heatbreak_fan_speed == 93 # 14/15
  110. assert mqtt_client.state.left_aux_fan_speed == 0
  111. class TestExhaustFanPresence:
  112. """device.airduct part id 3 (raw 48) presence -> state.exhaust_fan_present.
  113. The chamber exhaust fan is a P2S/X2D add-on kit (get_version module "eef").
  114. Its speed rides on the flat big_fan2_speed field, but the airduct only lists
  115. part id 3 when the kit is physically installed — so part-3 presence is the
  116. signal the UI uses to show/hide the Exhaust tile.
  117. """
  118. def test_defaults_to_false(self, mqtt_client):
  119. assert mqtt_client.state.exhaust_fan_present is False
  120. def test_present_when_part_3_reported(self, mqtt_client):
  121. # Full P2S parts list includes id 48 (>>4 = 3).
  122. mqtt_client._update_state(_airduct_device(P2S_PARTS_LEFT_AUX_80))
  123. assert mqtt_client.state.exhaust_fan_present is True
  124. def test_absent_when_part_3_missing(self, mqtt_client):
  125. mqtt_client.state.exhaust_fan_present = True # previously seen
  126. parts = [p for p in P2S_PARTS_LEFT_AUX_80 if p["id"] != 48]
  127. mqtt_client._update_state(_airduct_device(parts))
  128. assert mqtt_client.state.exhaust_fan_present is False
  129. def test_base_p2s_only_part_cooling_and_aux(self, mqtt_client):
  130. # A base P2S (no exhaust kit, no left aux kit) lists only ids 1 and 2.
  131. parts = [
  132. {"func": 0, "id": 16, "range": 6553600, "state": 0, "tar_state": 0},
  133. {"func": 6, "id": 32, "range": 6553600, "state": 0, "tar_state": 0},
  134. ]
  135. mqtt_client._update_state(_airduct_device(parts))
  136. assert mqtt_client.state.exhaust_fan_present is False
  137. assert mqtt_client.state.left_aux_fan_speed is None
  138. def test_diff_push_without_device_preserves_value(self, mqtt_client):
  139. mqtt_client._update_state(_airduct_device(P2S_PARTS_LEFT_AUX_80))
  140. mqtt_client._update_state({"nozzle_temper": 250.0})
  141. assert mqtt_client.state.exhaust_fan_present is True
  142. class TestLeftAuxFanCommand:
  143. """set_fan_speed must accept index 10 and emit M106 P10."""
  144. def test_set_fan_speed_10_sends_m106_p10(self, mqtt_client, monkeypatch):
  145. sent = []
  146. monkeypatch.setattr(mqtt_client, "send_gcode", lambda g: sent.append(g) or True)
  147. assert mqtt_client.set_fan_speed(10, 204) is True
  148. assert sent == ["M106 P10 S204"]
  149. def test_set_left_aux_fan_helper(self, mqtt_client, monkeypatch):
  150. sent = []
  151. monkeypatch.setattr(mqtt_client, "send_gcode", lambda g: sent.append(g) or True)
  152. assert mqtt_client.set_left_aux_fan(255) is True
  153. assert sent == ["M106 P10 S255"]
  154. def test_speed_clamped_to_255(self, mqtt_client, monkeypatch):
  155. sent = []
  156. monkeypatch.setattr(mqtt_client, "send_gcode", lambda g: sent.append(g) or True)
  157. mqtt_client.set_left_aux_fan(999)
  158. assert sent == ["M106 P10 S255"]
  159. def test_invalid_fan_index_rejected(self, mqtt_client, monkeypatch):
  160. sent = []
  161. monkeypatch.setattr(mqtt_client, "send_gcode", lambda g: sent.append(g) or True)
  162. assert mqtt_client.set_fan_speed(4, 100) is False
  163. assert mqtt_client.set_fan_speed(11, 100) is False
  164. assert sent == []
  165. def test_existing_fan_indexes_still_accepted(self, mqtt_client, monkeypatch):
  166. sent = []
  167. monkeypatch.setattr(mqtt_client, "send_gcode", lambda g: sent.append(g) or True)
  168. for idx in (1, 2, 3):
  169. assert mqtt_client.set_fan_speed(idx, 128) is True
  170. assert sent == ["M106 P1 S128", "M106 P2 S128", "M106 P3 S128"]
  171. class TestExhaustFanLabelModels:
  172. """P2S/X2D call the big_fan2 enclosure fan "Exhaust"; others say "Chamber"."""
  173. def test_p2s_and_x2d_use_exhaust_label(self):
  174. from backend.app.utils.printer_models import uses_exhaust_fan_label
  175. for model in ("P2S", "X2D", "p2s", " P2S ", "N7", "N6"):
  176. assert uses_exhaust_fan_label(model) is True, model
  177. def test_other_enclosed_models_keep_chamber_label(self):
  178. from backend.app.utils.printer_models import uses_exhaust_fan_label
  179. for model in ("X1C", "X1", "X1E", "P1S", "H2D", "H2C", "H2S", "A1"):
  180. assert uses_exhaust_fan_label(model) is False, model
  181. def test_unknown_or_missing_model_defaults_to_chamber(self):
  182. from backend.app.utils.printer_models import uses_exhaust_fan_label
  183. assert uses_exhaust_fan_label(None) is False
  184. assert uses_exhaust_fan_label("") is False
  185. assert uses_exhaust_fan_label("SomeFutureModel") is False