1 """Coordinator for BMW."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from bimmer_connected.account
import MyBMWAccount
9 from bimmer_connected.api.regions
import get_region_from_name
10 from bimmer_connected.models
import (
14 MyBMWCaptchaMissingError,
16 from httpx
import RequestError
25 from .const
import CONF_GCID, CONF_READ_ONLY, CONF_REFRESH_TOKEN, DOMAIN, SCAN_INTERVALS
27 _LOGGER = logging.getLogger(__name__)
31 """Class to manage fetching BMW data."""
35 def __init__(self, hass: HomeAssistant, *, entry: ConfigEntry) ->
None:
36 """Initialize account-wide BMW data updater."""
38 entry.data[CONF_USERNAME],
39 entry.data[CONF_PASSWORD],
40 get_region_from_name(entry.data[CONF_REGION]),
41 observer_position=GPSPosition(hass.config.latitude, hass.config.longitude),
44 self.
read_onlyread_only = entry.options[CONF_READ_ONLY]
47 if CONF_REFRESH_TOKEN
in entry.data:
48 self.
accountaccount.set_refresh_token(
49 refresh_token=entry.data[CONF_REFRESH_TOKEN],
50 gcid=entry.data.get(CONF_GCID),
56 name=f
"{DOMAIN}-{entry.data['username']}",
57 update_interval=
timedelta(seconds=SCAN_INTERVALS[entry.data[CONF_REGION]]),
64 """Fetch data from BMW."""
65 old_refresh_token = self.
accountaccount.refresh_token
68 await self.
accountaccount.get_vehicles()
69 except MyBMWCaptchaMissingError
as err:
72 translation_domain=DOMAIN,
73 translation_key=
"missing_captcha",
75 except MyBMWAuthError
as err:
82 except (MyBMWAPIError, RequestError)
as err:
85 if self.
accountaccount.refresh_token != old_refresh_token:
89 """Update or delete the refresh_token in the Config Entry."""
92 CONF_REFRESH_TOKEN: refresh_token,
95 data.pop(CONF_REFRESH_TOKEN)
96 self.
hasshass.config_entries.async_update_entry(self.
_entry_entry, data=data)
None _update_config_entry_refresh_token(self, str|None refresh_token)
None _async_update_data(self)
None __init__(self, HomeAssistant hass, *ConfigEntry entry)
ssl.SSLContext get_default_context()