test_main_plug_pick_2830.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. """The printer card's Power row and HA row, over the real API (#2830).
  2. The ranking itself is unit-tested. These drive the two endpoints the card calls,
  3. with real rows in the database, because the ranking is only worth anything if
  4. the endpoints use it -- and because the second endpoint has to agree with the
  5. first about which plug is the main one or the card draws it twice.
  6. """
  7. import pytest
  8. from httpx import AsyncClient
  9. pytestmark = [pytest.mark.integration, pytest.mark.asyncio]
  10. MAIN = "/api/v1/smart-plugs/by-printer/{}"
  11. ENTITIES = "/api/v1/smart-plugs/by-printer/{}/scripts"
  12. async def _ha(smart_plug_factory, printer, entity_id, **kwargs):
  13. return await smart_plug_factory(
  14. plug_type="homeassistant",
  15. ha_entity_id=entity_id,
  16. printer_id=printer.id,
  17. **kwargs,
  18. )
  19. class TestTheReportedStranding:
  20. """An X1C with two Home Assistant plugs: an exhaust fan with no power
  21. monitoring, added first, and the outlet the printer is actually plugged
  22. into. The card showed the fan, with "--" where its wattage would be."""
  23. async def test_the_outlet_takes_the_power_row(self, async_client: AsyncClient, printer_factory, smart_plug_factory):
  24. printer = await printer_factory()
  25. await _ha(
  26. smart_plug_factory,
  27. printer,
  28. "switch.print_farm_exhaust_fan",
  29. name="Print Farm Exhaust Fan",
  30. controls_printer_power=False,
  31. show_on_printer_card=False,
  32. )
  33. await _ha(
  34. smart_plug_factory,
  35. printer,
  36. "switch.bambu_x1c_outlet",
  37. name="Bambu X1C Outlet",
  38. ha_power_entity="sensor.bambu_x1c_outlet_power",
  39. )
  40. response = await async_client.get(MAIN.format(printer.id))
  41. assert response.status_code == 200
  42. assert response.json()["name"] == "Bambu X1C Outlet"
  43. async def test_the_outlet_is_not_also_drawn_in_the_ha_row(
  44. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  45. ):
  46. """It is rendered directly above that row with its own controls."""
  47. printer = await printer_factory()
  48. await _ha(smart_plug_factory, printer, "switch.fan", name="Fan", controls_printer_power=False)
  49. await _ha(smart_plug_factory, printer, "switch.outlet", name="Bambu X1C Outlet")
  50. response = await async_client.get(ENTITIES.format(printer.id))
  51. assert response.status_code == 200
  52. assert [p["name"] for p in response.json()] == ["Fan"]
  53. class TestTheHAEntityRow:
  54. async def test_scripts_and_lights_still_appear(
  55. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  56. ):
  57. printer = await printer_factory()
  58. await _ha(smart_plug_factory, printer, "switch.outlet", name="Outlet")
  59. await _ha(smart_plug_factory, printer, "script.start", name="Start Script", controls_printer_power=False)
  60. await _ha(smart_plug_factory, printer, "light.chamber", name="Chamber Light", controls_printer_power=False)
  61. response = await async_client.get(ENTITIES.format(printer.id))
  62. assert sorted(p["name"] for p in response.json()) == ["Chamber Light", "Start Script"]
  63. async def test_hidden_entities_stay_hidden(self, async_client: AsyncClient, printer_factory, smart_plug_factory):
  64. printer = await printer_factory()
  65. await _ha(smart_plug_factory, printer, "switch.outlet", name="Outlet")
  66. await _ha(
  67. smart_plug_factory,
  68. printer,
  69. "light.chamber",
  70. name="Chamber Light",
  71. controls_printer_power=False,
  72. show_on_printer_card=False,
  73. )
  74. response = await async_client.get(ENTITIES.format(printer.id))
  75. assert response.json() == []
  76. async def test_a_tasmota_main_plug_leaves_the_row_untouched(
  77. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  78. ):
  79. """Only HA entities are listed there, so excluding the main plug must
  80. not remove anything when the main plug was never in the list."""
  81. printer = await printer_factory()
  82. await smart_plug_factory(name="Tasmota Outlet", printer_id=printer.id)
  83. await _ha(smart_plug_factory, printer, "light.chamber", name="Chamber Light", controls_printer_power=False)
  84. response = await async_client.get(ENTITIES.format(printer.id))
  85. assert [p["name"] for p in response.json()] == ["Chamber Light"]
  86. class TestASinglePlugIsNeverDropped:
  87. """Hidden and disabled are ranking criteria, not filters. Excluding those
  88. outright would take the Power row -- and with it the on/off button, and the
  89. HA row nested inside it -- off a card that has one plug to show."""
  90. async def test_a_hidden_plug_still_holds_the_row(
  91. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  92. ):
  93. printer = await printer_factory()
  94. await _ha(smart_plug_factory, printer, "switch.outlet", name="Outlet", show_on_printer_card=False)
  95. response = await async_client.get(MAIN.format(printer.id))
  96. assert response.json()["name"] == "Outlet"
  97. async def test_an_accessory_still_holds_the_row(
  98. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  99. ):
  100. printer = await printer_factory()
  101. await smart_plug_factory(name="Filter Fan", printer_id=printer.id, controls_printer_power=False)
  102. response = await async_client.get(MAIN.format(printer.id))
  103. assert response.json()["name"] == "Filter Fan"
  104. async def test_no_plugs_means_null(self, async_client: AsyncClient, printer_factory):
  105. printer = await printer_factory()
  106. response = await async_client.get(MAIN.format(printer.id))
  107. assert response.status_code == 200
  108. assert response.json() is None
  109. class TestTheRestOfTheOrder:
  110. async def test_an_enabled_plug_beats_a_disabled_one(
  111. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  112. ):
  113. printer = await printer_factory()
  114. await smart_plug_factory(name="Disabled", printer_id=printer.id, enabled=False)
  115. await smart_plug_factory(name="Live", printer_id=printer.id)
  116. response = await async_client.get(MAIN.format(printer.id))
  117. assert response.json()["name"] == "Live"
  118. async def test_a_switch_beats_a_script(self, async_client: AsyncClient, printer_factory, smart_plug_factory):
  119. printer = await printer_factory()
  120. await _ha(smart_plug_factory, printer, "script.start", name="Start Script")
  121. await _ha(smart_plug_factory, printer, "switch.outlet", name="Outlet")
  122. response = await async_client.get(MAIN.format(printer.id))
  123. assert response.json()["name"] == "Outlet"
  124. async def test_a_printer_with_only_scripts_still_gets_one(
  125. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  126. ):
  127. printer = await printer_factory()
  128. await _ha(smart_plug_factory, printer, "script.a", name="First Script")
  129. await _ha(smart_plug_factory, printer, "script.b", name="Second Script")
  130. response = await async_client.get(MAIN.format(printer.id))
  131. assert response.json()["name"] == "First Script"
  132. async def test_a_script_in_the_power_row_keeps_its_button(
  133. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  134. ):
  135. """The script-only fallback is unchanged from before #2830, including
  136. the one-click run in the HA row. Only a switchable main plug is
  137. de-duplicated -- a script reached from the power row costs a confirm
  138. dialog it never used to need."""
  139. printer = await printer_factory()
  140. await _ha(smart_plug_factory, printer, "script.a", name="First Script")
  141. await _ha(smart_plug_factory, printer, "script.b", name="Second Script")
  142. response = await async_client.get(ENTITIES.format(printer.id))
  143. assert sorted(p["name"] for p in response.json()) == ["First Script", "Second Script"]
  144. async def test_a_switchable_plug_beats_a_monitor_only_mqtt_plug(
  145. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  146. ):
  147. """An MQTT plug reports watts but cannot be controlled -- the control
  148. endpoint rejects it. Put it in the power row and the on/off button
  149. answers with an error."""
  150. printer = await printer_factory()
  151. await smart_plug_factory(
  152. name="Monitor", plug_type="mqtt", printer_id=printer.id, mqtt_power_topic="tele/printer/SENSOR"
  153. )
  154. await _ha(smart_plug_factory, printer, "switch.outlet", name="Outlet")
  155. response = await async_client.get(MAIN.format(printer.id))
  156. assert response.json()["name"] == "Outlet"
  157. async def test_a_lone_monitor_only_plug_still_holds_the_row(
  158. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  159. ):
  160. printer = await printer_factory()
  161. await smart_plug_factory(
  162. name="Monitor", plug_type="mqtt", printer_id=printer.id, mqtt_power_topic="tele/printer/SENSOR"
  163. )
  164. response = await async_client.get(MAIN.format(printer.id))
  165. assert response.json()["name"] == "Monitor"
  166. class TestOtherPrintersAreUnaffected:
  167. async def test_plugs_are_not_borrowed_across_printers(
  168. self, async_client: AsyncClient, printer_factory, smart_plug_factory
  169. ):
  170. one = await printer_factory()
  171. two = await printer_factory()
  172. await smart_plug_factory(name="Plug One", printer_id=one.id)
  173. await smart_plug_factory(name="Plug Two", printer_id=two.id)
  174. await smart_plug_factory(name="Unlinked")
  175. assert (await async_client.get(MAIN.format(one.id))).json()["name"] == "Plug One"
  176. assert (await async_client.get(MAIN.format(two.id))).json()["name"] == "Plug Two"