Skip to content

Commit

Permalink
v0.7.2 - Add cloud mode configuration setting (#61)
Browse files Browse the repository at this point in the history
* Add authpath config

* Update docs

* Release notes
  • Loading branch information
jasonacox authored Dec 30, 2023
1 parent 8e87761 commit 24b40f8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ and call function to poll data. Here is an example:
set_debug(True, color=True)
Classes
Powerwall(host, password, email, timezone, pwcacheexpire, timeout, poolmaxsize)
Powerwall(host, password, email, timezone, pwcacheexpire, timeout, poolmaxsize,
cloudmode, siteid, authpath)
Parameters
host # Hostname or IP of the Tesla gateway
Expand All @@ -133,6 +134,8 @@ and call function to poll data. Here is an example:
poolmaxsize = 10 # Pool max size for http connection re-use (persistent
connections disabled if zero)
cloudmode = False # If True, use Tesla cloud for data (default is False)
siteid # If cloudmode is True, use this siteid (default is None)
authpath # Path to cloud auth and site cache files (default is "")
Functions
poll(api, json, force) # Return data from Powerwall api (dict if json=True, bypass cache force=True)
Expand Down
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.7.2 - Cloud Auth Path

* Add pypowerwall setting to define path to cloud auth cache and site files in the initialization. It will default to current directory.
* Add pypowerwall setting to define energy site id in the initialization. It will default to None.

```python
import pypowerwall

pw = pypowerwall.Powerwall(email="email@example.com",cloudmode=True,siteid=1234567,authpath=".auth")
```

* Proxy will now use `PW_AUTH_PATH` as an environmental variable to set the path for `.pypowerwall.auth` and `.pypowerwall.site`.
* Proxy also has `PW_SITEID` as an environmental variable to set `siteid`.

## v0.7.1 - Tesla Cloud Mode

* Simulate Powerwall Energy Gateway via Tesla Cloud API calls. In `cloudmode` API calls to pypowerwall APIs will result in calls made to the Tesla API to fetch the data.
Expand Down
4 changes: 4 additions & 0 deletions proxy/RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## pyPowerwall Proxy Release Notes

### Proxy t36 (30 Dec 2023)

* Add `PW_AUTH_PATH` to set location for cloud auth and site files.

### Proxy t35 (29 Dec 2023)

* Add `cloudmode` support for pypowerwall v0.7.1.
Expand Down
8 changes: 5 additions & 3 deletions proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import ssl
from transform import get_static, inject_js

BUILD = "t35"
BUILD = "t36"
ALLOWLIST = [
'/api/status', '/api/site_info/site_name', '/api/meters/site',
'/api/meters/solar', '/api/sitemaster', '/api/powerwalls',
Expand Down Expand Up @@ -71,6 +71,7 @@
port = int(os.getenv("PW_PORT", "8675"))
style = os.getenv("PW_STYLE", "clear") + ".js"
siteid = os.getenv("PW_SITEID", None)
authpath = os.getenv("PW_AUTH_PATH", "")

# Global Stats
proxystats = {}
Expand All @@ -86,7 +87,7 @@
proxystats['mem'] = 0
proxystats['site_name'] = ""
proxystats['cloudmode'] = False
proxystats['siteid'] = 0
proxystats['siteid'] = None
proxystats['counter'] = 0

if https_mode == "yes":
Expand Down Expand Up @@ -128,7 +129,8 @@ def get_value(a, key):
# Connect to Powerwall
# TODO: Add support for multiple Powerwalls
try:
pw = pypowerwall.Powerwall(host,password,email,timezone,cache_expire,timeout,pool_maxsize)
pw = pypowerwall.Powerwall(host,password,email,timezone,cache_expire,
timeout,pool_maxsize,siteid=siteid,authpath=authpath)
except Exception as e:
log.error(e)
log.error("Fatal Error: Unable to connect. Please fix config and restart.")
Expand Down
17 changes: 13 additions & 4 deletions pypowerwall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* Can use Tesla Cloud API instead of local Powerwall Gateway (if enabled)
Classes
Powerwall(host, password, email, timezone, pwcacheexpire, timeout, poolmaxsize, cloudmode)
Powerwall(host, password, email, timezone, pwcacheexpire, timeout, poolmaxsize,
cloudmode, siteid, authpath)
Parameters
host # Hostname or IP of the Tesla gateway
Expand All @@ -27,6 +28,8 @@
poolmaxsize = 10 # Pool max size for http connection re-use (persistent
connections disabled if zero)
cloudmode = False # If True, use Tesla cloud for data (default is False)
siteid = None # If cloudmode is True, use this siteid (default is None)
authpath = "" # Path to cloud auth and site files (default current directory)
Functions
poll(api, json, force) # Return data from Powerwall api (dict if json=True, bypass cache force=True)
Expand Down Expand Up @@ -69,7 +72,7 @@
from . import tesla_pb2 # Protobuf definition for vitals
from . import cloud # Tesla Cloud API

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

