Skip to content

Commit 09a9c38

Browse files
committed
Remove duplicate secrets import and reorganize
1 parent 48178b5 commit 09a9c38

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

Home_Assistant_Sensor/code.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
import json
1010
import alarm
1111
import board
12-
import busio
1312
import socketpool
1413
import wifi
1514
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1615
import adafruit_shtc3
1716

18-
sht = adafruit_shtc3.SHTC3(busio.I2C(board.SCL, board.SDA))
1917
PUBLISH_DELAY = 60
2018
MQTT_TOPIC = "state/temp-sensor"
21-
USE_DEEP_SLEEP = False
19+
USE_DEEP_SLEEP = True
20+
21+
# Connect to the Sensor
22+
sht = adafruit_shtc3.SHTC3(board.I2C())
2223

2324
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
2425
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
@@ -30,18 +31,9 @@
3031
print("WiFi secrets are kept in secrets.py, please add them there!")
3132
raise
3233

33-
print("Connecting to %s" % secrets["ssid"])
3434
wifi.radio.connect(secrets["ssid"], secrets["password"])
3535
print("Connected to %s!" % secrets["ssid"])
3636

37-
38-
### Secrets File Setup ###
39-
try:
40-
from secrets import secrets
41-
except ImportError:
42-
print("Connection secrets are kept in secrets.py, please add them there!")
43-
raise
44-
4537
# Create a socket pool
4638
pool = socketpool.SocketPool(wifi.radio)
4739

@@ -68,12 +60,12 @@
6860

6961
print("Publishing to %s" % MQTT_TOPIC)
7062
mqtt_client.publish(MQTT_TOPIC, json.dumps(output))
71-
last_update = time.monotonic()
7263

7364
if USE_DEEP_SLEEP:
7465
mqtt_client.disconnect()
7566
pause = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + PUBLISH_DELAY)
7667
alarm.exit_and_deep_sleep_until_alarms(pause)
7768
else:
69+
last_update = time.monotonic()
7870
while time.monotonic() < last_update + PUBLISH_DELAY:
7971
mqtt_client.loop()

0 commit comments

Comments
 (0)