Skip to content

Commit

Permalink
Allow extra states in schedules.
Browse files Browse the repository at this point in the history
This allows the use of "day" and "night" states, thus improving code readability.
  • Loading branch information
denpamusic committed Oct 6, 2023
1 parent 7e9f152 commit 7137e30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pyplumio/helpers/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@
START_OF_DAY: Final = "00:00"
END_OF_DAY: Final = "00:00"

STATE_NIGHT: Final = STATE_OFF
STATE_DAY: Final = STATE_ON
STATE_NIGHT: Final = "night"
STATE_DAY: Final = "day"

ENABLED: Final[list[str]] = [
STATE_ON,
STATE_DAY,
]

DISABLED: Final[list[str]] = [
STATE_OFF,
STATE_NIGHT,
]


def _parse_interval(start: str, end: str) -> tuple[int, int]:
Expand Down Expand Up @@ -77,14 +87,17 @@ def append(self, item) -> None:

def set_state(
self,
state: Literal["off", "on"],
state: Literal["off", "on", "day", "night"],
start: str = START_OF_DAY,
end: str = END_OF_DAY,
) -> None:
"""Set state for interval."""
if state not in [*ENABLED, *DISABLED]:
raise ValueError(f'state "{state}" is not allowed')

index, stop_index = _parse_interval(start, end)
while index < stop_index:
self._intervals[index] = state == STATE_ON
self._intervals[index] = state in ENABLED
index += 1

def set_on(self, start: str = START_OF_DAY, end: str = END_OF_DAY) -> None:
Expand Down
4 changes: 4 additions & 0 deletions tests/helpers/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def test_schedule_day(schedule_day: ScheduleDay) -> None:
with pytest.raises(ValueError):
schedule_day.set_state(STATE_ON, start, end)

# Test with incorrect state.
with pytest.raises(ValueError):
schedule_day.set_state("invalid_state", "00:00", "01:00") # type: ignore [arg-type]

# set whole day schedule.
schedule_day.set_on()
assert schedule_day.intervals == [True for _ in range(48)]
Expand Down

0 comments on commit 7137e30

Please sign in to comment.