Skip to content

Commit

Permalink
Properly handle credentials updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Jan 16, 2024
1 parent dbac414 commit f3ee3b9
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion custom_components/ohme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ async def async_setup_dependencies(hass, config):
await client.async_update_device_info()


async def async_update_listener(hass, entry):
"""Handle options flow credentials update."""
# Re-instantiate the API client
await async_setup_dependencies(hass, dict(entry.data))

# Refresh all coordinators for good measure
for coordinator in hass.data[DOMAIN][DATA_COORDINATORS]:
await coordinator.async_refresh()


async def async_setup_entry(hass, entry):
"""This is called from the config flow."""
hass.data.setdefault(DOMAIN, {})
Expand Down Expand Up @@ -70,6 +80,8 @@ async def async_setup_entry(hass, entry):
hass.config_entries.async_forward_entry_setup(entry, entity_type)
)

entry.async_on_unload(entry.add_update_listener(async_update_listener))

return True


Expand All @@ -78,6 +90,7 @@ async def async_unload_entry(hass, entry):

return await hass.config_entries.async_unload_platforms(entry, ENTITY_TYPES)


async def async_migrate_entry(hass: core.HomeAssistant, config_entry) -> bool:
"""Migrate old entry."""
# Version number has gone backwards
Expand All @@ -92,7 +105,7 @@ async def async_migrate_entry(hass: core.HomeAssistant, config_entry) -> bool:

config_entry.version = CONFIG_VERSION
hass.config_entries.async_update_entry(config_entry, data=new_data)

_LOGGER.debug("Migration to version %s successful", config_entry.version)

return True

0 comments on commit f3ee3b9

Please sign in to comment.