Don't stack up error messages, fix link, and ordering (#7291)
This commit is contained in:
parent
28aab33cd1
commit
f29e0bf53e
1 changed files with 30 additions and 25 deletions
|
@ -1,18 +1,20 @@
|
|||
"""Counts the days an HTTPS (TLS) certificate will expire (days).
|
||||
|
||||
For more details about this sensor please refer to the
|
||||
documentation at https://home-assistant.io/components/sensor.cert_expiry
|
||||
"""
|
||||
import logging
|
||||
import ssl
|
||||
import socket
|
||||
Counter for the days till a HTTPS (TLS) certificate will expire.
|
||||
|
||||
For more details about this sensor please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.cert_expiry/
|
||||
"""
|
||||
import datetime
|
||||
import logging
|
||||
import socket
|
||||
import ssl
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.const import (CONF_NAME, CONF_HOST, CONF_PORT)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -20,25 +22,27 @@ DEFAULT_NAME = 'SSL Certificate Expiry'
|
|||
DEFAULT_PORT = 443
|
||||
|
||||
SCAN_INTERVAL = datetime.timedelta(hours=12)
|
||||
|
||||
TIMEOUT = 10.0
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_HOST): cv.string,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup certificate expiry sensor."""
|
||||
"""Set up certificate expiry sensor."""
|
||||
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)])
|
||||
|
||||
add_devices([SSLCertificate(sensor_name, server_name, server_port)], True)
|
||||
|
||||
|
||||
class SSLCertificate(Entity):
|
||||
"""Implements certificate expiry sensor."""
|
||||
"""Implementation of the certificate expiry sensor."""
|
||||
|
||||
def __init__(self, sensor_name, server_name, server_port):
|
||||
"""Initialize the sensor."""
|
||||
|
@ -68,28 +72,29 @@ class SSLCertificate(Entity):
|
|||
return 'mdi:certificate'
|
||||
|
||||
def update(self):
|
||||
"""Fetch certificate information."""
|
||||
"""Fetch the certificate information."""
|
||||
try:
|
||||
ctx = ssl.create_default_context()
|
||||
sock = ctx.wrap_socket(socket.socket(),
|
||||
server_hostname=self.server_name)
|
||||
sock = ctx.wrap_socket(
|
||||
socket.socket(), server_hostname=self.server_name)
|
||||
sock.settimeout(TIMEOUT)
|
||||
sock.connect((self.server_name, self.server_port))
|
||||
except socket.gaierror:
|
||||
_LOGGER.error('Cannot resolve name %s', self.server_name)
|
||||
_LOGGER.error("Cannot resolve hostname: %s", self.server_name)
|
||||
return
|
||||
except socket.timeout:
|
||||
_LOGGER.error('Connection timeout with server %s',
|
||||
self.server_name)
|
||||
except OSError as excp:
|
||||
_LOGGER.error('Cannot connect to %s', self.server_name)
|
||||
raise excp
|
||||
_LOGGER.error(
|
||||
"Connection timeout with server: %s", self.server_name)
|
||||
return
|
||||
except OSError:
|
||||
_LOGGER.error("Cannot connect to %s", self.server_name)
|
||||
return
|
||||
|
||||
try:
|
||||
cert = sock.getpeercert()
|
||||
except OSError as excp:
|
||||
_LOGGER.error('Cannot fetch certificate from %s',
|
||||
(self.server_name))
|
||||
raise excp
|
||||
except OSError:
|
||||
_LOGGER.error("Cannot fetch certificate from %s", self.server_name)
|
||||
return
|
||||
|
||||
ts_seconds = ssl.cert_time_to_seconds(cert['notAfter'])
|
||||
timestamp = datetime.datetime.fromtimestamp(ts_seconds)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue