Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Fix options flow, add unique ID to config entry, cache coordinator da…
Browse files Browse the repository at this point in the history
…ta (#2)

* Fix options flow

* Add unique id to config entry

* Cache API response to avoid sensors showing unknown

* Bump version
  • Loading branch information
dan-r authored Apr 15, 2024
1 parent 37a6f12 commit 5817bb5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
17 changes: 10 additions & 7 deletions custom_components/o2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class O2ConfigFlow(ConfigFlow, domain=DOMAIN):
async def async_step_user(self, info):
errors = {}
if info is not None:
# Abort if entry already exists
await self.async_set_unique_id(info["email"])
self._abort_if_unique_id_configured()

# Validate credentials
session = O2ApiClient(
info["email"],
Expand All @@ -33,7 +37,7 @@ async def async_step_user(self, info):

if len(errors) == 0:
return self.async_create_entry(
title="O2 Account",
title=info['email'],
data=info
)

Expand All @@ -57,12 +61,11 @@ async def async_step_init(self, options):
if options is not None:
data = dict(self._config_entry.data)
# Validate credentials
session = O2ApiClient(
info["email"],
info["password"]
)

if "password" in options:
session = O2ApiClient(
options["email"],
options["password"]
)
try:
await self.hass.async_add_executor_job(session.create_session)
except:
Expand All @@ -78,7 +81,7 @@ async def async_step_init(self, options):
# Update data
data.update(options)
self.hass.config_entries.async_update_entry(
self._config_entry, data=data
self._config_entry, data=data, title=data['email']
)

# Update options
Expand Down
2 changes: 1 addition & 1 deletion custom_components/o2/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CONFIG_VERSION = 1
ENTITY_TYPES = ["sensor"]
USER_AGENT = "dan-r-homeassistant-o2"
INTEGRATION_VERSION = "0.1.0"
INTEGRATION_VERSION = "0.1.1"

DATA_COORDINATOR = "coordinator"
DATA_APICLIENT = "apiclient"
5 changes: 4 additions & 1 deletion custom_components/o2/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ def __init__(self, hass, config):
update_interval=timedelta(minutes=30),
)
self._hass = hass
self._data = None

async def _async_update_data(self):
"""Fetch data from API."""
client = self._hass.data[DOMAIN][DATA_APICLIENT]

try:
data = await self._hass.async_add_executor_job(client.get_allowances)
self._data = data

return data
except BaseException:
_LOGGER.warning("Error communicating with API")
return False
return self._data

0 comments on commit 5817bb5

Please sign in to comment.