Lightwell / prototype / lightwell.py
| 1 | """Development-only local automation simulator. Not a Matter implementation.""" |
| 2 | from dataclasses import dataclass |
| 3 | from datetime import datetime, time |
| 4 | from typing import Mapping, Protocol |
| 5 | |
| 6 | @dataclass(frozen=True) |
| 7 | class LightState: |
| 8 | on: bool |
| 9 | level: int |
| 10 | rgb: tuple[int,int,int] |
| 11 | |
| 12 | def __post_init__(self): |
| 13 | if type(self.on) is not bool or type(self.level) is not int or not 0 <= self.level <= 100: |
| 14 | raise ValueError("Expected on/off and a brightness from 0 to 100.") |
| 15 | if not isinstance(self.rgb, tuple) or len(self.rgb) != 3 or any(type(c) is not int or not 0 <= c <= 255 for c in self.rgb): |
| 16 | raise ValueError("Expected three color components from 0 to 255.") |
| 17 | |
| 18 | class LightTransport(Protocol): |
| 19 | def set_state(self, device: str, state: LightState) -> None: ... |
| 20 | |
| 21 | class SimulatedLights: |
| 22 | def __init__(self, *devices: str): |
| 23 | if not devices or any(not isinstance(d, str) or not d for d in devices): |
| 24 | raise ValueError("Provide at least one simulation device ID.") |
| 25 | self.states = {device: LightState(False, 0, (0,0,0)) for device in devices} |
| 26 | self.offline_devices: set[str] = set() |
| 27 | |
| 28 | def set_state(self, device, state): |
| 29 | if device in self.offline_devices: |
| 30 | raise ConnectionError("simulated device unreachable") |
| 31 | self.states[device] = state |
| 32 | |
| 33 | @dataclass(frozen=True) |
| 34 | class Schedule: |
| 35 | name: str |
| 36 | at: time |
| 37 | scene: str |
| 38 | |
| 39 | class Controller: |
| 40 | def __init__(self, transport: LightTransport, devices: set[str]): |
| 41 | self.transport = transport |
| 42 | self.devices = frozenset(devices) |
| 43 | self.scenes: dict[str, dict[str, LightState]] = {} |
| 44 | self.rules: dict[str, Schedule] = {} |
| 45 | self._ran: dict[str, object] = {} |
| 46 | |
| 47 | def add_scene(self, name: str, desired: Mapping[str, LightState]): |
| 48 | if not name or not desired or not set(desired) <= self.devices or not all(isinstance(s, LightState) for s in desired.values()): |
| 49 | raise ValueError("Scene needs a name and valid states for known devices.") |
| 50 | self.scenes[name] = dict(desired) |
| 51 | |
| 52 | def apply(self, name: str) -> dict[str, str]: |
| 53 | results = {} |
| 54 | for device, desired in self.scenes[name].items(): |
| 55 | try: |
| 56 | self.transport.set_state(device, desired) |
| 57 | results[device] = "applied" |
| 58 | except (TimeoutError, ConnectionError): |
| 59 | results[device] = "unreachable" |
| 60 | return results |
| 61 | |
| 62 | def add_schedule(self, rule: Schedule): |
| 63 | if not rule.name or rule.name in self.rules or rule.scene not in self.scenes or rule.at.tzinfo is not None or rule.at.second or rule.at.microsecond: |
| 64 | raise ValueError("Schedule needs a unique name, existing scene, and local hour and minute.") |
| 65 | self.rules[rule.name] = rule |
| 66 | |
| 67 | def run_due(self, local_now: datetime) -> dict[str, dict[str, str]]: |
| 68 | if local_now.utcoffset() is None: |
| 69 | raise ValueError("Pass the observed local time with offset.") |
| 70 | result = {} |
| 71 | for rule in self.rules.values(): |
| 72 | if (rule.at.hour, rule.at.minute) != (local_now.hour, local_now.minute) or self._ran.get(rule.name) == local_now.date(): |
| 73 | continue |
| 74 | result[rule.name] = self.apply(rule.scene) |
| 75 | self._ran[rule.name] = local_now.date() |
| 76 | return result |