diff --git a/apps/predbat/unit_test.py b/apps/predbat/unit_test.py index b7fec413..cf15d88b 100644 --- a/apps/predbat/unit_test.py +++ b/apps/predbat/unit_test.py @@ -997,6 +997,64 @@ def call_service_template(self, service, data, domain="charge", extra_data={}) ha.service_store_enable = False return failed +def run_car_charging_smart_test(test_name, my_predbat, battery_size=10.0, limit=8.0, soc=0, rate=10.0, loss=1.0, max_price=99, smart=True, plan_time="00:00:00", expect_cost=0, expect_kwh=0): + """ + Run a car charging smart test + """ + failed = False + + print("**** Running Test: {} ****".format(test_name)) + + my_predbat.car_charging_battery_size = [battery_size] + my_predbat.car_charging_limit = [limit] + my_predbat.car_charging_soc = [soc] + my_predbat.car_charging_soc_next = [None] + my_predbat.car_charging_rate = [rate] + my_predbat.car_charging_loss = loss + my_predbat.car_charging_plan_max_price = [max_price] + my_predbat.car_charging_plan_smart = [smart] + my_predbat.car_charging_plan_time = [plan_time] + my_predbat.num_cars = 1 + + slots = my_predbat.plan_car_charging(0, my_predbat.low_rates) + total_kwh = 0 + total_cost = 0 + for slot in slots: + total_kwh += slot["kwh"] + total_cost += slot["cost"] + if total_kwh != expect_kwh: + print("ERROR: Car charging total kwh should be {} got {}".format(expect_kwh, total_kwh)) + failed = True + print(slots) + if total_cost != expect_cost: + print("ERROR: Car charging total cost should be {} got {}".format(expect_cost, total_cost)) + failed = True + print(slots) + + return failed + +def run_car_charging_smart_tests(my_predbat): + """ + Test car charging smart + """ + failed = False + reset_inverter(my_predbat) + + print("**** Running Car Charging Smart tests ****") + import_rate = 10.0 + export_rate = 5.0 + reset_rates2(my_predbat, import_rate, export_rate) + my_predbat.low_rates, lowest, highest = my_predbat.rate_scan_window(my_predbat.rate_import, 5, my_predbat.rate_import_cost_threshold, False) + + failed |= run_car_charging_smart_test("smart1", my_predbat, battery_size=12.0, limit=10.0, soc=0, rate=10.0, loss=1.0, max_price=99, smart=True, expect_cost=100, expect_kwh=10) + failed |= run_car_charging_smart_test("smart2", my_predbat, battery_size=12.0, limit=10.0, soc=0, rate=10.0, loss=1.0, max_price=99, smart=False, expect_cost=150, expect_kwh=10) + failed |= run_car_charging_smart_test("smart3", my_predbat, battery_size=12.0, limit=10.0, soc=2, rate=10.0, loss=1.0, max_price=99, smart=True, expect_cost=80, expect_kwh=8) + failed |= run_car_charging_smart_test("smart4", my_predbat, battery_size=12.0, limit=10.0, soc=2, rate=10.0, loss=0.5, max_price=99, smart=True, expect_cost=160, expect_kwh=16) + failed |= run_car_charging_smart_test("smart5", my_predbat, battery_size=100.0, limit=100.0, soc=0, rate=1.0, loss=1, max_price=99, smart=True, expect_cost=12*15, expect_kwh=12, plan_time="00:00:00") + failed |= run_car_charging_smart_test("smart6", my_predbat, battery_size=100.0, limit=100.0, soc=0, rate=1.0, loss=1, max_price=99, smart=True, expect_cost=14*15, expect_kwh=14, plan_time="02:00:00") + failed |= run_car_charging_smart_test("smart7", my_predbat, battery_size=100.0, limit=100.0, soc=0, rate=1.0, loss=1, max_price=10, smart=True, expect_cost=7*10, expect_kwh=7, plan_time="02:00:00") + + return failed def run_inverter_tests(): """ @@ -1693,9 +1751,9 @@ def run_single_debug(test_name, my_predbat, debug_file, expected_file=None): print("Combined export slots {} min_improvement_export {} set_export_freeze_only {}".format(my_predbat.combine_export_slots, my_predbat.metric_min_improvement_export, my_predbat.set_export_freeze_only)) if not expected_file: pass - # my_predbat.combine_export_slots = False + #my_predbat.combine_export_slots = False # my_predbat.best_soc_keep = 1.0 - # my_predbat.metric_min_improvement_export = 5 + #my_predbat.metric_min_improvement_export = 5 if re_do_rates: # Set rate thresholds @@ -1785,7 +1843,12 @@ def run_single_debug(test_name, my_predbat, debug_file, expected_file=None): print("Wrote plan to {}".format(filename)) # Expected - actual_data = {"charge_limit_best": my_predbat.charge_limit_best, "charge_window_best": my_predbat.charge_window_best, "export_window_best": my_predbat.export_window_best, "export_limits_best": my_predbat.export_limits_best} + actual_data = { + "charge_limit_best": my_predbat.charge_limit_best, + "charge_window_best": my_predbat.charge_window_best, + "export_window_best": my_predbat.export_window_best, + "export_limits_best": my_predbat.export_limits_best + } actual_json = json.dumps(actual_data) if expected_file: print("Compare with {}".format(expected_file)) @@ -1804,7 +1867,6 @@ def run_single_debug(test_name, my_predbat, debug_file, expected_file=None): print("Wrote plan json to {}".format(filename)) return failed - def run_execute_tests(my_predbat): print("**** Running execute tests ****\n") reset_inverter(my_predbat) @@ -5028,6 +5090,8 @@ def main(): failed = 1 if not failed: failed |= run_inverter_tests() + if not failed: + failed |= run_car_charging_smart_tests(my_predbat) if not failed: failed |= run_intersect_window_tests(my_predbat) if not failed: