-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,8 +58,9 @@ | |
import logging | ||
import sys | ||
from . import tesla_pb2 # Protobuf definition for vitals | ||
from . import cloud # Tesla Cloud API | ||
|
||
version_tuple = (0, 6, 4) | ||
version_tuple = (0, 7, 0) | ||
version = __version__ = '%d.%d.%d' % version_tuple | ||
__author__ = 'jasonacox' | ||
|
||
|
@@ -86,7 +87,7 @@ class ConnectionError(Exception): | |
pass | ||
|
||
class Powerwall(object): | ||
def __init__(self, host="", password="", email="[email protected]", timezone="America/Los_Angeles", pwcacheexpire=5, timeout=10, poolmaxsize=10): | ||
def __init__(self, host="", password="", email="[email protected]", timezone="America/Los_Angeles", pwcacheexpire=5, timeout=10, poolmaxsize=10, cloudmode=False): | ||
""" | ||
Represents a Tesla Energy Gateway Powerwall device. | ||
|
@@ -99,6 +100,7 @@ def __init__(self, host="", password="", email="[email protected]", timezone="A | |
pwcacheexpire = Seconds to expire cached entries | ||
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 connection (default is False) | ||
""" | ||
|
||
|
@@ -114,6 +116,18 @@ def __init__(self, host="", password="", email="[email protected]", timezone="A | |
self.pwcachetime = {} # holds the cached data timestamps for api | ||
self.pwcache = {} # holds the cached data for api | ||
self.pwcacheexpire = pwcacheexpire # seconds to expire cache | ||
self.cloudmode = cloudmode # cloud mode (default) or local mode | ||
self.Tesla = False # cloud object for cloud connection | ||
|
||
# Check for cloud mode | ||
if self.cloudmode or self.host == "": | ||
log.debug('Tesla cloud mode enabled') | ||
self.Tesla = cloud.TeslaCloud(self.email, pwcacheexpire, timeout) | ||
# 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" | ||
log.debug(err) | ||
raise ConnectionError(err) | ||
|
||
if self.poolmaxsize > 0: | ||
# Create session object for http connection re-use | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
""" | ||
import sys | ||
import os | ||
import time | ||
import logging | ||
import json | ||
|
@@ -622,6 +623,9 @@ def setup(self): | |
TUSER = response | ||
break | ||
|
||
# Update the Tesla User | ||
self.email = TUSER | ||
|
||
# Create retry instance for use after successful login | ||
retry = Retry(total=2, status_forcelist=(500, 502, 503, 504), backoff_factor=10) | ||
|
||
|
@@ -655,39 +659,59 @@ def setup(self): | |
tesla.close() | ||
tesla = Tesla(self.email, retry=retry, cache_file=AUTHFILE) | ||
|
||
if __name__ == "__main__": | ||
|
||
# Test code | ||
set_debug(False) | ||
cloud = TeslaCloud("[email protected]") | ||
|
||
if not cloud.connect(): | ||
print("Failed to connect to Tesla Cloud") | ||
cloud.setup() | ||
if not cloud.connect(): | ||
print("Failed to connect to Tesla Cloud") | ||
exit(1) | ||
|
||
print("Connected to Tesla Cloud") | ||
|
||
#print("\nSite Data") | ||
#sites = cloud.getsites() | ||
#print(sites) | ||
|
||
#print("\Battery") | ||
#r = cloud.get_battery() | ||
#print(r) | ||
# Test code | ||
set_debug(False) | ||
# Check for .pypowerwall.auth file | ||
if os.path.isfile(AUTHFILE): | ||
# Read the json file | ||
with open(AUTHFILE) as json_file: | ||
try: | ||
data = json.load(json_file) | ||
TUSER = list(data.keys())[0] | ||
print(f"Using Tesla User: {TUSER}") | ||
except Exception as err: | ||
TUSER = None | ||
|
||
#print("\Site Power") | ||
#r = cloud.get_site_power() | ||
#print(r) | ||
while not TUSER: | ||
response = input("Tesla User Email address: ").strip() | ||
if "@" not in response: | ||
print("Invalid email address\n") | ||
else: | ||
TUSER = response | ||
break | ||
|
||
#print("\Site Config") | ||
#r = cloud.get_site_config() | ||
#print(r) | ||
cloud = TeslaCloud(TUSER) | ||
|
||
# Test Poll | ||
items = ['/api/status','/api/system_status/grid_status','/api/site_info/site_name','/api/devices/vitals','/api/system_status/soe','/api/meters/aggregates','/api/operation','/api/system_status'] #, '/api/logout','/api/login/Basic','/vitals','/api/meters/site','/api/meters/solar','/api/sitemaster','/api/powerwalls','/api/installer','/api/customer/registration','/api/system/update/status','/api/site_info','/api/system_status/grid_faults','/api/site_info/grid_codes','/api/solars','/api/solars/brands','/api/customer','/api/meters','/api/installer','/api/networks','/api/system/networks','/api/meters/readings','/api/synchrometer/ct_voltage_references'] | ||
for i in items: | ||
print(f"poll({i}):") | ||
print(cloud.poll(i)) | ||
print("\n") | ||
if not cloud.connect(): | ||
print("Failed to connect to Tesla Cloud") | ||
cloud.setup() | ||
if not cloud.connect(): | ||
print("Failed to connect to Tesla Cloud") | ||
exit(1) | ||
|
||
print("Connected to Tesla Cloud") | ||
|
||
#print("\nSite Data") | ||
#sites = cloud.getsites() | ||
#print(sites) | ||
|
||
#print("\Battery") | ||
#r = cloud.get_battery() | ||
#print(r) | ||
|
||
#print("\Site Power") | ||
#r = cloud.get_site_power() | ||
#print(r) | ||
|
||
#print("\Site Config") | ||
#r = cloud.get_site_config() | ||
#print(r) | ||
|
||
# Test Poll | ||
items = ['/api/status','/api/system_status/grid_status','/api/site_info/site_name','/api/devices/vitals','/api/system_status/soe','/api/meters/aggregates','/api/operation','/api/system_status'] #, '/api/logout','/api/login/Basic','/vitals','/api/meters/site','/api/meters/solar','/api/sitemaster','/api/powerwalls','/api/installer','/api/customer/registration','/api/system/update/status','/api/site_info','/api/system_status/grid_faults','/api/site_info/grid_codes','/api/solars','/api/solars/brands','/api/customer','/api/meters','/api/installer','/api/networks','/api/system/networks','/api/meters/readings','/api/synchrometer/ct_voltage_references'] | ||
for i in items: | ||
print(f"poll({i}):") | ||
print(cloud.poll(i)) | ||
print("\n") |