-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Goal: Extend existing keys block_on_switch_states and block_off_switch_states to optionally include time-windowed rules while keeping all existing simple list/string usages fully backward compatible.
Scope:
Accept four canonical input shapes per key: a) Scalar string block_on_switch_states: "on" b) List of strings block_on_switch_states: - "on" - "sleep" c) Single mapping (states as string or list + window) block_on_switch_states: { states: "on", start: "22:30", end: "06:30" } block_on_switch_states: { states: ["on","sleep"], start: "22:30", end: "06:30" }
Explicitly reject “loose” time entries not attached to a mapping and mixed mappings : INVALID (must log warning & ignore): a) block_on_switch_states: - "on" - "sleep" - start: "22:30" - end: "06:30" b) List of mixed mappings / strings block_on_switch_states: - "open" - states: ["on","sleep"] start: "22:30" end: "06:30" - states: ["do_not_disturb"] start: "13:00" end: "14:00"
Normalization target (internal representation): Each rule becomes a dict: { "states": [, , ...], # non-empty list of strings "start": "HH:MM" | "sunrise [+/- HH:MM]"?, # optional "end": "HH:MM" | "sunset [+/- HH:MM]"?, # optional "window": False | True (derived convenience), # optional flag after parsing } Rules lacking start/end are always-active. If states provided as a single string → wrap in list. Merge multiple plain strings (when ONLY strings given) into one rule for performance; otherwise treat each string as its own always-active rule.
Validation rules:
If mapping lacks states/above/below (future-proof) → warn & skip.
If only one of start/end present → warn & skip time portion (treat as always-active)
If start == end → treat as always-active? (WARN and treat always-active).
If end < start (chronologically) → treat as cross-midnight window (end on next day).
Time parsing must support: HH:MM (24h) sunrise [+|- HH:MM] sunset [+|- HH:MM]
Case-insensitive state strings preserved as-is (do not lower unless existing code does so).
Execution semantics (block check): A block is active if ANY rule is active and the current entity state matches ANY state in that rule. Window rule active when current time ∈ window (cross-midnight logic handled). Combine OR across rules.
Logging:
On first detection of deprecated/invalid structure (e.g., loose start/end) log one DEBUG/INFO warning.
For each skipped invalid rule, log a concise warning with context.
Backward compatibility:
Existing simple forms (string or list of strings) behave exactly the same.
No config migration required.
No behavioral change unless user adds windowed mappings.
Documentation (README):
Update configuration section for block_on_switch_states / block_off_switch_states with new accepted forms, examples, cross-midnight note, sunrise/sunset offsets.
Provide a “windowed rule” example.