From 5f910b6d08636f006c224237f7e48bc1813cd35a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 11 May 2020 21:27:26 -0500 Subject: [PATCH] Only load ssl default ssl certificates once (#35) Fixes I/O in the event loop Each update was creating a new ssl context which was doing blocking i/o to load the default ssl certs --- sense_energy/asyncsenseable.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sense_energy/asyncsenseable.py b/sense_energy/asyncsenseable.py index 279051f..2125e4f 100644 --- a/sense_energy/asyncsenseable.py +++ b/sense_energy/asyncsenseable.py @@ -2,6 +2,7 @@ import aiohttp import json import websockets +import ssl from .sense_api import * from .sense_exceptions import * @@ -14,6 +15,8 @@ async def authenticate(self, username, password): "password": password } + self.ssl_context = ssl.create_default_context() + # Get auth token try: async with aiohttp.ClientSession() as session: @@ -45,7 +48,7 @@ async def async_realtime_stream(self, callback=None, single=False): """ Reads realtime data from websocket""" url = WS_URL % (self.sense_monitor_id, self.sense_access_token) # hello, features, [updates,] data - async with websockets.connect(url) as ws: + async with websockets.connect(url, ssl=self.ssl_context) as ws: while True: try: message = await asyncio.wait_for(