Skip to content

Commit

Permalink
Merge pull request #115 from alandtse/#113
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse authored Dec 8, 2021
2 parents 1c39e63 + cba719f commit 3458fa9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 92 deletions.
20 changes: 3 additions & 17 deletions custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_DOMAIN,
CONF_SCAN_INTERVAL,
CONF_TOKEN,
CONF_USERNAME,
EVENT_HOMEASSISTANT_CLOSE,
)
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.httpx_client import SERVER_SOFTWARE, USER_AGENT
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

from .config_flow import CannotConnect, InvalidAuth, validate_input
from .const import (
Expand All @@ -39,21 +39,6 @@

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_TOKEN): cv.string,
vol.Optional(
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
): vol.All(cv.positive_int, vol.Clamp(min=MIN_SCAN_INTERVAL)),
}
)
},
extra=vol.ALLOW_EXTRA,
)


@callback
def _async_save_tokens(hass, config_entry, access_token, refresh_token, expiration):
Expand Down Expand Up @@ -149,6 +134,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
9 changes: 7 additions & 2 deletions 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_ACCESS_TOKEN] = result[CONF_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 3458fa9

Please sign in to comment.