Updating both .py files for ruff errors
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2026-09-06 00:56:02 -04:00
parent 7cf81914ec
commit 2a31e74fa8
2 changed files with 38 additions and 32 deletions
+30 -24
View File
@@ -1,8 +1,8 @@
import time
import struct
import socket
import network
import struct
import time
from machine import Pin, SoftI2C
import network
# -------------------------------------------------------------
# Configuration Loader
@@ -43,7 +43,9 @@ class SimpleMQTT:
flags |= 0x40
# Variable header: Protocol Name (MQTT) + Level (4) + Flags + KeepAlive (60s)
var_header = bytearray([0x00, 0x04, ord('M'), ord('Q'), ord('T'), ord('T'), 0x04, flags, 0x00, 0x3C])
var_header = bytearray(
[0x00, 0x04, ord("M"), ord("Q"), ord("T"), ord("T"), 0x04, flags, 0x00, 0x3C]
)
# Payload
payload = bytearray()
@@ -109,7 +111,7 @@ class BME280:
self.addr = addr
# Reset sensor
self.i2c.writeto_mem(self.addr, 0xE0, b"\xB6")
self.i2c.writeto_mem(self.addr, 0xE0, b"\xb6")
time.sleep(0.1)
self._read_calibration_data()
@@ -119,7 +121,7 @@ class BME280:
# Pressure oversampling x1, Temp oversampling x1, Normal mode
self.i2c.writeto_mem(self.addr, 0xF4, b"\x27")
# Standby 1000ms, filter off
self.i2c.writeto_mem(self.addr, 0xF5, b"\xA0")
self.i2c.writeto_mem(self.addr, 0xF5, b"\xa0")
def _read_calibration_data(self):
calib = self.i2c.readfrom_mem(self.addr, 0x88, 24)
@@ -149,13 +151,17 @@ class BME280:
# Temperature calculation (Celsius)
var1 = (((raw_t >> 3) - (self.dig_t1 << 1)) * self.dig_t2) >> 11
var2 = (((((raw_t >> 4) - self.dig_t1) * ((raw_t >> 4) - self.dig_t1)) >> 12) * self.dig_t3) >> 14
var2 = (
(((raw_t >> 4) - self.dig_t1) * ((raw_t >> 4) - self.dig_t1)) >> 12
) * self.dig_t3 >> 14
t_fine = var1 + var2
temp = ((t_fine * 5 + 128) >> 8) / 100.0
# Pressure calculation (hPa)
p_var1 = t_fine - 128000
p_var2 = p_var1 * p_var1 * self.dig_p6 + ((p_var1 * self.dig_p5) << 17) + (self.dig_p4 << 35)
p_var2 = (
p_var1 * p_var1 * self.dig_p6 + ((p_var1 * self.dig_p5) << 17) + (self.dig_p4 << 35)
)
p_var1 = ((p_var1 * p_var1 * self.dig_p3) >> 8) + ((p_var1 * self.dig_p2) << 12)
p_var1 = (((1 << 47) + p_var1) * self.dig_p1) >> 33
@@ -171,19 +177,19 @@ class BME280:
# Humidity calculation (%)
h_var = t_fine - 76800
h_var = (
((((raw_h << 14) - (self.dig_h4 << 20) - (self.dig_h5 * h_var)) + 16384) >> 15)
* (
h_var = ((((raw_h << 14) - (self.dig_h4 << 20) - (self.dig_h5 * h_var)) + 16384) >> 15) * (
(
(
(
((((h_var * self.dig_h6) >> 10) * (((h_var * self.dig_h3) >> 11) + 32768)) >> 10)
+ 2097152
(((h_var * self.dig_h6) >> 10) * (((h_var * self.dig_h3) >> 11) + 32768))
>> 10
)
* self.dig_h2
+ 8192
+ 2097152
)
>> 14
* self.dig_h2
+ 8192
)
>> 14
)
h_var = h_var - (((((h_var >> 15) * (h_var >> 15)) >> 7) * self.dig_h1) >> 4)
h_var = max(0, min(h_var, 419430400))
@@ -192,7 +198,7 @@ class BME280:
return {
"temperature": round(temp, 2),
"pressure": round(pressure, 2),
"humidity": round(humidity, 2)
"humidity": round(humidity, 2),
}
@@ -207,7 +213,7 @@ def register_ha_discovery(mqtt):
f'"name":"{NODE_NAME}",'
f'"model":"Pico 2 W",'
f'"manufacturer":"Raspberry Pi"'
f'}}'
f"}}"
)
configs = [
@@ -216,22 +222,22 @@ def register_ha_discovery(mqtt):
"name": "Temperature",
"unit": "°C",
"cls": "temperature",
"val_tpl": "{{ value_json.temperature }}"
"val_tpl": "{{ value_json.temperature }}",
},
{
"id": "humidity",
"name": "Humidity",
"unit": "%",
"cls": "humidity",
"val_tpl": "{{ value_json.humidity }}"
"val_tpl": "{{ value_json.humidity }}",
},
{
"id": "pressure",
"name": "Pressure",
"unit": "hPa",
"cls": "atmospheric_pressure",
"val_tpl": "{{ value_json.pressure }}"
}
"val_tpl": "{{ value_json.pressure }}",
},
]
for c in configs:
@@ -246,7 +252,7 @@ def register_ha_discovery(mqtt):
f'"unit_of_measurement":"{c["unit"]}",'
f'"state_topic":"{STATE_TOPIC}",'
f'"value_template":"{c["val_tpl"]}",'
f'{device_info}}}'
f"{device_info}}}"
)
mqtt.publish(topic, payload, retain=True)
print(f"Registered HA discovery: {c['id']}")
@@ -272,7 +278,7 @@ def connect_wifi():
def main():
# Adjust SDA/SCL pins according to your wiring (Default GP4/GP5 for SoftI2C)
# Adjust SDA/SCL pins according to your wiring
i2c = SoftI2C(sda=Pin(16), scl=Pin(17), freq=100000)
bme = BME280(i2c)