|
| 1 | +"""v2.13.1 — a redundant switch.turn_on rejection is not a start failure (issue #60). |
| 2 | +
|
| 3 | +Some charger integrations (Tesla Fleet) reject ``switch.turn_on`` when the |
| 4 | +vehicle is already charging ("Command was unsuccessful: is_charging"). Boost |
| 5 | +Charge enabled on top of an already-running session must not abort because of |
| 6 | +it: the desired outcome is already achieved. |
| 7 | +""" |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from types import SimpleNamespace |
| 11 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 12 | + |
| 13 | +import pytest |
| 14 | +from homeassistant.exceptions import HomeAssistantError |
| 15 | + |
| 16 | +from custom_components.ev_smart_charger.charger_controller import ChargerController |
| 17 | +from custom_components.ev_smart_charger.const import ( |
| 18 | + CONF_EV_CHARGER_CURRENT, |
| 19 | + CONF_EV_CHARGER_SWITCH, |
| 20 | +) |
| 21 | + |
| 22 | +CONFIG = { |
| 23 | + CONF_EV_CHARGER_SWITCH: "switch.charger", |
| 24 | + CONF_EV_CHARGER_CURRENT: "number.charger_current", |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def rejecting_services(hass): |
| 30 | + """Register a switch.turn_on that always rejects, like Tesla Fleet does.""" |
| 31 | + calls: list[tuple[str, str, dict]] = [] |
| 32 | + |
| 33 | + async def switch_turn_on(call): |
| 34 | + calls.append(("switch", "turn_on", dict(call.data))) |
| 35 | + raise HomeAssistantError("Command was unsuccessful: is_charging") |
| 36 | + |
| 37 | + async def switch_turn_off(call): |
| 38 | + calls.append(("switch", "turn_off", dict(call.data))) |
| 39 | + hass.states.async_set(call.data["entity_id"], "off") |
| 40 | + |
| 41 | + async def number_set_value(call): |
| 42 | + calls.append(("number", "set_value", dict(call.data))) |
| 43 | + hass.states.async_set(call.data["entity_id"], str(call.data["value"])) |
| 44 | + |
| 45 | + hass.services.async_register("switch", "turn_on", switch_turn_on) |
| 46 | + hass.services.async_register("switch", "turn_off", switch_turn_off) |
| 47 | + hass.services.async_register("number", "set_value", number_set_value) |
| 48 | + return calls |
| 49 | + |
| 50 | + |
| 51 | +def _runtime_with_power(watts: float | None): |
| 52 | + power_model = MagicMock() |
| 53 | + power_model.read_charging_power.return_value = watts |
| 54 | + power_model.is_charging.return_value = bool(watts and watts > 200) |
| 55 | + return SimpleNamespace(power_model=power_model, diagnostic_manager=None) |
| 56 | + |
| 57 | + |
| 58 | +async def test_rejected_start_while_switch_on_is_success( |
| 59 | + hass, rejecting_services |
| 60 | +): |
| 61 | + """Switch already ON + rejected turn_on → start succeeds, amperage applied.""" |
| 62 | + hass.states.async_set("switch.charger", "on") |
| 63 | + hass.states.async_set("number.charger_current", "6") |
| 64 | + controller = ChargerController(hass, "test_entry", CONFIG) |
| 65 | + await controller.async_setup() |
| 66 | + |
| 67 | + with patch("asyncio.sleep", new=AsyncMock()): |
| 68 | + result = await controller.start_charger(target_amps=16, reason="Boost charge") |
| 69 | + |
| 70 | + assert result.success is True |
| 71 | + assert result.amperage == 16 |
| 72 | + assert ( |
| 73 | + "number", |
| 74 | + "set_value", |
| 75 | + {"entity_id": "number.charger_current", "value": 16}, |
| 76 | + ) in rejecting_services |
| 77 | + assert ("switch", "turn_on", {"entity_id": "switch.charger"}) in rejecting_services |
| 78 | + |
| 79 | + |
| 80 | +async def test_rejected_start_with_measured_draw_is_success( |
| 81 | + hass, rejecting_services |
| 82 | +): |
| 83 | + """Stale OFF switch but the car measurably draws power → still a success.""" |
| 84 | + hass.states.async_set("switch.charger", "off") |
| 85 | + hass.states.async_set("number.charger_current", "16") |
| 86 | + controller = ChargerController( |
| 87 | + hass, "test_entry", CONFIG, runtime_data=_runtime_with_power(11224.0) |
| 88 | + ) |
| 89 | + await controller.async_setup() |
| 90 | + |
| 91 | + with patch("asyncio.sleep", new=AsyncMock()): |
| 92 | + result = await controller.start_charger(target_amps=16, reason="Boost charge") |
| 93 | + |
| 94 | + assert result.success is True |
| 95 | + |
| 96 | + |
| 97 | +async def test_rejected_start_while_really_off_is_failure( |
| 98 | + hass, rejecting_services |
| 99 | +): |
| 100 | + """Switch OFF and no power draw → the rejection is a genuine failure.""" |
| 101 | + hass.states.async_set("switch.charger", "off") |
| 102 | + hass.states.async_set("number.charger_current", "6") |
| 103 | + controller = ChargerController( |
| 104 | + hass, "test_entry", CONFIG, runtime_data=_runtime_with_power(0.0) |
| 105 | + ) |
| 106 | + await controller.async_setup() |
| 107 | + |
| 108 | + with patch("asyncio.sleep", new=AsyncMock()): |
| 109 | + result = await controller.start_charger(target_amps=16, reason="Boost charge") |
| 110 | + |
| 111 | + assert result.success is False |
| 112 | + assert "is_charging" in (result.error_message or "") |
| 113 | + |
| 114 | + |
| 115 | +async def test_power_below_floor_does_not_mask_failure(hass, rejecting_services): |
| 116 | + """Standby power below the drawing floor is not evidence of charging.""" |
| 117 | + hass.states.async_set("switch.charger", "off") |
| 118 | + hass.states.async_set("number.charger_current", "6") |
| 119 | + controller = ChargerController( |
| 120 | + hass, "test_entry", CONFIG, runtime_data=_runtime_with_power(40.0) |
| 121 | + ) |
| 122 | + await controller.async_setup() |
| 123 | + |
| 124 | + with patch("asyncio.sleep", new=AsyncMock()): |
| 125 | + result = await controller.start_charger(target_amps=16, reason="Boost charge") |
| 126 | + |
| 127 | + assert result.success is False |
| 128 | + |
| 129 | + |
| 130 | +async def test_rejected_turn_on_is_tolerated_in_recover_path( |
| 131 | + hass, rejecting_services |
| 132 | +): |
| 133 | + """recover_to_target from OFF uses the same tolerant turn_on.""" |
| 134 | + hass.states.async_set("switch.charger", "on") |
| 135 | + hass.states.async_set("number.charger_current", "6") |
| 136 | + controller = ChargerController(hass, "test_entry", CONFIG) |
| 137 | + await controller.async_setup() |
| 138 | + |
| 139 | + with patch("asyncio.sleep", new=AsyncMock()): |
| 140 | + await controller._start_charger_unlocked(16) |
| 141 | + |
| 142 | + assert hass.states.get("number.charger_current").state == "16" |
0 commit comments