State Proxy
StateProxy
Bases: Resource
In-memory cache of Home Assistant entity state, kept in sync via the WebSocket connection.
StateProxy is both a read-through cache (get_state, get_domain_states,
yield_domain_states, etc.) and the coordinator that keeps that cache synchronized with Home
Assistant across connects, disconnects, and reconnects. Synchronization is
connection-generation-aware: each WebSocket reconnect gets a new generation from
WebsocketService, and any state-changed event or synchronization result tagged with a
stale generation is rejected instead of corrupting the cache with data from a superseded
connection.
A full synchronization pass (_run_synchronization) fetches every entity's current state
via Api.get_states_raw() and replays a per-generation journal of state-changed events
observed while that fetch was in flight (JournalOperation / ActiveSynchronization),
so no update racing the bulk fetch is lost. On failure it retries with exponential backoff
(schedule_retry / compute_retry_delay in state_proxy_sync) unless the poll job
is installed, in which case the next scheduled poll (load_cache) is left to converge instead.
Its own Resource readiness means only "the synchronization coordinator is wired" — not
that Home Assistant data is usable yet. has_initial_state_capability() /
wait_initial_state_capability() is a separate, one-way latch that
AppBootstrapCoordinator waits on before releasing app bootstrap: it opens only once the
first full synchronization for a connected generation has committed, and never closes again
on a later disconnect (see StateCacheFreshness — a later loss of connection marks the
cache STALE rather than UNAVAILABLE, preserving stale reads for already-running apps).
Source code in src/hassette/core/state_proxy.py
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 444 445 446 447 448 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 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
config_log_level: LogLevel
property
Configured log level for the state proxy (hassette.config.logging.state_proxy).
synchronization_status: StateSynchronizationStatus
property
The kind of state synchronization work, if any, currently in flight.
cache_freshness: StateCacheFreshness
property
Freshness of the published state cache. See StateCacheFreshness.
has_cache_entries: bool
property
True if the state cache holds at least one entity.
maintained_generation: int | None
property
The connection generation the current cache contents were last synchronized against.
None if no synchronization has committed yet.
__init__(hassette: Hassette, *, parent: Resource | None = None) -> None
Initialize the state cache and synchronization bookkeeping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hassette
|
Hassette
|
The owning |
required |
parent
|
Resource | None
|
The parent resource, if any. |
None
|
Source code in src/hassette/core/state_proxy.py
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 | |
has_initial_state_capability() -> bool
Return whether the initial-state capability latch has opened.
True once the first full synchronization for a connected generation has committed at
least once. Never reverts to False afterward, even across later disconnects — see the
class docstring for how this differs from cache_freshness.
Source code in src/hassette/core/state_proxy.py
135 136 137 138 139 140 141 142 | |
wait_initial_state_capability(*, timeout: float | None = None) -> bool
async
Wait for the initial-state capability latch to open.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Maximum time to wait, in seconds. None waits indefinitely. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the capability is already, or becomes, available before the timeout; False |
bool
|
if the wait timed out. |
Source code in src/hassette/core/state_proxy.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
on_initialize() -> None
async
Wire the synchronization coordinator and start the background bootstrap sync.
ApiResource, BusService, and SchedulerService are guaranteed ready by
depends_on auto-wait. Subscribes to state-changed events and websocket connect/
disconnect observers, installs the periodic poll job, then marks this resource ready
and spawns the initial synchronization in the background.
Resource readiness here means only "wired," not that Home Assistant data is available
yet — see has_initial_state_capability.
Source code in src/hassette/core/state_proxy.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
subscribe_to_events() -> None
async
Subscribe to state-changed events and websocket connect/disconnect observers.
Idempotent: a no-op if a state-changed subscription is already active.
Source code in src/hassette/core/state_proxy.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
on_shutdown() -> None
async
Cancel all in-flight synchronization and retry work and reset the cache to empty.
Cancels the bootstrap task, any pending retry task, and any active synchronization task; resets synchronization bookkeeping (freshness, maintained generation, the initial-state capability latch); clears the state cache; and marks the resource not-ready.
Source code in src/hassette/core/state_proxy.py
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 | |
get_state(entity_id: str) -> HassStateDict | None
Get the current cached state for an entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
The entity ID to look up (e.g., "light.kitchen"). |
required |
Returns:
| Type | Description |
|---|---|
HassStateDict | None
|
The raw state dict if found, None otherwise. |
Raises:
| Type | Description |
|---|---|
ResourceNotReadyError
|
If the cache freshness is UNAVAILABLE (no synchronization has ever committed). Once the cache has been synchronized at least once, a later disconnect marks it STALE rather than UNAVAILABLE, so stale reads succeed instead of raising. |
Source code in src/hassette/core/state_proxy.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
get_state_once(entity_id: str) -> HassStateDict | None
Get the current cached state for an entity.
Equivalent to get_state; kept as a distinct name for call sites that want to be
explicit that a single readiness check is all that happens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
The entity ID to look up (e.g., "light.kitchen"). |
required |
Returns:
| Type | Description |
|---|---|
HassStateDict | None
|
The raw state dict if found, None otherwise. |
Raises:
| Type | Description |
|---|---|
ResourceNotReadyError
|
If the cache freshness is UNAVAILABLE. |
Source code in src/hassette/core/state_proxy.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
get_domain_states(domain: str) -> dict[str, HassStateDict]
Get all cached states for a specific domain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
str
|
The domain to filter by (e.g., "light"). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, HassStateDict]
|
A dictionary of entity_id to state for the specified domain. |
Raises:
| Type | Description |
|---|---|
ResourceNotReadyError
|
If the cache freshness is UNAVAILABLE (see |
Source code in src/hassette/core/state_proxy.py
286 287 288 289 290 291 292 293 294 295 296 297 298 | |
yield_domain_states(domain: str) -> Generator[tuple[str, HassStateDict], Any, None]
Yield all cached states for a specific domain.
This method is deliberately NOT a generator function itself: the readiness check runs
eagerly at call time, and iteration is delegated to a nested generator. Collapsing it
into a single generator function would defer the check to the first next(), so a
caller that builds the generator without iterating it would silently read an unavailable
cache instead of raising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
str
|
The domain to filter by (e.g., "light"). |
required |
Yields:
| Type | Description |
|---|---|
tuple[str, HassStateDict]
|
Tuples of (entity_id, state) for the specified domain. |
Raises:
| Type | Description |
|---|---|
ResourceNotReadyError
|
If the cache freshness is UNAVAILABLE (see |
Source code in src/hassette/core/state_proxy.py
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 | |
on_state_change(event: RawStateChangeEvent) -> None
async
Apply a state_changed event to the cache.
Rejects events whose websocket generation does not match the current (or, while disconnected, last-maintained) generation, so a stale event from a superseded connection cannot corrupt the cache. Accepted removals and updates are also appended to the in-flight synchronization journal, if one is active, so a concurrent full synchronization can replay them over its baseline fetch.
Source code in src/hassette/core/state_proxy.py
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 | |
on_disconnect() -> None
async
Handle websocket disconnection.
Cancels any pending retry task and detaches the active synchronization task (cancelling it if still running), then downgrades the cache freshness: to STALE if the initial-state capability has already been reached (so already-running apps keep reading stale data), or to UNAVAILABLE otherwise.
Source code in src/hassette/core/state_proxy.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
on_reconnect() -> None
async
Handle websocket reconnection by requesting a synchronization for the new generation.
A no-op if the websocket has no current connected generation (e.g. a subsequent disconnect already invalidated it by the time this handler runs).
Source code in src/hassette/core/state_proxy.py
416 417 418 419 420 421 422 423 424 425 | |
load_cache() -> None
async
Refresh the state cache via a poll-triggered synchronization.
Called periodically by the poll job to keep the cache fresh between reconnects. A no-op if there is no current connected generation.
Source code in src/hassette/core/state_proxy.py
433 434 435 436 437 438 439 440 441 442 443 | |
wait_for_sync_task(task: asyncio.Task[None]) -> bool
async
Wait for a shared synchronization task without coupling to its cancellation.
The sync task is shared by every requester (poll job, connected observers, retries), and
on_disconnect cancels it. Awaiting it directly would re-raise that CancelledError in
each requester, making a disconnect look like the requester itself was cancelled. Cancelling
the requester likewise leaves the shared task running.
Returns:
| Type | Description |
|---|---|
bool
|
False if the task was cancelled, True if it completed. Any other exception the task |
bool
|
raised is re-raised. |
Source code in src/hassette/core/state_proxy.py
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |