Skip to content

Conversation

@springfall2008
Copy link
Owner

Summary

Adds comprehensive unit tests for the unit conversion functionality in PredAI.

Changes

  • test_unit_conversion: Tests direct unit conversion for all supported conversions (kWh↔Wh, W↔kW)
  • test_get_history_with_unit_conversion: Tests unit conversion within the get_history flow (W → kW)
  • test_get_history_no_conversion_needed: Tests behavior when units already match

Technical Details

  • Updated convert_units function to work exclusively with list format (as returned by HA API)
  • Removed unused DataFrame handling code
  • All conversions properly handle the HA API format with state and last_updated fields

Testing

All 6 tests passing:

  • ✅ test_sine_wave_prediction
  • ✅ test_mocked_ha_interface_prediction
  • ✅ test_incrementing_sensor
  • ✅ test_unit_conversion (new)
  • ✅ test_get_history_with_unit_conversion (new)
  • ✅ test_get_history_no_conversion_needed (new)

- Added test_unit_conversion test for direct unit conversion testing
- Added test_get_history_with_unit_conversion to test unit conversion in get_history flow
- Added test_get_history_no_conversion_needed to test when units match
- Updated convert_units to work with list format from HA API
- All 6 tests passing successfully
Copilot AI review requested due to automatic review settings December 31, 2025 10:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive unit tests for the unit conversion functionality in PredAI, enabling proper handling of different measurement units (kWh↔Wh, W↔kW) when retrieving historical sensor data.

Key Changes:

  • Added three new test methods to validate unit conversion functionality
  • Implemented the convert_units function to handle conversions between supported units
  • Updated get_history function to accept a required_units parameter and perform automatic unit conversion

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
test_predai.py Adds three new unit tests: test_unit_conversion validates direct unit conversions, test_get_history_with_unit_conversion tests W→kW conversion in the get_history flow, and test_get_history_no_conversion_needed verifies behavior when units already match
predai/rootfs/predai.py Implements convert_units function with support for kWh↔Wh and W↔kW conversions, adds required_units parameter to get_history, and integrates unit conversion into the main data retrieval flow

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

"""

print("********* Starting PredAI *********")
print("********* Starting PredAI *********")
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change appears to be an unintentional whitespace modification. Two spaces were removed from the banner message, changing "Starting PredAI" alignment. This seems unrelated to the unit conversion feature being added.

Suggested change
print("********* Starting PredAI *********")
print("********* Starting PredAI *********")

Copilot uses AI. Check for mistakes.
Comment on lines +389 to +390
# Keep original if conversion fails
pass
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling silently ignores conversion failures without logging any information. When a ValueError or KeyError occurs, the original value is kept but no warning is printed. This makes debugging difficult if data is malformed. Consider adding a print statement or warning when conversion fails to maintain consistency with the warning message on line 380 for unsupported conversions.

Suggested change
# Keep original if conversion fails
pass
# Keep original if conversion fails, but log a warning for visibility
print(
"Warn: Failed to convert state '{}' from {} to {}. Keeping original value.".format(
item.get("state"), from_units, to_units
)
)

Copilot uses AI. Check for mistakes.
dataset, start, end = await interface.get_history(sensor_name, now, days=days)
if required_units:
units = await interface.get_state(sensor_name, attribute="unit_of_measurement")
if (required_units is not None) and (units is not None) and (units != required_units):
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition (required_units is not None) is redundant here. The outer if required_units: on line 400 already checks this condition, so this inner check is unnecessary and reduces code clarity.

Suggested change
if (required_units is not None) and (units is not None) and (units != required_units):
if (units is not None) and (units != required_units):

Copilot uses AI. Check for mistakes.
async def convert_units(dataset, from_units, to_units):
"""
Convert units in the dataset from from_units to to_units.
Currently supports conversion between kWh and Wh.
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation comment is incomplete and outdated. It states the function "Currently supports conversion between kWh and Wh" but the implementation also supports W↔kW conversions. The documentation should be updated to reflect all supported conversions: kWh↔Wh and W↔kW.

Suggested change
Currently supports conversion between kWh and Wh.
Currently supports conversion between kWh and Wh, and between W and kW (in both directions).

Copilot uses AI. Check for mistakes.
Handles list format from HA API.
"""
print("Converting units from {} to {}".format(from_units, to_units))
factor = 1.0
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to 'factor' is unnecessary as it is redefined before this value is used.
This assignment to 'factor' is unnecessary as it is redefined before this value is used.
This assignment to 'factor' is unnecessary as it is redefined before this value is used.
This assignment to 'factor' is unnecessary as it is redefined before this value is used.

Suggested change
factor = 1.0

Copilot uses AI. Check for mistakes.
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'predai', 'rootfs'))

from predai import HAInterface, Prophet, timestr_to_datetime
from predai import HAInterface, Prophet, timestr_to_datetime, convert_units, get_history
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'timestr_to_datetime' is not used.

Suggested change
from predai import HAInterface, Prophet, timestr_to_datetime, convert_units, get_history
from predai import HAInterface, Prophet, convert_units, get_history

Copilot uses AI. Check for mistakes.
@springfall2008 springfall2008 merged commit 82ab708 into main Dec 31, 2025
7 checks passed
@springfall2008 springfall2008 deleted the add-unit-conversion-tests branch December 31, 2025 10:49
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.

2 participants