Skip to content

Commit

Permalink
Provide the ability to override the stored API bearer/refresh tokens …
Browse files Browse the repository at this point in the history
…to bypass authentication issues
  • Loading branch information
ngardiner committed Jan 30, 2021
1 parent 216754f commit 5a7c7f2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ The Do not Charge action states that outside of scheduled or Track Green Energy
* Track Green Energy

This is an option that currently does not operate (and will only set Non-Scheduled Charging rate to 0). In future, this will allow continuing of Track Green Energy behaviour outside of the hard-coded daylight hours (6am - 8pm).

### Manual Tesla API key override

In some instances, you may prefer to obtain the Tesla API keys yourself. The main benefit of this approach is that you do not need to provide your Tesla username or password to TWCManager.

Another reason to use this feature might be as a temporary workaround if the Tesla authentication flow is changed or the TWCManager authentication function is faulty.

Note: Providing your Tesla username and password to TWCManager to automatically fetch your Tesla API access and refresh tokens does not put your credentials at significant risk as they are only used once to fetch the token before being destroyed, however there may nonetheless be a preference not to provide these credentials at all.

To obtain the key, you will need some knowledge of the Tesla API authentication flow. To assist with this, a <a href="http://registration.teslatasks.com/generateTokens">link</a> to a service which can assist you with this process is provided, however this does therefore require you to provide your credentials to that service. Otherwise, you may want to research the Tesla authentication flow and obtain the tokens yourself, or to obtain them from another application that you have previously authenticated to.

Providing any value for the Access or Refresh tokens will result in the current stored tokens being overridden with the value you supply. We don't perform any validation of the tokens and the previous values are lost. Back up your settings.json file prior to entering your token manually if you need to revert your settings.
16 changes: 15 additions & 1 deletion lib/TWCManager/Control/HTTPControl.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,22 @@ def process_save_schedule(self):

def process_save_settings(self):

# Write settings
# This function will write the settings submitted from the settings
# page to the settings dict, before triggering a write of the settings
# to file
for key in self.fields:

# If the key relates to the car API tokens, we need to pass these
# to the appropriate module, rather than directly updating the
# configuration file (as it would just be overwritten)
if (key == "carApiBearerToken" or key == "carApiRefreshToken") and self.getFieldValue(key) != "":
carapi = master.getModuleByName("TeslaAPI")
if key == "carApiBearerToken":
carapi.setCarApiBearerToken(self.getFieldValue(key))
elif key == "carApiRefreshToken":
carapi.setCarApiRefreshToken(self.getFieldValue(key))

# Write setting to dictionary
master.settings[key] = self.getFieldValue(key)

# If Non-Scheduled power action is either Do not Charge or
Expand Down
15 changes: 15 additions & 0 deletions lib/TWCManager/Control/themes/Default/settings.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@
})|safe }}
</td>
</tr>
<tr>
<th>Manual Tesla API key override (<a href="http://registration.teslatasks.com/generateTokens" target="_blank">link</a>):</th>
<td>
<table>
<tr>
<th>Access Token</th>
<th>Refresh Token</th>
</tr>
<tr>
<td><input type='text' class='form-control' id='carApiBearerToken' name='carApiBearerToken' value='' /></td>
<td><input type='text' class='form-control' id='carApiRefreshToken' name='carApiRefreshToken' value='' /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input class='btn btn-outline-success' type=submit value='Save Settings' /></td>
Expand Down

0 comments on commit 5a7c7f2

Please sign in to comment.