Skip to content

Commit

Permalink
Fix cert expiry config flow check and update (#26638)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Cereal2nd authored and balloob committed Sep 17, 2019
1 parent 504b8c7 commit 9114ed3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/cert_expiry/.translations/en.json
Expand Up @@ -21,4 +21,4 @@
},
"title": "Certificate Expiry"
}
}
}
14 changes: 3 additions & 11 deletions homeassistant/components/cert_expiry/__init__.py
@@ -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
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
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
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

0 comments on commit 9114ed3

Please sign in to comment.