From 7724507510523ebc79ed73f4c539de3fe86d621a Mon Sep 17 00:00:00 2001 From: Jason Lawrence Date: Wed, 2 Oct 2019 20:04:46 -0500 Subject: [PATCH 1/6] Don't force extra update on startup --- homeassistant/components/cert_expiry/sensor.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/homeassistant/components/cert_expiry/sensor.py b/homeassistant/components/cert_expiry/sensor.py index b564cff7338584..fccfb295c0fff2 100644 --- a/homeassistant/components/cert_expiry/sensor.py +++ b/homeassistant/components/cert_expiry/sensor.py @@ -9,12 +9,7 @@ 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, - EVENT_HOMEASSISTANT_START, -) +from homeassistant.const import CONF_NAME, CONF_HOST, CONF_PORT from homeassistant.helpers.entity import Entity from .const import DOMAIN, DEFAULT_NAME, DEFAULT_PORT @@ -87,15 +82,6 @@ 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: From 9ed758a975d3508d0b72558753ecc527d76c4dc3 Mon Sep 17 00:00:00 2001 From: Jason Lawrence Date: Wed, 2 Oct 2019 20:52:30 -0500 Subject: [PATCH 2/6] Skip on entity add instead --- homeassistant/components/cert_expiry/sensor.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/cert_expiry/sensor.py b/homeassistant/components/cert_expiry/sensor.py index fccfb295c0fff2..ea375c8e588f84 100644 --- a/homeassistant/components/cert_expiry/sensor.py +++ b/homeassistant/components/cert_expiry/sensor.py @@ -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 @@ -41,7 +46,7 @@ async def async_setup_entry(hass, entry, async_add_entities): """Add cert-expiry entry.""" async_add_entities( [SSLCertificate(entry.title, entry.data[CONF_HOST], entry.data[CONF_PORT])], - True, + False, ) return True @@ -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: From f431395ec7c02de56c8495cecc88edda41573685 Mon Sep 17 00:00:00 2001 From: Jason Lawrence Date: Wed, 2 Oct 2019 22:25:21 -0500 Subject: [PATCH 3/6] Conditional update based on HA state --- homeassistant/components/cert_expiry/sensor.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/cert_expiry/sensor.py b/homeassistant/components/cert_expiry/sensor.py index ea375c8e588f84..897cdba262f1ec 100644 --- a/homeassistant/components/cert_expiry/sensor.py +++ b/homeassistant/components/cert_expiry/sensor.py @@ -47,6 +47,7 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities( [SSLCertificate(entry.title, entry.data[CONF_HOST], entry.data[CONF_PORT])], False, + # Don't update in case we're checking our own cert. ) return True @@ -84,7 +85,7 @@ def icon(self): @property def available(self): - """Icon to use in the frontend, if any.""" + """Return the availability of the sensor.""" return self._available async def async_added_to_hass(self): @@ -94,7 +95,11 @@ 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) + if self.hass.is_running: + await self.update() + else: + # Delay until HA is fully started in case we're checking our own cert. + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, do_update) def update(self): """Fetch the certificate information.""" @@ -120,3 +125,4 @@ def update(self): expiry = timestamp - datetime.today() self._available = True self._state = expiry.days + self.schedule_update_ha_state() From febb415828db67347c3decfcae4c188d7719e231 Mon Sep 17 00:00:00 2001 From: Jason Lawrence Date: Wed, 2 Oct 2019 22:28:41 -0500 Subject: [PATCH 4/6] Only force entity state update when postponed --- homeassistant/components/cert_expiry/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/cert_expiry/sensor.py b/homeassistant/components/cert_expiry/sensor.py index 897cdba262f1ec..f3ea2cfa339a12 100644 --- a/homeassistant/components/cert_expiry/sensor.py +++ b/homeassistant/components/cert_expiry/sensor.py @@ -94,6 +94,7 @@ async def async_added_to_hass(self): def do_update(_): """Run the update method when the start event was fired.""" self.update() + self.schedule_update_ha_state() if self.hass.is_running: await self.update() @@ -125,4 +126,3 @@ def update(self): expiry = timestamp - datetime.today() self._available = True self._state = expiry.days - self.schedule_update_ha_state() From 523cd16e03c7e216f5058e344c10c38bf76d05d3 Mon Sep 17 00:00:00 2001 From: Jason Lawrence Date: Thu, 3 Oct 2019 06:53:33 -0500 Subject: [PATCH 5/6] Clean up state updating --- homeassistant/components/cert_expiry/sensor.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/cert_expiry/sensor.py b/homeassistant/components/cert_expiry/sensor.py index f3ea2cfa339a12..14c772e06d66d1 100644 --- a/homeassistant/components/cert_expiry/sensor.py +++ b/homeassistant/components/cert_expiry/sensor.py @@ -15,6 +15,7 @@ CONF_PORT, EVENT_HOMEASSISTANT_START, ) +from homeassistant.core import callback from homeassistant.helpers.entity import Entity from .const import DOMAIN, DEFAULT_NAME, DEFAULT_PORT @@ -91,13 +92,13 @@ def available(self): async def async_added_to_hass(self): """Once the entity is added we should update to get the initial data loaded.""" + @callback def do_update(_): """Run the update method when the start event was fired.""" - self.update() - self.schedule_update_ha_state() + self.async_schedule_update_ha_state(True) if self.hass.is_running: - await self.update() + self.async_schedule_update_ha_state(True) else: # Delay until HA is fully started in case we're checking our own cert. self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, do_update) From d5d7ce46bd51d4857f37fc724864a3f9a959f449 Mon Sep 17 00:00:00 2001 From: Jason Lawrence Date: Thu, 3 Oct 2019 09:12:32 -0500 Subject: [PATCH 6/6] Delay YAML import --- homeassistant/components/cert_expiry/sensor.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/cert_expiry/sensor.py b/homeassistant/components/cert_expiry/sensor.py index 14c772e06d66d1..a5b879e5661e85 100644 --- a/homeassistant/components/cert_expiry/sensor.py +++ b/homeassistant/components/cert_expiry/sensor.py @@ -36,11 +36,18 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up certificate expiry sensor.""" - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_IMPORT}, data=dict(config) + + @callback + def do_import(_): + """Process YAML import after HA is fully started.""" + hass.async_create_task( + hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_IMPORT}, data=dict(config) + ) ) - ) + + # Delay to avoid validation during setup in case we're checking our own cert. + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, do_import) async def async_setup_entry(hass, entry, async_add_entities):