Send microwave states to lora

This commit is contained in:
2026-08-16 15:50:57 +02:00
parent 1a77794fd3
commit c6e56b58a0
5 changed files with 83 additions and 26 deletions
+17 -5
View File
@@ -81,6 +81,15 @@ def notify_display_update():
except RuntimeError:
pass
def on_microwave_change(state: MicrowaveState, field: MicrowaveStateFields):
if field != MicrowaveStateFields.STATE: # No external screen or lcd uses this field, so we can skip updates for it
notify_display_update()
if field == MicrowaveStateFields.STATE and state.state in (MicrowaveState.DONE, MicrowaveState.ANALYZING, MicrowaveState.WAITING_FOR_CLOUD):
# Send to LoRa device if state changed
lora.send_reliable(payloads.lora_microwave_state(state.state), max_retries=5)
def set_cloud_alert(state: bool):
"""Setter for cloud_alert that triggers a display update when changed."""
global cloud_alert
@@ -91,7 +100,7 @@ def set_cloud_alert(state: bool):
update_lcd_cloud_alert()
# Global state trackers
microwave_states = {"2": MicrowaveState("2", on_change_callback=notify_display_update)}
microwave_states = {"2": MicrowaveState("2", on_change_callback=on_microwave_change)}
button_state = False
cloud_alert = None # Global status flag for screen / UI display
async_event_queue = None
@@ -434,7 +443,7 @@ async def request_cloud_cooking_plan(microwave_id, sensors_data):
if c_time is None or c_power is None or c_temp is None:
print(f"[{microwave_id}] Invalid plan received: {res_json}")
microwave_states[microwave_id].set_state(MicrowaveState.IDLE)
microwave_states[microwave_id].set_state(MicrowaveState.DONE)
return
print(f"[{microwave_id}] Cloud Plan Received! Starting microwave: {c_time}s @ {c_power}W")
@@ -530,7 +539,7 @@ async def process_messages_task():
# Pass change callback to newly connected microwave
microwave_states[component_id] = MicrowaveState(
component_id,
on_change_callback=notify_display_update
on_change_callback=on_microwave_change
)
notify_display_update() # Update the external screens
update_lcd_microwave_count() # Update the LCD with new microwave count
@@ -641,8 +650,11 @@ async def monitor_dish_height_task():
while True:
dist = await get_filtered_dish_height()
current_state = microwave_states.get(mw_id, MicrowaveState.IDLE).state
try:
current_state = microwave_states.get(mw_id).state
except AttributeError:
current_state = MicrowaveState.IDLE
if dist is not None:
# Hysteresis Thresholds:
# - Must be > 2.5 cm to detect dish insertion