Skip to content

Commit

Permalink
Migrate one time listeners to use async_run_hass_job (home-assistant#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Mar 13, 2024
1 parent 488dae4 commit d6f1405
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ def __repr__(self) -> str:
@dataclass(slots=True)
class _OneTimeListener:
hass: HomeAssistant
listener: Callable[[Event], Coroutine[Any, Any, None] | None]
listener_job: HassJob[[Event], Coroutine[Any, Any, None] | None]
remove: CALLBACK_TYPE | None = None

@callback
Expand All @@ -1275,14 +1275,14 @@ def __call__(self, event: Event) -> None:
return
self.remove()
self.remove = None
self.hass.async_run_job(self.listener, event)
self.hass.async_run_hass_job(self.listener_job, event)

def __repr__(self) -> str:
"""Return the representation of the listener and source module."""
module = inspect.getmodule(self.listener)
module = inspect.getmodule(self.listener_job.target)
if module:
return f"<_OneTimeListener {module.__name__}:{self.listener}>"
return f"<_OneTimeListener {self.listener}>"
return f"<_OneTimeListener {module.__name__}:{self.listener_job.target}>"
return f"<_OneTimeListener {self.listener_job.target}>"


class EventBus:
Expand Down Expand Up @@ -1472,7 +1472,7 @@ def async_listen_once(
This method must be run in the event loop.
"""
one_time_listener = _OneTimeListener(self._hass, listener)
one_time_listener = _OneTimeListener(self._hass, HassJob(listener))
remove = self._async_listen_filterable_job(
event_type,
(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3098,7 +3098,7 @@ def test_one_time_listener_repr(hass: HomeAssistant) -> None:
def _listener(event: ha.Event):
"""Test listener."""

one_time_listener = ha._OneTimeListener(hass, _listener)
one_time_listener = ha._OneTimeListener(hass, HassJob(_listener))
repr_str = repr(one_time_listener)
assert "OneTimeListener" in repr_str
assert "test_core" in repr_str
Expand Down

0 comments on commit d6f1405

Please sign in to comment.