Bus Service
BusService
Bases: Service
EventBus service that handles event dispatching and listener management.
Source code in src/hassette/core/bus_service.py
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 | |
stream: MemoryObjectReceiveStream[Event[Any]] = stream
instance-attribute
Stream to receive events from.
router: Router = Router()
instance-attribute
Router to manage event listeners.
config_log_all_events: bool
property
Return whether to log all events.
Live read (not cached) so config hot-reload is honored at dispatch time, the same
way the sibling config_log_level property re-reads config on every access. Do
not change this back to @cached_property — it would freeze the value after the
first dispatch and silently ignore hot-reload.
duration_timers_active: int
property
Number of currently active duration timers.
on_dispatch_done(_task: asyncio.Task[Any]) -> None
Callback for dispatch task completion — decrements pending counter.
Source code in src/hassette/core/bus_service.py
137 138 139 140 141 142 143 144 | |
decrement_dispatch_pending() -> None
Decrement the pending-dispatch counter and signal idle when it reaches zero.
Source code in src/hassette/core/bus_service.py
146 147 148 149 150 | |
release_dispatch_slot(_task: asyncio.Task[Any]) -> None
Release one dispatch slot when a fan-out task finishes (success, error, or cancel).
Attached only to fan-out tasks, which each acquire exactly one slot. The immediate-fire
path in add_listener spawns without acquiring, so it must not get this callback.
Source code in src/hassette/core/bus_service.py
152 153 154 155 156 157 158 | |
warn_dispatch_saturated() -> None
Log a rate-limited warning when dispatch is at its concurrency ceiling.
Source code in src/hassette/core/bus_service.py
160 161 162 163 164 165 166 167 168 169 170 171 172 | |
add_listener(listener: Listener) -> int
async
Add a listener to the bus.
Route insertion is synchronous. DB registration is awaited inline before
returning — the listener's db_id is set and valid on return. For duration
listeners, wires the timer and delegates cancel listener creation to
DurationHoldManager. Immediate-fire tasks are tracked in
_dispatch_pending so await_dispatch_idle drains them.
Returns:
| Type | Description |
|---|---|
int
|
The db_id assigned to the listener by the database. |
Source code in src/hassette/core/bus_service.py
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 | |
build_registration(listener: Listener) -> ListenerRegistration
Build a ListenerRegistration struct from listener identity and options.
Source code in src/hassette/core/bus_service.py
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 | |
live_execution_counts() -> dict[int, LiveCounts]
Return a snapshot of live execution counts keyed by listener db_id.
Reads each active listener's ExecutionModeGuard and HandlerInvoker from the
router. Live and in-memory only — no DB access. Listeners not yet assigned a db_id
are skipped; the web layer treats a missing entry as LiveCounts(0, 0, 0). The
counters reset on guard restart and are never persisted.
Returns:
| Type | Description |
|---|---|
dict[int, LiveCounts]
|
A dict mapping listener |
Source code in src/hassette/core/bus_service.py
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 | |
mark_listener_cancelled(db_id: int) -> None
async
Persist durable cancellation state for a listener by setting cancelled_at in the DB.
Delegates to CommandExecutor.mark_listener_cancelled. Called from the bus cancel
path (Subscription.cancel → Bus.remove_listener, replace's cancel-old
step, or a once-listener firing) so that a replaced or cancelled listener's removal
is observable in telemetry, mirroring SchedulerService.mark_job_cancelled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_id
|
int
|
The |
required |
Source code in src/hassette/core/bus_service.py
277 278 279 280 281 282 283 284 285 286 287 288 | |
register_removal_callback(owner_id: str, callback: Callable[[Listener], None]) -> None
Register a callback invoked whenever a listener belonging to owner_id is removed.
If a callback is already registered for owner_id, the new one silently replaces it. This handles hot-reload cycles where the old Bus is orphaned without a formal shutdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_id
|
str
|
The owner whose listener removals should trigger the callback. |
required |
callback
|
Callable[[Listener], None]
|
Called with the removed Listener as its single argument. |
required |
Source code in src/hassette/core/bus_service.py
290 291 292 293 294 295 296 297 298 299 300 | |
deregister_removal_callback(owner_id: str) -> None
Remove the removal callback for owner_id, if any.
No-op when owner_id has no registered callback. Called by Bus.on_shutdown so the slot is freed before the Bus is re-initialized (e.g. during a hot-reload cycle).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_id
|
str
|
The owner whose callback should be removed. |
required |
Source code in src/hassette/core/bus_service.py
302 303 304 305 306 307 308 309 310 311 | |
remove_listener(listener: Listener) -> None
Synchronously cancel and remove a listener from the routing table.
Fires the per-owner removal callback (if registered) after removal. This closes the once-fire gap: the dispatch finally block calls this method directly, bypassing Bus.remove_listener, so the callback is the only way to notify the Bus that the once-listener's natural key should be released from _registered_listeners.
Source code in src/hassette/core/bus_service.py
313 314 315 316 317 318 319 320 321 322 323 | |
remove_listeners_by_owner(owner: str) -> None
Remove all listeners owned by a specific owner synchronously.
Fires the per-owner removal callback for each removed listener, mirroring SchedulerService._remove_jobs_by_owner. On the shutdown path Bus.remove_all_listeners pre-clears _registered_listeners first, so the callbacks find nothing to pop and skip the cancelled_at spawn; firing them here keeps this method correct for any future caller that removes listeners without that pre-clear.
Source code in src/hassette/core/bus_service.py
325 326 327 328 329 330 331 332 333 334 335 336 337 | |
fire_removal_callback(listener: Listener) -> None
Invoke the per-owner removal callback for listener, if one is registered.
Singular (one listener per call) by design: the bus fires on each removal as it happens, where SchedulerService.fire_removal_callbacks batches a list at the end of a bulk operation.
Source code in src/hassette/core/bus_service.py
339 340 341 342 343 344 345 346 347 348 | |
get_listeners_by_owner(owner: str) -> list[Listener]
Get all listeners owned by a specific owner.
Source code in src/hassette/core/bus_service.py
350 351 352 | |
should_log_event(event: Event[Any]) -> bool
Determine if an event should be logged based on its type.
Source code in src/hassette/core/bus_service.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
dispatch(base_topic: str, event: Event[Any]) -> None
async
Dispatch an event to all matching listeners for the given topic.
Source code in src/hassette/core/bus_service.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
read_entity_state(entity_id: str) -> HassStateDict | None
Read entity state from StateProxy; returns None on any error.
Absorbs ResourceNotReadyError and unexpected exceptions so the
caller (DurationHoldManager) never needs to handle state-read failures.
Source code in src/hassette/core/bus_service.py
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 | |
await_dispatch_idle(*, timeout: float = _DISPATCH_IDLE_DEFAULT_TIMEOUT) -> None
async
Wait until all dispatched handler tasks have completed.
Polls with a stability check to handle in-transit events from the anyio memory channel pipeline. Duration timer callbacks are NOT tracked here.
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If dispatch tasks are still running after |
Source code in src/hassette/core/bus_service.py
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 | |
serve() -> None
async
Worker loop that processes events from the stream.
Source code in src/hassette/core/bus_service.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
make_synthetic_state_event(entity_id: str, current_state: HassStateDict) -> RawStateChangeEvent
Build a synthetic RawStateChangeEvent with old_state=None.
Injected into the bus kernel so it stays free of HA event type imports.
Source code in src/hassette/core/bus_service.py
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 | |
compute_elapsed(current_state: HassStateDict, duration_config: DurationConfig) -> float
Compute how long an entity has been in its current state.
Reads the HA-specific last_changed field from the state dict. Injected
into the bus kernel so it stays free of HA event type imports.
For attribute listeners, returns 0.0 (elapsed time is not tracked the same way). Returns a value clamped to [0.0, duration_config.duration].
Source code in src/hassette/core/bus_service.py
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 | |
strip_old_state(event: Event[Any]) -> Event[Any]
Strip old_state from a RawStateChangeEvent for cancel-predicate evaluation.
For RawStateChangeEvent with a non-None old_state, returns a copy with
old_state=None so that transition predicates (StateFrom, StateDidChange)
do not falsely cancel a duration timer. For all other events, returns the event
unchanged.
Source code in src/hassette/core/bus_service.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 | |