Skip to content

Commit

Permalink
Merge pull request #62 from jasonacox/v0.7.3
Browse files Browse the repository at this point in the history
v0.7.3 - Cloud Mode Setup
  • Loading branch information
jasonacox authored Dec 31, 2023
2 parents 4587549 + f27313b commit 20e4ec0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# RELEASE NOTES

## v0.7.3 - Cloud Mode Setup

* Setup will now check for `PW_AUTH_PATH` environmental variable to set the path for `.pypowerwall.auth` and `.pypowerwall.site` by @mcbirse in https://github.com/jasonacox/pypowerwall/pull/62
* Proxy t37 - Move signal handler to capture SIGTERM when proxy halts due to config error by @mcbirse in https://github.com/jasonacox/pypowerwall/pull/62. This ensures a containerized proxy will exit without delay when stopping or restarting the container.

## 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.
Expand Down
2 changes: 1 addition & 1 deletion proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.10-alpine
WORKDIR /app
RUN pip3 install pypowerwall==0.7.2 bs4
RUN pip3 install pypowerwall==0.7.3 bs4
COPY . .
CMD ["python3", "server.py"]
EXPOSE 8675
22 changes: 14 additions & 8 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 = "t36"
BUILD = "t37"
ALLOWLIST = [
'/api/status', '/api/site_info/site_name', '/api/meters/site',
'/api/meters/solar', '/api/sitemaster', '/api/powerwalls',
Expand Down Expand Up @@ -118,6 +118,11 @@
(pypowerwall.version, BUILD, httptype, port))
log.info("pyPowerwall Proxy Started")

# Signal handler - Exit on SIGTERM
def sigTermHandle(signum, frame):
raise SystemExit
signal.signal(signal.SIGTERM, sigTermHandle)

# Get Value Function - Key to Value or Return Null
def get_value(a, key):
if key in a:
Expand All @@ -135,7 +140,10 @@ def get_value(a, key):
log.error(e)
log.error("Fatal Error: Unable to connect. Please fix config and restart.")
while True:
time.sleep(5) # Infinite loop to keep container running
try:
time.sleep(5) # Infinite loop to keep container running
except (KeyboardInterrupt, SystemExit):
os._exit(0)
if pw.cloudmode:
log.info("pyPowerwall Proxy Server - Cloud Mode")
log.info("Connected to Site ID %s (%s)" % (pw.Tesla.siteid, pw.site_name()))
Expand All @@ -144,7 +152,10 @@ def get_value(a, key):
if not pw.Tesla.change_site(siteid):
log.error("Fatal Error: Unable to connect. Please fix config and restart.")
while True:
time.sleep(5) # Infinite loop to keep container running
try:
time.sleep(5) # Infinite loop to keep container running
except (KeyboardInterrupt, SystemExit):
os._exit(0)
else:
log.info("pyPowerwall Proxy Server - Local Mode")
log.info("Connected to Energy Gateway %s (%s)" % (host, pw.site_name()))
Expand Down Expand Up @@ -427,11 +438,6 @@ def do_GET(self):
except:
log.error("Socket broken sending response [doGET]")

def sigTermHandle(signum, frame):
raise SystemExit

signal.signal(signal.SIGTERM, sigTermHandle)

with ThreadingHTTPServer((bind_address, port), handler) as server:
if(https_mode == "yes"):
# Activate HTTPS
Expand Down
2 changes: 1 addition & 1 deletion pypowerwall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
from . import tesla_pb2 # Protobuf definition for vitals
from . import cloud # Tesla Cloud API

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

Expand Down
4 changes: 3 additions & 1 deletion pypowerwall/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
# Modules
import pypowerwall
import sys
import os
from . import scan
from . import cloud

# Global Variables
AUTHFILE = ".pypowerwall.auth"
authpath = os.getenv("PW_AUTH_PATH", "")
timeout = 1.0
state = 0
color = True
Expand Down Expand Up @@ -46,7 +48,7 @@
if(state == 1):
print("pyPowerwall [%s] - Cloud Mode Setup\n" % (pypowerwall.version))
# Run Setup
c = cloud.TeslaCloud(None)
c = cloud.TeslaCloud(None, authpath=authpath)
if c.setup():
print("Setup Complete. Auth file %s ready to use." % (AUTHFILE))
else:
Expand Down
20 changes: 10 additions & 10 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, 3)
version_tuple = (0, 0, 4)
version = __version__ = '%d.%d.%d' % version_tuple
__author__ = 'jasonacox'

Expand Down Expand Up @@ -813,9 +813,9 @@ def setup(self):
print("-" * 60)
tuser = ""
# Check for .pypowerwall.auth file
if os.path.isfile(AUTHFILE):
print(" Found existing Tesla Cloud setup file ({})".format(AUTHFILE))
with open(AUTHFILE) as json_file:
if os.path.isfile(self.authfile):
print(" Found existing Tesla Cloud setup file ({})".format(self.authfile))
with open(self.authfile) as json_file:
try:
data = json.load(json_file)
tuser = list(data.keys())[0]
Expand All @@ -824,7 +824,7 @@ def setup(self):
response = input("\n Overwrite existing file? [y/N]: ").strip()
if response.lower() == "y":
tuser = ""
os.remove(AUTHFILE)
os.remove(self.authfile)
else:
self.email = tuser
except Exception as err:
Expand All @@ -844,7 +844,7 @@ def setup(self):
self.email = tuser

# Create Tesla instance
tesla = Tesla(self.email, cache_file=AUTHFILE)
tesla = Tesla(self.email, cache_file=self.authfile)

if not tesla.authorized:
# Login to Tesla account and cache token
Expand All @@ -860,7 +860,7 @@ def setup(self):
print("\nAfter login, paste the URL of the 'Page Not Found' webpage below.\n")

tesla.close()
tesla = Tesla(self.email, state=state, code_verifier=code_verifier, cache_file=AUTHFILE)
tesla = Tesla(self.email, state=state, code_verifier=code_verifier, cache_file=self.authfile)

if not tesla.authorized:
try:
Expand All @@ -885,8 +885,8 @@ def setup(self):
print("-"*60)

# Check for existing site file
if os.path.isfile(SITEFILE):
with open(SITEFILE) as file:
if os.path.isfile(self.sitefile):
with open(self.sitefile) as file:
try:
self.siteid = int(file.read())
except:
Expand Down Expand Up @@ -922,7 +922,7 @@ def setup(self):
self.site = sites[self.siteindex]
print("\nSelected site %d - %s (%s)" % (self.siteindex+1, sites[self.siteindex]["site_name"], self.siteid))
# Write the site id to the sitefile
with open(SITEFILE, "w") as f:
with open(self.sitefile, "w") as f:
f.write(str(self.siteid))

return True
Expand Down

0 comments on commit 20e4ec0

Please sign in to comment.