power: add initial state support for all devices

Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-10-19 08:20:23 -04:00
parent 6131c7afc1
commit 1f1c590409
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 38 additions and 2 deletions

View File

@ -242,6 +242,9 @@ class PowerDevice:
if config.has_option('on_when_upload_queued'): if config.has_option('on_when_upload_queued'):
self.on_when_queued = config.getboolean('on_when_upload_queued', self.on_when_queued = config.getboolean('on_when_upload_queued',
False, deprecate=True) False, deprecate=True)
self.initial_state: Optional[bool] = config.getboolean(
'initial_state', None
)
async def _check_klippy_printing(self) -> bool: async def _check_klippy_printing(self) -> bool:
kapis: APIComp = self.server.lookup_component('klippy_apis') kapis: APIComp = self.server.lookup_component('klippy_apis')
@ -428,6 +431,13 @@ class HTTPDevice(PowerDevice):
else: else:
self.init_task = None self.init_task = None
self.state = state self.state = state
if (
self.initial_state is not None and
state in ["on", "off"]
):
new_state = "on" if self.initial_state else "off"
if new_state != state:
await self.set_power(new_state)
self.notify_power_changed() self.notify_power_changed()
return return
@ -480,7 +490,8 @@ class GpioDevice(PowerDevice):
initial_val: Optional[int] = None initial_val: Optional[int] = None
) -> None: ) -> None:
super().__init__(config) super().__init__(config)
self.initial_state = config.getboolean('initial_state', False) 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(
@ -492,6 +503,7 @@ class GpioDevice(PowerDevice):
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: def init_state(self) -> None:
assert self.initial_state is not None
self.set_power("on" if self.initial_state else "off") self.set_power("on" if self.initial_state else "off")
def refresh_status(self) -> None: def refresh_status(self) -> None:
@ -582,6 +594,14 @@ class KlipperDevice(PowerDevice):
else: else:
assert data is not None assert data is not None
self._set_state_from_data(data) self._set_state_from_data(data)
if (
self.initial_state is not None and
self.state in ["on", "off"]
):
new_state = "on" if self.initial_state else "off"
if new_state != self.state:
await self.set_power(new_state)
self.notify_power_changed()
async def _handle_disconnect(self) -> None: async def _handle_disconnect(self) -> None:
self.is_shutdown = False self.is_shutdown = False
@ -663,7 +683,7 @@ class KlipperDevice(PowerDevice):
in_event = self.update_fut is not None in_event = self.update_fut is not None
last_state = self.state last_state = self.state
self.state = state self.state = state
if last_state != state and not in_event: if last_state not in [state, "init"] and not in_event:
self.notify_power_changed() self.notify_power_changed()
def _check_timer(self): def _check_timer(self):
@ -849,6 +869,13 @@ class TPLinkSmartPlug(PowerDevice):
else: else:
self.init_task = None self.init_task = None
self.state = "on" if state else "off" self.state = "on" if state else "off"
if (
self.initial_state is not None and
self.state in ["on", "off"]
):
new_state = "on" if self.initial_state else "off"
if new_state != self.state:
await self.set_power(new_state)
self.notify_power_changed() self.notify_power_changed()
return return
@ -1221,6 +1248,15 @@ class MQTTDevice(PowerDevice):
else: else:
logging.info( logging.info(
f"MQTT Power Device {self.name} initialized") f"MQTT Power Device {self.name} initialized")
if (
self.initial_state is not None and
self.state in ["on", "off"]
):
new_state = "on" if self.initial_state else "off"
if new_state != self.state:
await self.set_power(new_state)
# Don't reset on next connection
self.initial_state = None
self.notify_power_changed() self.notify_power_changed()
async def _on_mqtt_disconnected(self): async def _on_mqtt_disconnected(self):