diff --git a/homeassistant/components/google_assistant/helpers.py b/homeassistant/components/google_assistant/helpers.py index 72d9c7bb089ff..d39ea97537a26 100644 --- a/homeassistant/components/google_assistant/helpers.py +++ b/homeassistant/components/google_assistant/helpers.py @@ -112,7 +112,7 @@ async def sync_google(_): """Sync entities to Google.""" await self.async_sync_entities_all() - self._on_deinitialize.append(start.async_at_start(self.hass, sync_google)) + self._on_deinitialize.append(start.async_at_started(self.hass, sync_google)) @callback def async_deinitialize(self) -> None: diff --git a/tests/components/cloud/test_google_config.py b/tests/components/cloud/test_google_config.py index 91a7b2169c358..3083c24a6137e 100644 --- a/tests/components/cloud/test_google_config.py +++ b/tests/components/cloud/test_google_config.py @@ -372,6 +372,10 @@ async def test_sync_google_on_home_assistant_start( hass.bus.async_fire(EVENT_HOMEASSISTANT_START) await hass.async_block_till_done() + assert len(mock_sync.mock_calls) == 0 + + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) + await hass.async_block_till_done() assert len(mock_sync.mock_calls) == 1 diff --git a/tests/components/google_assistant/test_http.py b/tests/components/google_assistant/test_http.py index 6d0888dcf38a1..f4804ea3be94a 100644 --- a/tests/components/google_assistant/test_http.py +++ b/tests/components/google_assistant/test_http.py @@ -28,8 +28,12 @@ _get_homegraph_token, async_get_users, ) -from homeassistant.const import CLOUD_NEVER_EXPOSED_ENTITIES -from homeassistant.core import HomeAssistant, State +from homeassistant.const import ( + CLOUD_NEVER_EXPOSED_ENTITIES, + EVENT_HOMEASSISTANT_START, + EVENT_HOMEASSISTANT_STARTED, +) +from homeassistant.core import CoreState, HomeAssistant, State from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util @@ -78,6 +82,25 @@ } +async def test_sync_google_does_not_block_startup(hass: HomeAssistant) -> None: + """Test that Google entity sync runs after startup, not during.""" + hass.set_state(CoreState.not_running) + config = GoogleConfig(hass, DUMMY_CONFIG) + + with patch.object(config, "async_sync_entities_all") as mock_sync: + await config.async_initialize() + + # Fire EVENT_HOMEASSISTANT_START - sync should NOT run yet + hass.bus.async_fire(EVENT_HOMEASSISTANT_START) + await hass.async_block_till_done() + mock_sync.assert_not_called() + + # Fire EVENT_HOMEASSISTANT_STARTED - now sync should run + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) + await hass.async_block_till_done() + mock_sync.assert_called_once() + + async def test_get_jwt(hass: HomeAssistant) -> None: """Test signing of key."""