Fix creating cert_expiry configs during runtime (#56298)

* Fix creating cert_expiry configs during runtime

* Address review feedback on tests

* Improve delayed startup test
This commit is contained in:
jjlawren 2021-09-18 12:54:54 -05:00 committed by GitHub
parent 312a9e5df2
commit f31b9eae61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 15 deletions

View file

@ -12,6 +12,7 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_STARTED,
STATE_UNAVAILABLE,
)
from homeassistant.core import CoreState
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
@ -23,6 +24,8 @@ from tests.common import MockConfigEntry, async_fire_time_changed
async def test_setup_with_config(hass):
"""Test setup component with config."""
assert hass.state is CoreState.running
config = {
SENSOR_DOMAIN: [
{"platform": DOMAIN, CONF_HOST: HOST, CONF_PORT: PORT},
@ -49,6 +52,8 @@ async def test_setup_with_config(hass):
async def test_update_unique_id(hass):
"""Test updating a config entry without a unique_id."""
assert hass.state is CoreState.running
entry = MockConfigEntry(domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT})
entry.add_to_hass(hass)
@ -71,6 +76,8 @@ async def test_update_unique_id(hass):
@patch("homeassistant.util.dt.utcnow", return_value=static_datetime())
async def test_unload_config_entry(mock_now, hass):
"""Test unloading a config entry."""
assert hass.state is CoreState.running
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: HOST, CONF_PORT: PORT},
@ -107,3 +114,35 @@ async def test_unload_config_entry(mock_now, hass):
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is None
async def test_delay_load_during_startup(hass):
"""Test delayed loading of a config entry during startup."""
hass.state = CoreState.not_running
entry = MockConfigEntry(domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT})
entry.add_to_hass(hass)
assert await async_setup_component(hass, DOMAIN, {}) is True
await hass.async_block_till_done()
assert hass.state is CoreState.not_running
assert entry.state is ConfigEntryState.LOADED
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is None
timestamp = future_timestamp(100)
with patch(
"homeassistant.components.cert_expiry.get_cert_expiry_timestamp",
return_value=timestamp,
):
await hass.async_start()
await hass.async_block_till_done()
assert hass.state is CoreState.running
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state.state == timestamp.isoformat()
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")