Skip to content

Commit

Permalink
Add option to load previous auth
Browse files Browse the repository at this point in the history
  • Loading branch information
kbickar committed Feb 12, 2022
1 parent 936cb52 commit 688904e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
15 changes: 10 additions & 5 deletions sense_energy/asyncsenseable.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@ def __init__(
api_timeout=API_TIMEOUT,
wss_timeout=WSS_TIMEOUT,
client_session=None,
ssl_verify=True,
ssl_cafile=""
):
"""Init the ASyncSenseable object."""
self._client_session = client_session or aiohttp.ClientSession()
self.ssl_context = None

super().__init__(
username=username,
password=password,
api_timeout=api_timeout,
wss_timeout=wss_timeout,
ssl_verify=ssl_verify,
ssl_cafile=ssl_cafile,
)

async def authenticate(self, username, password, ssl_verify=True, ssl_cafile=""):
auth_data = {"email": username, "password": password}

# Use custom ssl verification, if specified
def set_ssl_context(self, ssl_verify, ssl_cafile):
"""Create or set the SSL context. Use custom ssl verification, if specified."""
if not ssl_verify:
self.ssl_context = ssl.create_default_context()
self.ssl_context.check_hostname = False
Expand All @@ -41,6 +42,10 @@ async def authenticate(self, username, password, ssl_verify=True, ssl_cafile="")
self.ssl_context = ssl.create_default_context(cafile=ssl_cafile)
else:
self.ssl_context = ssl.create_default_context()

async def authenticate(self, username, password, ssl_verify=True, ssl_cafile=""):
auth_data = {"email": username, "password": password}
self.set_ssl_context(ssl_verify, ssl_cafile)

# Get auth token
async with self._client_session.post(
Expand Down
13 changes: 12 additions & 1 deletion sense_energy/sense_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
class SenseableBase(object):

def __init__(self, username=None, password=None,
api_timeout=API_TIMEOUT, wss_timeout=WSS_TIMEOUT):
api_timeout=API_TIMEOUT, wss_timeout=WSS_TIMEOUT,
ssl_verify=True,
ssl_cafile=""):

# Timeout instance variables
self.api_timeout = api_timeout
Expand All @@ -32,9 +34,18 @@ def __init__(self, username=None, password=None,
self._trend_data = {}
self._monitor = {}
for scale in valid_scales: self._trend_data[scale] = {}
self.set_ssl_context(ssl_verify, ssl_cafile)

if username and password:
self.authenticate(username, password)

def load_auth(self, access_token, user_id, monitor_id):
data = {
'access_token': access_token,
'user_id': user_id,
'monitors': [{'id':monitor_id}]
}
self.set_auth_data(data)

def set_auth_data(self, data):
self.sense_access_token = data['access_token']
Expand Down
14 changes: 8 additions & 6 deletions sense_energy/senseable.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

class Senseable(SenseableBase):

def set_ssl_context(self, ssl_verify, ssl_cafile):
"""Create or set the SSL context. Use custom ssl verification, if specified."""
if not ssl_verify:
self.s.verify = False
elif ssl_cafile:
self.s.verify = ssl_cafile

def authenticate(self, username, password, ssl_verify=True, ssl_cafile=''):
auth_data = {
"email": username,
Expand All @@ -18,12 +25,7 @@ def authenticate(self, username, password, ssl_verify=True, ssl_cafile=''):

# Create session
self.s = requests.session()

# Use custom ssl verification, if specified
if not ssl_verify:
self.s.verify = False
elif ssl_cafile:
self.s.verify = ssl_cafile
self.set_ssl_context(ssl_verify, ssl_cafile)

# Get auth token
try:
Expand Down

0 comments on commit 688904e

Please sign in to comment.