Skip to content

Commit

Permalink
fix: allow specifying auth_domain
Browse files Browse the repository at this point in the history
This is necessary for China which uses auth.tesla.cn.

closes #113
  • Loading branch information
alandtse committed Dec 8, 2021
1 parent 1c39e63 commit 22817bd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 74 deletions.
3 changes: 3 additions & 0 deletions custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_DOMAIN,
CONF_SCAN_INTERVAL,
CONF_TOKEN,
CONF_USERNAME,
Expand All @@ -21,6 +22,7 @@
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
import httpx
from teslajsonpy import Controller as TeslaAPI
from teslajsonpy.const import AUTH_DOMAIN
from teslajsonpy.exceptions import IncompleteCredentials, TeslaException
import voluptuous as vol

Expand Down Expand Up @@ -149,6 +151,7 @@ async def async_setup_entry(hass, config_entry):
refresh_token=config[CONF_TOKEN],
access_token=config[CONF_ACCESS_TOKEN],
expiration=config.get(CONF_EXPIRATION, 0),
auth_domain=config.get(CONF_DOMAIN, AUTH_DOMAIN),
update_interval=config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
Expand Down
7 changes: 6 additions & 1 deletion custom_components/tesla_custom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from homeassistant import config_entries, core, exceptions
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_DOMAIN,
CONF_SCAN_INTERVAL,
CONF_TOKEN,
CONF_USERNAME,
Expand All @@ -14,6 +15,7 @@
from homeassistant.helpers.httpx_client import SERVER_SOFTWARE, USER_AGENT
import httpx
from teslajsonpy import Controller as TeslaAPI, TeslaException
from teslajsonpy.const import AUTH_DOMAIN
from teslajsonpy.exceptions import IncompleteCredentials
import voluptuous as vol

Expand Down Expand Up @@ -97,6 +99,7 @@ def _async_schema(self):
{
vol.Required(CONF_USERNAME, default=self.username): str,
vol.Required(CONF_TOKEN): str,
vol.Required(CONF_DOMAIN, default=AUTH_DOMAIN): str,
}
)

Expand Down Expand Up @@ -155,13 +158,15 @@ async def validate_input(hass: core.HomeAssistant, data):
email=data[CONF_USERNAME],
refresh_token=data[CONF_TOKEN],
update_interval=DEFAULT_SCAN_INTERVAL,
expiration=config.get(CONF_EXPIRATION, 0),
expiration=data.get(CONF_EXPIRATION, 0),
auth_domain=data.get(CONF_DOMAIN, AUTH_DOMAIN),
)
result = await controller.connect(test_login=True)
config[CONF_TOKEN] = result["refresh_token"]
config[CONF_ACCESS_TOKEN] = result["access_token"]
config[CONF_EXPIRATION] = result[CONF_EXPIRATION]
config[CONF_USERNAME] = data[CONF_USERNAME]
config[CONF_DOMAIN] = data.get(CONF_DOMAIN, AUTH_DOMAIN)

except IncompleteCredentials as ex:
_LOGGER.error("Authentication error: %s %s", ex.message, ex)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/tesla_custom/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://github.com/alandtse/tesla",
"issue_tracker": "https://github.com/alandtse/tesla/issues",
"requirements": ["teslajsonpy==1.4.0"],
"requirements": ["teslajsonpy==1.4.1"],
"codeowners": ["@alandtse"],
"dependencies": ["http"],
"dhcp": [
Expand Down
98 changes: 30 additions & 68 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ license = "Apache-2.0"

[tool.poetry.dependencies]
python = "^3.9"
teslajsonpy = "^1.4.0"
teslajsonpy = "^1.4.1"

[tool.poetry.dev-dependencies]
homeassistant = "^2021.3.4"
pytest-homeassistant-custom-component = "~=0.3.1"
bandit = "^1.7.0"
black = {version = "^21.12b0", allow-prereleases = true}
mypy = "^0.812"
black = {version = ">=21.12b0", allow-prereleases = true}
mypy = ">=0.812"
pre-commit = "^2.11.1"
pydocstyle = "^6.0.0"
prospector = {extras = ["with_all"], version = "^1.3.1"}
aiohttp_cors = "^0.7.0"
aiohttp_cors = ">=0.7.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
3 changes: 3 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_DOMAIN,
CONF_SCAN_INTERVAL,
CONF_TOKEN,
CONF_USERNAME,
)
from pytest_homeassistant_custom_component.common import MockConfigEntry
from teslajsonpy.const import AUTH_DOMAIN
from teslajsonpy.exceptions import IncompleteCredentials, TeslaException

from custom_components.tesla_custom.const import (
Expand Down Expand Up @@ -63,6 +65,7 @@ async def test_form(hass):
CONF_TOKEN: TEST_TOKEN,
CONF_ACCESS_TOKEN: TEST_ACCESS_TOKEN,
CONF_EXPIRATION: TEST_VALID_EXPIRATION,
CONF_DOMAIN: AUTH_DOMAIN,
}
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
Expand Down

0 comments on commit 22817bd

Please sign in to comment.