Guest User

Untitled

a guest
Jan 18th, 2026
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. Option A — Don’t change the library: add a helper that cycles to a target
  2.  
  3. Use spa.get_pumps() to refresh state, then toggle the required number of steps.
  4.  
  5. from smarttub.api import SpaPump
  6.  
  7. PUMP_CYCLE = [SpaPump.PumpState.OFF, SpaPump.PumpState.LOW, SpaPump.PumpState.HIGH]
  8.  
  9. async def set_pump_state(spa, pump_id: str, target: SpaPump.PumpState) -> SpaPump:
  10. # 1) read current
  11. pumps = await spa.get_pumps()
  12. pump = next(p for p in pumps if p.id == pump_id)
  13.  
  14. # 2) compute forward steps in the OFF->LOW->HIGH cycle
  15. cur_i = PUMP_CYCLE.index(pump.state)
  16. tgt_i = PUMP_CYCLE.index(target)
  17. steps = (tgt_i - cur_i) % len(PUMP_CYCLE)
  18.  
  19. # 3) toggle steps times, refreshing state each time
  20. for _ in range(steps):
  21. await pump.toggle() # sends POST pumps/{id}/toggle and waits for a change
  22. pumps = await spa.get_pumps()
  23. pump = next(p for p in pumps if p.id == pump_id)
  24.  
  25. return pump
  26.  
  27.  
  28. That gives you deterministic control of OFF, LOW, and HIGH, even though the underlying API is “toggle only”.
  29.  
  30. Option B — Improve python-smarttub: add set_state() (and make toggle() update self.state)
  31.  
  32. If you want this to feel “first-class”, you can patch the library so SpaPump has a set_state() method.
  33.  
  34. Conceptually:
  35.  
  36. class SpaPump:
  37. async def toggle(self):
  38. current_state = self.state
  39. await self.spa.request("POST", f"pumps/{self.id}/toggle")
  40. state = await self.spa._wait_for_state_change(
  41. lambda s: any(p.state != current_state for p in s.pumps if p.id == self.id),
  42. get_status_method=self.spa.get_status_full,
  43. )
  44. # update our state from the returned fullStatus
  45. self.state = next(p.state for p in state.pumps if p.id == self.id)
  46. return self.state
  47.  
  48. async def set_state(self, target_state):
  49. order = [self.PumpState.OFF, self.PumpState.LOW, self.PumpState.HIGH]
  50. steps = (order.index(target_state) - order.index(self.state)) % len(order)
  51. for _ in range(steps):
  52. await self.toggle()
  53.  
  54.  
  55. Then Home Assistant (or your own UI) can expose a 3-option Select instead of a Switch:
  56.  
  57. OFF
  58.  
  59. LOW
  60.  
  61. HIGH
Advertisement
Add Comment
Please, Sign In to add comment