Added MissingIntentWarning hook to wait_for
@@ -106,7 +106,27 @@ class EventManagerComponentBase(event_dispatcher.IEventDispatcherComponent, even
) -> event_dispatcher.AsyncCallbackT[event_dispatcher.EventT_co]:
# `_nested` is used to show the correct source code snippet if an intent
# warning is triggered.
+ self._check_intents(event_type, _nested)
+ if event_type not in self._listeners:
+ self._listeners[event_type] = []
+
+ if not asyncio.iscoroutinefunction(callback):
+ raise TypeError("Event callbacks must be coroutine functions (`async def')")
+
+ _LOGGER.debug(
+ "subscribing callback 'async def %s%s' to event-type %s.%s",
+ getattr(callback, "__name__", "<anon>"),
+ reflect.resolve_signature(callback),
+ event_type.__module__,
+ event_type.__qualname__,
+ )
+
+ self._listeners[event_type].append(callback) # type: ignore[arg-type]
+
+ return callback
+
+ def _check_intents(self, event_type: typing.Type[event_dispatcher.EventT_co], nested: int) -> None:
# If None, the user is on v6 with intents disabled, so we don't care.
if self._intents is not None:
# Collection of combined bitfield combinations of intents that
@@ -124,27 +144,9 @@ class EventManagerComponentBase(event_dispatcher.IEventDispatcherComponent, even
f"You have tried to listen to {event_type.__name__}, but this will only ever be triggered if "
f"you enable one of the following intents: {expected_intents_str}.",
category=errors.MissingIntentWarning,
- stacklevel=_nested + 2,
+ stacklevel=nested + 3,
)
- if event_type not in self._listeners:
- self._listeners[event_type] = []
-
- if not asyncio.iscoroutinefunction(callback):
- raise TypeError("Event callbacks must be coroutine functions (`async def')")
-
- _LOGGER.debug(
- "subscribing callback 'async def %s%s' to event-type %s.%s",
- getattr(callback, "__name__", "<anon>"),
- reflect.resolve_signature(callback),
- event_type.__module__,
- event_type.__qualname__,
- )
-
- self._listeners[event_type].append(callback) # type: ignore[arg-type]
-
- return callback
-
def get_listeners(
self, event_type: typing.Type[event_dispatcher.EventT_co], *, polymorphic: bool = True,
) -> typing.Collection[event_dispatcher.AsyncCallbackT[event_dispatcher.EventT_co]]:
@@ -308,6 +310,8 @@ class EventManagerComponentBase(event_dispatcher.IEventDispatcherComponent, even
if predicate is None:
predicate = _default_predicate
+ self._check_intents(event_type, 1)
+
future: asyncio.Future[event_dispatcher.EventT_co] = asyncio.get_event_loop().create_future()
try:
