Skip to content

fix(slots): handle cross-midnight demand windows (#896)#896

Open
jackmcintyre wants to merge 1 commit into
mainfrom
issue/896
Open

fix(slots): handle cross-midnight demand windows (#896)#896
jackmcintyre wants to merge 1 commit into
mainfrom
issue/896

Conversation

@jackmcintyre

Copy link
Copy Markdown
Owner

Summary

The DW membership test dw_start_time <= slot_time < dw_end_time is false for every slot of the day when end <= start (e.g. 22:0006:00 overnight peak). The feature silently disabled itself — no slot flagged, no DW entry detected, no pre-charge — with no error or warning.

_parse_time_option (slots.py:473) does no cross-midnight handling, and there's no validation in config_flow. The default 15:00–21:00 doesn't cross midnight, so this is latent for most users but bites anyone on an overnight-peak tariff.

Changes

engine/slots.py:_process_single_slot detects the cross-midnight case (end <= start) and applies the wrap:

if dw_end_time <= dw_start_time:
    in_demand_window = slot_time >= dw_start_time or slot_time < dw_end_time
else:
    in_demand_window = dw_start_time <= slot_time < dw_end_time

Validation

  • TDD: 5 new tests in TestDemandWindowCrossMidnight — slots at 23:00 (in), 02:00 (in), 12:00 (out), 22:00 (entry boundary), 06:00 (exit, exclusive). 3 failed red (23:00, 02:00, 22:00 all wrongly False), all green after fix.
  • Full suite: 3219 passed, 1 xfailed. Coverage: 95%.
  • uv run ruff check clean on slots.py (pre-existing import-sort warnings in test file are unrelated).

Out of scope

Config-flow validation (warning the user when end <= start) is intentionally not added — overnight windows are legitimate, so the fix is to handle them correctly rather than block them.

The DW membership test `dw_start_time <= slot_time < dw_end_time` is false for
every slot of the day when end <= start (e.g. 22:00 -> 06:00 overnight peak).
The feature silently disabled itself — no slot flagged, no DW entry detected,
no pre-charge — with no error or warning.

Now detects the cross-midnight case (end <= start) and applies the wrap:
a slot is in the DW if its time is >= start OR < end.

TDD: 5 new tests in TestDemandWindowCrossMidnight — slots at 23:00, 02:00,
12:00 (outside), 22:00 (entry boundary), 06:00 (exit, exclusive). 3 failed
red (23:00, 02:00, 22:00 all wrongly False), all green after fix.
3219 pass, coverage 95%.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant