Skip to content

Commit

Permalink
v0.1.2 Added Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonacox committed Jan 2, 2022
1 parent 6ed764a commit 24e23fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
14 changes: 14 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# RELEASE NOTES

## v0.1.2 - Error Handling and Proxy Stats

* PyPI 0.1.2
* Added better Error handling for calls to Powerwall with debug info for timeout and connection errors.
* Added more stats to pypowerwall proxy server.py (via URI /stats and /stats/clear)

```
DEBUG:pypowerwall [0.1.2]
DEBUG:loaded auth from cache file .powerwall
DEBUG:Starting new HTTPS connection (1): 10.0.1.2:443
DEBUG:ERROR Timeout waiting for Powerwall API https://10.0.1.2/api/devices/vitals
```

## v0.1.1 - New System Info Functions

* PyPI 0.1.1
Expand Down
33 changes: 24 additions & 9 deletions pypowerwall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import sys
from . import tesla_pb2 # Protobuf definition for vitals

version_tuple = (0, 1, 1)
version_tuple = (0, 1, 2)
version = __version__ = '%d.%d.%d' % version_tuple
__author__ = 'jasonacox'

Expand Down Expand Up @@ -118,7 +118,7 @@ def _close_session(self):
g = requests.get(url, cookies=self.auth, verify=False, timeout=self.timeout)
self.auth = {}

def poll(self, api='/api/site_info/site_name', jsonformat=False, raw=False):
def poll(self, api='/api/site_info/site_name', jsonformat=False, raw=False, recursive=False):
# Query powerwall and return payload as string
# First check to see if in cache
fetch = True
Expand All @@ -132,17 +132,28 @@ def poll(self, api='/api/site_info/site_name', jsonformat=False, raw=False):
fetch = False
if(fetch):
url = "https://%s%s" % (self.host, api)
if(raw):
r = requests.get(url, cookies=self.auth, verify=False, timeout=self.timeout, stream=True)
else:
r = requests.get(url, cookies=self.auth, verify=False, timeout=self.timeout)
if r.status_code >= 400 and r.status_code < 500:
# Session Expired - get a new one
self._get_session()
try:
if(raw):
r = requests.get(url, cookies=self.auth, verify=False, timeout=self.timeout, stream=True)
else:
r = requests.get(url, cookies=self.auth, verify=False, timeout=self.timeout)
except requests.exceptions.Timeout:
log.debug('ERROR Timeout waiting for Powerwall API %s' % url)
return None
except requests.exceptions.ConnectionError:
log.debug('ERROR Unable to connect to Powerwall at %s' % url)
return None
except:
log.debug('ERROR Unknown error connecting to Powerwall at %s' % url)
return None
if r.status_code >= 400 and r.status_code < 500:
# Session Expired - Try to get a new one unless we already tried
if(not recursive):
self._get_session()
return self.poll(api, jsonformat, raw, True)
else:
log.debug('ERROR Unable to establish session with Powerwall at %s - check password' % url)
return None
if(raw):
payload = r.raw.data
else:
Expand Down Expand Up @@ -195,6 +206,8 @@ def _fetchpower(self, sensor, verbose=False):
def vitals(self, jsonformat=False):
# Pull vitals payload - binary protobuf
stream = self.poll('/api/devices/vitals')
if(not stream):
return None

# Protobuf payload processing
pb = tesla_pb2.DevicesWithVitals()
Expand Down Expand Up @@ -244,6 +257,8 @@ def strings(self, jsonformat=False, verbose=False):
devicemap = ['','1','2','3','4','5','6','7','8']
deviceidx = 0
v = self.vitals(jsonformat=False)
if(not v):
return None
for device in v:
if device.split('--')[0] == 'PVAC':
# Check for PVS data
Expand Down

0 comments on commit 24e23fb

Please sign in to comment.