Skip to content

Commit

Permalink
Fix hass.data storage to add account_id
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Nov 21, 2024
1 parent 3648ba8 commit 0eb82b0
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 26 deletions.
17 changes: 11 additions & 6 deletions custom_components/nissan_connect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,29 @@ async def async_setup(hass, config) -> bool:
async def async_update_listener(hass, entry):
"""Handle options flow credentials update."""
config = entry.data
account_id = config['email']

# Loop each vehicle and update its session with the new credentials
for vehicle in hass.data[DOMAIN][DATA_VEHICLES]:
await hass.async_add_executor_job(hass.data[DOMAIN][DATA_VEHICLES][vehicle].session.login,
for vehicle in hass.data[DOMAIN][account_id][DATA_VEHICLES]:
await hass.async_add_executor_job(hass.data[DOMAIN][account_id][DATA_VEHICLES][vehicle].session.login,
config.get("email"),
config.get("password")
)

# Update intervals for coordinators
hass.data[DOMAIN][DATA_COORDINATOR_STATISTICS].update_interval = timedelta(minutes=config.get("interval_statistics", DEFAULT_INTERVAL_STATISTICS))
hass.data[DOMAIN][DATA_COORDINATOR_FETCH].update_interval = timedelta(minutes=config.get("interval_fetch", DEFAULT_INTERVAL_FETCH))
hass.data[DOMAIN][account_id][DATA_COORDINATOR_STATISTICS].update_interval = timedelta(minutes=config.get("interval_statistics", DEFAULT_INTERVAL_STATISTICS))
hass.data[DOMAIN][account_id][DATA_COORDINATOR_FETCH].update_interval = timedelta(minutes=config.get("interval_fetch", DEFAULT_INTERVAL_FETCH))

# Refresh fetch coordinator
await hass.data[DOMAIN][DATA_COORDINATOR_FETCH].async_refresh()
await hass.data[DOMAIN][account_id][DATA_COORDINATOR_FETCH].async_refresh()


async def async_setup_entry(hass, entry):
"""This is called from the config flow."""
account_id = entry.data['email']

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN].setdefault(account_id, {})

config = dict(entry.data)

Expand All @@ -40,7 +45,7 @@ async def async_setup_entry(hass, entry):
unique_id=entry.unique_id
)

data = hass.data[DOMAIN] = {
data = hass.data[DOMAIN][account_id] = {
DATA_VEHICLES: {}
}

Expand Down
6 changes: 4 additions & 2 deletions custom_components/nissan_connect/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

async def async_setup_entry(hass, config, async_add_entities):
"""Set up the Kamereon sensors."""
data = hass.data[DOMAIN][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][DATA_COORDINATOR_FETCH]
account_id = config.data['email']

data = hass.data[DOMAIN][account_id][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][account_id][DATA_COORDINATOR_FETCH]

entities = []

Expand Down
10 changes: 6 additions & 4 deletions custom_components/nissan_connect/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@


async def async_setup_entry(hass, config, async_add_entities):
data = hass.data[DOMAIN][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][DATA_COORDINATOR_POLL]
coordinator_fetch = hass.data[DOMAIN][DATA_COORDINATOR_FETCH]
stats_coordinator = hass.data[DOMAIN][DATA_COORDINATOR_STATISTICS]
account_id = config.data['email']

data = hass.data[DOMAIN][account_id][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][account_id][DATA_COORDINATOR_POLL]
coordinator_fetch = hass.data[DOMAIN][account_id][DATA_COORDINATOR_FETCH]
stats_coordinator = hass.data[DOMAIN][account_id][DATA_COORDINATOR_STATISTICS]

entities = []

Expand Down
8 changes: 5 additions & 3 deletions custom_components/nissan_connect/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@


async def async_setup_entry(hass, config, async_add_entities):
data = hass.data[DOMAIN][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][DATA_COORDINATOR_FETCH]
account_id = config.data['email']

data = hass.data[DOMAIN][account_id][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][account_id][DATA_COORDINATOR_FETCH]

for vehicle in data:
if Feature.CLIMATE_ON_OFF in data[vehicle].features:
Expand Down Expand Up @@ -117,7 +119,7 @@ async def _async_fetch_loop(self, target_state):

for _ in range(10):
await loop.run_in_executor(None, self.vehicle.refresh)
await self._hass.data[DOMAIN][DATA_COORDINATOR_FETCH].async_refresh()
await self.coordinator.async_refresh()

# We have our update, break out
if target_state == self.vehicle.hvac_status:
Expand Down
13 changes: 8 additions & 5 deletions custom_components/nissan_connect/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def __init__(self, hass, config):
update_interval=timedelta(minutes=config.get("interval_fetch", DEFAULT_INTERVAL_FETCH)),
)
self._hass = hass
self._vehicles = hass.data[DOMAIN][DATA_VEHICLES]
self._account_id = config['email']
self._vehicles = hass.data[DOMAIN][self._account_id][DATA_VEHICLES]

