Fix update on cert_expiry startup (#27137)

* Don't force extra update on startup

* Skip on entity add instead

* Conditional update based on HA state

* Only force entity state update when postponed

* Clean up state updating

* Delay YAML import
pull/27155/head
jjlawren 2019-10-03 10:39:14 -05:00 committed by Martin Hjelmare
parent 2307cac942
commit bb45bdd8dd
1 changed files with 22 additions and 8 deletions

View File

@ -15,6 +15,7 @@ from homeassistant.const import (
CONF_PORT,
EVENT_HOMEASSISTANT_START,
)
from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
from .const import DOMAIN, DEFAULT_NAME, DEFAULT_PORT
@ -35,18 +36,26 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
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):
"""Add cert-expiry entry."""
async_add_entities(
[SSLCertificate(entry.title, entry.data[CONF_HOST], entry.data[CONF_PORT])],
True,
False,
# Don't update in case we're checking our own cert.
)
return True
@ -89,17 +98,22 @@ class SSLCertificate(Entity):
@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):
"""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.async_schedule_update_ha_state(True)
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, do_update)
if self.hass.is_running:
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)
def update(self):
"""Fetch the certificate information."""