diff --git a/tests/components/cert_expiry/test_config_flow.py b/tests/components/cert_expiry/test_config_flow.py index d59d60b11da..e5d90e12d13 100644 --- a/tests/components/cert_expiry/test_config_flow.py +++ b/tests/components/cert_expiry/test_config_flow.py @@ -164,7 +164,7 @@ async def test_bad_import(hass): async def test_abort_if_already_setup(hass): """Test we abort if the cert is already setup.""" MockConfigEntry( - domain="cert_expiry", + domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT}, unique_id=f"{HOST}:{PORT}", ).add_to_hass(hass) diff --git a/tests/components/cert_expiry/test_sensors.py b/tests/components/cert_expiry/test_sensors.py index 66df9e60c57..f58534bc612 100644 --- a/tests/components/cert_expiry/test_sensors.py +++ b/tests/components/cert_expiry/test_sensors.py @@ -3,6 +3,7 @@ from datetime import timedelta import socket import ssl +from homeassistant.components.cert_expiry.const import DOMAIN from homeassistant.config_entries import ENTRY_STATE_SETUP_RETRY from homeassistant.const import CONF_HOST, CONF_PORT, STATE_UNAVAILABLE, STATE_UNKNOWN import homeassistant.util.dt as dt_util @@ -18,7 +19,7 @@ from tests.common import MockConfigEntry, async_fire_time_changed async def test_async_setup_entry(mock_now, hass): """Test async_setup_entry.""" entry = MockConfigEntry( - domain="cert_expiry", + domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT}, unique_id=f"{HOST}:{PORT}", ) @@ -51,7 +52,7 @@ async def test_async_setup_entry(mock_now, hass): async def test_async_setup_entry_bad_cert(hass): """Test async_setup_entry with a bad/expired cert.""" entry = MockConfigEntry( - domain="cert_expiry", + domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT}, unique_id=f"{HOST}:{PORT}", ) @@ -75,7 +76,7 @@ async def test_async_setup_entry_bad_cert(hass): async def test_async_setup_entry_host_unavailable(hass): """Test async_setup_entry when host is unavailable.""" entry = MockConfigEntry( - domain="cert_expiry", + domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT}, unique_id=f"{HOST}:{PORT}", ) @@ -106,7 +107,7 @@ async def test_async_setup_entry_host_unavailable(hass): async def test_update_sensor(mock_now, hass): """Test async_update for sensor.""" entry = MockConfigEntry( - domain="cert_expiry", + domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT}, unique_id=f"{HOST}:{PORT}", ) @@ -121,6 +122,8 @@ async def test_update_sensor(mock_now, hass): assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() + coordinator = hass.data[DOMAIN][entry.entry_id] + state = hass.states.get("sensor.cert_expiry_example_com") assert state is not None assert state.state != STATE_UNAVAILABLE @@ -137,13 +140,11 @@ async def test_update_sensor(mock_now, hass): timestamp2 = future_timestamp(99) - next_update = dt_util.utcnow() + timedelta(hours=12) - async_fire_time_changed(hass, next_update) - with patch( "homeassistant.components.cert_expiry.get_cert_expiry_timestamp", return_value=timestamp2, ): + await coordinator.async_refresh() await hass.async_block_till_done() state = hass.states.get("sensor.cert_expiry_example_com") @@ -165,7 +166,7 @@ async def test_update_sensor(mock_now, hass): async def test_update_sensor_network_errors(mock_now, hass): """Test async_update for sensor.""" entry = MockConfigEntry( - domain="cert_expiry", + domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT}, unique_id=f"{HOST}:{PORT}", ) @@ -180,6 +181,8 @@ async def test_update_sensor_network_errors(mock_now, hass): assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() + coordinator = hass.data[DOMAIN][entry.entry_id] + state = hass.states.get("sensor.cert_expiry_example_com") assert state is not None assert state.state != STATE_UNAVAILABLE @@ -194,25 +197,21 @@ async def test_update_sensor_network_errors(mock_now, hass): assert state.attributes.get("error") == "None" assert state.attributes.get("is_valid") - next_update = dt_util.utcnow() + timedelta(hours=12) - async_fire_time_changed(hass, next_update) - with patch( "homeassistant.components.cert_expiry.helper.get_cert", side_effect=socket.gaierror, ): + await coordinator.async_refresh() await hass.async_block_till_done() state = hass.states.get("sensor.cert_expiry_example_com") assert state.state == STATE_UNAVAILABLE - next_update = dt_util.utcnow() + timedelta(hours=12) - async_fire_time_changed(hass, next_update) - with patch( "homeassistant.components.cert_expiry.get_cert_expiry_timestamp", return_value=future_timestamp(99), ): + await coordinator.async_refresh() await hass.async_block_till_done() state = hass.states.get("sensor.cert_expiry_example_com") @@ -222,13 +221,11 @@ async def test_update_sensor_network_errors(mock_now, hass): assert state.attributes.get("error") == "None" assert state.attributes.get("is_valid") - next_update = dt_util.utcnow() + timedelta(hours=12) - async_fire_time_changed(hass, next_update) - with patch( "homeassistant.components.cert_expiry.helper.get_cert", side_effect=ssl.SSLError("something bad"), ): + await coordinator.async_refresh() await hass.async_block_till_done() state = hass.states.get("sensor.cert_expiry_example_com") @@ -244,12 +241,10 @@ async def test_update_sensor_network_errors(mock_now, hass): assert state.attributes.get("error") == "something bad" assert not state.attributes.get("is_valid") - next_update = dt_util.utcnow() + timedelta(hours=12) - async_fire_time_changed(hass, next_update) - with patch( "homeassistant.components.cert_expiry.helper.get_cert", side_effect=Exception() ): + await coordinator.async_refresh() await hass.async_block_till_done() state = hass.states.get("sensor.cert_expiry_example_com")