power: process bound services when initial_state is set

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-11-14 08:11:40 -05:00
parent 975011d190
commit 6c6b7dcff0
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 23 additions and 14 deletions

View File

@ -295,14 +295,7 @@ class PowerDevice:
async def process_power_changed(self) -> None: async def process_power_changed(self) -> None:
self.notify_power_changed() self.notify_power_changed()
if self.bound_services: if self.bound_services:
machine_cmp: Machine = self.server.lookup_component("machine") await self.process_bound_services()
action = "start" if self.state == "on" else "stop"
for svc in self.bound_services:
logging.info(
f"Power Device {self.name}: Performing {action} action "
f"on bound service {svc}"
)
await machine_cmp.do_service_action(action, svc)
if self.state == "on" and self.klipper_restart: if self.state == "on" and self.klipper_restart:
self.need_scheduled_restart = True self.need_scheduled_restart = True
klippy_state = self.server.get_klippy_state() klippy_state = self.server.get_klippy_state()
@ -313,6 +306,18 @@ class PowerDevice:
return return
self._schedule_firmware_restart(klippy_state) self._schedule_firmware_restart(klippy_state)
async def process_bound_services(self) -> None:
if not self.bound_services:
return
machine_cmp: Machine = self.server.lookup_component("machine")
action = "start" if self.state == "on" else "stop"
for svc in self.bound_services:
logging.info(
f"Power Device {self.name}: Performing {action} action "
f"on bound service {svc}"
)
await machine_cmp.do_service_action(action, svc)
def process_klippy_shutdown(self) -> None: def process_klippy_shutdown(self) -> None:
if not self.off_when_shutdown: if not self.off_when_shutdown:
return return
@ -466,6 +471,7 @@ class HTTPDevice(PowerDevice):
f"state to {new_state}" f"state to {new_state}"
) )
await self.set_power(new_state) await self.set_power(new_state)
await self.process_bound_services()
self.notify_power_changed() self.notify_power_changed()
return return
@ -515,8 +521,6 @@ class GpioDevice(PowerDevice):
initial_val: Optional[int] = None initial_val: Optional[int] = None
) -> None: ) -> None:
super().__init__(config) super().__init__(config)
if self.initial_state is None:
self.initial_state = False
self.timer: Optional[float] = config.getfloat('timer', None) self.timer: Optional[float] = config.getfloat('timer', None)
if self.timer is not None and self.timer < 0.000001: if self.timer is not None and self.timer < 0.000001:
raise config.error( raise config.error(
@ -524,12 +528,15 @@ class GpioDevice(PowerDevice):
"be above 0.0") "be above 0.0")
self.timer_handle: Optional[asyncio.TimerHandle] = None self.timer_handle: Optional[asyncio.TimerHandle] = None
if initial_val is None: if initial_val is None:
initial_val = int(self.initial_state) initial_val = int(self.initial_state or 0)
self.gpio_out = config.getgpioout('pin', initial_value=initial_val) self.gpio_out = config.getgpioout('pin', initial_value=initial_val)
def init_state(self) -> None: async def init_state(self) -> None:
assert self.initial_state is not None if self.initial_state is None:
self.set_power("on" if self.initial_state else "off") self.set_power("off")
else:
self.set_power("on" if self.initial_state else "off")
await self.process_bound_services()
def refresh_status(self) -> None: def refresh_status(self) -> None:
pass pass
@ -904,6 +911,7 @@ class TPLinkSmartPlug(PowerDevice):
f"state to {new_state}" f"state to {new_state}"
) )
await self.set_power(new_state) await self.set_power(new_state)
await self.process_bound_services()
self.notify_power_changed() self.notify_power_changed()
return return
@ -1291,6 +1299,7 @@ class MQTTDevice(PowerDevice):
f"state to {new_state}" f"state to {new_state}"
) )
await self.set_power(new_state) await self.set_power(new_state)
await self.process_bound_services()
# Don't reset on next connection # Don't reset on next connection
self.initial_state = None self.initial_state = None
self.notify_power_changed() self.notify_power_changed()