diff --git a/moonraker/plugins/power.py b/moonraker/plugins/power.py index 1c401e9..b5cc5f8 100644 --- a/moonraker/plugins/power.py +++ b/moonraker/plugins/power.py @@ -69,9 +69,9 @@ class PrinterPower: await GPIO.verify_pin(self.devices[dev]["pin"], self.devices[dev]["active_low"]) if path == "/machine/gpio_power/on": - GPIO.set_pin_value(self.devices[dev]["pin"], 1) + await self._power_dev(dev, "on") elif path == "/machine/gpio_power/off": - GPIO.set_pin_value(self.devices[dev]["pin"], 0) + await self._power_dev(dev, "off") elif path != "/machine/gpio_power/status": raise self.server.error("Unsupported power request") @@ -81,6 +81,23 @@ class PrinterPower: result[dev] = self.devices[dev]["status"] return result + async def _power_dev(self, dev, status): + if dev not in self.devices: + return + + if (status == "on"): + GPIO.set_pin_value(self.devices[dev]["pin"], 1) + self.server.send_event("gpio_power:power_changed", { + "device": dev, + "status": "on" + }) + elif (status == "off"): + GPIO.set_pin_value(self.devices[dev]["pin"], 0) + self.server.send_event("gpio_power:power_changed", { + "device": dev, + "status": "off" + }) + async def initialize_devices(self, devices): for name, device in devices.items(): try: diff --git a/moonraker/websockets.py b/moonraker/websockets.py index b1b7576..cb95e56 100644 --- a/moonraker/websockets.py +++ b/moonraker/websockets.py @@ -117,6 +117,8 @@ class WebsocketManager: "file_manager:filelist_changed", self._handle_filelist_changed) self.server.register_event_handler( "file_manager:metadata_update", self._handle_metadata_update) + self.server.register_event_handler( + "gpio_power:power_changed", self._handle_power_changed) async def _handle_klippy_disconnect(self): await self.notify_websockets("klippy_disconnected") @@ -133,6 +135,9 @@ class WebsocketManager: async def _handle_metadata_update(self, metadata): await self.notify_websockets("metadata_update", metadata) + async def _handle_power_changed(self, pstatus): + await self.notify_websockets("power_changed", pstatus) + def register_local_handler(self, api_def, callback): for ws_method, req_method in \ zip(api_def.ws_methods, api_def.request_methods):