power: introduce "off_when_shutdown" device option

This allows devices to power down when a Klippy shutdown is detected.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-01-02 19:38:24 -05:00
parent ea1ad0277d
commit 222f0c4695
1 changed files with 14 additions and 0 deletions

View File

@ -34,6 +34,8 @@ class PrinterPower:
self._handle_power_request) self._handle_power_request)
self.server.register_remote_method( self.server.register_remote_method(
"set_device_power", self.set_device_power) "set_device_power", self.set_device_power)
self.server.register_event_handler(
"server:klippy_shutdown", self._handle_klippy_shutdown)
self.chip_factory = GpioChipFactory() self.chip_factory = GpioChipFactory()
self.current_dev = None self.current_dev = None
@ -53,6 +55,15 @@ class PrinterPower:
raise config.error(f"Unsupported Device Type: {dev_type}") raise config.error(f"Unsupported Device Type: {dev_type}")
self.devices[dev.get_name()] = dev self.devices[dev.get_name()] = dev
async def _handle_klippy_shutdown(self):
for name, dev in self.devices.items():
if hasattr(dev, "off_when_shutdown"):
if dev.off_when_shutdown:
logging.info(
f"Powering off device [{name}] due to"
" klippy shutdown")
await self._process_request(dev, "off")
async def _handle_list_devices(self, web_request): async def _handle_list_devices(self, web_request):
dev_list = [d.get_device_info() for d in self.devices.values()] dev_list = [d.get_device_info() for d in self.devices.values()]
output = {"devices": dev_list} output = {"devices": dev_list}
@ -164,6 +175,7 @@ class GpioDevice:
f"Unable to init {pin}. Make sure the gpio is not in " f"Unable to init {pin}. Make sure the gpio is not in "
"use by another program or exported by sysfs.") "use by another program or exported by sysfs.")
raise config.error("Power GPIO Config Error") raise config.error("Power GPIO Config Error")
self.off_when_shutdown = config.getboolean('off_when_shutdown', False)
initial_state = config.getboolean('initial_state', False) initial_state = config.getboolean('initial_state', False)
self.set_power("on" if initial_state else "off") self.set_power("on" if initial_state else "off")
@ -238,6 +250,7 @@ class TPLinkSmartPlug:
self.server = config.get_server() self.server = config.get_server()
self.addr = config.get("address") self.addr = config.get("address")
self.port = config.getint("port", 9999) self.port = config.getint("port", 9999)
self.off_when_shutdown = config.getboolean('off_when_shutdown', False)
name_parts = config.get_name().split(maxsplit=1) name_parts = config.get_name().split(maxsplit=1)
if len(name_parts) != 2: if len(name_parts) != 2:
raise config.error(f"Invalid Section Name: {config.get_name()}") raise config.error(f"Invalid Section Name: {config.get_name()}")
@ -341,6 +354,7 @@ class Tasmota:
self.addr = config.get("address") self.addr = config.get("address")
self.output_id = config.getint("output_id", 1) self.output_id = config.getint("output_id", 1)
self.password = config.get("password", "") self.password = config.get("password", "")
self.off_when_shutdown = config.getboolean('off_when_shutdown', False)
name_parts = config.get_name().split(maxsplit=1) name_parts = config.get_name().split(maxsplit=1)
if len(name_parts) != 2: if len(name_parts) != 2:
raise config.error(f"Invalid Section Name: {config.get_name()}") raise config.error(f"Invalid Section Name: {config.get_name()}")