Skip to content

Commit

Permalink
Only load ssl default ssl certificates once (#35)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bdraco authored May 12, 2020
1 parent 00ab044 commit 5f910b6
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sense_energy/asyncsenseable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import aiohttp
import json
import websockets
import ssl

from .sense_api import *
from .sense_exceptions import *
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 5f910b6

Please sign in to comment.