Conversation
There was a problem hiding this comment.
Pull request overview
This PR adjusts how the charge log’s energy mix (“power_source”) is calculated, switching from measurement-log-derived totals to a calculation based on charged_energy_by_source and imported_since_mode_switch in the current session.
Changes:
- Remove dependency on
analyse_percentage(get_log_from_date_until_now(...))for computing the energy mix. - Add
_calc_power_source_percentages(log_data)to compute mix from accumulatedcharged_energy_by_source. - Use the new calculation when creating charge log entries.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| power_source = {} | ||
| for source in ENERGY_SOURCES: | ||
| power_source[source] = log_data.charged_energy_by_source[source] / log_data.imported_since_mode_switch |
There was a problem hiding this comment.
_calc_power_source_percentages can raise ZeroDivisionError when log_data.imported_since_mode_switch is 0/0.0 (e.g., export-only sessions, or very small imports that were rounded to 0.0 in _create_entry). Right now this is only handled indirectly via get_value_or_default, which will emit a full exception log for a normal/expected scenario. Consider explicitly guarding on imported_since_mode_switch <= 0 and returning the zero-dict without raising, and optionally rounding the ratios (e.g., to 4 decimals) to keep the JSON output stable/consistent with other energy-mix calculations.
| power_source = {} | |
| for source in ENERGY_SOURCES: | |
| power_source[source] = log_data.charged_energy_by_source[source] / log_data.imported_since_mode_switch | |
| """Calculate share of each energy source in imported energy. | |
| Returns a dict mapping each source to its fraction of the total imported | |
| energy since the last mode switch. If the imported energy is zero or | |
| negative (e.g. export-only session), all fractions are returned as 0.0. | |
| """ | |
| # Avoid ZeroDivisionError in export-only or zero-import sessions. | |
| if getattr(log_data, "imported_since_mode_switch", 0) <= 0: | |
| return {source: 0.0 for source in ENERGY_SOURCES} | |
| total_imported = log_data.imported_since_mode_switch | |
| power_source: Dict[str, float] = {} | |
| for source in ENERGY_SOURCES: | |
| # Use .get to be robust if a source key is missing. | |
| charged_by_source = log_data.charged_energy_by_source.get(source, 0.0) | |
| ratio = charged_by_source / total_imported | |
| # Round to keep JSON output stable/consistent with other energy-mix calculations. | |
| power_source[source] = round(ratio, 4) |
| energy_source = get_value_or_default(lambda: _calc_power_source_percentages(log_data), { | ||
| source: 0 for source in ENERGY_SOURCES}) |
There was a problem hiding this comment.
The energy-mix for the charge log entry now comes from charged_energy_by_source / imported_since_mode_switch, which is a behavioral change from using the measurement-log totals. Since this file already has unit tests for cost/source calculations (chargelog_test.py), please add coverage for the new log-entry mix calculation (including edge cases like imported_since_mode_switch == 0 and small rounded values) to prevent regressions.
#66000952