Skip to content

Fix tests: replace deprecated asyncio.get_event_loop() for Python 3.12+#1748

Merged
AlexxIT merged 3 commits into
AlexxIT:masterfrom
Antonio112009:fix/tests-asyncio-event-loop
Mar 2, 2026
Merged

Fix tests: replace deprecated asyncio.get_event_loop() for Python 3.12+#1748
AlexxIT merged 3 commits into
AlexxIT:masterfrom
Antonio112009:fix/tests-asyncio-event-loop

Conversation

@Antonio112009

@Antonio112009 Antonio112009 commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Problem

asyncio.get_event_loop() no longer auto-creates an event loop in Python 3.12+ and raises RuntimeError in Python 3.14. This caused test_light_group, test_light_22, test_1394, and test_bulk to fail.

Additionally, the asyncio.create_task mock in test setup discarded coroutines without closing them, producing RuntimeWarning: coroutine was never awaited warnings.

Changes

tests/__init__.py

  • DummyRegistry.call(): replaced asyncio.get_event_loop() with asyncio.new_event_loop() + asyncio.set_event_loop()
  • init(): changed asyncio.create_task = lambda _: None to lambda coro: coro.close() to properly close discarded coroutines and eliminate warnings

tests/test_entity.py

  • await_(): replaced asyncio.get_event_loop() with asyncio.new_event_loop() + asyncio.set_event_loop()

tests/test_misc.py

  • test_bulk(): replaced asyncio.get_event_loop() with asyncio.new_event_loop() + asyncio.set_event_loop()

Result

  • All 71 tests pass
  • Coroutine warnings eliminated
  • Only 1 remaining warning from homeassistant's own code (aiohttp deprecation in HomeAssistantApplication)

asyncio.get_event_loop() no longer auto-creates an event loop in Python
3.12+ and raises RuntimeError in 3.14. Replaced with
asyncio.new_event_loop() + asyncio.set_event_loop() in:
- tests/__init__.py: DummyRegistry.call()
- tests/test_entity.py: await_()
- tests/test_misc.py: test_bulk()
Changed asyncio.create_task mock from discarding coroutines to properly
closing them, eliminating RuntimeWarning about unawaited coroutines.
AlexxIT added a commit to Antonio112009/SonoffLAN that referenced this pull request Mar 2, 2026
@AlexxIT
AlexxIT force-pushed the fix/tests-asyncio-event-loop branch from 94d8062 to fd3c3ab Compare March 2, 2026 13:02
@AlexxIT
AlexxIT merged commit ebe7fec into AlexxIT:master Mar 2, 2026
2 checks passed
@AlexxIT

AlexxIT commented Mar 2, 2026

Copy link
Copy Markdown
Owner

Thanks!

@AlexxIT AlexxIT added this to the master milestone Mar 2, 2026
@AlexxIT

AlexxIT commented Mar 5, 2026

Copy link
Copy Markdown
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants