Skip to content

Commit c413397

Browse files
authored
Merge pull request #245 from justmobilize/remove-secrets-usage
Remove secrets usage
2 parents 08253c4 + 29e12f6 commit c413397

24 files changed

+396
-400
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ repos:
1111
- id: end-of-file-fixer
1212
- id: trailing-whitespace
1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: v0.3.4
14+
rev: v0.9.9
1515
hooks:
16-
- id: ruff-format
1716
- id: ruff
1817
args: ["--fix"]
18+
- id: ruff-format
1919
- repo: https://github.com/fsfe/reuse-tool
2020
rev: v3.0.1
2121
hooks:

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
3030
"""
3131

32+
# ruff: noqa: PLR6104,PLR6201,PLR6301 non-augmented-assignment,literal-membership,no-self-use
33+
3234
import errno
3335
import struct
3436
import time
@@ -111,7 +113,7 @@ def __init__(self) -> None:
111113
setattr(NullLogger, log_level, self.nothing)
112114

113115

114-
class MQTT:
116+
class MQTT: # noqa: PLR0904 # too-many-public-methods
115117
"""MQTT Client for CircuitPython.
116118
117119
:param str broker: MQTT Broker URL or IP Address.

examples/cellular/minimqtt_adafruitio_cellular.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import adafruit_fona.adafruit_fona_network as network
@@ -14,12 +14,13 @@
1414

1515
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1616

17-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
18-
# with your GPRS credentials. Add your Adafruit IO username and key as well.
19-
# DO NOT share that file or commit it into Git or other source control.
20-
21-
aio_username = os.getenv("aio_username")
22-
aio_key = os.getenv("aio_key")
17+
# Get FONA details and Adafruit IO keys, ensure these are setup in settings.toml
18+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
19+
apn = getenv("apn")
20+
apn_username = getenv("apn_username")
21+
apn_password = getenv("apn_password")
22+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
23+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2324

2425
### Cellular ###
2526

@@ -32,10 +33,10 @@
3233
### Feeds ###
3334

3435
# Setup a feed named 'photocell' for publishing to a feed
35-
photocell_feed = aio_username + "/feeds/photocell"
36+
photocell_feed = f"{aio_username}/feeds/photocell"
3637

3738
# Setup a feed named 'onoff' for subscribing to changes
38-
onoff_feed = aio_username + "/feeds/onoff"
39+
onoff_feed = f"{aio_username}/feeds/onoff"
3940

4041
### Code ###
4142

@@ -44,7 +45,7 @@
4445
def connected(client, userdata, flags, rc):
4546
# This function will be called when the client is connected
4647
# successfully to the broker.
47-
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
48+
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
4849
# Subscribe to all changes on the onoff_feed.
4950
client.subscribe(onoff_feed)
5051

@@ -61,9 +62,7 @@ def message(client, topic, message):
6162

6263

6364
# Initialize cellular data network
64-
network = network.CELLULAR(
65-
fona, (os.getenv("apn"), os.getenv("apn_username"), os.getenv("apn_password"))
66-
)
65+
network = network.CELLULAR(fona, (apn, apn_username, apn_password))
6766

6867
while not network.is_attached:
6968
print("Attaching to network...")
@@ -105,7 +104,7 @@ def message(client, topic, message):
105104
mqtt_client.loop()
106105

107106
# Send a new message
108-
print("Sending photocell value: %d..." % photocell_val)
107+
print(f"Sending photocell value: {photocell_val}...")
109108
mqtt_client.publish(photocell_feed, photocell_val)
110109
print("Sent!")
111110
photocell_val += 1

examples/cellular/minimqtt_simpletest_cellular.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import adafruit_fona.adafruit_fona_network as network
@@ -14,12 +14,14 @@
1414

1515
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1616

17-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
18-
# with your GPRS credentials. Add your Adafruit IO username and key as well.
19-
# DO NOT share that file or commit it into Git or other source control.
20-
21-
aio_username = os.getenv("aio_username")
22-
aio_key = os.getenv("aio_key")
17+
# Get FONA details and Adafruit IO keys, ensure these are setup in settings.toml
18+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
19+
apn = getenv("apn")
20+
apn_username = getenv("apn_username")
21+
apn_password = getenv("apn_password")
22+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
23+
aio_key = getenv("ADAFRUIT_AIO_KEY")
24+
broker = getenv("broker", "io.adafruit.com")
2325

2426
# Create a serial connection for the FONA connection
2527
uart = busio.UART(board.TX, board.RX)
@@ -70,9 +72,7 @@ def publish(client, userdata, topic, pid):
7072

7173

