Skip to content

Commit

Permalink
power device code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
nlef committed Dec 14, 2024
1 parent e72990c commit b410655
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions bot/klippy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,22 @@ async def toggle_device(self) -> bool:
# Todo: return exception?
async def switch_device(self, state: bool) -> bool:
with self._state_lock:
if state:
res = await self._klippy.make_request("POST", f"/machine/device_power/device?device={self.name}&action=on")
if res.is_success:
self._device_on = True
return True
else:
logger.error("Power device switch failed: %s", res)
return state
res = await self._klippy.make_request("POST", f"/machine/device_power/device?device={self.name}&action={'on' if state else 'off'}")
if res.is_success:
self._device_on = state
else:
res = await self._klippy.make_request("POST", f"/machine/device_power/device?device={self.name}&action=off")
if res.is_success:
self._device_on = False
return False
else:
logger.error("Power device switch failed: %s", res)
return state
logger.error("Power device switch failed: %s", res)
return self._device_on

# Todo: return exception?
def switch_device_sync(self, state: bool) -> bool:
with self._state_lock:
if state:
res = self._klippy.make_request_sync("POST", f"/machine/device_power/device?device={self.name}&action=on")
if res.is_success:
self._device_on = True
return True
else:
logger.error("Power device switch failed: %s", res)
return state
res = self._klippy.make_request_sync("POST", f"/machine/device_power/device?device={self.name}&action={'on' if state else 'off'}")
if res.is_success:
self._device_on = state
else:
res = self._klippy.make_request_sync("POST", f"/machine/device_power/device?device={self.name}&action=off")
if res.is_success:
self._device_on = False
return False
else:
logger.error("Power device switch failed: %s", res)
return state
logger.error("Power device switch failed: %s", res)
return self._device_on


class Klippy:
Expand Down

0 comments on commit b410655

Please sign in to comment.