Code cleanup phase 1
This commit is contained in:
@@ -23,7 +23,6 @@ Serial log format: [HH:MM:SS.mmm] LEVEL message
|
|||||||
import network
|
import network
|
||||||
import utime
|
import utime
|
||||||
import ujson
|
import ujson
|
||||||
import urandom
|
|
||||||
from umqtt.robust import MQTTClient
|
from umqtt.robust import MQTTClient
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
from neopixel import NeoPixel
|
from neopixel import NeoPixel
|
||||||
@@ -208,15 +207,8 @@ gauge_topics = [make_gauge_topics(MQTT_PREFIX, g["id"]) for g in gauges]
|
|||||||
|
|
||||||
|
|
||||||
T_SET = f"{MQTT_PREFIX}/set"
|
T_SET = f"{MQTT_PREFIX}/set"
|
||||||
T_STATE = f"{MQTT_PREFIX}/state"
|
|
||||||
T_STATUS = f"{MQTT_PREFIX}/status"
|
|
||||||
T_ZERO = f"{MQTT_PREFIX}/zero"
|
T_ZERO = f"{MQTT_PREFIX}/zero"
|
||||||
|
|
||||||
T_DISC_GAUGE = f"homeassistant/number/{MQTT_CLIENT_ID}/config"
|
|
||||||
T_DISC_RED = f"homeassistant/switch/{MQTT_CLIENT_ID}_red/config"
|
|
||||||
T_DISC_GREEN = f"homeassistant/switch/{MQTT_CLIENT_ID}_green/config"
|
|
||||||
T_DISC_BL = f"homeassistant/light/{MQTT_CLIENT_ID}_bl/config"
|
|
||||||
|
|
||||||
|
|
||||||
_DEVICE = {
|
_DEVICE = {
|
||||||
"identifiers": [MQTT_CLIENT_ID],
|
"identifiers": [MQTT_CLIENT_ID],
|
||||||
@@ -230,7 +222,6 @@ _DEVICE = {
|
|||||||
# WiFi
|
# WiFi
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
_wifi_reconnect_delay_s = 5
|
|
||||||
_wifi_check_interval_ms = 30000
|
_wifi_check_interval_ms = 30000
|
||||||
_last_wifi_check = 0
|
_last_wifi_check = 0
|
||||||
_wifi_sta = None
|
_wifi_sta = None
|
||||||
@@ -262,7 +253,7 @@ def connect_wifi(ssid, password, timeout_s=15):
|
|||||||
|
|
||||||
|
|
||||||
def check_wifi():
|
def check_wifi():
|
||||||
global _wifi_sta, _last_wifi_check, _wifi_reconnect_delay_s
|
global _wifi_sta, _last_wifi_check
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if utime.ticks_diff(now, _last_wifi_check) < _wifi_check_interval_ms:
|
if utime.ticks_diff(now, _last_wifi_check) < _wifi_check_interval_ms:
|
||||||
return
|
return
|
||||||
@@ -293,7 +284,6 @@ def check_wifi():
|
|||||||
num_gauges = len(gauges)
|
num_gauges = len(gauges)
|
||||||
leds_red = []
|
leds_red = []
|
||||||
leds_green = []
|
leds_green = []
|
||||||
leds_bl = []
|
|
||||||
for g in gauges:
|
for g in gauges:
|
||||||
leds_red.append(Pin(g["red_pin"], Pin.OUT, value=0))
|
leds_red.append(Pin(g["red_pin"], Pin.OUT, value=0))
|
||||||
leds_green.append(Pin(g["green_pin"], Pin.OUT, value=0))
|
leds_green.append(Pin(g["green_pin"], Pin.OUT, value=0))
|
||||||
@@ -452,7 +442,6 @@ def _publish(topic, payload, retain=False):
|
|||||||
|
|
||||||
|
|
||||||
def on_message(topic, payload):
|
def on_message(topic, payload):
|
||||||
global backlight_brightness, backlight_color
|
|
||||||
if client_ref is None:
|
if client_ref is None:
|
||||||
return
|
return
|
||||||
topic = topic.decode()
|
topic = topic.decode()
|
||||||
@@ -647,16 +636,16 @@ def check_mqtt():
|
|||||||
port=MQTT_PORT,
|
port=MQTT_PORT,
|
||||||
user=MQTT_USER,
|
user=MQTT_USER,
|
||||||
password=MQTT_PASSWORD,
|
password=MQTT_PASSWORD,
|
||||||
keepalive=60,
|
keepalive=30,
|
||||||
)
|
)
|
||||||
client_ref.set_last_will(T_STATUS, b"offline", retain=True, qos=0)
|
|
||||||
client_ref.set_callback(on_message)
|
client_ref.set_callback(on_message)
|
||||||
client_ref.connect()
|
client_ref.connect()
|
||||||
_subscribe_all(client_ref)
|
|
||||||
_mqtt_connected = True
|
_mqtt_connected = True
|
||||||
info("MQTT reconnected!")
|
info("MQTT reconnected!")
|
||||||
publish_discovery(client_ref)
|
publish_discovery(client_ref)
|
||||||
|
_subscribe_all(client_ref)
|
||||||
publish_state(client_ref)
|
publish_state(client_ref)
|
||||||
|
publish_backlight_states(client_ref)
|
||||||
return True
|
return True
|
||||||
except Exception as e2:
|
except Exception as e2:
|
||||||
log_err(f"MQTT reconnect attempt {attempt + 1} failed: {e2}")
|
log_err(f"MQTT reconnect attempt {attempt + 1} failed: {e2}")
|
||||||
@@ -878,6 +867,7 @@ def main():
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
client_ref.check_msg()
|
client_ref.check_msg()
|
||||||
|
_flush_backlight_state()
|
||||||
|
|
||||||
pending = [g.steps_toward(gauge_targets[i],limit=50) for i, g in enumerate(gauge_objects)]
|
pending = [g.steps_toward(gauge_targets[i],limit=50) for i, g in enumerate(gauge_objects)]
|
||||||
|
|
||||||
|
|||||||
7
ota.py
7
ota.py
@@ -284,13 +284,6 @@ def _save_manifest(manifest, commit_sha=None):
|
|||||||
warn(f"Could not save manifest: {e}")
|
warn(f"Could not save manifest: {e}")
|
||||||
|
|
||||||
|
|
||||||
def _wipe_manifest():
|
|
||||||
try:
|
|
||||||
os.remove(MANIFEST_FILE)
|
|
||||||
info("Manifest wiped — full re-fetch on next update")
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def _ok_flag_exists():
|
def _ok_flag_exists():
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user