Skip to content

Commit

Permalink
Merge pull request #13 from leeyuentuen/dev
Browse files Browse the repository at this point in the history
Merge Dev to master
  • Loading branch information
leeyuentuen authored Dec 19, 2023
2 parents b218d81 + 3401ea4 commit 6e7ec75
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 198 deletions.
47 changes: 8 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,22 @@ Polestar API
This application is not an official app affiliated with Polestar.


## Create a Polestar/Volvo Account
First you need to create a Polestar Developer account on this website:
https://developer.volvocars.com/apis/extended-vehicle/v1/overview/
on your right side you can choose "Sign Up" and just follow the step.


## Create 'vcc_api_key'
Sign into your Polestar Account and go to your account:
https://developer.volvocars.com/account/
Create a new Application. In my case it call 'My Polestar'

![image](https://github.com/leeyuentuen/polestar_api/assets/1487966/1e4694fe-90ee-4915-b198-55d6b084dc50)

After create, you will see 2 vcc_api_key

![image](https://github.com/leeyuentuen/polestar_api/assets/1487966/b660dffb-096d-4a15-afaa-7213fff24359)
## Use your Polestar account
This is the account that you also use in the polestar APP on your mobile phone
try here to login if it works or not:
https://polestarid.eu.polestar.com/PolestarLogin/login


## Add in HA Integration
Add custom repository in HACS: https://github.com/leeyuentuen/polestar_api
Search for integration 'polestar_api'

and fill the information:
email and password: are these from your polestar developer account
email and password: this are the credential to login on your polestar account
VIN: is the car identification number that you can find in your polestar app or polestar account
VCC api key: the key above that you have generate (i've take the first one)

issue with create account? these are the steps: Thanks to @Ottmar0815: https://github.com/leeyuentuen/polestar_api/issues/3#issuecomment-1817916621
```
Steps:
1. Generated a Volvo-ID with your Link https://volvoid.eu.volvocars.com/VolvoLogin/login
2. Generated a Developer-Account with Google-Login.
3. Under developer account generate the app.
4. Get the 'vcc api key'.
5. Used HACS, add the repository and install the HACS-Integration.
6. Go to HomeAssistant integration and add polestar_api integration
7. Use the Volvo-ID credentials, your VIN and the developer 'vcc api key' at integration-setup in HomeAssistant.
Now Data are show instantly.
```




![image](https://github.com/leeyuentuen/polestar_api/assets/1487966/11d7586b-9d88-4b65-bd2b-0c5f66ff52fa)

![image](https://github.com/leeyuentuen/polestar_api/assets/1487966/a8ae1b78-912b-40b5-9498-2534b07f4200)

![image](https://github.com/leeyuentuen/polestar_api/assets/1487966/30645415-ce93-4c73-ad60-6cbff78e691a)

Please note that the VCC_api_key provided is meant for testing purposes, allowing a limited number of calls, capped at 10,000 per day. I've attempted to restrict the calls in the code to enable caching, aiming to avoid surpassing this limit. However, a challenge with the home assistant integration is the absence of a fixed callback URL for registration
Result:
![image](https://github.com/leeyuentuen/polestar_api/assets/1487966/6805a981-4264-4ede-a331-448599be194a)
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
27 changes: 15 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,31 +23,35 @@ 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()

# check if we have a token, otherwise throw exception
if device.access_token is None:
_LOGGER.exception(
"No token, Could be wrong credentials (invalid email or password))")
return self.async_abort(reason="no_token")

except asyncio.TimeoutError:
return self.async_abort(reason="api_timeout")
except ClientError:
Expand All @@ -57,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 @@ -66,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 = 15
CACHE_TIME = 30
Loading

0 comments on commit 6e7ec75

Please sign in to comment.