33#
44# SPDX-License-Identifier: MIT
55
6+ import os
67import time
78import board
89import digitalio
1415MOTION_TIMEOUT = 300 # Timeout in seconds
1516USE_MQTT = False
1617
17- if USE_MQTT :
18- try :
19- from secrets import secrets
20- except ImportError :
21- print ("WiFi secrets are kept in secrets.py, please add them there!" )
22- raise
18+ # Use dict to avoid reassigning the variable
19+ timestamps = {
20+ "last_pir" : None
21+ }
2322
2423def set_outlet_state (value ):
25- global last_pir_timestamp
2624 if value :
2725 funhouse .peripherals .dotstars .fill (0x00FF00 )
28- last_pir_timestamp = time .monotonic ()
26+ timestamps [ "last_pir" ] = time .monotonic ()
2927 else :
3028 funhouse .peripherals .dotstars .fill (0xFF0000 )
31- last_pir_timestamp = time .monotonic () - MOTION_TIMEOUT
29+ timestamps [ "last_pir" ] = time .monotonic () - MOTION_TIMEOUT
3230
3331 outlet .value = value
3432 publish_outlet_state ()
@@ -42,35 +40,34 @@ def publish_outlet_state():
4240 funhouse .network .mqtt_publish (OUTLET_STATE_TOPIC , output )
4341 funhouse .peripherals .led = False
4442
45- def connected (client , userdata , result , payload ):
43+ def connected (client , _userdata , _result , _payload ):
4644 status .fill = 0x00FF00
4745 status .outline = 0x008800
4846 print ("Connected to MQTT! Subscribing..." )
4947 client .subscribe (OUTLET_COMMAND_TOPIC )
5048
51- def disconnected (client ):
49+ def disconnected (_client ):
5250 status .fill = 0xFF0000
5351 status .outline = 0x880000
5452
55- def message (client , topic , payload ):
53+ def message (_client , topic , payload ):
5654 print ("Topic {0} received new value: {1}" .format (topic , payload ))
5755 if topic == OUTLET_COMMAND_TOPIC :
5856 set_outlet_state (payload == "on" )
5957
6058def timeleft ():
61- seconds = int (last_pir_timestamp + MOTION_TIMEOUT - time .monotonic ())
59+ seconds = int (timestamps [ "last_pir" ] + MOTION_TIMEOUT - time .monotonic ())
6260 if outlet .value and seconds >= 0 :
6361 minutes = seconds // 60
6462 seconds -= minutes * 60
6563 return "{:01}:{:02}" .format (minutes , seconds )
66- return "Off"
64+ return "Off"
6765
6866# Set Initial States
6967funhouse = FunHouse (default_bg = 0x0F0F00 )
7068funhouse .peripherals .dotstars .fill (0 )
7169outlet = digitalio .DigitalInOut (board .A0 )
7270outlet .direction = digitalio .Direction .OUTPUT
73- last_pir_timestamp = None
7471funhouse .display .show (None )
7572funhouse .add_text (
7673 text = "Timeout Left:" ,
@@ -92,27 +89,27 @@ def timeleft():
9289# Initialize a new MQTT Client object
9390if USE_MQTT :
9491 funhouse .network .init_mqtt (
95- secrets [ "mqtt_broker" ] ,
96- secrets [ "mqtt_port" ] ,
97- secrets [ "mqtt_username" ] ,
98- secrets [ "mqtt_password" ] ,
92+ os . getenv ( "MQTT_BROKER" ) ,
93+ os . getenv ( "MQTT_PORT" ) ,
94+ os . getenv ( "MQTT_USERNAME" ) ,
95+ os . getenv ( "MQTT_PASSWORD" ) ,
9996 )
10097 funhouse .network .on_mqtt_connect = connected
10198 funhouse .network .on_mqtt_disconnect = disconnected
10299 funhouse .network .on_mqtt_message = message
103100
104- print ("Attempting to connect to {}" .format (secrets [ "mqtt_broker" ] ))
101+ print ("Attempting to connect to {}" .format (os . getenv ( "MQTT_BROKER" ) ))
105102 funhouse .network .mqtt_connect ()
106103set_outlet_state (False )
107104
108105while True :
109106 if funhouse .peripherals .pir_sensor :
110- last_pir_timestamp = time .monotonic ()
107+ timestamps [ "last_pir" ] = time .monotonic ()
111108 if not outlet .value :
112109 set_outlet_state (True )
113- if outlet .value and time .monotonic () >= last_pir_timestamp + MOTION_TIMEOUT :
110+ if outlet .value and time .monotonic () >= timestamps [ "last_pir" ] + MOTION_TIMEOUT :
114111 set_outlet_state (False )
115- funhouse .set_text (timeleft (), countdown_label )
112+ funhouse .set_text (timeleft (), countdown_label )
116113 # Check any topics we are subscribed to
117114 if USE_MQTT :
118115 funhouse .network .mqtt_loop (0.5 )
0 commit comments