diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index 0bd766589a1..6939c1f7073 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -249,9 +249,19 @@ async def async_setup(hass, config): await hassio.update_hass_api(config.get("http", {}), refresh_token) + last_timezone = None + async def push_config(_): """Push core config to Hass.io.""" - await hassio.update_hass_timezone(str(hass.config.time_zone)) + nonlocal last_timezone + + new_timezone = str(hass.config.time_zone) + + if new_timezone == last_timezone: + return + + last_timezone = new_timezone + await hassio.update_hass_timezone(new_timezone) hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, push_config) diff --git a/homeassistant/core.py b/homeassistant/core.py index 1df05150b14..cfe74874d40 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -263,11 +263,11 @@ class HomeAssistant: This method is a coroutine. """ _LOGGER.info("Starting Home Assistant") - self.state = CoreState.starting - setattr(self.loop, "_thread_ident", threading.get_ident()) - self.bus.async_fire(EVENT_HOMEASSISTANT_START) + + self.state = CoreState.starting self.bus.async_fire(EVENT_CORE_CONFIG_UPDATE) + self.bus.async_fire(EVENT_HOMEASSISTANT_START) try: # Only block for EVENT_HOMEASSISTANT_START listener @@ -293,8 +293,9 @@ class HomeAssistant: return self.state = CoreState.running - _async_create_timer(self) + self.bus.async_fire(EVENT_CORE_CONFIG_UPDATE) self.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) + _async_create_timer(self) def add_job(self, target: Callable[..., Any], *args: Any) -> None: """Add job to the executor pool. diff --git a/tests/components/hassio/test_discovery.py b/tests/components/hassio/test_discovery.py index 9d148745f18..1cfb5ed9f7b 100644 --- a/tests/components/hassio/test_discovery.py +++ b/tests/components/hassio/test_discovery.py @@ -104,7 +104,7 @@ async def test_hassio_discovery_startup_done(hass, aioclient_mock, hassio_client await async_setup_component(hass, "hassio", {}) await hass.async_block_till_done() - assert aioclient_mock.call_count == 3 + assert aioclient_mock.call_count == 2 assert mock_mqtt.called mock_mqtt.assert_called_with( { diff --git a/tests/test_core.py b/tests/test_core.py index 9fc257eaf2d..c4079328f1f 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -22,12 +22,14 @@ from homeassistant.const import ( EVENT_HOMEASSISTANT_CLOSE, EVENT_HOMEASSISTANT_FINAL_WRITE, EVENT_HOMEASSISTANT_START, + EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP, EVENT_SERVICE_REGISTERED, EVENT_SERVICE_REMOVED, EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, EVENT_TIMER_OUT_OF_SYNC, + MATCH_ALL, __version__, ) import homeassistant.core as ha @@ -1333,3 +1335,35 @@ async def test_additional_data_in_core_config(hass, hass_storage): } await config.async_load() assert config.location_name == "Test Name" + + +async def test_start_events(hass): + """Test events fired when starting Home Assistant.""" + hass.state = ha.CoreState.not_running + + all_events = [] + + @ha.callback + def capture_events(ev): + all_events.append(ev.event_type) + + hass.bus.async_listen(MATCH_ALL, capture_events) + + core_states = [] + + @ha.callback + def capture_core_state(_): + core_states.append(hass.state) + + hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, capture_core_state) + + await hass.async_start() + await hass.async_block_till_done() + + assert all_events == [ + EVENT_CORE_CONFIG_UPDATE, + EVENT_HOMEASSISTANT_START, + EVENT_CORE_CONFIG_UPDATE, + EVENT_HOMEASSISTANT_STARTED, + ] + assert core_states == [ha.CoreState.starting, ha.CoreState.running]