Fixed cert_expiry sensor to delay firing on HA startup (#8920)
* Fixed cert_expiry sensor to delay firing on HA startup * Addressed Travis complaints * Added imports * Fixed cert_expiry sensor to delay firing on HA startup * Changed comment
This commit is contained in:
parent
4a98b32a03
commit
79f45b5176
1 changed files with 19 additions and 9 deletions
|
@ -4,16 +4,17 @@ Counter for the days till a HTTPS (TLS) certificate will expire.
|
||||||
For more details about this sensor please refer to the documentation at
|
For more details about this sensor please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.cert_expiry/
|
https://home-assistant.io/components/sensor.cert_expiry/
|
||||||
"""
|
"""
|
||||||
import datetime
|
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
import ssl
|
import ssl
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
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 homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -21,7 +22,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
DEFAULT_NAME = 'SSL Certificate Expiry'
|
DEFAULT_NAME = 'SSL Certificate Expiry'
|
||||||
DEFAULT_PORT = 443
|
DEFAULT_PORT = 443
|
||||||
|
|
||||||
SCAN_INTERVAL = datetime.timedelta(hours=12)
|
SCAN_INTERVAL = timedelta(hours=12)
|
||||||
|
|
||||||
TIMEOUT = 10.0
|
TIMEOUT = 10.0
|
||||||
|
|
||||||
|
@ -34,11 +35,20 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up certificate expiry sensor."""
|
"""Set up certificate expiry sensor."""
|
||||||
server_name = config.get(CONF_HOST)
|
def run_setup(event):
|
||||||
server_port = config.get(CONF_PORT)
|
"""Wait until Home Assistant is fully initialized before creating.
|
||||||
sensor_name = config.get(CONF_NAME)
|
|
||||||
|
|
||||||
add_devices([SSLCertificate(sensor_name, server_name, server_port)], True)
|
Delay the setup until Home Assistant is fully initialized.
|
||||||
|
"""
|
||||||
|
server_name = config.get(CONF_HOST)
|
||||||
|
server_port = config.get(CONF_PORT)
|
||||||
|
sensor_name = config.get(CONF_NAME)
|
||||||
|
|
||||||
|
add_devices([SSLCertificate(sensor_name, server_name, server_port)],
|
||||||
|
True)
|
||||||
|
|
||||||
|
# To allow checking of the HA certificate we must first be running.
|
||||||
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, run_setup)
|
||||||
|
|
||||||
|
|
||||||
class SSLCertificate(Entity):
|
class SSLCertificate(Entity):
|
||||||
|
@ -97,6 +107,6 @@ class SSLCertificate(Entity):
|
||||||
return
|
return
|
||||||
|
|
||||||
ts_seconds = ssl.cert_time_to_seconds(cert['notAfter'])
|
ts_seconds = ssl.cert_time_to_seconds(cert['notAfter'])
|
||||||
timestamp = datetime.datetime.fromtimestamp(ts_seconds)
|
timestamp = datetime.fromtimestamp(ts_seconds)
|
||||||
expiry = timestamp - datetime.datetime.today()
|
expiry = timestamp - datetime.today()
|
||||||
self._state = expiry.days
|
self._state = expiry.days
|
||||||
|
|
Loading…
Add table
Reference in a new issue