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 utime
|
||||
import ujson
|
||||
import urandom
|
||||
from umqtt.robust import MQTTClient
|
||||
from machine import Pin
|
||||
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_STATE = f"{MQTT_PREFIX}/state"
|
||||
T_STATUS = f"{MQTT_PREFIX}/status"
|
||||
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 = {
|
||||
"identifiers": [MQTT_CLIENT_ID],
|
||||
@@ -230,7 +222,6 @@ _DEVICE = {
|
||||
# WiFi
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_wifi_reconnect_delay_s = 5
|
||||
_wifi_check_interval_ms = 30000
|
||||
_last_wifi_check = 0
|
||||
_wifi_sta = None
|
||||
@@ -262,7 +253,7 @@ def connect_wifi(ssid, password, timeout_s=15):
|
||||
|
||||
|
||||
def check_wifi():
|
||||
global _wifi_sta, _last_wifi_check, _wifi_reconnect_delay_s
|
||||
global _wifi_sta, _last_wifi_check
|
||||
now = utime.ticks_ms()
|
||||
if utime.ticks_diff(now, _last_wifi_check) < _wifi_check_interval_ms:
|
||||
return
|
||||
@@ -293,7 +284,6 @@ def check_wifi():
|
||||
num_gauges = len(gauges)
|
||||
leds_red = []
|
||||
leds_green = []
|
||||
leds_bl = []
|
||||
for g in gauges:
|
||||
leds_red.append(Pin(g["red_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):
|
||||
global backlight_brightness, backlight_color
|
||||
if client_ref is None:
|
||||
return
|
||||
topic = topic.decode()
|
||||
@@ -647,16 +636,16 @@ def check_mqtt():
|
||||
port=MQTT_PORT,
|
||||
user=MQTT_USER,
|
||||
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.connect()
|
||||
_subscribe_all(client_ref)
|
||||
_mqtt_connected = True
|
||||
info("MQTT reconnected!")
|
||||
publish_discovery(client_ref)
|
||||
_subscribe_all(client_ref)
|
||||
publish_state(client_ref)
|
||||
publish_backlight_states(client_ref)
|
||||
return True
|
||||
except Exception as e2:
|
||||
log_err(f"MQTT reconnect attempt {attempt + 1} failed: {e2}")
|
||||
@@ -878,6 +867,7 @@ def main():
|
||||
continue
|
||||
|
||||
client_ref.check_msg()
|
||||
_flush_backlight_state()
|
||||
|
||||
pending = [g.steps_toward(gauge_targets[i],limit=50) for i, g in enumerate(gauge_objects)]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user