|
| 1 | +# Zendure HA — Codebase Quality Sweep |
| 2 | + |
| 3 | +## What This Is |
| 4 | + |
| 5 | +A comprehensive quality pass on the Zendure Home Assistant integration — a custom component that manages Zendure solar/battery devices via MQTT and BLE. The goal is to audit the entire codebase, fix bugs, clean up code structure, and improve reliability and performance. |
| 6 | + |
| 7 | +## Core Value |
| 8 | + |
| 9 | +Clean, reliable code that a developer can confidently read, extend, and maintain. |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +### Validated |
| 14 | + |
| 15 | +- ✓ Home Assistant integration with config flow — existing |
| 16 | +- ✓ Cloud MQTT device communication — existing |
| 17 | +- ✓ Local MQTT support — existing |
| 18 | +- ✓ BLE device communication via ZenSDK — existing |
| 19 | +- ✓ Power distribution and management — existing |
| 20 | +- ✓ Multi-device support (SolarFlow, Hub, ACE, AIO, Hyper) — existing |
| 21 | +- ✓ Entity auto-creation from device properties — existing |
| 22 | +- ✓ State persistence across restarts — existing |
| 23 | +- ✓ Fuse group power constraints — existing |
| 24 | +- ✓ P1 meter integration for smart matching — existing |
| 25 | + |
| 26 | +### Active |
| 27 | + |
| 28 | +- [ ] Fix known bugs (attribute name typos, missing base class attributes) |
| 29 | +- [ ] Replace broad exception handling with specific exception types |
| 30 | +- [ ] Clean up code structure (reduce complexity, extract methods) |
| 31 | +- [ ] Improve thread/async safety in MQTT callbacks |
| 32 | +- [ ] Remove credentials from log output |
| 33 | +- [ ] Replace datetime sentinel values with explicit state tracking |
| 34 | +- [ ] Improve BLE connection reliability |
| 35 | +- [ ] Optimize performance (BLE scanner caching, power calculation caching) |
| 36 | +- [ ] Add MQTT payload validation |
| 37 | + |
| 38 | +### Out of Scope |
| 39 | + |
| 40 | +- New device support — this is a quality pass, not a feature release |
| 41 | +- Test infrastructure — separate initiative after code is clean |
| 42 | +- Migration from paho-mqtt to async MQTT — major change, defer |
| 43 | +- UI/dashboard changes — not part of codebase cleanup |
| 44 | +- SHA1/MD5 replacement — constrained by Zendure API requirements |
| 45 | + |
| 46 | +## Context |
| 47 | + |
| 48 | +This is a brownfield Home Assistant custom integration with ~1500 lines across core modules. The codebase has no tests, broad exception handling throughout, and several known bugs identified during mapping. The integration supports multiple Zendure device families via MQTT (cloud and local) and BLE protocols, with a power distribution algorithm managing charge/discharge across devices. |
| 49 | + |
| 50 | +Key concerns from codebase audit: |
| 51 | +- **27 broad exception handlers** masking specific failures |
| 52 | +- **Attribute name typo** (`hemsStateUpdate` vs `hemsStateUpdated`) causing runtime errors |
| 53 | +- **Missing base class attribute** (`aggrOffGrid`) causing AttributeError on some device types |
| 54 | +- **Credentials logged** in plaintext |
| 55 | +- **Complex methods** with high cyclomatic complexity (entityUpdate, powerChanged) |
| 56 | +- **Thread safety issues** in MQTT callback handlers |
| 57 | + |
| 58 | +## Constraints |
| 59 | + |
| 60 | +- **Compatibility**: Must remain compatible with Home Assistant 2026.2.0+ |
| 61 | +- **Protocol**: SHA1/MD5 usage is dictated by Zendure's cloud API — cannot change unilaterally |
| 62 | +- **Dependencies**: paho-mqtt 2.1.0 is the current MQTT client — no library migration in this scope |
| 63 | +- **Behavior**: Existing functionality must continue working — this is cleanup, not redesign |
| 64 | + |
| 65 | +## Key Decisions |
| 66 | + |
| 67 | +| Decision | Rationale | Outcome | |
| 68 | +|----------|-----------|---------| |
| 69 | +| Full sweep, no off-limits areas | User wants comprehensive cleanup | — Pending | |
| 70 | +| Keep external behavior identical | Quality pass should not break existing setups | — Pending | |
| 71 | +| Defer test infrastructure | Clean code first, then add tests | — Pending | |
| 72 | +| Defer async MQTT migration | Too large for a quality pass | — Pending | |
| 73 | + |
| 74 | +--- |
| 75 | +*Last updated: 2026-03-27 after initialization* |
0 commit comments