mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Life360: Fix config entry handling for accounts imported from configuration (#24455)
Was improperly generating a warning each restart. Was not properly handling a password change in configuration. Was not properly removing config entries for accounts removed from configuration.
This commit is contained in:
parent
af926db211
commit
d86837cc4d
@ -121,11 +121,36 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Set up integration."""
|
"""Set up integration."""
|
||||||
conf = config.get(DOMAIN, LIFE360_SCHEMA({}))
|
conf = config.get(DOMAIN, LIFE360_SCHEMA({}))
|
||||||
hass.data[DOMAIN] = {'config': conf, 'apis': []}
|
hass.data[DOMAIN] = {'config': conf, 'apis': {}}
|
||||||
discovery.load_platform(hass, DEVICE_TRACKER, DOMAIN, None, config)
|
discovery.load_platform(hass, DEVICE_TRACKER, DOMAIN, None, config)
|
||||||
|
|
||||||
if CONF_ACCOUNTS in conf:
|
if CONF_ACCOUNTS not in conf:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Check existing config entries. For any that correspond to an entry in
|
||||||
|
# configuration.yaml, and whose password has not changed, nothing needs to
|
||||||
|
# be done with that config entry or that account from configuration.yaml.
|
||||||
|
# But if the config entry was created by import and the account no longer
|
||||||
|
# exists in configuration.yaml, or if the password has changed, then delete
|
||||||
|
# that out-of-date config entry.
|
||||||
|
already_configured = []
|
||||||
|
for entry in hass.config_entries.async_entries(DOMAIN):
|
||||||
|
# Find corresponding configuration.yaml entry and its password.
|
||||||
|
password = None
|
||||||
for account in conf[CONF_ACCOUNTS]:
|
for account in conf[CONF_ACCOUNTS]:
|
||||||
|
if account[CONF_USERNAME] == entry.data[CONF_USERNAME]:
|
||||||
|
password = account[CONF_PASSWORD]
|
||||||
|
if password == entry.data[CONF_PASSWORD]:
|
||||||
|
already_configured.append(entry.data[CONF_USERNAME])
|
||||||
|
continue
|
||||||
|
if (not password and entry.source == config_entries.SOURCE_IMPORT
|
||||||
|
or password and password != entry.data[CONF_PASSWORD]):
|
||||||
|
hass.async_create_task(hass.config_entries.async_remove(
|
||||||
|
entry.entry_id))
|
||||||
|
|
||||||
|
# Create config entries for accounts listed in configuration.
|
||||||
|
for account in conf[CONF_ACCOUNTS]:
|
||||||
|
if account[CONF_USERNAME] not in already_configured:
|
||||||
hass.async_create_task(hass.config_entries.flow.async_init(
|
hass.async_create_task(hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={'source': config_entries.SOURCE_IMPORT},
|
DOMAIN, context={'source': config_entries.SOURCE_IMPORT},
|
||||||
data=account))
|
data=account))
|
||||||
@ -134,6 +159,15 @@ def setup(hass, config):
|
|||||||
|
|
||||||
async def async_setup_entry(hass, entry):
|
async def async_setup_entry(hass, entry):
|
||||||
"""Set up config entry."""
|
"""Set up config entry."""
|
||||||
hass.data[DOMAIN]['apis'].append(
|
hass.data[DOMAIN]['apis'][entry.data[CONF_USERNAME]] = get_api(
|
||||||
get_api(entry.data[CONF_AUTHORIZATION]))
|
entry.data[CONF_AUTHORIZATION])
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def async_unload_entry(hass, entry):
|
||||||
|
"""Unload config entry."""
|
||||||
|
try:
|
||||||
|
hass.data[DOMAIN]['apis'].pop(entry.data[CONF_USERNAME])
|
||||||
|
return True
|
||||||
|
except KeyError:
|
||||||
|
return False
|
||||||
|
@ -82,9 +82,6 @@ class Life360ConfigFlow(config_entries.ConfigFlow):
|
|||||||
"""Import a config flow from configuration."""
|
"""Import a config flow from configuration."""
|
||||||
username = user_input[CONF_USERNAME]
|
username = user_input[CONF_USERNAME]
|
||||||
password = user_input[CONF_PASSWORD]
|
password = user_input[CONF_PASSWORD]
|
||||||
if username in self.configured_usernames:
|
|
||||||
_LOGGER.warning('%s already configured', username)
|
|
||||||
return self.async_abort(reason='user_already_configured')
|
|
||||||
try:
|
try:
|
||||||
authorization = self._api.get_authorization(username, password)
|
authorization = self._api.get_authorization(username, password)
|
||||||
except LoginError:
|
except LoginError:
|
||||||
|
@ -305,7 +305,7 @@ class Life360Scanner:
|
|||||||
circles_updated = []
|
circles_updated = []
|
||||||
members_updated = []
|
members_updated = []
|
||||||
|
|
||||||
for api in self._apis:
|
for api in self._apis.values():
|
||||||
err_key = 'get_circles'
|
err_key = 'get_circles'
|
||||||
try:
|
try:
|
||||||
circles = api.get_circles()
|
circles = api.get_circles()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user