|
| 1 | +# LEDnetWF BLE v2 Integration - Development Guidelines |
| 2 | + |
| 3 | +## Project Mission |
| 4 | + |
| 5 | +Build a **new** Home Assistant integration (`lednetwf_ble`) for LEDnetWF Bluetooth Low Energy devices from scratch, based on reverse-engineered protocol documentation rather than copying from the old integration. |
| 6 | + |
| 7 | +### Goals |
| 8 | +- Clean, maintainable codebase with minimal files |
| 9 | +- Protocol-driven implementation based on `protocol_docs/` |
| 10 | +- Dynamic capability detection for unknown devices |
| 11 | +- Proper BLE advertisement parsing and state monitoring |
| 12 | +- Support for all LEDnetWF/IOTWF/IOTB device variants |
| 13 | + |
| 14 | +## Architecture |
| 15 | + |
| 16 | +### Key Principles |
| 17 | +1. **Protocol docs are the source of truth** - Always check `protocol_docs/` before implementing |
| 18 | +2. **No per-model files** - Use capability probing instead of hardcoded model classes |
| 19 | +3. **Accept unknown devices** - Probe capabilities at runtime rather than rejecting |
| 20 | +4. **Separate transport layer** - 8-byte header wrapping/unwrapping in `protocol.py` |
| 21 | + |
| 22 | +### File Structure |
| 23 | +``` |
| 24 | +custom_components/lednetwf_ble/ |
| 25 | +├── __init__.py # Integration setup, Bluetooth callbacks |
| 26 | +├── config_flow.py # Device discovery and configuration UI |
| 27 | +├── const.py # Constants, enums, capability mappings |
| 28 | +├── device.py # BLE connection management, state, commands |
| 29 | +├── light.py # Home Assistant Light entity |
| 30 | +├── number.py # Effect speed Number entity |
| 31 | +├── protocol.py # Transport layer, command builders, response parsers |
| 32 | +├── manifest.json # Integration metadata |
| 33 | +├── strings.json # UI strings |
| 34 | +└── translations/ # Localization files |
| 35 | +``` |
| 36 | + |
| 37 | +## Protocol Reference |
| 38 | + |
| 39 | +### Device Discovery |
| 40 | +- **Name filter**: Accept `LEDnetWF*`, `IOTWF*`, `IOTB*` |
| 41 | +- **Company ID**: Must be in range 0x5A00-0x5AFF (23040-23295) |
| 42 | +- **Payload length**: Must be exactly 27 bytes (Format B) |
| 43 | +- **Product ID**: Bytes 8-9 of payload (big-endian) |
| 44 | + |
| 45 | +### Command Formats |
| 46 | +- **0x3B**: Modern power/color commands (BLE version >= 5) |
| 47 | +- **0x31**: Legacy color command with RGB+WW+CW |
| 48 | +- **0x61**: Simple effects (IDs 37-56) |
| 49 | +- **0x38**: Symphony effects (scene 1-44, build 100-399) |
| 50 | +- **0x62**: LED settings (count, IC type, color order) |
| 51 | +- **0x81**: State query/response |
| 52 | + |
| 53 | +### Transport Layer |
| 54 | +All commands wrapped with 8-byte header: |
| 55 | +``` |
| 56 | +[0x00, seq, 0x80, payload_len, 0x00, cmd_family, 0x00, 0x00] + payload |
| 57 | +``` |
| 58 | + |
| 59 | +## Capability Detection |
| 60 | + |
| 61 | +### Known Devices |
| 62 | +Product IDs in `PRODUCT_CAPABILITIES` table have documented capabilities. |
| 63 | + |
| 64 | +### Unknown Devices |
| 65 | +For unknown product IDs: |
| 66 | +1. Accept the device (don't reject) |
| 67 | +2. Mark as `needs_probing: True` |
| 68 | +3. During setup, probe by: |
| 69 | + - Send state query, wait for response |
| 70 | + - Test RGB: set R=0x32, check if reflected in state |
| 71 | + - Test WW: set WW=0x32, check state |
| 72 | + - Test CW: set CW=0x32, check state |
| 73 | + - Restore original state |
| 74 | +4. Cache probed capabilities in config entry |
| 75 | + |
| 76 | +## Code Standards |
| 77 | + |
| 78 | +### Python Style |
| 79 | +- Type hints on all function signatures |
| 80 | +- Async/await for all I/O operations |
| 81 | +- Use `protocol_docs/` references in docstrings |
| 82 | + |
| 83 | +### Home Assistant Conventions |
| 84 | +- Proper entity lifecycle (async_added_to_hass, async_will_remove_from_hass) |
| 85 | +- Use Home Assistant's bluetooth component APIs |
| 86 | +- Implement options flow for runtime configuration |
| 87 | +- Support Bluetooth advertisement state updates |
| 88 | + |
| 89 | +### Error Handling |
| 90 | +- Never crash on unknown device features |
| 91 | +- Log warnings with hex dumps for debugging |
| 92 | +- Provide safe defaults for unknown modes |
| 93 | +- Graceful degradation for unsupported capabilities |
| 94 | + |
| 95 | +## Key Protocol Documentation |
| 96 | + |
| 97 | +| Document | Content | |
| 98 | +|----------|---------| |
| 99 | +| `02_ble_scanning_device_discovery.md` | Device name filters, scan config | |
| 100 | +| `03_manufacturer_data_parsing.md` | Format A/B parsing, field offsets | |
| 101 | +| `04_device_identification_capabilities.md` | Product ID mapping, probing methods | |
| 102 | +| `06_transport_layer_protocol.md` | 8-byte header format, wrapping | |
| 103 | +| `07_control_commands.md` | Power, color, white, effect commands | |
| 104 | +| `08_state_query_response_parsing.md` | 0x81 response format | |
| 105 | +| `09_effects_addressable_led_support.md` | LED settings, effect types | |
| 106 | + |
| 107 | +## Testing |
| 108 | + |
| 109 | +- Test with actual hardware when possible |
| 110 | +- Request debug logs from users for new device variants |
| 111 | +- Verify capability probing works for unknown product IDs |
| 112 | +- Check state updates from BLE advertisements |
| 113 | +- Validate disconnect delay behavior |
| 114 | + |
| 115 | +## Adding Support for New Features |
| 116 | + |
| 117 | +1. Check protocol documentation first |
| 118 | +2. Add to `protocol.py` with source reference in docstring |
| 119 | +3. Update `const.py` if new capabilities needed |
| 120 | +4. Update `device.py` to expose new functionality |
| 121 | +5. Update entity files (light.py, number.py) if UI needed |
0 commit comments