Skip to content

Commit e8a2fee

Browse files
authored
Merge pull request #2614 from makermelissa/main
Update and Lint Home Assistant Sensor code to work with settings.toml
2 parents f821708 + a85a203 commit e8a2fee

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

Home_Assistant_Sensor/code.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Author: Melissa LeBlanc-Williams for Adafruit Industries
99
"""
1010

11+
import os
1112
import time
1213
import ssl
1314
import json
@@ -27,28 +28,18 @@
2728
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
2829
sht = adafruit_shtc3.SHTC3(i2c)
2930

30-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
31-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
32-
# source control.
33-
# pylint: disable=no-name-in-module,wrong-import-order
34-
try:
35-
from secrets import secrets
36-
except ImportError:
37-
print("WiFi secrets are kept in secrets.py, please add them there!")
38-
raise
39-
40-
wifi.radio.connect(secrets["ssid"], secrets["password"])
41-
print("Connected to %s!" % secrets["ssid"])
31+
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
32+
print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID"))
4233

4334
# Create a socket pool
4435
pool = socketpool.SocketPool(wifi.radio)
4536

4637
# Set up a MiniMQTT Client
4738
mqtt_client = MQTT.MQTT(
48-
broker=secrets["mqtt_broker"],
49-
port=secrets["mqtt_port"],
50-
username=secrets["mqtt_username"],
51-
password=secrets["mqtt_password"],
39+
broker=os.getenv("MQTT_BROKER"),
40+
port=os.getenv("MQTT_PORT"),
41+
username=os.getenv("MQTT_USERNAME"),
42+
password=os.getenv("MQTT_PASSWORD"),
5243
socket_pool=pool,
5344
ssl_context=ssl.create_default_context(),
5445
)

0 commit comments

Comments
 (0)