Skip to content

fix: populate projected budget bill and deferred amount sensors - #93

Merged
CamiloValderruten merged 3 commits into
masterfrom
fix/92-budget-billing-sensors
Aug 23, 2026
Merged

fix: populate projected budget bill and deferred amount sensors#93
CamiloValderruten merged 3 commits into
masterfrom
fix/92-budget-billing-sensors

Conversation

@CamiloValderruten

Copy link
Copy Markdown
Collaborator

Summary

Fixes #92 by addressing the KeyError and data fetching ordering when collecting Budget Billing details for enrolled accounts.

Root Cause

  1. In FplMainRegionApiClient.update(), __getBBL_async() was executed before get_energy_usage().
  2. Inside __getBBL_async(), projectedBillData["projected_bill"] threw a KeyError because projected bill data had not been fetched yet (and the key is stored as camelCase projectedBill).
  3. The broad try...except caught the KeyError and returned an empty dictionary, skipping both the calculation of budget_billing_projected_bill / budget_billing_daily_avg and the subsequent call to URL_BUDGET_BILLING_GRAPH (which populates defered_amount).

Changes

  • Reordered Account Update: Fetch get_energy_usage() and get_appliance_usage() prior to evaluating BBL program and running __getBBL_async().
  • Safe Dictionary Lookups: Used .get() for projectedBill and as_of_days in __getBBL_async().
  • Isolated HTTP Requests: Separated URL_BUDGET_BILLING_PREMISE_DETAILS and URL_BUDGET_BILLING_GRAPH into dedicated try...except blocks with clear error logging.
  • Defensive Fallback: Handled None checks in BillToDateSensor.
  • Unit Tests: Added tests/test_budget_billing.py covering BBL API client calculations, ordering in update(), partial failure resilience, and sensor state resolutions.

Verification

Ran test suite:

.venv/bin/python -m unittest discover tests

All 10 tests passed.

- Fetch energy usage before computing Budget Billing data so projected bill figures are available
- Safely access projectedBill dictionary keys to avoid KeyError
- Separate premise details and budget billing graph calls into isolated try/except blocks
- Add defensive fallback in BillToDateSensor
- Add comprehensive unit tests for budget billing client and sensors

Closes #92
@CamiloValderruten
CamiloValderruten merged commit 68a05b9 into master Aug 23, 2026
2 checks passed
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.

projected_budget_bill and defered_amount sensors stuck on "unknown" for Budget Billing accounts

1 participant