diff --git a/homeassistant/components/cert_expiry/__init__.py b/homeassistant/components/cert_expiry/__init__.py index babf81048df..22339b9f4c4 100644 --- a/homeassistant/components/cert_expiry/__init__.py +++ b/homeassistant/components/cert_expiry/__init__.py @@ -6,7 +6,7 @@ import logging from typing import Optional from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_HOST, CONF_PORT +from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STARTED from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -27,7 +27,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: port = entry.data[CONF_PORT] coordinator = CertExpiryDataUpdateCoordinator(hass, host, port) - await coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = coordinator @@ -35,7 +34,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if entry.unique_id is None: hass.config_entries.async_update_entry(entry, unique_id=f"{host}:{port}") - hass.config_entries.async_setup_platforms(entry, PLATFORMS) + async def async_finish_startup(_): + await coordinator.async_refresh() + hass.config_entries.async_setup_platforms(entry, PLATFORMS) + + entry.async_on_unload( + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, async_finish_startup) + ) return True diff --git a/tests/components/cert_expiry/test_init.py b/tests/components/cert_expiry/test_init.py index dbe5e74d891..25771c39250 100644 --- a/tests/components/cert_expiry/test_init.py +++ b/tests/components/cert_expiry/test_init.py @@ -9,6 +9,7 @@ from homeassistant.const import ( CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_START, + EVENT_HOMEASSISTANT_STARTED, STATE_UNAVAILABLE, ) from homeassistant.setup import async_setup_component @@ -87,6 +88,7 @@ async def test_unload_config_entry(mock_now, hass): return_value=timestamp, ): assert await async_setup_component(hass, DOMAIN, {}) is True + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() assert entry.state is ConfigEntryState.LOADED diff --git a/tests/components/cert_expiry/test_sensors.py b/tests/components/cert_expiry/test_sensors.py index 099fe78ca39..0ca228dc344 100644 --- a/tests/components/cert_expiry/test_sensors.py +++ b/tests/components/cert_expiry/test_sensors.py @@ -5,8 +5,13 @@ import ssl from unittest.mock import patch from homeassistant.components.cert_expiry.const import DOMAIN -from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import CONF_HOST, CONF_PORT, STATE_UNAVAILABLE, STATE_UNKNOWN +from homeassistant.const import ( + CONF_HOST, + CONF_PORT, + EVENT_HOMEASSISTANT_STARTED, + STATE_UNAVAILABLE, + STATE_UNKNOWN, +) from homeassistant.util.dt import utcnow from .const import HOST, PORT @@ -32,6 +37,7 @@ async def test_async_setup_entry(mock_now, hass): ): entry.add_to_hass(hass) assert await hass.config_entries.async_setup(entry.entry_id) + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() state = hass.states.get("sensor.cert_expiry_timestamp_example_com") @@ -56,6 +62,7 @@ async def test_async_setup_entry_bad_cert(hass): ): entry.add_to_hass(hass) assert await hass.config_entries.async_setup(entry.entry_id) + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() state = hass.states.get("sensor.cert_expiry_timestamp_example_com") @@ -65,36 +72,6 @@ async def test_async_setup_entry_bad_cert(hass): assert not state.attributes.get("is_valid") -async def test_async_setup_entry_host_unavailable(hass): - """Test async_setup_entry when host is unavailable.""" - entry = MockConfigEntry( - domain=DOMAIN, - data={CONF_HOST: HOST, CONF_PORT: PORT}, - unique_id=f"{HOST}:{PORT}", - ) - - with patch( - "homeassistant.components.cert_expiry.helper.get_cert", - side_effect=socket.gaierror, - ): - entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(entry.entry_id) is False - await hass.async_block_till_done() - - assert entry.state is ConfigEntryState.SETUP_RETRY - - next_update = utcnow() + timedelta(seconds=45) - async_fire_time_changed(hass, next_update) - with patch( - "homeassistant.components.cert_expiry.helper.get_cert", - side_effect=socket.gaierror, - ): - await hass.async_block_till_done() - - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") - assert state is None - - async def test_update_sensor(hass): """Test async_update for sensor.""" entry = MockConfigEntry( @@ -112,6 +89,7 @@ async def test_update_sensor(hass): ): entry.add_to_hass(hass) assert await hass.config_entries.async_setup(entry.entry_id) + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() state = hass.states.get("sensor.cert_expiry_timestamp_example_com") @@ -154,6 +132,7 @@ async def test_update_sensor_network_errors(hass): ): entry.add_to_hass(hass) assert await hass.config_entries.async_setup(entry.entry_id) + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() state = hass.states.get("sensor.cert_expiry_timestamp_example_com")