Enhanced MQTT logging and fixed event loading in frontend

- Added detailed MQTT message logging
- Added validation for MQTT commands
- Fixed frontend event loading to preserve loaded events
- Added proper handling of new events while viewing history
This commit is contained in:
Josh Finlay 2025-01-08 10:30:07 +10:00
parent 597d0b9204
commit 3e2511f019
3 changed files with 35 additions and 5 deletions

View File

@ -733,6 +733,17 @@ async def init_mqtt(settings: Settings):
if settings.mqtt.enabled: if settings.mqtt.enabled:
ha_mqtt.update_settings(settings.mqtt) ha_mqtt.update_settings(settings.mqtt)
ha_mqtt.enable(True) ha_mqtt.enable(True)
# Set up command callback to handle open/close commands
async def command_handler(command: str):
logger.info(f"Processing MQTT command: {command}")
if command in ["OPEN", "CLOSE"]:
should_open = command == "OPEN"
await handle_mqtt_command(should_open)
else:
logger.warning(f"Received unknown MQTT command: {command}")
ha_mqtt.set_command_callback(command_handler)
logger.info("MQTT integration enabled") logger.info("MQTT integration enabled")
else: else:
ha_mqtt.enable(False) ha_mqtt.enable(False)

View File

@ -91,13 +91,18 @@ class HomeAssistantMQTT:
def on_message(self, client, topic, payload, qos, properties): def on_message(self, client, topic, payload, qos, properties):
"""Callback when message received""" """Callback when message received"""
try: try:
decoded_payload = payload.decode()
logger.info(f"MQTT message received - Topic: {topic}, Payload: {decoded_payload}, QoS: {qos}")
# Handle command messages # Handle command messages
if topic == self.command_topic and self.command_callback: if topic == self.command_topic and self.command_callback:
command = payload.decode().upper() command = decoded_payload.upper()
logger.debug(f"Received command: {command}") logger.debug(f"Processing command: {command}")
self.command_callback(command) self.command_callback(command)
else:
logger.debug(f"Message received on non-command topic: {topic}")
except Exception as e: except Exception as e:
logger.error(f"Error handling message: {e}") logger.error(f"Error handling message: {e}", exc_info=True)
def on_subscribe(self, client, mid, qos, properties): def on_subscribe(self, client, mid, qos, properties):
"""Callback when subscription confirmed""" """Callback when subscription confirmed"""

View File

@ -66,11 +66,25 @@ function App() {
api.getEvents(10, 0) api.getEvents(10, 0)
]); ]);
setGateStatus(statusData); setGateStatus(statusData);
// Only update events if we're on the first page
if (events.length <= 10) { // Update only if we're on the first page and don't have more events loaded
if (events.length <= 10 && !hasMoreEvents) {
setEvents(eventsData.events); setEvents(eventsData.events);
setHasMoreEvents(eventsData.hasMore); setHasMoreEvents(eventsData.hasMore);
setTotalEvents(eventsData.total); setTotalEvents(eventsData.total);
} else {
// If we have more events loaded, just prepend any new events
const newEvents = eventsData.events.filter(
newEvent => !events.some(
existingEvent =>
existingEvent.timestamp === newEvent.timestamp &&
existingEvent.action === newEvent.action
)
);
if (newEvents.length > 0) {
setEvents(prev => [...newEvents, ...prev]);
setTotalEvents(prev => prev + newEvents.length);
}
} }
} catch (err) { } catch (err) {
console.error('Failed to update status:', err); console.error('Failed to update status:', err);