3
3
#
4
4
# SPDX-License-Identifier: MIT
5
5
6
+ import os
6
7
import time
7
8
import board
8
9
import digitalio
14
15
MOTION_TIMEOUT = 300 # Timeout in seconds
15
16
USE_MQTT = False
16
17
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
+ }
23
22
24
23
def set_outlet_state (value ):
25
- global last_pir_timestamp
26
24
if value :
27
25
funhouse .peripherals .dotstars .fill (0x00FF00 )
28
- last_pir_timestamp = time .monotonic ()
26
+ timestamps [ "last_pir" ] = time .monotonic ()
29
27
else :
30
28
funhouse .peripherals .dotstars .fill (0xFF0000 )
31
- last_pir_timestamp = time .monotonic () - MOTION_TIMEOUT
29
+ timestamps [ "last_pir" ] = time .monotonic () - MOTION_TIMEOUT
32
30
33
31
outlet .value = value
34
32
publish_outlet_state ()
@@ -42,35 +40,34 @@ def publish_outlet_state():
42
40
funhouse .network .mqtt_publish (OUTLET_STATE_TOPIC , output )
43
41
funhouse .peripherals .led = False
44
42
45
- def connected (client , userdata , result , payload ):
43
+ def connected (client , _userdata , _result , _payload ):
46
44
status .fill = 0x00FF00
47
45
status .outline = 0x008800
48
46
print ("Connected to MQTT! Subscribing..." )
49
47
client .subscribe (OUTLET_COMMAND_TOPIC )
50
48
51
- def disconnected (client ):
49
+ def disconnected (_client ):
52
50
status .fill = 0xFF0000
53
51
status .outline = 0x880000
54
52
55
- def message (client , topic , payload ):
53
+ def message (_client , topic , payload ):
56
54
print ("Topic {0} received new value: {1}" .format (topic , payload ))
57
55
if topic == OUTLET_COMMAND_TOPIC :
58
56
set_outlet_state (payload == "on" )
59
57
60
58
def timeleft ():
61
- seconds = int (last_pir_timestamp + MOTION_TIMEOUT - time .monotonic ())
59
+ seconds = int (timestamps [ "last_pir" ] + MOTION_TIMEOUT - time .monotonic ())
62
60
if outlet .value and seconds >= 0 :
63
61
minutes = seconds // 60
64
62
seconds -= minutes * 60
65
63
return "{:01}:{:02}" .format (minutes , seconds )
66
- return "Off"
64
+ return "Off"
67
65
68
66
# Set Initial States
69
67
funhouse = FunHouse (default_bg = 0x0F0F00 )
70
68
funhouse .peripherals .dotstars .fill (0 )
71
69
outlet = digitalio .DigitalInOut (board .A0 )
72
70
outlet .direction = digitalio .Direction .OUTPUT
73
- last_pir_timestamp = None
74
71
funhouse .display .show (None )
75
72
funhouse .add_text (
76
73
text = "Timeout Left:" ,
@@ -92,27 +89,27 @@ def timeleft():
92
89
# Initialize a new MQTT Client object
93
90
if USE_MQTT :
94
91
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" ) ,
99
96
)
100
97
funhouse .network .on_mqtt_connect = connected
101
98
funhouse .network .on_mqtt_disconnect = disconnected
102
99
funhouse .network .on_mqtt_message = message
103
100
104
- print ("Attempting to connect to {}" .format (secrets [ "mqtt_broker" ] ))
101
+ print ("Attempting to connect to {}" .format (os . getenv ( "MQTT_BROKER" ) ))
105
102
funhouse .network .mqtt_connect ()
106
103
set_outlet_state (False )
107
104
108
105
while True :
109
106
if funhouse .peripherals .pir_sensor :
110
- last_pir_timestamp = time .monotonic ()
107
+ timestamps [ "last_pir" ] = time .monotonic ()
111
108
if not outlet .value :
112
109
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 :
114
111
set_outlet_state (False )
115
- funhouse .set_text (timeleft (), countdown_label )
112
+ funhouse .set_text (timeleft (), countdown_label )
116
113
# Check any topics we are subscribed to
117
114
if USE_MQTT :
118
115
funhouse .network .mqtt_loop (0.5 )
0 commit comments