Skip to content

Commit

Permalink
Fix errors with site references
Browse files Browse the repository at this point in the history
The change site function, especially the site name reference, can break
users that only have Solar-Only Tesla inverters. Make the two
change_site functions consistent and fix other issues where site_name is
not provided by the Tesla API.
  • Loading branch information
Nexarian committed Nov 26, 2024
1 parent 96dfc6b commit c50c69f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
15 changes: 8 additions & 7 deletions pypowerwall/cloud/pypowerwall_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,14 @@ def change_site(self, siteid):
return False
# Set siteindex - Find siteid in sites
for idx, site in enumerate(sites):
if site['energy_site_id'] == siteid:
self.siteid = siteid
self.siteindex = idx
self.site = sites[self.siteindex]
site_name = sites[self.siteindex].get('site_name') or 'Unknown'
log.debug(f"Changed site to {self.siteid} ({site_name}) for {self.email}")
return True
if site.get('energy_site_id') != siteid:
continue
self.siteid = siteid
self.siteindex = idx
self.site = site
site_name = site.get('site_name', 'Unknown')
log.debug(f"Changed site to {self.siteid} ({site_name}) for {self.email}")
return True
log.error("Site %d not found for %s" % (siteid, self.email))
return False

Expand Down
20 changes: 12 additions & 8 deletions pypowerwall/fleetapi/pypowerwall_fleetapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ def connect(self):
log.error("Site %r not found for %s" % (self.siteid, self.email))
return False
# Set site
self.siteid = sites[self.siteindex].get('energy_site_id')
siteref = sites[self.siteindex]
self.siteid = siteref.get('energy_site_id')
site_name = siteref.get('site_name', 'Unknown')
log.debug(f"Connected to Tesla FleetAPI - Site {self.siteid} "
f"({sites[self.siteindex]['site_name']}) for {self.email}")
f"({site_name}) for {self.email}")
return True

# Function to map Powerwall API to Tesla FleetAPI Data
Expand Down Expand Up @@ -242,12 +244,14 @@ def change_site(self, siteid):
return False
# Set siteindex - Find siteid in sites
for idx, site in enumerate(sites):
if site['energy_site_id'] == siteid:
self.siteid = siteid
self.siteindex = idx
self.siteid = sites[self.siteindex]
log.debug(f"Changed site to {self.siteid} ({sites[self.siteindex]['site_name']}) for {self.email}")
return True
if site.get('energy_site_id') != siteid:
continue
self.siteid = siteid
self.siteindex = idx
self.site = site
site_name = site.get('site_name', 'Unknown')
log.debug(f"Changed site to {self.siteid} ({site_name}) for {self.email}")
return True
log.error("Site %d not found for %s" % (siteid, self.email))
return False

Expand Down

0 comments on commit c50c69f

Please sign in to comment.