Skip to content

Commit

Permalink
Implement login via refresh_token
Browse files Browse the repository at this point in the history
  • Loading branch information
rikroe committed May 9, 2022
1 parent 8e0e47b commit 31ceace
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 31 deletions.
15 changes: 2 additions & 13 deletions custom_components/bmw_connected_drive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_DEVICE_ID,
CONF_ENTITY_ID,
CONF_NAME,
CONF_PASSWORD,
CONF_REGION,
CONF_USERNAME,
Platform,
)
from homeassistant.const import CONF_DEVICE_ID, CONF_ENTITY_ID, CONF_NAME, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -82,10 +74,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Set up one data coordinator per account/config entry
coordinator = BMWDataUpdateCoordinator(
hass,
username=entry.data[CONF_USERNAME],
password=entry.data[CONF_PASSWORD],
region=entry.data[CONF_REGION],
read_only=entry.options[CONF_READ_ONLY],
entry=entry,
)
await coordinator.async_config_entry_first_refresh()

Expand Down
1 change: 1 addition & 0 deletions custom_components/bmw_connected_drive/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
CONF_ALLOWED_REGIONS = ["china", "north_america", "rest_of_world"]
CONF_READ_ONLY = "read_only"
CONF_ACCOUNT = "account"
CONF_REFRESH_TOKEN = "refresh_token"

DATA_HASS_CONFIG = "hass_config"

Expand Down
37 changes: 21 additions & 16 deletions custom_components/bmw_connected_drive/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
from bimmer_connected.api.regions import get_region_from_name
from bimmer_connected.vehicle.models import GPSPosition

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DOMAIN
from .const import CONF_READ_ONLY, CONF_REFRESH_TOKEN, DOMAIN

SCAN_INTERVAL = timedelta(seconds=300)
_LOGGER = logging.getLogger(__name__)
Expand All @@ -23,38 +25,41 @@ class BMWDataUpdateCoordinator(DataUpdateCoordinator):

account: MyBMWAccount

def __init__(
self,
hass: HomeAssistant,
*,
username: str,
password: str,
region: str,
read_only: bool = False,
) -> None:
def __init__(self, hass: HomeAssistant, *, entry: ConfigEntry) -> None:
"""Initialize account-wide BMW data updater."""
self.account = MyBMWAccount(
username,
password,
get_region_from_name(region),
entry.data[CONF_USERNAME],
entry.data[CONF_PASSWORD],
get_region_from_name(entry.data[CONF_REGION]),
observer_position=GPSPosition(hass.config.latitude, hass.config.longitude),
)
self.read_only = read_only
self.read_only = entry.options[CONF_READ_ONLY]
self._entry = entry

self.async_config_entry_first_refresh()
if CONF_REFRESH_TOKEN in entry.data:
self.account.set_refresh_token(entry.data[CONF_REFRESH_TOKEN])

super().__init__(
hass,
_LOGGER,
name=f"{DOMAIN}-{username}",
name=f"{DOMAIN}-{entry.data['username']}",
update_interval=SCAN_INTERVAL,
)

async def _async_update_data(self) -> None:
"""Fetch data from BMW."""
try:
old_refresh_token = self.account.refresh_token
async with async_timeout.timeout(15):
await self.account.get_vehicles()
if self.account.refresh_token != old_refresh_token:
self.hass.config_entries.async_update_entry(
self._entry,
data={
**self._entry.data,
CONF_REFRESH_TOKEN: self.account.refresh_token,
},
)
except OSError as err:
raise UpdateFailed(f"Error communicating with API: {err}") from err

Expand Down
4 changes: 2 additions & 2 deletions custom_components/bmw_connected_drive/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"domain": "bmw_connected_drive",
"name": "BMW Connected Drive",
"documentation": "https://www.home-assistant.io/integrations/bmw_connected_drive",
"requirements": ["bimmer_connected==0.9.0.0b8"],
"requirements": ["bimmer_connected==0.9.0.0b9"],
"codeowners": ["@gerard33", "@rikroe"],
"config_flow": true,
"iot_class": "cloud_polling",
"version": "2022.4.2-custom",
"version": "2022.5.0-custom",
"loggers": ["bimmer_connected"]
}

0 comments on commit 31ceace

Please sign in to comment.