Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option A — Don’t change the library: add a helper that cycles to a target
- Use spa.get_pumps() to refresh state, then toggle the required number of steps.
- from smarttub.api import SpaPump
- PUMP_CYCLE = [SpaPump.PumpState.OFF, SpaPump.PumpState.LOW, SpaPump.PumpState.HIGH]
- async def set_pump_state(spa, pump_id: str, target: SpaPump.PumpState) -> SpaPump:
- # 1) read current
- pumps = await spa.get_pumps()
- pump = next(p for p in pumps if p.id == pump_id)
- # 2) compute forward steps in the OFF->LOW->HIGH cycle
- cur_i = PUMP_CYCLE.index(pump.state)
- tgt_i = PUMP_CYCLE.index(target)
- steps = (tgt_i - cur_i) % len(PUMP_CYCLE)
- # 3) toggle steps times, refreshing state each time
- for _ in range(steps):
- await pump.toggle() # sends POST pumps/{id}/toggle and waits for a change
- pumps = await spa.get_pumps()
- pump = next(p for p in pumps if p.id == pump_id)
- return pump
- That gives you deterministic control of OFF, LOW, and HIGH, even though the underlying API is “toggle only”.
- Option B — Improve python-smarttub: add set_state() (and make toggle() update self.state)
- If you want this to feel “first-class”, you can patch the library so SpaPump has a set_state() method.
- Conceptually:
- class SpaPump:
- async def toggle(self):
- current_state = self.state
- await self.spa.request("POST", f"pumps/{self.id}/toggle")
- state = await self.spa._wait_for_state_change(
- lambda s: any(p.state != current_state for p in s.pumps if p.id == self.id),
- get_status_method=self.spa.get_status_full,
- )
- # update our state from the returned fullStatus
- self.state = next(p.state for p in state.pumps if p.id == self.id)
- return self.state
- async def set_state(self, target_state):
- order = [self.PumpState.OFF, self.PumpState.LOW, self.PumpState.HIGH]
- steps = (order.index(target_state) - order.index(self.state)) % len(order)
- for _ in range(steps):
- await self.toggle()
- Then Home Assistant (or your own UI) can expose a 3-option Select instead of a Switch:
- OFF
- LOW
- HIGH
Advertisement
Add Comment
Please, Sign In to add comment