import pytest from custom_components.easee_solar_charging.const import ( AMPS_TO_POWER_W, MAX_CHARGER_CURRENT_A, MIN_CHARGER_CURRENT_A, PHASES, VOLTAGE_V, solar_excess_to_charger_current, ) # --------------------------------------------------------------------------- # solar_excess_to_charger_current # --------------------------------------------------------------------------- def test_zero_excess_returns_zero(): assert solar_excess_to_charger_current(0) == 0 def test_just_below_minimum_threshold_returns_zero(): min_power = PHASES * VOLTAGE_V * MIN_CHARGER_CURRENT_A assert solar_excess_to_charger_current(min_power - 1) == 0 def test_exactly_at_minimum_threshold(): min_power = PHASES * VOLTAGE_V * MIN_CHARGER_CURRENT_A assert solar_excess_to_charger_current(min_power) == MIN_CHARGER_CURRENT_A def test_fractional_watts_floor(): # 3 * 230 * 8 = 5520 W → 8 A; adding 229 W is still not enough for 9 A assert solar_excess_to_charger_current(PHASES * VOLTAGE_V * 8 + 229) == 8 def test_intermediate_value(): assert solar_excess_to_charger_current(PHASES * VOLTAGE_V * 10) == 10 def test_clamps_at_maximum(): assert solar_excess_to_charger_current(999_999) == MAX_CHARGER_CURRENT_A def test_negative_excess_returns_zero(): assert solar_excess_to_charger_current(-500) == 0 @pytest.mark.parametrize("amps", range(MIN_CHARGER_CURRENT_A, MAX_CHARGER_CURRENT_A + 1)) def test_round_trip(amps): """Power for N amps should convert back to exactly N amps.""" assert solar_excess_to_charger_current(AMPS_TO_POWER_W[amps]) == amps # --------------------------------------------------------------------------- # AMPS_TO_POWER_W table # --------------------------------------------------------------------------- def test_table_covers_full_range(): assert set(AMPS_TO_POWER_W) == set(range(MIN_CHARGER_CURRENT_A, MAX_CHARGER_CURRENT_A + 1)) def test_table_minimum_entry(): assert AMPS_TO_POWER_W[MIN_CHARGER_CURRENT_A] == PHASES * VOLTAGE_V * MIN_CHARGER_CURRENT_A def test_table_maximum_entry(): assert AMPS_TO_POWER_W[MAX_CHARGER_CURRENT_A] == PHASES * VOLTAGE_V * MAX_CHARGER_CURRENT_A def test_table_is_strictly_increasing(): values = [AMPS_TO_POWER_W[a] for a in sorted(AMPS_TO_POWER_W)] assert values == sorted(values) and len(set(values)) == len(values)