Timer
timer
timer domain implementation.
Beyond the per-entity actions, the timer domain exposes a
collection-level create operation on the DomainAccessor:
timer = await ha.timer.create(name="cooldown", duration="00:01:00")
The accessor also subscribes to the timer.finished and
timer.cancelled HA events so that Timer.on_finished and
Timer.on_cancelled listeners fire correctly.
SPEC
module-attribute
SPEC: DomainSpec[Timer] = register_domain(DomainSpec(name='timer', entity_cls=Timer, event_subscriptions=('timer.finished', 'timer.cancelled'), on_event=_on_timer_event, accessor_cls=TimerAccessor))
The DomainSpec registered with the shared DomainRegistry.
Timer
Bases: Entity
A Home Assistant timer entity.
Timer states: idle, active, paused.
Actions use intent-specific names: start, pause, cancel,
finish, change.
Obtain a proxy to an existing timer via ha.timer("name"). To
create a library-managed helper, use await ha.timer.create(...).
Ephemeral timers (the default) are deleted automatically when they
return to idle. Pass persistent=True to keep the helper alive.
Timers obtained via the accessor are never auto-deleted, regardless
of how they were originally created in HA.
Notes
The time_remaining property computes live seconds remaining from
finishes_at when active, or parses remaining when paused.
Source code in src/haclient/domains/timer.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
persistent
property
persistent: bool
Whether this timer keeps its HA helper after returning to idle.
Returns:
| Type | Description |
|---|---|
bool
|
|
is_active
property
is_active: bool
Whether the timer is currently running.
is_paused
property
is_paused: bool
Whether the timer is paused.
is_idle
property
is_idle: bool
Whether the timer is idle.
duration
property
duration: str | None
Configured duration as a raw HA duration string.
Returns:
| Type | Description |
|---|---|
str or None
|
The HA |
remaining
property
remaining: str | None
Time remaining as a raw HA duration string.
Returns:
| Type | Description |
|---|---|
str or None
|
The HA |
finishes_at
property
finishes_at: str | None
ISO-8601 datetime when the timer will finish.
Returns:
| Type | Description |
|---|---|
str or None
|
The raw HA |
time_remaining
property
time_remaining: float | None
Live seconds remaining on the timer.
Returns:
| Type | Description |
|---|---|
float or None
|
Computed live on every access:
|
Notes
The value is not cached; each call may produce a slightly different result for active timers. No I/O is performed.
__init__
__init__(entity_id: str, services: ServiceCaller, store: StateStore, clock: Clock) -> None
Initialise the timer and its event-driven listener lists.
Beyond the base Entity setup, this also primes the persistence
flags used by the auto-cleanup logic in _handle_state_changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
Fully-qualified entity id (e.g. |
required |
services
|
ServiceCaller
|
Service-call port used to invoke HA services. |
required |
store
|
StateStore
|
State store the entity registers itself with. |
required |
clock
|
Clock
|
Scheduler used to dispatch async listeners. |
required |
Source code in src/haclient/domains/timer.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
delete
async
delete() -> None
Delete the timer helper from Home Assistant.
Sends timer/delete. After deletion the entity's _ensured
flag is reset so a subsequent action will re-create the helper.
Source code in src/haclient/domains/timer.py
238 239 240 241 242 243 244 245 246 247 | |
start
async
start(*, duration: str | None = None) -> None
Start (or restart) the timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duration
|
str or None
|
Optional override duration in HA format (e.g. |
None
|
Source code in src/haclient/domains/timer.py
251 252 253 254 255 256 257 258 259 260 261 | |
pause
async
pause() -> None
Pause the timer.
Invokes the timer.pause Home Assistant service.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call (for example, the timer is not currently active). |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/timer.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
cancel
async
cancel() -> None
Cancel the timer, returning it to idle.
Invokes the timer.cancel Home Assistant service.
Notes
Returning to idle from a non-idle state triggers the standard
timer state-change listeners. For ephemeral timers created via
TimerAccessor.create with persistent=False (the default),
this transition also schedules an auto-cleanup that deletes the
HA helper, after which the entity's state is reset to
"unknown" and the proxy can be re-ensured by the next
action.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call. |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/timer.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
finish
async
finish() -> None
Finish the timer immediately.
Invokes the timer.finish Home Assistant service. Behaves as
if the configured duration had elapsed: a timer.finished
event fires and the timer returns to idle.
Notes
For ephemeral timers (see cancel) the transition to idle
also schedules auto-cleanup of the HA helper.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call. |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/timer.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
change
async
change(*, duration: str) -> None
Add or subtract time from a running timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duration
|
str
|
Signed HA duration string (e.g. |
required |
Source code in src/haclient/domains/timer.py
335 336 337 338 339 340 341 342 343 344 | |
on_start
on_start(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for when the timer starts (becomes active).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Sync or async callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/timer.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
on_pause
on_pause(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for when the timer is paused.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Sync or async callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/timer.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
on_idle
on_idle(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for when the timer becomes idle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Sync or async callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/timer.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |
on_finished
on_finished(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for natural timer expiry.
Driven by the HA timer.finished event (not state changes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Sync or async callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/timer.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
on_cancelled
on_cancelled(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for explicit timer cancellation.
Driven by the HA timer.cancelled event (not state changes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/timer.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | |
TimerAccessor
Bases: DomainAccessor[Timer]
Typed domain accessor for the timer domain.
Returned by ha.timer. Provides a statically-typed
:meth:create method in addition to the standard entity lookup
methods inherited from DomainAccessor.
Source code in src/haclient/domains/timer.py
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
create
async
create(*, name: str | None = None, duration: str = '00:01:00', persistent: bool = False) -> Timer
Create a library-managed timer helper in Home Assistant.
Sends a timer/create WebSocket command and returns a Timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str or None
|
Short object-id; auto-generated when omitted (only allowed for ephemeral timers). |
None
|
duration
|
str
|
Initial duration for the helper in HA format (e.g.
|
'00:01:00'
|
persistent
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Timer
|
The newly created (or already-ensured) timer entity. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/haclient/domains/timer.py
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |