From cdd78316c440750c7dc4423244f4bd81d6027151 Mon Sep 17 00:00:00 2001
From: Alan Tse <alandtse@users.noreply.github.com>
Date: Wed, 10 Feb 2021 10:01:24 -0800
Subject: [PATCH] Use oauthv3 for Tesla (#45766)

---
 homeassistant/components/tesla/__init__.py    | 4 ++++
 homeassistant/components/tesla/config_flow.py | 2 ++
 homeassistant/components/tesla/manifest.json  | 8 ++++----
 requirements_all.txt                          | 2 +-
 requirements_test_all.txt                     | 2 +-
 tests/components/tesla/test_config_flow.py    | 2 ++
 6 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/homeassistant/components/tesla/__init__.py b/homeassistant/components/tesla/__init__.py
index 51090d34271..8981b269a56 100644
--- a/homeassistant/components/tesla/__init__.py
+++ b/homeassistant/components/tesla/__init__.py
@@ -103,6 +103,8 @@ async def async_setup(hass, base_config):
         _update_entry(
             email,
             data={
+                CONF_USERNAME: email,
+                CONF_PASSWORD: password,
                 CONF_ACCESS_TOKEN: info[CONF_ACCESS_TOKEN],
                 CONF_TOKEN: info[CONF_TOKEN],
             },
@@ -136,6 +138,8 @@ async def async_setup_entry(hass, config_entry):
     try:
         controller = TeslaAPI(
             websession,
+            email=config.get(CONF_USERNAME),
+            password=config.get(CONF_PASSWORD),
             refresh_token=config[CONF_TOKEN],
             access_token=config[CONF_ACCESS_TOKEN],
             update_interval=config_entry.options.get(
diff --git a/homeassistant/components/tesla/config_flow.py b/homeassistant/components/tesla/config_flow.py
index debe896c9cf..683ef314a06 100644
--- a/homeassistant/components/tesla/config_flow.py
+++ b/homeassistant/components/tesla/config_flow.py
@@ -140,6 +140,8 @@ async def validate_input(hass: core.HomeAssistant, data):
         (config[CONF_TOKEN], config[CONF_ACCESS_TOKEN]) = await controller.connect(
             test_login=True
         )
+        config[CONF_USERNAME] = data[CONF_USERNAME]
+        config[CONF_PASSWORD] = data[CONF_PASSWORD]
     except TeslaException as ex:
         if ex.code == HTTP_UNAUTHORIZED:
             _LOGGER.error("Invalid credentials: %s", ex)
diff --git a/homeassistant/components/tesla/manifest.json b/homeassistant/components/tesla/manifest.json
index 3679c0f74d1..9236aae7fb6 100644
--- a/homeassistant/components/tesla/manifest.json
+++ b/homeassistant/components/tesla/manifest.json
@@ -3,11 +3,11 @@
   "name": "Tesla",
   "config_flow": true,
   "documentation": "https://www.home-assistant.io/integrations/tesla",
-  "requirements": ["teslajsonpy==0.10.4"],
+  "requirements": ["teslajsonpy==0.11.5"],
   "codeowners": ["@zabuldon", "@alandtse"],
   "dhcp": [
-     {"hostname":"tesla_*","macaddress":"4CFCAA*"},
-     {"hostname":"tesla_*","macaddress":"044EAF*"},
-     {"hostname":"tesla_*","macaddress":"98ED5C*"}
+    { "hostname": "tesla_*", "macaddress": "4CFCAA*" },
+    { "hostname": "tesla_*", "macaddress": "044EAF*" },
+    { "hostname": "tesla_*", "macaddress": "98ED5C*" }
   ]
 }
diff --git a/requirements_all.txt b/requirements_all.txt
index 202049341e5..d0959d87e8f 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -2184,7 +2184,7 @@ temperusb==1.5.3
 tesla-powerwall==0.3.3
 
 # homeassistant.components.tesla
-teslajsonpy==0.10.4
+teslajsonpy==0.11.5
 
 # homeassistant.components.tensorflow
 # tf-models-official==2.3.0
diff --git a/requirements_test_all.txt b/requirements_test_all.txt
index 143ec43a5d4..ec4bce771f6 100644
--- a/requirements_test_all.txt
+++ b/requirements_test_all.txt
@@ -1105,7 +1105,7 @@ tellduslive==0.10.11
 tesla-powerwall==0.3.3
 
 # homeassistant.components.tesla
-teslajsonpy==0.10.4
+teslajsonpy==0.11.5
 
 # homeassistant.components.toon
 toonapi==0.2.0
diff --git a/tests/components/tesla/test_config_flow.py b/tests/components/tesla/test_config_flow.py
index 7fb308ecc43..136633c9a5c 100644
--- a/tests/components/tesla/test_config_flow.py
+++ b/tests/components/tesla/test_config_flow.py
@@ -48,6 +48,8 @@ async def test_form(hass):
     assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
     assert result2["title"] == "test@email.com"
     assert result2["data"] == {
+        CONF_USERNAME: "test@email.com",
+        CONF_PASSWORD: "test",
         CONF_TOKEN: "test-refresh-token",
         CONF_ACCESS_TOKEN: "test-access-token",
     }
-- 
GitLab