|
| 1 | +"""``timer`` domain implementation.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from ..entity import Entity |
| 8 | + |
| 9 | + |
| 10 | +class Timer(Entity): |
| 11 | + """A Home Assistant timer entity. |
| 12 | +
|
| 13 | + Timer states: ``idle``, ``active``, ``paused``. |
| 14 | + """ |
| 15 | + |
| 16 | + domain = "timer" |
| 17 | + |
| 18 | + # -- State properties -- |
| 19 | + |
| 20 | + @property |
| 21 | + def is_active(self) -> bool: |
| 22 | + """``True`` if the timer is currently running.""" |
| 23 | + return self.state == "active" |
| 24 | + |
| 25 | + @property |
| 26 | + def is_paused(self) -> bool: |
| 27 | + """``True`` if the timer is paused.""" |
| 28 | + return self.state == "paused" |
| 29 | + |
| 30 | + @property |
| 31 | + def is_idle(self) -> bool: |
| 32 | + """``True`` if the timer is idle (not started or finished).""" |
| 33 | + return self.state == "idle" |
| 34 | + |
| 35 | + @property |
| 36 | + def duration(self) -> str | None: |
| 37 | + """Configured duration (e.g. ``"0:05:00"``).""" |
| 38 | + val = self.attributes.get("duration") |
| 39 | + return str(val) if val is not None else None |
| 40 | + |
| 41 | + @property |
| 42 | + def remaining(self) -> str | None: |
| 43 | + """Time remaining (e.g. ``"0:04:30"``).""" |
| 44 | + val = self.attributes.get("remaining") |
| 45 | + return str(val) if val is not None else None |
| 46 | + |
| 47 | + @property |
| 48 | + def finishes_at(self) -> str | None: |
| 49 | + """ISO-8601 datetime when the timer will finish, if active.""" |
| 50 | + val = self.attributes.get("finishes_at") |
| 51 | + return str(val) if val is not None else None |
| 52 | + |
| 53 | + # -- Actions -- |
| 54 | + |
| 55 | + async def start(self, *, duration: str | None = None) -> None: |
| 56 | + """Start (or restart) the timer. |
| 57 | +
|
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + duration : str or None, optional |
| 61 | + Override duration (e.g. ``"00:05:00"``). |
| 62 | + """ |
| 63 | + data: dict[str, Any] | None = {"duration": duration} if duration else None |
| 64 | + await self.call_service("start", data) |
| 65 | + |
| 66 | + async def pause(self) -> None: |
| 67 | + """Pause the timer.""" |
| 68 | + await self.call_service("pause") |
| 69 | + |
| 70 | + async def cancel(self) -> None: |
| 71 | + """Cancel the timer (returns to idle).""" |
| 72 | + await self.call_service("cancel") |
| 73 | + |
| 74 | + async def finish(self) -> None: |
| 75 | + """Finish the timer immediately.""" |
| 76 | + await self.call_service("finish") |
| 77 | + |
| 78 | + async def change(self, *, duration: str) -> None: |
| 79 | + """Add or subtract time from a running timer. |
| 80 | +
|
| 81 | + Parameters |
| 82 | + ---------- |
| 83 | + duration : str |
| 84 | + Duration to add/subtract (e.g. ``"00:01:00"`` or ``"-00:00:30"``). |
| 85 | + """ |
| 86 | + await self.call_service("change", {"duration": duration}) |
| 87 | + |
| 88 | + # -- Listener decorators -- |
| 89 | + |
| 90 | + def on_start(self, func: Any) -> Any: |
| 91 | + """Register a listener for when the timer starts (becomes active). |
| 92 | +
|
| 93 | + Callback: ``(old_state, new_state)``. |
| 94 | + """ |
| 95 | + return self._register_state_transition_listener("active", func) |
| 96 | + |
| 97 | + def on_pause(self, func: Any) -> Any: |
| 98 | + """Register a listener for when the timer is paused. |
| 99 | +
|
| 100 | + Callback: ``(old_state, new_state)``. |
| 101 | + """ |
| 102 | + return self._register_state_transition_listener("paused", func) |
| 103 | + |
| 104 | + def on_idle(self, func: Any) -> Any: |
| 105 | + """Register a listener for when the timer becomes idle (finished or cancelled). |
| 106 | +
|
| 107 | + Callback: ``(old_state, new_state)``. |
| 108 | + """ |
| 109 | + return self._register_state_transition_listener("idle", func) |
0 commit comments