From 1fd5f64dcc21f4b873b9ac959f86c70906cf1469 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 7 Apr 2024 16:30:54 -1000 Subject: [PATCH] Migrate matrix to use run_immediately for start listener (#115167) * Migrate matrix to use run_immediately for start listener * Migrate matrix to use run_immediately for start listener --- homeassistant/components/matrix/__init__.py | 2 +- tests/components/matrix/conftest.py | 3 +++ tests/components/matrix/test_rooms.py | 28 ++++++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/matrix/__init__.py b/homeassistant/components/matrix/__init__.py index a283ba20dcb..98653ba19ad 100644 --- a/homeassistant/components/matrix/__init__.py +++ b/homeassistant/components/matrix/__init__.py @@ -220,7 +220,7 @@ class MatrixBot: ) # milliseconds. self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_START, handle_startup, run_immediately=False + EVENT_HOMEASSISTANT_START, handle_startup, run_immediately=True ) def _load_commands(self, commands: list[ConfigCommand]) -> None: diff --git a/tests/components/matrix/conftest.py b/tests/components/matrix/conftest.py index 2c24f4d0e75..18227914df4 100644 --- a/tests/components/matrix/conftest.py +++ b/tests/components/matrix/conftest.py @@ -281,6 +281,9 @@ async def matrix_bot( assert await async_setup_component(hass, MATRIX_DOMAIN, MOCK_CONFIG_DATA) assert await async_setup_component(hass, NOTIFY_DOMAIN, MOCK_CONFIG_DATA) await hass.async_block_till_done() + + # Accessing hass.data in tests is not desirable, but all the tests here + # currently do this. assert isinstance(matrix_bot := hass.data[MATRIX_DOMAIN], MatrixBot) await hass.async_start() diff --git a/tests/components/matrix/test_rooms.py b/tests/components/matrix/test_rooms.py index 29081b80fd5..66d1afbf532 100644 --- a/tests/components/matrix/test_rooms.py +++ b/tests/components/matrix/test_rooms.py @@ -1,14 +1,36 @@ """Test MatrixBot._join.""" +import pytest + from homeassistant.components.matrix import MatrixBot +from homeassistant.components.matrix.const import DOMAIN as MATRIX_DOMAIN +from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN +from homeassistant.const import EVENT_HOMEASSISTANT_START +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from .conftest import MOCK_CONFIG_DATA from tests.components.matrix.conftest import TEST_BAD_ROOM, TEST_JOINABLE_ROOMS -async def test_join(hass, matrix_bot: MatrixBot, caplog): +async def test_join( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mock_client, + mock_save_json, + mock_allowed_path, +) -> None: """Test joining configured rooms.""" + assert await async_setup_component(hass, MATRIX_DOMAIN, MOCK_CONFIG_DATA) + assert await async_setup_component(hass, NOTIFY_DOMAIN, MOCK_CONFIG_DATA) + hass.bus.async_fire(EVENT_HOMEASSISTANT_START) + await hass.async_block_till_done(wait_background_tasks=True) + + # Accessing hass.data in tests is not desirable, but all the tests here + # currently do this. + matrix_bot = hass.data[MATRIX_DOMAIN] - await hass.async_start() for room_id in TEST_JOINABLE_ROOMS: assert f"Joined or already in room '{room_id}'" in caplog.messages @@ -21,7 +43,7 @@ async def test_join(hass, matrix_bot: MatrixBot, caplog): ) -async def test_resolve_aliases(hass, matrix_bot: MatrixBot): +async def test_resolve_aliases(hass: HomeAssistant, matrix_bot: MatrixBot) -> None: """Test resolving configured room aliases into room ids.""" await hass.async_start()