From aa71c8e8f006d7d9879d3bf4335477a9b39152aa Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 10 Jun 2023 12:53:09 -0500 Subject: [PATCH] Reduce I/O from cert_expiry (#94399) --- homeassistant/components/cert_expiry/helper.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/cert_expiry/helper.py b/homeassistant/components/cert_expiry/helper.py index 582c6118f57..0817025c703 100644 --- a/homeassistant/components/cert_expiry/helper.py +++ b/homeassistant/components/cert_expiry/helper.py @@ -1,4 +1,5 @@ """Helper functions for the Cert Expiry platform.""" +from functools import cache import socket import ssl @@ -14,12 +15,18 @@ from .errors import ( ) +@cache +def _get_default_ssl_context(): + """Return the default SSL context.""" + return ssl.create_default_context() + + def get_cert( host: str, port: int, ): """Get the certificate for the host and port combination.""" - ctx = ssl.create_default_context() + ctx = _get_default_ssl_context() address = (host, port) with socket.create_connection(address, timeout=TIMEOUT) as sock, ctx.wrap_socket( sock, server_hostname=address[0]