Skip to content

Improve Safety State Reporting and MQTT Reconnect Availability - #119

Merged
chvvkumar merged 4 commits into
mainfrom
dev
Oct 9, 2026
Merged

chvvkumar merged 4 commits into
mainfrom
dev

Conversation

@chvvkumar

@chvvkumar chvvkumar commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

📋 Overview

This pull request significantly improves the system's reliability by ensuring consistent safety state reporting across the external API, dashboard, and MQTT, resolving previous discrepancies. It also addresses a critical bug that prevented Home Assistant devices from regaining availability after an MQTT broker reconnect, enhancing integration stability.

🔄 Changes by Category

  • Bug Fixes:
  • Features:
    • Implemented consistent safety state reporting across external API, dashboard, and MQTT, all reflecting the real debounced detector state.
  • Refactoring:
    • Modified the core is_safe() method to return the debounced detector state directly, removing the ASCOM connection gate.
    • Applied the ASCOM client connection gate directly within Alpaca's issafe and devicestate routes, per client.
    • Renamed the dashboard tile from "Safety" to "Safety State" with an updated tooltip.
    • Updated MQTT setup paths to include an on_connect handler for republishing discovery configs.
  • Documentation:
    • Updated EXTERNAL_API.md to accurately describe the new behavior of the is_safe endpoint.
  • Tests:
    • Added new comprehensive tests for the is_safe gate logic to ensure correct behavior with and without ASCOM client connections.
    • Introduced new tests to verify Home Assistant availability after MQTT reconnects.
    • Updated existing multi-client verification tests to align with the new safety state logic.
  • Configuration:
    • Updated .gitignore to include new entries for build artifacts and remove an outdated one.

📊 Analyzed 4 commit(s) | 🕐 Updated: 2026-10-08T07:33:54.485Z | Generated by GitHub Actions


02:06:43.978 ASCOM Universal Device Conformance Checker Version 4.5.0.53834, Build time: Thu 06 August 2026 11:42:42
02:06:43.978 
02:06:43.978 Operating system is Windows 11 (25H2), Processor is Intel/AMD 64bit, Application is 64bit.
02:06:43.979 
02:06:43.979 Alpaca device: Manually configured device (127.0.0.1:11112 SafetyMonitor/0)
02:06:43.979 
02:06:43.979 CreateDevice                        INFO     Creating Alpaca device: IP address: 127.0.0.1, IP Port: 11112, Alpaca device number: 0
02:06:43.979 CreateDevice                        INFO     Alpaca device created OK
02:06:43.979 CreateDevice                        INFO     Successfully created driver
02:06:44.998 CreateDevice                        OK       Found a valid interface version: 3
02:06:44.998 CreateDevice                        OK       Driver instance created successfully
02:06:44.999 
02:06:44.999 Pre-connect checks
02:06:44.999 
02:06:45.000 Connect to device
02:06:45.007 Connected                           OK       Connected to device successfully using Connected = True
02:06:45.519 Connected                           OK       Disconnected from device successfully using Connected = False
02:06:45.525 Connect                             OK       Connected to device successfully using Connect()
02:06:45.525 
02:06:45.525 Common Driver Methods
02:06:45.525 InterfaceVersion                    OK       3
02:06:45.526 Connected                           OK       True
02:06:45.528 Description                         OK       ASCOM SafetyMonitor based on ML cloud detection
02:06:45.530 DriverInfo                          OK       ASCOM Alpaca SafetyMonitor v2.0 - Cloud Detection Driver
02:06:45.531 DriverVersion                       OK       2.0
02:06:45.532 Name                                OK       SCD Local Test (issue 117)
02:06:45.532 
02:06:45.534 SupportedActions                    OK       Driver returned an empty action list
02:06:45.534 Action                              INFO     Conform cannot test the Action method
02:06:45.534 
02:06:45.537 DeviceState                         OK       Received 1 operational state property.
02:06:45.537 DeviceState                         OK         IsSafe = False
02:06:45.538 DeviceState                         OK       Found all expected operational properties
02:06:45.538 
02:06:45.538 Properties
02:06:45.539 IsSafe                              OK       False
02:06:45.539 
02:06:45.539 Performance
02:06:50.540 DeviceState                         INFO     Transaction rate: 1276.3 per second
02:06:55.540 IsSafe                              INFO     Transaction rate: 1317.4 per second
02:06:55.541 
02:06:55.541 Disconnect from device
02:06:55.547 Disconnect                          OK       Disconnected from device successfully using Disconnect()
02:06:55.547 
02:06:55.548 Conformance test has finished
02:06:55.548 
02:06:55.548 Congratulations, no errors, warnings or issues found: your driver passes ASCOM validation!!
02:06:55.548  
02:06:55.548 Timing Summary                               See Help for further information.
02:06:55.548 Timing Summary                               FAST target response time: 0.1 seconds, (configuration and state reporting members).
02:06:55.548 Timing Summary                               STANDARD target response time: 1.0 second, (property write and asynchronous initiators).
02:06:55.548 Timing Summary                               EXTENDED target response time: 600.0 seconds, (synchronous methods, ImageArray and ImageArrayVariant).
02:06:55.549 Timing Summary                               Conform is configured to report both good and bad timing outcomes.
02:06:55.549 
02:06:55.549 Connect                                      At 02:06:45.521 Connect                  0.002 seconds. ✓ (STANDARD)
02:06:55.549 InterfaceVersion                             At 02:06:45.525 InterfaceVersion         0.000 seconds. ✓ (FAST)
02:06:55.549 Connected                                    At 02:06:45.526 Connected                0.001 seconds. ✓ (FAST)
02:06:55.549 Description                                  At 02:06:45.528 Description              0.002 seconds. ✓ (FAST)
02:06:55.549 DriverInfo                                   At 02:06:45.530 DriverInfo               0.001 seconds. ✓ (FAST)
02:06:55.549 DriverVersion                                At 02:06:45.531 DriverVersion            0.001 seconds. ✓ (FAST)
02:06:55.549 Name                                         At 02:06:45.532 Name                     0.001 seconds. ✓ (FAST)
02:06:55.549 SupportedActions                             At 02:06:45.534 SupportedActions         0.002 seconds. ✓ (FAST)
02:06:55.549 DeviceState                                  At 02:06:45.537 DeviceState              0.003 seconds. ✓ (FAST)
02:06:55.549 IsSafe                                       At 02:06:45.539 IsSafe                   0.001 seconds. ✓ (FAST)
02:06:55.549 Disconnect                                   At 02:06:55.543 Disconnect               0.001 seconds. ✓ (STANDARD)
02:06:55.549 
02:06:55.550 Congratulations, all members returned within their target response times!!

