Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions homeassistant/components/cert_expiry/.translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"error": {
"certificate_fetch_failed": "Can not fetch certificate from this host and port combination",
"connection_timeout": "Timeout whemn connecting to this host",
"connection_timeout": "Timeout when connecting to this host",
"host_port_exists": "This host and port combination is already configured",
"resolve_failed": "This host can not be resolved"
},
Expand All @@ -21,4 +21,4 @@
},
"title": "Certificate Expiry"
}
}
}
14 changes: 3 additions & 11 deletions homeassistant/components/cert_expiry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""The cert_expiry component."""
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_START
from homeassistant.core import callback
from homeassistant.helpers.typing import HomeAssistantType


Expand All @@ -13,13 +11,7 @@ async def async_setup(hass, config):
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
"""Load the saved entities."""

@callback
def async_start(_):
"""Load the entry after the start event."""
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, async_start)

hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)
return True
8 changes: 5 additions & 3 deletions homeassistant/components/cert_expiry/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ def _prt_in_configuration_exists(self, user_input) -> bool:
return True
return False

def _test_connection(self, user_input=None):
async def _test_connection(self, user_input=None):
"""Test connection to the server and try to get the certtificate."""
try:
get_cert(user_input[CONF_HOST], user_input.get(CONF_PORT, DEFAULT_PORT))
await self.hass.async_add_executor_job(
get_cert, user_input[CONF_HOST], user_input.get(CONF_PORT, DEFAULT_PORT)
)
return True
except socket.gaierror:
self._errors[CONF_HOST] = "resolve_failed"
Expand All @@ -59,7 +61,7 @@ async def async_step_user(self, user_input=None):
if self._prt_in_configuration_exists(user_input):
self._errors[CONF_HOST] = "host_port_exists"
else:
if self._test_connection(user_input):
if await self._test_connection(user_input):
host = user_input[CONF_HOST]
name = slugify(user_input.get(CONF_NAME, DEFAULT_NAME))
prt = user_input.get(CONF_PORT, DEFAULT_PORT)
Expand Down
16 changes: 15 additions & 1 deletion homeassistant/components/cert_expiry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import homeassistant.helpers.config_validation as cv
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, CONF_HOST, CONF_PORT
from homeassistant.const import (
CONF_NAME,
CONF_HOST,
CONF_PORT,
EVENT_HOMEASSISTANT_START,
)
from homeassistant.helpers.entity import Entity

from .const import DOMAIN, DEFAULT_NAME, DEFAULT_PORT
Expand Down Expand Up @@ -82,6 +87,15 @@ def available(self):
"""Icon to use in the frontend, if any."""
return self._available

async def async_added_to_hass(self):
"""Once the entity is added we should update to get the initial data loaded."""

def do_update(_):
"""Run the update method when the start event was fired."""
self.update()

self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, do_update)

def update(self):
"""Fetch the certificate information."""
try:
Expand Down
4 changes: 2 additions & 2 deletions tests/components/cert_expiry/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.components.cert_expiry.const import DEFAULT_PORT
from homeassistant.const import CONF_PORT, CONF_NAME, CONF_HOST

from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, mock_coro

NAME = "Cert Expiry test 1 2 3"
PORT = 443
Expand All @@ -20,7 +20,7 @@ def mock_controller():
"""Mock a successfull _prt_in_configuration_exists."""
with patch(
"homeassistant.components.cert_expiry.config_flow.CertexpiryConfigFlow._test_connection",
return_value=True,
side_effect=lambda *_: mock_coro(True),
):
yield

Expand Down