Skip to content

fix: republish Total Battery Capacity after HA number update#445

Merged
nanomad merged 2 commits into
developfrom
fix/republish-total-battery-capacity
May 10, 2026
Merged

fix: republish Total Battery Capacity after HA number update#445
nanomad merged 2 commits into
developfrom
fix/republish-total-battery-capacity

Conversation

@nanomad
Copy link
Copy Markdown
Contributor

@nanomad nanomad commented May 10, 2026

Summary

  • The HA "Total Battery Capacity" number and sensor share the same MQTT state topic. Setting the number was only mutating VehicleInfo.custom_battery_capacity and returning RESULT_DO_NOTHING, so the sensor stayed pinned to the previously published value (typically the per-model default in rvs_charge_status.get_actual_battery_capacity) until the next charge-status poll. Result in HA: number widget shows the new value, sensor still shows the old one.
  • The handler now republishes vehicle.real_battery_capacity to the state topic right after the in-memory update, so the sensor catches up immediately. Reading through real_battery_capacity keeps the payload == 0 (clear override) path consistent — it falls back to the per-model default instead of publishing 0.
  • The kWh-derived sensors (SoC_kWh, Last Charge SoC kWh, the two Power Usage sensors) still use the previous battery_capacity_correction_factor until the next charge-status update — same staleness window as before, intentional for this local-only fix.

Test plan

  • Updated test_retained_battery_capacity_replays_to_vehicle_info to stub vehicle.real_battery_capacity and assert the new state-topic publish.
  • Added test_battery_capacity_zero_payload_publishes_model_default (payload 0 → per-model default republished).
  • Added test_battery_capacity_skips_publish_when_no_default (real_battery_capacity is None → publish skipped).
  • poetry run pytest — 276 passed.

nanomad added 2 commits May 10, 2026 16:28
The HA "Total Battery Capacity" number and sensor share the same MQTT
state topic. The `_set` handler used to mutate the in-memory override
on `VehicleInfo.custom_battery_capacity` and return `RESULT_DO_NOTHING`,
relying on the next charge-status poll to refresh the shared sensor
topic. As a result the HA number widget displayed the user's retained
`/set` value while the sensor (and any UI binding to it) remained stuck
on the previous value — typically the per-model default published from
`rvs_charge_status.get_actual_battery_capacity`.

Republish `vehicle.real_battery_capacity` to the state topic from the
handler itself so the sensor reflects the change immediately, without
forcing an extra vehicle poll. Reading through `real_battery_capacity`
keeps the `payload == 0` "clear override" path consistent: it falls
back to the per-model default instead of publishing `0`.

The kWh-derived sensors (`SoC_kWh`, `Last Charge SoC kWh`, the two
`Power Usage` sensors) still use the previous correction factor until
the next charge-status update, same staleness window as before.
Stub `vehicle.real_battery_capacity` on the mock so the existing retained-
replay test exercises the new state-topic publish, and add coverage for the
two interesting branches:
  * payload `0` clears the override and republishes the per-model default
  * unknown model (`real_battery_capacity is None`) skips the publish
@nanomad nanomad merged commit fe86989 into develop May 10, 2026
6 checks passed
@nanomad nanomad deleted the fix/republish-total-battery-capacity branch May 10, 2026 14:31
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