Expand All @@ -96,7 +99,9 @@ class ConnectionError(Exception):
pass

class Powerwall(object):
def __init__(self, host="", password="", email="nobody@nowhere.com", timezone="America/Los_Angeles", pwcacheexpire=5, timeout=5, poolmaxsize=10, cloudmode=False):
def __init__(self, host="", password="", email="nobody@nowhere.com",
timezone="America/Los_Angeles", pwcacheexpire=5, timeout=5, poolmaxsize=10,
cloudmode=False, siteid=None, authpath=""):
"""
Represents a Tesla Energy Gateway Powerwall device.
Expand All @@ -110,6 +115,8 @@ def __init__(self, host="", password="", email="nobody@nowhere.com", timezone="A
timeout = Seconds for the timeout on http requests
poolmaxsize = Pool max size for http connection re-use (persistent connections disabled if zero)
cloudmode = If True, use Tesla cloud for data (default is False)
siteid = If cloudmode is True, use this siteid (default is None)
authpath = Path to cloud auth and site cache files (default current directory)
"""

Expand All @@ -126,13 +133,15 @@ def __init__(self, host="", password="", email="nobody@nowhere.com", timezone="A
self.pwcache = {} # holds the cached data for api
self.pwcacheexpire = pwcacheexpire # seconds to expire cache
self.cloudmode = cloudmode # cloud mode or local mode (default)
self.siteid = siteid # siteid for cloud mode
self.authpath = authpath # path to auth and site cache files
self.Tesla = None # cloud object for cloud connection

# Check for cloud mode
if self.cloudmode or self.host == "":
self.cloudmode = True
log.debug('Tesla cloud mode enabled')
self.Tesla = cloud.TeslaCloud(self.email, pwcacheexpire, timeout)
self.Tesla = cloud.TeslaCloud(self.email, pwcacheexpire, timeout, siteid, authpath)
# Check to see if we can connect to the cloud
if not self.Tesla.connect():
err = "Unable to connect to Tesla Cloud - run pypowerwall setup"
Expand Down
15 changes: 9 additions & 6 deletions pypowerwall/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
SITE_CONFIG_TTL = 59 # Site config cache TTL in seconds

# pypowerwall cloud module version
version_tuple = (0, 0, 2)
version_tuple = (0, 0, 3)
version = __version__ = '%d.%d.%d' % version_tuple
__author__ = 'jasonacox'

Expand Down Expand Up @@ -82,9 +82,7 @@ def lookup(data, keylist):
return data

class TeslaCloud:
def __init__(self, email, pwcacheexpire=5, timeout=5, siteid=None):
self.authfile = AUTHFILE
self.sitefile = SITEFILE
def __init__(self, email, pwcacheexpire=5, timeout=5, siteid=None, authpath=""):
self.email = email
self.timeout = timeout
self.site = None
Expand All @@ -96,6 +94,10 @@ def __init__(self, email, pwcacheexpire=5, timeout=5, siteid=None):
self.siteindex = 0 # site index to use
self.siteid = siteid # site id to use
self.counter = 0 # counter for SITE_DATA API
self.authpath = authpath # path to cloud auth and site files

self.authfile = os.path.join(authpath, AUTHFILE)
self.sitefile = os.path.join(authpath, SITEFILE)

if self.siteid is None:
# Check for site file
Expand All @@ -107,7 +109,8 @@ def __init__(self, email, pwcacheexpire=5, timeout=5, siteid=None):
self.siteid = 0
else:
self.siteindex = 0

log.debug(f" -- cloud: Using site {self.siteid} for {self.email}")

# Check for auth file
if not os.path.exists(self.authfile):
log.debug("WARNING: Missing auth file %s - run setup" % self.authfile)
Expand Down Expand Up @@ -148,7 +151,7 @@ def connect(self):
found = True
break
if not found:
log.error("Site %d not found for %s" % (self.siteid, self.email))
log.error("Site %r not found for %s" % (self.siteid, self.email))
return False
# Set site
self.site = sites[self.siteindex]
Expand Down

0 comments on commit 24b40f8

Please sign in to comment.