Skip to content

Commit

Permalink
rework Alpha 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuen Lee committed Dec 19, 2023
1 parent 01aab31 commit f478e17
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 192 deletions.
4 changes: 2 additions & 2 deletions custom_components/polestar_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from aiohttp import ClientConnectionError
from async_timeout import timeout

from .polestar_api import PolestarApi

from homeassistant.config_entries import ConfigEntry
Expand All @@ -20,7 +21,6 @@

from .const import (
CONF_VIN,
CONF_VCC_API_KEY,
DOMAIN,
TIMEOUT
)
Expand All @@ -43,7 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b

_LOGGER.debug("async_setup_entry: %s", config_entry)
polestarApi = PolestarApi(
hass, conf[CONF_USERNAME], conf[CONF_PASSWORD], conf[CONF_VIN], conf[CONF_VCC_API_KEY])
hass, conf[CONF_USERNAME], conf[CONF_PASSWORD], conf[CONF_VIN])
await polestarApi.init()

hass.data.setdefault(DOMAIN, {})
Expand Down
20 changes: 8 additions & 12 deletions custom_components/polestar_api/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .polestar import PolestarApi

from .const import CONF_VIN, CONF_VCC_API_KEY, DOMAIN, TIMEOUT
from .const import CONF_VIN, DOMAIN, TIMEOUT

_LOGGER = logging.getLogger(__name__)

Expand All @@ -23,29 +23,26 @@ class FlowHandler(config_entries.ConfigFlow):
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL

async def _create_entry(self, username: str, password: str, vin: str, vcc_api_key: str) -> None:
async def _create_entry(self, username: str, password: str, vin: str) -> None:
"""Register new entry."""
return self.async_create_entry(
title='Polestar EV',
data={
CONF_USERNAME: username,
CONF_PASSWORD: password,
CONF_VIN: vin,
CONF_VCC_API_KEY: vcc_api_key
}
)

async def _create_device(self, username: str, password: str, vin: str, vcc_api_key: str) -> None:
async def _create_device(self, username: str, password: str, vin: str) -> None:
"""Create device."""

try:
device = PolestarApi(
self.hass,
username,
password,
vin,
vcc_api_key
)
vin)
with timeout(TIMEOUT):
await device.init()

Expand All @@ -64,7 +61,7 @@ async def _create_device(self, username: str, password: str, vin: str, vcc_api_k
_LOGGER.exception("Unexpected error creating device")
return self.async_abort(reason="api_failed")

return await self._create_entry(username, password, vin, vcc_api_key)
return await self._create_entry(username, password, vin)

async def async_step_user(self, user_input: dict = None) -> None:
"""User initiated config flow."""
Expand All @@ -73,12 +70,11 @@ async def async_step_user(self, user_input: dict = None) -> None:
step_id="user", data_schema=vol.Schema({
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_VIN): str,
vol.Required(CONF_VCC_API_KEY): str,
vol.Required(CONF_VIN): str
})
)
return await self._create_device(user_input[CONF_USERNAME], user_input[CONF_PASSWORD], user_input[CONF_VIN], user_input[CONF_VCC_API_KEY])
return await self._create_device(user_input[CONF_USERNAME], user_input[CONF_PASSWORD], user_input[CONF_VIN])

async def async_step_import(self, user_input: dict) -> None:
"""Import a config entry."""
return await self._create_device(user_input[CONF_USERNAME], user_input[CONF_PASSWORD], user_input[CONF_VIN], user_input[CONF_VCC_API_KEY])
return await self._create_device(user_input[CONF_USERNAME], user_input[CONF_PASSWORD], user_input[CONF_VIN])
4 changes: 1 addition & 3 deletions custom_components/polestar_api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@


CONF_VIN = "vin"
CONF_VCC_API_KEY = "vcc_api_key"

ACCESS_TOKEN_MANAGER_ID = "JWTh4Yf0b"
GRANT_TYPE = "password"
AUTHORIZATION = "Basic aDRZZjBiOlU4WWtTYlZsNnh3c2c1WVFxWmZyZ1ZtSWFEcGhPc3kxUENhVXNpY1F0bzNUUjVrd2FKc2U0QVpkZ2ZJZmNMeXc="

HEADER_AUTHORIZATION = "authorization"
HEADER_VCC_API_KEY = "vcc-api-key"

CACHE_TIME = 10
CACHE_TIME = 30
Loading

0 comments on commit f478e17

Please sign in to comment.