Fix cert expiry config flow check and update (#26638)

* Fix typo in translations

* Work on bug #26619

* readd the homeassistant.start event

* Remove the callback

* Added the executor_job for _test_connection

* Update test_config_flow.py
This commit is contained in:
Maikel Punie 2019-09-17 22:39:46 +02:00 committed by Paulus Schoutsen
parent 504b8c7685
commit 9114ed36cd
5 changed files with 26 additions and 18 deletions

View file

@ -21,4 +21,4 @@
},
"title": "Certificate Expiry"
}
}
}

View file

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

View file

@ -38,10 +38,12 @@ class CertexpiryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
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"
@ -59,7 +61,7 @@ class CertexpiryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
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)

View file

@ -9,7 +9,12 @@ import voluptuous as vol
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
@ -82,6 +87,15 @@ class SSLCertificate(Entity):
"""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:

View file

@ -8,7 +8,7 @@ from homeassistant.components.cert_expiry import config_flow
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
@ -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