… of ASCOM connections

The driver used to force IsSafe to False whenever no ASCOM client was
connected, and that gate lived inside the shared is_safe() method, so it
leaked into /api/ext/v1/status and the dashboard too. Webhook, script and
microcontroller users of the external API therefore always saw is_safe:false
unless NINA happened to be attached (issue #117), and the dashboard appeared
frozen with no client (issue #90). MQTT had already bypassed the gate, so the
three surfaces disagreed with each other.

The gate was never an ASCOM requirement. The spec defines Connected per
client and says IsSafe raises NotConnected when not connected; it never asks
for a False value. ConformU dropped its "IsSafe must be False before connect"
check on 2025-03-16 by Core Team decision.

is_safe() now returns the debounced detector state. The Alpaca issafe and
devicestate routes apply the gate themselves, per client (IP + ClientID), so
a client that never connected still sees False exactly as before, while the
external API, dashboard and MQTT all report the same real state. The
dashboard tile is renamed Safety State with a tooltip describing the split.
Verified with ConformU 4.5 (no issues) and a local Docker run.
When the broker session dropped, the broker published the retained "offline"
Last Will for the device. paho's loop_start() then reconnected silently, but
discovery and availability were only ever published once at startup, so
nothing set the device back to "online". Home Assistant showed the cloud
detector as permanently unavailable even though its state topics kept
updating every detection cycle.

Both MQTT setup paths now install an on_connect handler that republishes the
discovery configs, which includes the retained "online" availability message,
on every successful connect rather than only the first. The handler accepts
the paho 1.x and 2.x callback shapes and ignores failed connects. The
ha_discovery attribute is initialised before the client is created because
the callback fires from the network thread and may run before discovery has
been set up; it simply no-ops in that case and the normal startup publish
covers it.
Improve safety state reporting and MQTT reconnect availability
@github-actions github-actions Bot changed the title Dev Improve Safety State Reporting and MQTT Reconnect Availability Oct 8, 2026
@chvvkumar
chvvkumar merged commit 863a5fe into main Oct 9, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Option to evaluate IsSafe / external API status without requiring an active ASCOM client connection

1 participant