async def _async_update_data(self):
"""Fetch data from API."""
Expand All @@ -31,7 +32,7 @@ async def _async_update_data(self):
return False

# Set interval for polling (the other coordinator)
self._hass.data[DOMAIN][DATA_COORDINATOR_POLL].set_next_interval()
self._hass.data[DOMAIN][self._account_id][DATA_COORDINATOR_POLL].set_next_interval()

return True

Expand All @@ -47,7 +48,8 @@ def __init__(self, hass, config):
update_interval=timedelta(minutes=15),
)
self._hass = hass
self._vehicles = hass.data[DOMAIN][DATA_VEHICLES]
self._account_id = config['email']
self._vehicles = hass.data[DOMAIN][self._account_id][DATA_VEHICLES]
self._config = config

self._pluggednotcharging = {key: 0 for key in self._vehicles}
Expand Down Expand Up @@ -114,7 +116,7 @@ async def _async_update_data(self):
_LOGGER.warning("Error communicating with API")
return False

self._hass.async_create_task(self._hass.data[DOMAIN][DATA_COORDINATOR_FETCH].async_refresh())
self._hass.async_create_task(self._hass.data[DOMAIN][self._account_id][DATA_COORDINATOR_FETCH].async_refresh())
return True


Expand All @@ -128,7 +130,8 @@ def __init__(self, hass, config):
update_interval=timedelta(minutes=config.get("interval_statistics", DEFAULT_INTERVAL_STATISTICS)),
)
self._hass = hass
self._vehicles = hass.data[DOMAIN][DATA_VEHICLES]
self._account_id = config['email']
self._vehicles = hass.data[DOMAIN][self._account_id][DATA_VEHICLES]

async def _async_update_data(self):
"""Fetch data from API."""
Expand Down
6 changes: 4 additions & 2 deletions custom_components/nissan_connect/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass, entry, async_add_entities):
data = hass.data[DOMAIN][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][DATA_COORDINATOR_FETCH]
account_id = entry.data['email']

data = hass.data[DOMAIN][account_id][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][account_id][DATA_COORDINATOR_FETCH]

entities = []

Expand Down
2 changes: 1 addition & 1 deletion custom_components/nissan_connect/kamereon/kamereon.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def _request(self, method, url, headers=None, params=None, data=None, max_retrie
_LOGGER.debug("Token expired. Refreshing session and retrying.")
self.session.login()
except Exception as e:
_LOGGER.warning(f"Request failed on attempt {attempt + 1} of {max_retries}: {e}")
_LOGGER.debug(f"Request failed on attempt {attempt + 1} of {max_retries}: {e}")
if attempt == max_retries - 1: # Exhausted retries
raise
time.sleep(2 ** attempt) # Exponential backoff on retry
Expand Down
8 changes: 5 additions & 3 deletions custom_components/nissan_connect/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

async def async_setup_entry(hass, config, async_add_entities):
"""Set up the Kamereon sensors."""
data = hass.data[DOMAIN][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][DATA_COORDINATOR_FETCH]
coordinator_stats = hass.data[DOMAIN][DATA_COORDINATOR_STATISTICS]
account_id = config.data['email']

data = hass.data[DOMAIN][account_id][DATA_VEHICLES]
coordinator = hass.data[DOMAIN][account_id][DATA_COORDINATOR_FETCH]
coordinator_stats = hass.data[DOMAIN][account_id][DATA_COORDINATOR_STATISTICS]

entities = []

Expand Down

0 comments on commit 0eb82b0

Please sign in to comment.