Skip to content

feat: add HA Event entity for command errors#432

Merged
nanomad merged 5 commits intoSAIC-iSmart-API:developfrom
nanomad:feat/command-error-event
Mar 14, 2026
Merged

feat: add HA Event entity for command errors#432
nanomad merged 5 commits intoSAIC-iSmart-API:developfrom
nanomad:feat/command-error-event

Conversation

@nanomad
Copy link
Contributor

@nanomad nanomad commented Mar 14, 2026

Summary

  • Adds a Home Assistant MQTT Event entity (event.command_error) that fires whenever a vehicle command fails
  • Publishes a JSON event with event_type, command (topic), and detail (error message) so users can build HA automations around command failures
  • Covers all error paths: gateway exceptions, API errors, relogin failures, retry failures, and unexpected exceptions

Closes #272

Test plan

  • Verify HA discovery message is published for event type with correct event_types config
  • Trigger a command failure and confirm the event entity fires in HA with correct attributes
  • Confirm existing result topic behavior is unchanged
  • Run ruff check, mypy, and pytest — all pass

🤖 Generated with Claude Code

Closes SAIC-iSmart-API#272. When a command to the vehicle fails, a JSON event is now
published to the command/error topic so Home Assistant users can build
automations around command failures.

Changes:
- Add _publish_event() to HA discovery base for MQTT Event entities
- Register a "Command error" diagnostic event entity in discovery
- Publish error events from all failure paths in VehicleCommandHandler
- Wrap error event publishing in try-except to prevent masking original errors
- Cover the "no handler found" path with error events too

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nanomad nanomad force-pushed the feat/command-error-event branch from 56b3bf6 to f41fa80 Compare March 14, 2026 08:37
nanomad and others added 4 commits March 14, 2026 09:39
Replace repeated publish_str + LOG + publish_command_error calls across
all except blocks with a single __report_command_failure method.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Log the original error before attempting any MQTT publish
- Wrap both publish_str and publish_json in try-except
- Make exc optional so "no handler found" uses LOG.error (no traceback)
- Use safe "unexpected error" detail for the catch-all exception path

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Cover all error paths in VehicleCommandHandler:
- Success path (no error event)
- No handler found (error event without traceback)
- MqttGatewayException, SaicApiException, unexpected Exception
- SaicLogoutException: relogin success/failure, retry failure
- Resilience: publish_str/publish_json failures don't propagate
- Event payload structure and topic verification

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Restructure tests to avoid accessing private name-mangled attributes
and asserting on typed Publisher methods. Drive exceptions through
saic_api mocks instead of patching internal command handlers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nanomad nanomad merged commit d88f995 into SAIC-iSmart-API:develop Mar 14, 2026
3 checks passed
@nanomad nanomad deleted the feat/command-error-event branch March 14, 2026 08:52
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.

1 participant