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:
Phil Bruckner 2019-06-10 14:45:22 -05:00 committed by Paulus Schoutsen
parent af926db211
commit d86837cc4d
3 changed files with 39 additions and 8 deletions

View File

@ -121,11 +121,36 @@ CONFIG_SCHEMA = vol.Schema({
def setup(hass, config):
"""Set up integration."""
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)
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]:
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(
DOMAIN, context={'source': config_entries.SOURCE_IMPORT},
data=account))
@ -134,6 +159,15 @@ def setup(hass, config):
async def async_setup_entry(hass, entry):
"""Set up config entry."""
hass.data[DOMAIN]['apis'].append(
get_api(entry.data[CONF_AUTHORIZATION]))
hass.data[DOMAIN]['apis'][entry.data[CONF_USERNAME]] = get_api(
entry.data[CONF_AUTHORIZATION])
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

View File

@ -82,9 +82,6 @@ class Life360ConfigFlow(config_entries.ConfigFlow):
"""Import a config flow from configuration."""
username = user_input[CONF_USERNAME]
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:
authorization = self._api.get_authorization(username, password)
except LoginError:

View File

@ -305,7 +305,7 @@ class Life360Scanner:
circles_updated = []
members_updated = []
for api in self._apis:
for api in self._apis.values():
err_key = 'get_circles'
try:
circles = api.get_circles()