|
| 1 | +"""Test calculated current sensors for PowerSensor.""" |
| 2 | +import pytest |
| 3 | +from custom_components.enpal_webparser.utils import add_calculated_current_sensors |
| 4 | + |
| 5 | + |
| 6 | +def test_calculate_current_sensors(): |
| 7 | + """Test that current is calculated correctly from power and voltage.""" |
| 8 | + # Test data based on user's screenshot: |
| 9 | + # Phase A: -61W / 231.1V = -0.26A |
| 10 | + # Phase B: -19W / 230.1V = -0.08A |
| 11 | + # Phase C: 77W / 230.3V = 0.33A |
| 12 | + |
| 13 | + sensors = [ |
| 14 | + { |
| 15 | + "name": "PowerSensor: Power AC Phase (A)", |
| 16 | + "value": "-61", |
| 17 | + "unit": "W", |
| 18 | + "group": "PowerSensor", |
| 19 | + "enabled": True, |
| 20 | + "enpal_last_update": "2025-10-31T09:57:14.635Z", |
| 21 | + }, |
| 22 | + { |
| 23 | + "name": "PowerSensor: Voltage Phase (A)", |
| 24 | + "value": "231.1", |
| 25 | + "unit": "V", |
| 26 | + "group": "PowerSensor", |
| 27 | + "enabled": True, |
| 28 | + "enpal_last_update": "2025-10-31T09:57:14.634Z", |
| 29 | + }, |
| 30 | + { |
| 31 | + "name": "PowerSensor: Power AC Phase (B)", |
| 32 | + "value": "-19", |
| 33 | + "unit": "W", |
| 34 | + "group": "PowerSensor", |
| 35 | + "enabled": True, |
| 36 | + "enpal_last_update": "2025-10-31T09:57:14.635Z", |
| 37 | + }, |
| 38 | + { |
| 39 | + "name": "PowerSensor: Voltage Phase (B)", |
| 40 | + "value": "230.1", |
| 41 | + "unit": "V", |
| 42 | + "group": "PowerSensor", |
| 43 | + "enabled": True, |
| 44 | + "enpal_last_update": "2025-10-31T09:57:14.634Z", |
| 45 | + }, |
| 46 | + { |
| 47 | + "name": "PowerSensor: Power AC Phase (C)", |
| 48 | + "value": "77", |
| 49 | + "unit": "W", |
| 50 | + "group": "PowerSensor", |
| 51 | + "enabled": True, |
| 52 | + "enpal_last_update": "2025-10-31T09:57:14.636Z", |
| 53 | + }, |
| 54 | + { |
| 55 | + "name": "PowerSensor: Voltage Phase (C)", |
| 56 | + "value": "230.3", |
| 57 | + "unit": "V", |
| 58 | + "group": "PowerSensor", |
| 59 | + "enabled": True, |
| 60 | + "enpal_last_update": "2025-10-31T09:57:14.634Z", |
| 61 | + }, |
| 62 | + ] |
| 63 | + |
| 64 | + result = add_calculated_current_sensors(sensors) |
| 65 | + |
| 66 | + # Should have original 6 sensors + 3 calculated current sensors |
| 67 | + assert len(result) == 9 |
| 68 | + |
| 69 | + # Find calculated current sensors by name |
| 70 | + current_a = next((s for s in result if "Current Phase (A)" in s["name"]), None) |
| 71 | + current_b = next((s for s in result if "Current Phase (B)" in s["name"]), None) |
| 72 | + current_c = next((s for s in result if "Current Phase (C)" in s["name"]), None) |
| 73 | + |
| 74 | + assert current_a is not None, "Current sensor for phase A not found" |
| 75 | + assert current_b is not None, "Current sensor for phase B not found" |
| 76 | + assert current_c is not None, "Current sensor for phase C not found" |
| 77 | + |
| 78 | + # Check calculated values (I = P / U) |
| 79 | + # Phase A: -61 / 231.1 = -0.26A |
| 80 | + assert float(current_a["value"]) == pytest.approx(-0.26, abs=0.01) |
| 81 | + assert current_a["unit"] == "A" |
| 82 | + assert current_a["device_class"] == "current" |
| 83 | + assert current_a["group"] == "PowerSensor" |
| 84 | + |
| 85 | + # Phase B: -19 / 230.1 = -0.08A |
| 86 | + assert float(current_b["value"]) == pytest.approx(-0.08, abs=0.01) |
| 87 | + assert current_b["unit"] == "A" |
| 88 | + |
| 89 | + # Phase C: 77 / 230.3 = 0.33A |
| 90 | + assert float(current_c["value"]) == pytest.approx(0.33, abs=0.01) |
| 91 | + assert current_c["unit"] == "A" |
| 92 | + |
| 93 | + |
| 94 | +def test_calculate_current_sensors_missing_voltage(): |
| 95 | + """Test that calculation handles missing voltage gracefully.""" |
| 96 | + sensors = [ |
| 97 | + { |
| 98 | + "name": "PowerSensor: Power AC Phase (A)", |
| 99 | + "value": "-61", |
| 100 | + "unit": "W", |
| 101 | + "group": "PowerSensor", |
| 102 | + "enabled": True, |
| 103 | + "enpal_last_update": "2025-10-31T09:57:14.635Z", |
| 104 | + }, |
| 105 | + # Missing voltage sensor for phase A |
| 106 | + ] |
| 107 | + |
| 108 | + result = add_calculated_current_sensors(sensors) |
| 109 | + |
| 110 | + # Should have only the original sensor (no current calculated) |
| 111 | + assert len(result) == 1 |
| 112 | + assert "Power AC Phase (A)" in result[0]["name"] |
| 113 | + |
| 114 | + |
| 115 | +def test_calculate_current_sensors_zero_voltage(): |
| 116 | + """Test that calculation handles zero voltage (division by zero).""" |
| 117 | + sensors = [ |
| 118 | + { |
| 119 | + "name": "PowerSensor: Power AC Phase (A)", |
| 120 | + "value": "-61", |
| 121 | + "unit": "W", |
| 122 | + "group": "PowerSensor", |
| 123 | + "enabled": True, |
| 124 | + "enpal_last_update": "2025-10-31T09:57:14.635Z", |
| 125 | + }, |
| 126 | + { |
| 127 | + "name": "PowerSensor: Voltage Phase (A)", |
| 128 | + "value": "0", |
| 129 | + "unit": "V", |
| 130 | + "group": "PowerSensor", |
| 131 | + "enabled": True, |
| 132 | + "enpal_last_update": "2025-10-31T09:57:14.634Z", |
| 133 | + }, |
| 134 | + ] |
| 135 | + |
| 136 | + result = add_calculated_current_sensors(sensors) |
| 137 | + |
| 138 | + # Should have only the original sensors (no current calculated due to zero voltage) |
| 139 | + assert len(result) == 2 |
| 140 | + assert not any("Current Phase (A)" in s["name"] for s in result) |
| 141 | + |
| 142 | + |
| 143 | +def test_calculate_current_sensors_non_powersensor_group(): |
| 144 | + """Test that calculation only applies to PowerSensor group.""" |
| 145 | + sensors = [ |
| 146 | + { |
| 147 | + "name": "Inverter: Power AC Phase (A)", |
| 148 | + "value": "100", |
| 149 | + "unit": "W", |
| 150 | + "group": "Inverter", # Not PowerSensor |
| 151 | + "enabled": True, |
| 152 | + "enpal_last_update": "2025-10-31T09:57:14.635Z", |
| 153 | + }, |
| 154 | + { |
| 155 | + "name": "Inverter: Voltage Phase (A)", |
| 156 | + "value": "230", |
| 157 | + "unit": "V", |
| 158 | + "group": "Inverter", |
| 159 | + "enabled": True, |
| 160 | + "enpal_last_update": "2025-10-31T09:57:14.634Z", |
| 161 | + }, |
| 162 | + ] |
| 163 | + |
| 164 | + result = add_calculated_current_sensors(sensors) |
| 165 | + |
| 166 | + # Should have only the original sensors (no calculation for non-PowerSensor group) |
| 167 | + assert len(result) == 2 |
| 168 | + assert not any("Current Phase" in s["name"] for s in result) |
0 commit comments