Skip to content

Bus Service

BusService

Bases: Service

EventBus service that handles event dispatching and listener management.

Source code in src/hassette/core/bus_service.py
 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
class BusService(Service):
    """EventBus service that handles event dispatching and listener management."""

    depends_on: ClassVar[list[type["Resource"]]] = [DatabaseService]
    restart_spec: ClassVar[RestartSpec] = RestartSpec(
        restart_type=RestartType.PERMANENT,
        budget_intensity=2,
        budget_period_seconds=30,
    )

    stream: "MemoryObjectReceiveStream[Event[Any]]"
    """Stream to receive events from."""

    router: "Router"
    """Router to manage event listeners."""

    def __init__(
        self,
        hassette: "Hassette",
        *,
        stream: "MemoryObjectReceiveStream[Event[Any]]",
        executor: "CommandExecutor",
        parent: "Resource | None" = None,
    ) -> None:
        super().__init__(hassette, parent=parent)
        self.stream = stream
        self._executor = executor
        self.router = Router()
        # Dispatch tracking for deterministic drain in test harnesses.
        self._dispatch_pending: int = 0
        self._dispatch_idle_event: asyncio.Event = asyncio.Event()
        self._dispatch_idle_event.set()  # starts idle

        self._event_filter = EventFilter(
            excluded_domains=hassette.config.bus_excluded_domains,
            excluded_entities=hassette.config.bus_excluded_entities,
            logger=self.logger,
        )

        # Lambda — must remain callable so hot-reload picks up config changes at fire time.
        self._config_resolver: Callable[[], float | None] = (
            lambda: self.hassette.config.lifecycle.event_handler_timeout_seconds
        )

        self._duration_hold = DurationHoldManager(
            executor=self._executor,
            config_resolver=self._config_resolver,
            state_reader=self._read_entity_state,
            remove_listener=self.remove_listener,
            router=self.router,
            task_bucket=self.task_bucket,
            logger=self.logger,
        )

    @property
    def config_log_level(self) -> LOG_LEVEL_TYPE:
        return self.hassette.config.logging.bus_service

    @cached_property
    def config_log_all_events(self) -> bool:
        """Return whether to log all events."""
        return self.hassette.config.logging.all_events

    def _on_dispatch_done(self, _task: asyncio.Task[Any]) -> None:
        """Callback for dispatch task completion — decrements pending counter."""
        if self._dispatch_pending <= 0:
            self.logger.warning("_dispatch_pending underflow detected (was %d); resetting to 0", self._dispatch_pending)
            self._dispatch_pending = 0
            self._dispatch_idle_event.set()
            return
        self._dispatch_pending -= 1
        if self._dispatch_pending == 0:
            self._dispatch_idle_event.set()

    async def add_listener(self, listener: "Listener") -> int:
        """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:
            The db_id assigned to the listener by the database.
        """
        if listener.duration_config is not None and listener.duration_config.duration is not None:

            def make_cancel_sub() -> Subscription:
                return self._duration_hold.create_cancel_listener(listener)

            def on_timer_cancel() -> None:
                self._duration_hold.decrement_timers_active()

            listener.duration_config.attach_timer(
                task_bucket=self.task_bucket,
                owner_id=listener.identity.owner_id,
                create_cancel_sub=make_cancel_sub,
                on_cancel=on_timer_cancel,
            )

        # Await DB registration inline — db_id is set before route insertion.
        reg = self._build_registration(listener)
        db_id = await self._executor.register_listener(reg)
        listener.mark_registered(db_id)

        # Sync: insert route — listener is routable after DB registration.
        self.router.add_route(listener.topic, listener)

        if listener.duration_config is not None and listener.duration_config.immediate:
            self._dispatch_pending += 1
            self._dispatch_idle_event.clear()
            immediate_task = self.task_bucket.spawn(
                self._duration_hold.immediate_fire_task(listener),
                name="bus:immediate_fire",
            )
            immediate_task.add_done_callback(self._on_dispatch_done)

        return db_id

    def _build_registration(self, listener: Listener) -> ListenerRegistration:
        """Build a ``ListenerRegistration`` struct from listener identity and options."""
        source_location = listener.identity.source_location
        registration_source: str | None = listener.identity.registration_source or None
        human_description: str | None = None
        if listener.predicate is not None:
            human_description = summarize_top_level(listener.predicate)
        return ListenerRegistration(
            app_key=listener.identity.app_key,
            instance_index=listener.identity.instance_index,
            handler_method=listener.identity.handler_name,
            topic=listener.topic,
            debounce=listener.options.debounce,
            throttle=listener.options.throttle,
            once=listener.options.once,
            priority=listener.options.priority,
            predicate_description=repr(listener.predicate) if listener.predicate else None,
            human_description=human_description,
            source_location=source_location,
            registration_source=registration_source,
            name=listener.identity.name,
            source_tier=listener.identity.source_tier,
            immediate=listener.duration_config.immediate if listener.duration_config else False,
            duration=listener.duration_config.duration if listener.duration_config else None,
            entity_id=listener.duration_config.entity_id if listener.duration_config else None,
        )

    def remove_listener(self, listener: "Listener") -> None:
        """Synchronously cancel and remove a listener from the routing table."""
        listener.cancel()
        self.router.remove_listener_by_id(listener.topic, listener.listener_id)

    def remove_listeners_by_owner(self, owner: str) -> None:
        """Remove all listeners owned by a specific owner synchronously."""
        removed = self.router.clear_owner(owner)
        for listener in removed:
            listener.cancel()

    def get_listeners_by_owner(self, owner: str) -> list["Listener"]:
        """Get all listeners owned by a specific owner."""
        return self.router.get_listeners_by_owner(owner)

    def _should_log_event(self, event: "Event[Any]") -> bool:
        """Determine if an event should be logged based on its type."""
        if not event.payload:
            return False

        if self.config_log_all_events:
            return True

        if self.hassette.config.logging.all_hass_events and event.topic.startswith(_HASS_TOPIC_PREFIX):
            return True

        if self.hassette.config.logging.all_hassette_events and event.topic.startswith(_HASSETTE_TOPIC_PREFIX):
            return True

        return False

    async def dispatch(self, base_topic: str, event: "Event[Any]") -> None:
        """Dispatch an event to all matching listeners for the given topic."""

        if self._event_filter.should_skip(base_topic, event):
            return

        if self._should_log_event(event):
            self.logger.debug("Event: %r", event)

        routes = self._expand_topics(base_topic, event)  # ordered: most specific -> least
        chosen: dict[int, tuple[str, Listener]] = {}  # listener_id -> (matched_route, listener)

        # Route first, then dedupe by "first match wins" because routes are ordered by specificity
        for route in routes:
            listeners = self.router.get_topic_listeners(route)
            for listener in listeners:
                if listener.listener_id in chosen:
                    continue
                if listener.matches(event):
                    chosen[listener.listener_id] = (route, listener)

        if not chosen:
            return

        # group by route for logging
        listeners_by_route = defaultdict(list)
        for route, listener in chosen.values():
            listeners_by_route[route].append(listener)

        # loop over routes so we always log in order of specificity
        for route in routes:
            listeners = listeners_by_route.get(route)
            if not listeners:
                continue

            self.logger.debug("Dispatch fanout %s -> %s (%d listener(s))", base_topic, route, len(listeners))
            for listener in listeners:
                self._dispatch_pending += 1
                self._dispatch_idle_event.clear()
                task = self.task_bucket.spawn(self._dispatch(route, event, listener), name="bus:dispatch_listener")
                task.add_done_callback(self._on_dispatch_done)

    def _expand_topics(self, topic: str, event: Event[Any]) -> list[str]:
        payload = event.payload
        if not isinstance(payload, HassPayload):
            return [topic]

        if payload.event_type != "state_changed":
            return [topic]

        entity_id = payload.entity_id
        if not valid_entity_id(entity_id):
            self.logger.debug("Cannot expand topics for invalid entity_id: %r", entity_id)
            return [topic]

        domain, _ = split_entity_id(entity_id)
        return [
            f"{topic}.{entity_id}",  # hass.event.state_changed.light.office
            f"{topic}.{domain}.*",  # hass.event.state_changed.light.*
            topic,  # hass.event.state_changed
        ]

    def _read_entity_state(self, 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.
        """
        try:
            state_proxy = self.hassette._state_proxy
            if state_proxy is None:
                self.logger.debug("read_entity_state: StateProxy not available for entity %s, skipping", entity_id)
                return None
            current_state = state_proxy.states.get(entity_id)
            if current_state is None:
                self.logger.debug("read_entity_state: entity %s not found in StateProxy, skipping", entity_id)
                return None
            return current_state
        except ResourceNotReadyError as exc:
            self.logger.error(
                "read_entity_state: ResourceNotReadyError for entity %s (sequencing violation).",
                entity_id,
                exc_info=exc,
            )
            return None
        except Exception as exc:
            self.logger.warning(
                "read_entity_state: unexpected error reading state for entity %s.",
                entity_id,
                exc_info=exc,
            )
            return None

    @property
    def duration_timers_active(self) -> int:
        """Number of currently active duration timers."""
        return self._duration_hold.duration_timers_active

    async def _dispatch(self, topic: str, event: "Event[Any]", listener: "Listener") -> None:
        """Dispatch an event to a specific listener.

        Builds an invoke_fn via ``build_tracked_invoke_fn``. Duration listeners
        delegate to ``DurationHoldManager.start_duration_timer``; non-duration
        listeners dispatch inline with ``once`` removal in a ``finally`` block.
        """
        # invoke_fn captures the original triggering event. Duration timer callbacks
        # re-verify current state via hold predicates but dispatch via this invoke_fn
        # — the handler receives the event that started the timer.
        invoke_fn = build_tracked_invoke_fn(listener, event, topic, self._executor, self._config_resolver)

        if listener.duration_config is not None and listener.duration_config.duration is not None:
            if listener.is_cancelled:
                return

            duration_config = listener.duration_config
            entity_id = duration_config.entity_id
            if not entity_id:
                self.logger.error(
                    "duration_fire: listener has no entity_id — construction invariant violated. "
                    "Listener owner=%s topic=%s",
                    listener.identity.owner_id,
                    listener.topic,
                )
                return

            self._duration_hold.start_duration_timer(listener, entity_id, duration_config, invoke_fn)
            return

        # Non-duration path (unchanged behavior).
        try:
            await listener.invoker.dispatch(invoke_fn)
        finally:
            if listener.options.once:
                self.remove_listener(listener)

    async def before_initialize(self) -> None:
        await self.hassette.ready_event.wait()

    @property
    def is_dispatch_idle(self) -> bool:
        return self._dispatch_idle_event.is_set()

    @property
    def dispatch_pending_count(self) -> int:
        return self._dispatch_pending

    async def await_dispatch_idle(self, *, timeout: float = _DISPATCH_IDLE_DEFAULT_TIMEOUT) -> None:
        """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:
            TimeoutError: If dispatch tasks are still running after ``timeout``.
        """
        deadline = asyncio.get_running_loop().time() + timeout

        def timeout_error() -> TimeoutError:
            return TimeoutError(
                f"BusService dispatch tasks did not complete within {timeout}s "
                f"({self._dispatch_pending} dispatch tasks still pending)"
            )

        while True:
            remaining = deadline - asyncio.get_running_loop().time()
            if remaining <= 0:
                raise timeout_error()
            try:
                await asyncio.wait_for(self._dispatch_idle_event.wait(), timeout=remaining)
            except TimeoutError:
                raise timeout_error() from None

            # Stability check: yield to let any in-transit events from the
            # anyio memory channel reach serve() → dispatch(). If idle_event
            # is still set after the yield, no new dispatches were triggered.
            await asyncio.sleep(_DISPATCH_STABILITY_SLEEP)
            if self._dispatch_idle_event.is_set():
                break

    async def serve(self) -> None:
        """Worker loop that processes events from the stream."""

        async with self.stream:
            self.mark_ready(reason="Stream opened")
            async for event in self.stream:
                if self.shutdown_event.is_set():
                    active_timers = self._duration_hold.duration_timers_active
                    if active_timers > 0:
                        self.logger.info(
                            "Shutdown: %d active duration timer(s) will be cancelled",
                            active_timers,
                        )
                    self.logger.debug("Hassette is shutting down, exiting bus loop")
                    self.mark_not_ready(reason="Hassette is shutting down")
                    break
                try:
                    await self.dispatch(str(event.topic), event)
                except Exception as e:
                    self.logger.exception("Error processing event: %s", e)

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 cached property

Return whether to log all events.

duration_timers_active: int property

Number of currently active duration timers.

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
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
async def add_listener(self, listener: "Listener") -> int:
    """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:
        The db_id assigned to the listener by the database.
    """
    if listener.duration_config is not None and listener.duration_config.duration is not None:

        def make_cancel_sub() -> Subscription:
            return self._duration_hold.create_cancel_listener(listener)

        def on_timer_cancel() -> None:
            self._duration_hold.decrement_timers_active()

        listener.duration_config.attach_timer(
            task_bucket=self.task_bucket,
            owner_id=listener.identity.owner_id,
            create_cancel_sub=make_cancel_sub,
            on_cancel=on_timer_cancel,
        )

    # Await DB registration inline — db_id is set before route insertion.
    reg = self._build_registration(listener)
    db_id = await self._executor.register_listener(reg)
    listener.mark_registered(db_id)

    # Sync: insert route — listener is routable after DB registration.
    self.router.add_route(listener.topic, listener)

    if listener.duration_config is not None and listener.duration_config.immediate:
        self._dispatch_pending += 1
        self._dispatch_idle_event.clear()
        immediate_task = self.task_bucket.spawn(
            self._duration_hold.immediate_fire_task(listener),
            name="bus:immediate_fire",
        )
        immediate_task.add_done_callback(self._on_dispatch_done)

    return db_id

remove_listener(listener: Listener) -> None

Synchronously cancel and remove a listener from the routing table.

Source code in src/hassette/core/bus_service.py
187
188
189
190
def remove_listener(self, listener: "Listener") -> None:
    """Synchronously cancel and remove a listener from the routing table."""
    listener.cancel()
    self.router.remove_listener_by_id(listener.topic, listener.listener_id)

remove_listeners_by_owner(owner: str) -> None

Remove all listeners owned by a specific owner synchronously.

Source code in src/hassette/core/bus_service.py
192
193
194
195
196
def remove_listeners_by_owner(self, owner: str) -> None:
    """Remove all listeners owned by a specific owner synchronously."""
    removed = self.router.clear_owner(owner)
    for listener in removed:
        listener.cancel()

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
198
199
200
def get_listeners_by_owner(self, owner: str) -> list["Listener"]:
    """Get all listeners owned by a specific owner."""
    return self.router.get_listeners_by_owner(owner)

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
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
async def dispatch(self, base_topic: str, event: "Event[Any]") -> None:
    """Dispatch an event to all matching listeners for the given topic."""

    if self._event_filter.should_skip(base_topic, event):
        return

    if self._should_log_event(event):
        self.logger.debug("Event: %r", event)

    routes = self._expand_topics(base_topic, event)  # ordered: most specific -> least
    chosen: dict[int, tuple[str, Listener]] = {}  # listener_id -> (matched_route, listener)

    # Route first, then dedupe by "first match wins" because routes are ordered by specificity
    for route in routes:
        listeners = self.router.get_topic_listeners(route)
        for listener in listeners:
            if listener.listener_id in chosen:
                continue
            if listener.matches(event):
                chosen[listener.listener_id] = (route, listener)

    if not chosen:
        return

    # group by route for logging
    listeners_by_route = defaultdict(list)
    for route, listener in chosen.values():
        listeners_by_route[route].append(listener)

    # loop over routes so we always log in order of specificity
    for route in routes:
        listeners = listeners_by_route.get(route)
        if not listeners:
            continue

        self.logger.debug("Dispatch fanout %s -> %s (%d listener(s))", base_topic, route, len(listeners))
        for listener in listeners:
            self._dispatch_pending += 1
            self._dispatch_idle_event.clear()
            task = self.task_bucket.spawn(self._dispatch(route, event, listener), name="bus:dispatch_listener")
            task.add_done_callback(self._on_dispatch_done)

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 timeout.

Source code in src/hassette/core/bus_service.py
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
async def await_dispatch_idle(self, *, timeout: float = _DISPATCH_IDLE_DEFAULT_TIMEOUT) -> None:
    """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:
        TimeoutError: If dispatch tasks are still running after ``timeout``.
    """
    deadline = asyncio.get_running_loop().time() + timeout

    def timeout_error() -> TimeoutError:
        return TimeoutError(
            f"BusService dispatch tasks did not complete within {timeout}s "
            f"({self._dispatch_pending} dispatch tasks still pending)"
        )

    while True:
        remaining = deadline - asyncio.get_running_loop().time()
        if remaining <= 0:
            raise timeout_error()
        try:
            await asyncio.wait_for(self._dispatch_idle_event.wait(), timeout=remaining)
        except TimeoutError:
            raise timeout_error() from None

        # Stability check: yield to let any in-transit events from the
        # anyio memory channel reach serve() → dispatch(). If idle_event
        # is still set after the yield, no new dispatches were triggered.
        await asyncio.sleep(_DISPATCH_STABILITY_SLEEP)
        if self._dispatch_idle_event.is_set():
            break

serve() -> None async

Worker loop that processes events from the stream.

Source code in src/hassette/core/bus_service.py
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
async def serve(self) -> None:
    """Worker loop that processes events from the stream."""

    async with self.stream:
        self.mark_ready(reason="Stream opened")
        async for event in self.stream:
            if self.shutdown_event.is_set():
                active_timers = self._duration_hold.duration_timers_active
                if active_timers > 0:
                    self.logger.info(
                        "Shutdown: %d active duration timer(s) will be cancelled",
                        active_timers,
                    )
                self.logger.debug("Hassette is shutting down, exiting bus loop")
                self.mark_not_ready(reason="Hassette is shutting down")
                break
            try:
                await self.dispatch(str(event.topic), event)
            except Exception as e:
                self.logger.exception("Error processing event: %s", e)