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:
self.notify_power_changed()
if self.bound_services:
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)
await self.process_bound_services()
if self.state == "on" and self.klipper_restart:
self.need_scheduled_restart = True
klippy_state = self.server.get_klippy_state()
@ -313,6 +306,18 @@ class PowerDevice:
return
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:
if not self.off_when_shutdown:
return
@ -466,6 +471,7 @@ class HTTPDevice(PowerDevice):
f"state to {new_state}"
)
await self.set_power(new_state)
await self.process_bound_services()
self.notify_power_changed()
return
@ -515,8 +521,6 @@ class GpioDevice(PowerDevice):
initial_val: Optional[int] = None
) -> None:
super().__init__(config)
if self.initial_state is None:
self.initial_state = False
self.timer: Optional[float] = config.getfloat('timer', None)
if self.timer is not None and self.timer < 0.000001:
raise config.error(
@ -524,12 +528,15 @@ class GpioDevice(PowerDevice):
"be above 0.0")
self.timer_handle: Optional[asyncio.TimerHandle] = 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)
def init_state(self) -> None:
assert self.initial_state is not None
self.set_power("on" if self.initial_state else "off")
async def init_state(self) -> None:
if self.initial_state is None:
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:
pass
@ -904,6 +911,7 @@ class TPLinkSmartPlug(PowerDevice):
f"state to {new_state}"
)
await self.set_power(new_state)
await self.process_bound_services()
self.notify_power_changed()
return
@ -1291,6 +1299,7 @@ class MQTTDevice(PowerDevice):
f"state to {new_state}"
)
await self.set_power(new_state)
await self.process_bound_services()
# Don't reset on next connection
self.initial_state = None
self.notify_power_changed()