Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions custom_components/pv_excess_control/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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(
Expand Down
88 changes: 88 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}