diff --git a/homeassistant/core.py b/homeassistant/core.py index 1050d25ee715e61f5cb3d0fcc5ba2eb0aa695d8d..b797798134e9fe982948fdaefe6a21ef1e23a636 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -451,9 +451,7 @@ class HomeAssistant: self.import_executor = InterruptibleThreadPoolExecutor( max_workers=1, thread_name_prefix="ImportExecutor" ) - self.loop_thread_id = getattr( - self.loop, "_thread_ident", getattr(self.loop, "_thread_id") - ) + self.loop_thread_id = getattr(self.loop, "_thread_id") def verify_event_loop_thread(self, what: str) -> None: """Report and raise if we are not running in the event loop thread.""" diff --git a/homeassistant/runner.py b/homeassistant/runner.py index a1510336302c1eeff5e8b9d28f753a283e60bb68..4bac12ec399a5044a72cb4ceec5111568b8f4276 100644 --- a/homeassistant/runner.py +++ b/homeassistant/runner.py @@ -107,7 +107,6 @@ class HassEventLoopPolicy(asyncio.DefaultEventLoopPolicy): def new_event_loop(self) -> asyncio.AbstractEventLoop: """Get the event loop.""" loop: asyncio.AbstractEventLoop = super().new_event_loop() - setattr(loop, "_thread_ident", threading.get_ident()) loop.set_exception_handler(_async_loop_exception_handler) if self.debug: loop.set_debug(True) diff --git a/homeassistant/util/async_.py b/homeassistant/util/async_.py index dcb788f0685e318194af3099e9428ee242665ef3..d010d8cb341e3e1161df8c234681b6ccda37eff2 100644 --- a/homeassistant/util/async_.py +++ b/homeassistant/util/async_.py @@ -57,7 +57,7 @@ def run_callback_threadsafe[_T, *_Ts]( Return a concurrent.futures.Future to access the result. """ - if (ident := loop.__dict__.get("_thread_ident")) and ident == threading.get_ident(): + if (ident := loop.__dict__.get("_thread_id")) and ident == threading.get_ident(): raise RuntimeError("Cannot be called from within the event loop") future: concurrent.futures.Future[_T] = concurrent.futures.Future() diff --git a/tests/common.py b/tests/common.py index 2f0c032616ac6817efd96af13b3dd6a6b5fc5d09..3a4c3f2851c381bb0011617c0c6ab8f54a3cd028 100644 --- a/tests/common.py +++ b/tests/common.py @@ -204,8 +204,6 @@ def get_test_home_assistant() -> Generator[HomeAssistant]: hass.start = start_hass hass.stop = stop_hass - loop._thread_ident = threading.get_ident() - hass_created_event.set() loop.run_forever() diff --git a/tests/util/test_async.py b/tests/util/test_async.py index 17349cf6ff98984c14c16ebe000dce1619b54b4c..cda10b69c3f472385103fd915b13cfd8a8ca1a3d 100644 --- a/tests/util/test_async.py +++ b/tests/util/test_async.py @@ -22,18 +22,18 @@ def test_run_callback_threadsafe_from_inside_event_loop( loop = Mock(spec=["call_soon_threadsafe"]) - loop._thread_ident = None + loop._thread_id = None mock_ident.return_value = 5 hasync.run_callback_threadsafe(loop, callback) assert len(loop.call_soon_threadsafe.mock_calls) == 1 - loop._thread_ident = 5 + loop._thread_id = 5 mock_ident.return_value = 5 with pytest.raises(RuntimeError): hasync.run_callback_threadsafe(loop, callback) assert len(loop.call_soon_threadsafe.mock_calls) == 1 - loop._thread_ident = 1 + loop._thread_id = 1 mock_ident.return_value = 5 hasync.run_callback_threadsafe(loop, callback) assert len(loop.call_soon_threadsafe.mock_calls) == 2 @@ -78,7 +78,7 @@ async def test_run_callback_threadsafe(hass: HomeAssistant) -> None: nonlocal it_ran it_ran = True - with patch.dict(hass.loop.__dict__, {"_thread_ident": -1}): + with patch.dict(hass.loop.__dict__, {"_thread_id": -1}): assert hasync.run_callback_threadsafe(hass.loop, callback) assert it_ran is False @@ -98,7 +98,7 @@ async def test_callback_is_always_scheduled(hass: HomeAssistant) -> None: hasync.shutdown_run_callback_threadsafe(hass.loop) with ( - patch.dict(hass.loop.__dict__, {"_thread_ident": -1}), + patch.dict(hass.loop.__dict__, {"_thread_id": -1}), patch.object(hass.loop, "call_soon_threadsafe") as mock_call_soon_threadsafe, pytest.raises(RuntimeError), ):