2017-04-25 10:40:13 +00:00
|
|
|
"""
|
2018-08-19 20:29:08 +00:00
|
|
|
Counter for the days until an HTTPS (TLS) certificate will expire.
|
2017-04-24 20:01:00 +00:00
|
|
|
|
2017-04-25 10:40:13 +00:00
|
|
|
For more details about this sensor please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.cert_expiry/
|
2017-04-24 20:01:00 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import socket
|
2017-04-25 10:40:13 +00:00
|
|
|
import ssl
|
2017-08-13 06:49:15 +00:00
|
|
|
from datetime import datetime, timedelta
|
2017-04-25 10:40:13 +00:00
|
|
|
|
2017-04-24 20:01:00 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2017-08-13 06:49:15 +00:00
|
|
|
from homeassistant.const import (CONF_NAME, CONF_HOST, CONF_PORT,
|
|
|
|
EVENT_HOMEASSISTANT_START)
|
2017-04-25 10:40:13 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-04-24 20:01:00 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'SSL Certificate Expiry'
|
|
|
|
DEFAULT_PORT = 443
|
|
|
|
|
2017-08-13 06:49:15 +00:00
|
|
|
SCAN_INTERVAL = timedelta(hours=12)
|
2017-04-25 10:40:13 +00:00
|
|
|
|
2017-04-24 20:01:00 +00:00
|
|
|
TIMEOUT = 10.0
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2017-04-25 10:40:13 +00:00
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
2017-04-24 20:01:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-25 10:40:13 +00:00
|
|
|
"""Set up certificate expiry sensor."""
|
2017-08-13 06:49:15 +00:00
|
|
|
def run_setup(event):
|
|
|
|
"""Wait until Home Assistant is fully initialized before creating.
|
2017-04-25 10:40:13 +00:00
|
|
|
|
2017-08-13 06:49:15 +00:00
|
|
|
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)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([SSLCertificate(sensor_name, server_name, server_port)],
|
|
|
|
True)
|
2017-08-13 06:49:15 +00:00
|
|
|
|
|
|
|
# To allow checking of the HA certificate we must first be running.
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, run_setup)
|
2017-04-24 20:01:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SSLCertificate(Entity):
|
2017-04-25 10:40:13 +00:00
|
|
|
"""Implementation of the certificate expiry sensor."""
|
2017-04-24 20:01:00 +00:00
|
|
|
|
|
|
|
def __init__(self, sensor_name, server_name, server_port):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.server_name = server_name
|
|
|
|
self.server_port = server_port
|
|
|
|
self._name = sensor_name
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit this state is expressed in."""
|
|
|
|
return 'days'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return 'mdi:certificate'
|
|
|
|
|
|
|
|
def update(self):
|
2017-04-25 10:40:13 +00:00
|
|
|
"""Fetch the certificate information."""
|
2017-04-24 20:01:00 +00:00
|
|
|
try:
|
|
|
|
ctx = ssl.create_default_context()
|
2017-04-25 10:40:13 +00:00
|
|
|
sock = ctx.wrap_socket(
|
|
|
|
socket.socket(), server_hostname=self.server_name)
|
2017-04-24 20:01:00 +00:00
|
|
|
sock.settimeout(TIMEOUT)
|
|
|
|
sock.connect((self.server_name, self.server_port))
|
|
|
|
except socket.gaierror:
|
2017-04-25 10:40:13 +00:00
|
|
|
_LOGGER.error("Cannot resolve hostname: %s", self.server_name)
|
|
|
|
return
|
2017-04-24 20:01:00 +00:00
|
|
|
except socket.timeout:
|
2017-04-25 10:40:13 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Connection timeout with server: %s", self.server_name)
|
|
|
|
return
|
|
|
|
except OSError:
|
|
|
|
_LOGGER.error("Cannot connect to %s", self.server_name)
|
|
|
|
return
|
2017-04-24 20:01:00 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
cert = sock.getpeercert()
|
2017-04-25 10:40:13 +00:00
|
|
|
except OSError:
|
|
|
|
_LOGGER.error("Cannot fetch certificate from %s", self.server_name)
|
|
|
|
return
|
2017-04-24 20:01:00 +00:00
|
|
|
|
|
|
|
ts_seconds = ssl.cert_time_to_seconds(cert['notAfter'])
|
2017-08-13 06:49:15 +00:00
|
|
|
timestamp = datetime.fromtimestamp(ts_seconds)
|
|
|
|
expiry = timestamp - datetime.today()
|
2017-04-24 20:01:00 +00:00
|
|
|
self._state = expiry.days
|