Skip to content

Commit

Permalink
Add manual control feature and skip message handling and automatic sh…
Browse files Browse the repository at this point in the history
…utdown when manual control is enabled
  • Loading branch information
pajikos committed Mar 11, 2024
1 parent b35db8e commit c8dfbe0
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions mqtt-switch2.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,18 @@ def on_message(self, client, userdata, msg):
self.handle_message(payload)

def handle_message(self, payload):
state = switch.value
if payload == 'ON' and state != 1:
switch.on()
logger.info("Turning on the device.")
elif payload == 'OFF' and state == 1:
switch.off()
logger.info("Turning off the device.")
global manual_control
if manual_control:
logger.info("Manual control is enabled, skipping message handling.")
else:
state = switch.value
if payload == 'ON' and state != 1:
switch.on()
logger.info("Turning on the device.")
elif payload == 'OFF' and state == 1:
switch.off()
logger.info("Turning off the device.")

self.publish_state()

def on_subscribe(self, client, userdata, mid, reason_codes, properties):
Expand Down Expand Up @@ -155,7 +160,10 @@ def stop(self):

# Replace the timeloop jobs with instances of ScheduledTask
def scheduled_turn_off_function():
global last_call
global last_call, manual_control
if manual_control:
logger.info("Manual control is enabled, skipping automatic shutdown.")
return
if last_call and (datetime.now() - last_call).seconds / 60 > AUTOMATIC_SHUTDOWN_DELAY:
logger.info("No recent activity, turning off the device.")
switch.off()
Expand Down

0 comments on commit c8dfbe0

Please sign in to comment.