|
21 | 21 | build_sensor_entity, |
22 | 22 | EnpalBaseSensor, |
23 | 23 | EnpalEnergySensor, |
| 24 | + EnpalWallboxPowerSensor, |
24 | 25 | ) |
25 | 26 |
|
26 | 27 | class DummyCoordinator(DataUpdateCoordinator): |
@@ -117,3 +118,98 @@ async def test_build_energy_sensor_full(hass: HomeAssistant, mock_sensor_dict, h |
117 | 118 | assert "enpal_last_update" in sensor.extra_state_attributes |
118 | 119 |
|
119 | 120 |
|
| 121 | +# ---------- Wallbox power zero-override tests ---------- |
| 122 | + |
| 123 | +class _FakeState: |
| 124 | + """Minimal stand-in for homeassistant.core.State.""" |
| 125 | + def __init__(self, state: str): |
| 126 | + self.state = state |
| 127 | + |
| 128 | + |
| 129 | +class _FakeHass: |
| 130 | + """Minimal hass stub with a states registry.""" |
| 131 | + def __init__(self, states_map: dict): |
| 132 | + self._map = states_map |
| 133 | + |
| 134 | + class _States: |
| 135 | + def __init__(self, m): |
| 136 | + self._m = m |
| 137 | + def get(self, entity_id): |
| 138 | + return self._m.get(entity_id) |
| 139 | + |
| 140 | + @property |
| 141 | + def states(self): |
| 142 | + return self._States(self._map) |
| 143 | + |
| 144 | + |
| 145 | +def _wallbox_power_sensor(status_state: str | None): |
| 146 | + """Create an EnpalWallboxPowerSensor with a faked wallbox status.""" |
| 147 | + sensor_dict = { |
| 148 | + "name": "Power Wallbox Connector 1 Charging", |
| 149 | + "value": "4500", |
| 150 | + "unit": "W", |
| 151 | + "device_class": "power", |
| 152 | + "enabled": True, |
| 153 | + "group": "Wallbox", |
| 154 | + } |
| 155 | + entity = build_sensor_entity(sensor_dict, DummyCoordinator(), use_wallbox=True) |
| 156 | + assert isinstance(entity, EnpalWallboxPowerSensor) |
| 157 | + |
| 158 | + if status_state is not None: |
| 159 | + entity.hass = _FakeHass({"sensor.wallbox_status": _FakeState(status_state)}) |
| 160 | + else: |
| 161 | + entity.hass = _FakeHass({}) |
| 162 | + return entity |
| 163 | + |
| 164 | + |
| 165 | +def test_wallbox_power_zero_when_not_charging(): |
| 166 | + """Power sensor returns 0 when wallbox status is not 'charging'.""" |
| 167 | + entity = _wallbox_power_sensor("connected") |
| 168 | + assert entity.native_value == 0 |
| 169 | + assert entity.extra_state_attributes.get("enpal_raw_value") == "4500" |
| 170 | + assert entity.extra_state_attributes.get("enpal_zero_reason") == "wallbox not charging" |
| 171 | + |
| 172 | + |
| 173 | +def test_wallbox_power_passthrough_when_charging(): |
| 174 | + """Power sensor returns raw value when wallbox status is 'charging'.""" |
| 175 | + entity = _wallbox_power_sensor("charging") |
| 176 | + assert entity.native_value == "4500" |
| 177 | + assert "enpal_raw_value" not in entity.extra_state_attributes |
| 178 | + |
| 179 | + |
| 180 | +def test_wallbox_power_passthrough_when_status_missing(): |
| 181 | + """Power sensor returns raw value when wallbox_status entity doesn't exist.""" |
| 182 | + entity = _wallbox_power_sensor(None) |
| 183 | + assert entity.native_value == "4500" |
| 184 | + assert "enpal_raw_value" not in entity.extra_state_attributes |
| 185 | + |
| 186 | + |
| 187 | +def test_wallbox_current_sensor_zero_override(): |
| 188 | + """Current sensor also gets zero-override treatment.""" |
| 189 | + sensor_dict = { |
| 190 | + "name": "Current Wallbox Connector 1 Phase (A)", |
| 191 | + "value": "12.31", |
| 192 | + "unit": "A", |
| 193 | + "device_class": "current", |
| 194 | + "enabled": True, |
| 195 | + "group": "Wallbox", |
| 196 | + } |
| 197 | + entity = build_sensor_entity(sensor_dict, DummyCoordinator(), use_wallbox=True) |
| 198 | + assert isinstance(entity, EnpalWallboxPowerSensor) |
| 199 | + entity.hass = _FakeHass({"sensor.wallbox_status": _FakeState("connected")}) |
| 200 | + assert entity.native_value == 0 |
| 201 | + |
| 202 | + |
| 203 | +def test_wallbox_power_not_used_without_flag(): |
| 204 | + """Without use_wallbox=True, the regular base sensor is returned.""" |
| 205 | + sensor_dict = { |
| 206 | + "name": "Power Wallbox Connector 1 Charging", |
| 207 | + "value": "4500", |
| 208 | + "unit": "W", |
| 209 | + "device_class": "power", |
| 210 | + } |
| 211 | + entity = build_sensor_entity(sensor_dict, DummyCoordinator(), use_wallbox=False) |
| 212 | + assert isinstance(entity, EnpalBaseSensor) |
| 213 | + assert not isinstance(entity, EnpalWallboxPowerSensor) |
| 214 | + |
| 215 | + |
0 commit comments