From 0209638138a4c8344394d7e20544896438372d05 Mon Sep 17 00:00:00 2001 From: MonsterD1 Date: Fri, 30 Oct 2026 19:54:35 +0100 Subject: [PATCH] Keep on-only, paused and overridden appliances on when the master switch is disabled Disabling the master switch turned off every managed appliance that was on, including on-only appliances the integration never switches off and appliances under manual control (Pause or Override). A running dryer or laptop was cut off. _turn_off_all_managed() now skips on-only, paused and overridden appliances and logs that they were left on. Other appliances are still turned off as before. Fixes #71 Co-Authored-By: Claude Opus 5.5 --- .../pv_excess_control/coordinator.py | 17 ++++ tests/test_init.py | 88 +++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/custom_components/pv_excess_control/coordinator.py b/custom_components/pv_excess_control/coordinator.py index 3e135647..4666588b 100644 --- a/custom_components/pv_excess_control/coordinator.py +++ b/custom_components/pv_excess_control/coordinator.py @@ -2367,6 +2367,9 @@ async def _turn_off_all_managed(self) -> None: """Turn off all currently-ON managed appliances (M11). Called when the master switch transitions from enabled to disabled. + On-only, paused and overridden appliances are left running (#71): + the integration never turns on-only appliances off, and paused or + overridden ones are under manual control. """ subentries = getattr(self.config_entry, "subentries", {}) for subentry_id, subentry in subentries.items(): @@ -2380,6 +2383,20 @@ async def _turn_off_all_managed(self) -> None: continue domain = entity_id.split(".")[0] if "." in entity_id else "switch" name = subentry.data.get(CONF_APPLIANCE_NAME, subentry_id) + if subentry.data.get(CONF_ON_ONLY, False): + keep_reason = "on-only" + elif getattr(self, "appliance_paused", {}).get(subentry_id, False): + keep_reason = "paused" + elif self.appliance_overrides.get(subentry_id, False): + keep_reason = "override active" + else: + keep_reason = None + if keep_reason: + _LOGGER.info( + "Master switch disabled: left %s (%s) on (%s)", + name, entity_id, keep_reason, + ) + continue try: async with asyncio.timeout(10): await self.hass.services.async_call( diff --git a/tests/test_init.py b/tests/test_init.py index c180486d..7692308f 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -2299,3 +2299,91 @@ def test_stale_previous_is_on_cleaned_on_subentry_removal(self): # Stale cleanup should have removed it assert "sub_1" not in coord._previous_is_on + + +# --------------------------------------------------------------------------- +# Tests: master switch disable keeps manual/on-only appliances running (#71) +# --------------------------------------------------------------------------- + + +class TestMasterDisableTurnOff: + """_turn_off_all_managed() on the master switch enabled -> disabled edge.""" + + @staticmethod + def _sub(entity_id: str, on_only: bool = False) -> MagicMock: + from custom_components.pv_excess_control.const import ( + CONF_APPLIANCE_ENTITY, + CONF_APPLIANCE_NAME, + CONF_ON_ONLY, + ) + + sub = MagicMock() + sub.data = { + CONF_APPLIANCE_NAME: entity_id, + CONF_APPLIANCE_ENTITY: entity_id, + CONF_ON_ONLY: on_only, + } + return sub + + def _coord(self, subs: dict[str, MagicMock]): + from custom_components.pv_excess_control.const import CONF_APPLIANCE_ENTITY + + states = { + sub.data[CONF_APPLIANCE_ENTITY]: MockState("on") for sub in subs.values() + } + coord = _make_coordinator( + states=states, entry=_make_config_entry(subentries=subs), + ) + coord.appliance_paused = {} + return coord + + @staticmethod + def _turned_off(coord) -> set[str]: + return { + data["entity_id"] + for _domain, service, data in coord.hass.services.calls + if service == "turn_off" + } + + @pytest.mark.asyncio + async def test_regular_appliance_is_turned_off(self): + coord = self._coord({"sub_1": self._sub("switch.heater")}) + + await coord._turn_off_all_managed() + + assert self._turned_off(coord) == {"switch.heater"} + + @pytest.mark.asyncio + async def test_on_only_appliance_is_left_on(self): + coord = self._coord({ + "sub_1": self._sub("switch.laptop", on_only=True), + "sub_2": self._sub("switch.heater"), + }) + + await coord._turn_off_all_managed() + + assert self._turned_off(coord) == {"switch.heater"} + + @pytest.mark.asyncio + async def test_paused_appliance_is_left_on(self): + coord = self._coord({ + "sub_1": self._sub("switch.dryer"), + "sub_2": self._sub("switch.heater"), + }) + coord.appliance_paused = {"sub_1": True} + + await coord._turn_off_all_managed() + + assert self._turned_off(coord) == {"switch.heater"} + + @pytest.mark.asyncio + async def test_overridden_appliance_is_left_on(self): + coord = self._coord({ + "sub_1": self._sub("switch.pump"), + "sub_2": self._sub("switch.heater"), + }) + coord.appliance_overrides = {"sub_1": True} + + await coord._turn_off_all_managed() + + assert self._turned_off(coord) == {"switch.heater"}