Delay startup for cert_expiry to allow for self checks (#56266)

* Delay startup of cert_expiry

* Update tests
This commit is contained in:
jjlawren 2021-09-16 06:31:36 -05:00 committed by GitHub
parent c668dcb1ac
commit 0438c9308c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 35 deletions

View File

@ -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

View File

@ -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

View File

@ -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")