Skip to content

feat: configurable report mode, batch history import, and offline timeout#70

Open
wxtry wants to merge 19 commits intomash2k3:mainfrom
wxtry:pr/upstream-improvements
Open

feat: configurable report mode, batch history import, and offline timeout#70
wxtry wants to merge 19 commits intomash2k3:mainfrom
wxtry:pr/upstream-improvements

Conversation

@wxtry
Copy link

@wxtry wxtry commented Mar 8, 2026

Summary

Three enhancements for TLV and JSON device support:

  • Configurable auto-switch report mode — Adds an auto_switch_report_mode option (default: disabled) so users can control whether the integration automatically switches between realtime/historic mode on charge state changes.

  • Batch history data import to HA Statistics — Imports historical data points (JSON Type 17, TLV CMD 0x42 and CMD 0x31) into Home Assistant long-term statistics via async_add_external_statistics. Data is aggregated into hourly buckets with proper timestamp alignment. Supports both firmware 1.x (CMD 0x42) and 2.x (CMD 0x31) formats.

  • Configurable offline timeout — Adds an offline_timeout_minutes option (default: 65, range: 5–1440) to accommodate devices in Historic reporting mode that only report every ~60 minutes.

All three options are configurable per-device via the Options Flow UI.

Changes

  • sensor.py: Add _import_batch_statistics(), configurable timeout logic, auto-switch guard, CMD 0x31 support
  • config_flow.py: Extend OptionsFlowHandler with new options
  • const.py: New constants for options and defaults
  • manifest.json: Add recorder dependency
  • translations/en.json: UI labels for new options
  • tests/: Unit tests for all three features

Test plan

  • test_auto_switch.py — verifies auto-switch guard logic
  • test_batch_history.py — verifies statistics metadata, timestamp alignment, hourly averaging, zero-timestamp filtering
  • test_offline_timeout.py — verifies configurable timeout behavior
  • Manual testing: 6 devices (3× CGS1 JSON, 2× CGP22W TLV, 1× CGP22C TLV) verified on HA instance

🤖 Generated with Claude Code

wxtry and others added 19 commits March 4, 2026 22:07
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Options were silently wiped by async_create_entry(data={})
- Now: async_update_entry for model data, async_create_entry for options
- Use config_entry.entry_id instead of private _config_entry_id
- Guard _auto_switch_report_mode_on_battery_state() with options check
- Default initial TLV config to Historic mode
- Change JSON default update_interval from 15s to 300s

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- TLV Historic mode uses config_entry.options timeout (default 65 min)
- JSON devices use config_entry.options timeout (default 65 min)
- Real-time mode keeps fixed 300s timeout

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add _import_batch_statistics() helper
- CMD 0x42 history: all points imported via async_import_statistics
- Latest point still updates entity current state
- Statistics aligned to 5-minute boundaries
- Fix test_batch_history alignment test math (1709500123 % 300 = 223, not 123)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Type 17: parse batch sensorData, update entities from latest point
- Import all batch points to HA long-term statistics
- Send ACK when need_ack=1
- Type 13 explicitly ignored (was silently dropped before)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
HA's async_import_statistics requires statistic_id to be lowercase.
async_import_statistics validates statistic_id as entity_id format and
requires source="recorder". External statistics need
async_add_external_statistics which accepts the colon-separated format.

Also adds enhanced status logging and error handling for statistics
import to aid debugging offline timeout behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
HA requires timestamps at the top of the hour (minutes=0, seconds=0).
Changed from 5-minute alignment to 1-hour alignment, and aggregate
multiple data points within the same hour by averaging.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Firmware 2.0.6 sends historical data via CMD 0x31 instead of CMD 0x42.
Both carry identical sensorData[] arrays. Treat CMD 0x31 the same as
CMD 0x42 for batch statistics import.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Keep recorder dependency addition but revert fork-specific name,
documentation URL, issue tracker, and version to upstream values.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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