7274
# Initialize cellular data network
73-
network = network.CELLULAR(
74-
fona, (os.getenv("apn"), os.getenv("apn_username"), os.getenv("apn_password"))
75-
)
75+
network = network.CELLULAR(fona, (apn, apn_username, apn_password))
7676

7777
while not network.is_attached:
7878
print("Attaching to network...")
@@ -89,9 +89,9 @@ def publish(client, userdata, topic, pid):
8989

9090
# Set up a MiniMQTT Client
9191
client = MQTT.MQTT(
92-
broker=os.getenv("broker"),
93-
username=os.getenv("username"),
94-
password=os.getenv("password"),
92+
broker=broker,
93+
username=aio_username,
94+
password=aio_key,
9595
is_ssl=False,
9696
socket_pool=pool,
9797
ssl_context=ssl_context,
@@ -104,17 +104,17 @@ def publish(client, userdata, topic, pid):
104104
client.on_unsubscribe = unsubscribe
105105
client.on_publish = publish
106106

107-
print("Attempting to connect to %s" % client.broker)
107+
print(f"Attempting to connect to {client.broker}")
108108
client.connect()
109109

110-
print("Subscribing to %s" % mqtt_topic)
110+
print(f"Subscribing to {mqtt_topic}")
111111
client.subscribe(mqtt_topic)
112112

113-
print("Publishing to %s" % mqtt_topic)
113+
print(f"Publishing to {mqtt_topic}")
114114
client.publish(mqtt_topic, "Hello Broker!")
115115

116-
print("Unsubscribing from %s" % mqtt_topic)
116+
print(f"Unsubscribing from {mqtt_topic}")
117117
client.unsubscribe(mqtt_topic)
118118

119-
print("Disconnecting from %s" % client.broker)
119+
print(f"Disconnecting from {client.broker}")
120120
client.disconnect()

examples/cpython/minimqtt_adafruitio_cpython.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import socket
65
import ssl
76
import time
7+
from os import getenv
88

99
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1010

11-
### Secrets File Setup ###
11+
### Key Setup ###
1212

13-
# Add settings.toml to your filesystem. Add your Adafruit IO username and key as well.
14-
# DO NOT share that file or commit it into Git or other source control.
13+
# Add your Adafruit IO username and key to your env.
14+
# example:
15+
# export ADAFRUIT_AIO_USERNAME=your-aio-username
16+
# export ADAFRUIT_AIO_KEY=your-aio-key
1517

16-
aio_username = os.getenv("aio_username")
17-
aio_key = os.getenv("aio_key")
18+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
19+
aio_key = getenv("ADAFRUIT_AIO_KEY")
1820

1921
### Feeds ###
2022

2123
# Setup a feed named 'photocell' for publishing to a feed
22-
photocell_feed = aio_username + "/feeds/photocell"
24+
photocell_feed = f"{aio_username}/feeds/photocell"
2325

2426
# Setup a feed named 'onoff' for subscribing to changes
25-
onoff_feed = aio_username + "/feeds/onoff"
27+
onoff_feed = f"{aio_username}/feeds/onoff"
2628

2729
### Code ###
2830

@@ -31,7 +33,7 @@
3133
def connected(client, userdata, flags, rc):
3234
# This function will be called when the client is connected
3335
# successfully to the broker.
34-
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
36+
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
3537
# Subscribe to all changes on the onoff_feed.
3638
client.subscribe(onoff_feed)
3739

@@ -72,7 +74,7 @@ def message(client, topic, message):
7274
mqtt_client.loop()
7375

7476
# Send a new message
75-
print("Sending photocell value: %d..." % photocell_val)
77+
print(f"Sending photocell value: {photocell_val}...")
7678
mqtt_client.publish(photocell_feed, photocell_val)
7779
print("Sent!")
7880
photocell_val += 1

examples/cpython/minimqtt_simpletest_cpython.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import socket
65
import ssl
6+
from os import getenv
77

88
import adafruit_minimqtt.adafruit_minimqtt as MQTT
99

10-
# Add settings.toml to your filesystems. Add your Adafruit IO username and key as well.
11-
# DO NOT share that file or commit it into Git or other source control.
10+
# Add your Adafruit IO username and key to your env.
11+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
12+
# example:
13+
# export ADAFRUIT_AIO_USERNAME=your-aio-username
14+
# export ADAFRUIT_AIO_KEY=your-aio-key
15+
# export broker=io.adafruit.com
1216

13-
aio_username = os.getenv("aio_username")
14-
aio_key = os.getenv("aio_key")
17+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
18+
aio_key = getenv("ADAFRUIT_AIO_KEY")
19+
broker = getenv("broker", "io.adafruit.com")
1520

1621
### Topic Setup ###
1722

@@ -21,7 +26,7 @@
2126

2227
# Adafruit IO-style Topic
2328
# Use this topic if you'd like to connect to io.adafruit.com
24-
# mqtt_topic = aio_username + "/feeds/temperature"
29+
# mqtt_topic = f"{aio_username}/feeds/temperature"
2530

2631

2732
### Code ###
@@ -61,7 +66,7 @@ def message(client, topic, message):
6166

6267
# Set up a MiniMQTT Client
6368
mqtt_client = MQTT.MQTT(
64-
broker=os.getenv("broker"),
69+
broker=broker,
6570
username=aio_username,
6671
password=aio_key,
6772
socket_pool=socket,
@@ -76,17 +81,17 @@ def message(client, topic, message):
7681
mqtt_client.on_publish = publish
7782
mqtt_client.on_message = message
7883

79-
print("Attempting to connect to %s" % mqtt_client.broker)
84+
print(f"Attempting to connect to {mqtt_client.broker}")
8085
mqtt_client.connect()
8186

82-
print("Subscribing to %s" % mqtt_topic)
87+
print(f"Subscribing to {mqtt_topic}")
8388
mqtt_client.subscribe(mqtt_topic)
8489

85-
print("Publishing to %s" % mqtt_topic)
90+
print(f"Publishing to {mqtt_topic}")
8691
mqtt_client.publish(mqtt_topic, "Hello Broker!")
8792

88-
print("Unsubscribing from %s" % mqtt_topic)
93+
print(f"Unsubscribing from {mqtt_topic}")
8994
mqtt_client.unsubscribe(mqtt_topic)
9095

91-
print("Disconnecting from %s" % mqtt_client.broker)
96+
print(f"Disconnecting from {mqtt_client.broker}")
9297
mqtt_client.disconnect()

examples/esp32spi/minimqtt_adafruitio_esp32spi.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import os
54
import time
5+
from os import getenv
66

77
import adafruit_connection_manager
88
import board
@@ -13,12 +13,12 @@
1313

1414
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1515

16-
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
17-
# with your WiFi credentials. Add your Adafruit IO username and key as well.
18-
# DO NOT share that file or commit it into Git or other source control.
19-
20-
aio_username = os.getenv("aio_username")
21-
aio_key = os.getenv("aio_key")
16+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
17+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
18+
ssid = getenv("CIRCUITPY_WIFI_SSID")
19+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
20+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
21+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2222

2323
# If you are using a board with pre-defined ESP32 Pins:
2424
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -33,24 +33,24 @@
3333
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3434
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3535
"""Use below for Most Boards"""
36-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
36+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
3737
"""Uncomment below for ItsyBitsy M4"""
38-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
38+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
3939
# Uncomment below for an externally defined RGB LED
4040
# import adafruit_rgbled
4141
# from adafruit_esp32spi import PWMOut
4242
# RED_LED = PWMOut.PWMOut(esp, 26)
4343
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4444
# BLUE_LED = PWMOut.PWMOut(esp, 25)
45-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
45+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
4646

4747
### Feeds ###
4848

4949
# Setup a feed named 'photocell' for publishing to a feed
50-
photocell_feed = aio_username + "/feeds/photocell"
50+
photocell_feed = f"{aio_username}/feeds/photocell"
5151

5252
# Setup a feed named 'onoff' for subscribing to changes
53-
onoff_feed = aio_username + "/feeds/onoff"
53+
onoff_feed = f"{aio_username}/feeds/onoff"
5454

5555
### Code ###
5656

@@ -59,7 +59,7 @@
5959
def connected(client, userdata, flags, rc):
6060
# This function will be called when the client is connected
6161
# successfully to the broker.
62-
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
62+
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
6363
# Subscribe to all changes on the onoff_feed.
6464
client.subscribe(onoff_feed)
6565

@@ -77,7 +77,7 @@ def message(client, topic, message):
7777

7878
# Connect to WiFi
7979
print("Connecting to WiFi...")
80-
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
80+
esp.connect_AP(ssid, password)
8181
print("Connected!")
8282

8383
pool = adafruit_connection_manager.get_radio_socketpool(esp)
@@ -107,7 +107,7 @@ def message(client, topic, message):
107107
mqtt_client.loop()
108108

109109
# Send a new message
110-
print("Sending photocell value: %d..." % photocell_val)
110+
print(f"Sending photocell value: {photocell_val}...")
111111
mqtt_client.publish(photocell_feed, photocell_val)
112112
print("Sent!")
113113
photocell_val += 1

0 commit comments

Comments
 (0)