Skip to content

Commit

Permalink
Add siteid selection to setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonacox committed Dec 24, 2023
1 parent cc0f5da commit d57fdc5
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,4 @@ tools/set-mode.conf
tools/tedapi/request.bin
tools/tedapi/app*
.pypowerwall.auth
.pypowerwall.site
22 changes: 20 additions & 2 deletions proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@
/api/system_status/soe - You can containerize it and run it as
an endpoint for tools like telegraf to pull metrics.
This proxy also supports pyPowerwall data for /vitals and /strings
Local Powerwall Mode
The default mode for this proxy is to connect to a local Powerwall
to pull data. This works with the Tesla Energy Gateway (TEG) for
Powerwall 1, 2 and +. It will also support pulling /vitals and /strings
data if available.
Set: PW_HOST to Powerwall Address and PW_PASSWORD to use this mode.
Cloud Mode
An optional mode is to connect to the Tesla Cloud to pull data. This
requires that you have a Tesla Account and have registered your
Tesla Solar System or Powerwall with the Tesla App. It requires that
you run the setup 'python -m pypowerwall setup' process to create the
required API keys and tokens. This mode doesn't support /vitals or
/strings data.
Set: PW_EMAIL and leave PW_HOST blank to use this mode.
"""
import pypowerwall
Expand Down Expand Up @@ -46,7 +60,7 @@
bind_address = os.getenv("PW_BIND_ADDRESS", "")
password = os.getenv("PW_PASSWORD", "password")
email = os.getenv("PW_EMAIL", "email@example.com")
host = os.getenv("PW_HOST", "hostname")
host = os.getenv("PW_HOST", "")
timezone = os.getenv("PW_TIMEZONE", "America/Los_Angeles")
debugmode = os.getenv("PW_DEBUG", "no")
cache_expire = int(os.getenv("PW_CACHE_EXPIRE", "5"))
Expand All @@ -56,6 +70,7 @@
https_mode = os.getenv("PW_HTTPS", "no")
port = int(os.getenv("PW_PORT", "8675"))
style = os.getenv("PW_STYLE", "clear") + ".js"
siteid = os.getenv("PW_SITEID", None)

# Global Stats
proxystats = {}
Expand Down Expand Up @@ -113,6 +128,9 @@ def get_value(a, key):
os._exit(1)
if pw.cloudmode:
log.info("pyPowerwall Proxy Server - Cloud Mode")
if siteid:
log.info("Switch to Site ID to %s" % siteid)
pw.Tesla.change_site(siteid)
else:
log.info("pyPowerwall Proxy Server - Connected to %s" % host)

Expand Down
39 changes: 4 additions & 35 deletions pypowerwall/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,13 @@

# State 1 = Cloud Mode Setup
if(state == 1):
print("pyPowerwall [%s]\n" % (pypowerwall.version))
print("Cloud Mode Setup\n")

# Check for existing auth file
if os.path.isfile(AUTHFILE):
with open(AUTHFILE) as json_file:
try:
data = json.load(json_file)
tuser = list(data.keys())[0]
except Exception as err:
tuser = None
# Ask to overwrite
print(f"Found {AUTHFILE} configuration file for {tuser}")
answer = input("Overwrite and run setup? (y/n) ")
if answer.lower() == "y":
os.remove(AUTHFILE)
else:
print("Exiting")
exit(0)

print("pyPowerwall [%s] - Cloud Mode Setup\n" % (pypowerwall.version))
# Run Setup
c = cloud.TeslaCloud(None)
c.setup()
tuser = c.email

# Test Connection
print("Testing connection to Tesla Cloud...")
c = cloud.TeslaCloud(tuser)
if c.connect():
print("Connected to Tesla Cloud...")
sites = c.getsites()
print("Found %d Powerwall Sites:" % (len(sites)))
for s in sites:
print(" %s (%s) - Type: %s" % (s["site_name"],
s["energy_site_id"], s["resource_type"]))
print(f"\nSetup Complete. Auth file {AUTHFILE} ready to use.")
if c.setup():
print("Setup Complete. Auth file %s ready to use." % (AUTHFILE))
else:
print("ERROR: Failed to connect to Tesla Cloud")
print("ERROR: Failed to setup Tesla Cloud Mode")
exit(1)

# State 2 = Show Usage
Expand Down
Loading

0 comments on commit d57fdc5

Please sign in to comment.