diff --git a/homeassistant/components/habitica/__init__.py b/homeassistant/components/habitica/__init__.py index 974469d8c27c66..2cd15cbfe0c470 100644 --- a/homeassistant/components/habitica/__init__.py +++ b/homeassistant/components/habitica/__init__.py @@ -19,10 +19,12 @@ from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.helpers import config_validation as cv from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.selector import ConfigEntrySelector from homeassistant.helpers.typing import ConfigType from .const import ( ATTR_ARGS, + ATTR_CONFIG_ENTRY, ATTR_DATA, ATTR_PATH, CONF_API_USER, @@ -81,7 +83,7 @@ def has_all_unique_users_names(value): SERVICE_API_CALL_SCHEMA = vol.Schema( { - vol.Required(ATTR_NAME): str, + vol.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector(), vol.Required(ATTR_PATH): vol.All(cv.ensure_list, [str]), vol.Optional(ATTR_ARGS): dict, } @@ -115,14 +117,12 @@ def __call__(self, **kwargs): return super().__call__(websession, **kwargs) async def handle_api_call(call: ServiceCall) -> None: - name = call.data[ATTR_NAME] path = call.data[ATTR_PATH] - entries = hass.config_entries.async_entries(DOMAIN) + entry_id: str = call.data[ATTR_CONFIG_ENTRY] + api = None - for entry in entries: - if entry.data[CONF_NAME] == name: - api = hass.data[DOMAIN].get(entry.entry_id) - break + api = hass.data[DOMAIN].get(entry_id) + if api is None: _LOGGER.error("API_CALL: User '%s' not configured", name) return @@ -151,12 +151,7 @@ async def handle_api_call(call: ServiceCall) -> None: name = config.get(CONF_NAME) config_dict = {"url": url, "login": username, "password": password} api = HAHabitipyAsync(config_dict) - user = await api.user.get() - if name != user["auth"]["local"]["username"]: - hass.config_entries.async_update_entry( - entry, - data={**entry.data, CONF_NAME: user["auth"]["local"]["username"]}, - ) + data[entry.entry_id] = api await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/homeassistant/components/habitica/config_flow.py b/homeassistant/components/habitica/config_flow.py index 4b81bd2a31dca1..3b6a5afdfbad4f 100644 --- a/homeassistant/components/habitica/config_flow.py +++ b/homeassistant/components/habitica/config_flow.py @@ -99,7 +99,6 @@ async def async_step_login( data={ CONF_API_USER: login_response["id"], CONF_API_KEY: login_response["apiToken"], - CONF_USERNAME: login_response["username"], CONF_URL: DEFAULT_URL, CONF_VERIFY_SSL: True, }, @@ -149,10 +148,8 @@ async def async_step_advanced( else: await self.async_set_unique_id(user_input[CONF_API_USER]) self._abort_if_unique_id_configured() - user_input[CONF_USERNAME] = api_response["auth"]["local"]["username"] - return self.async_create_entry( - title=user_input[CONF_USERNAME], data=user_input - ) + title = api_response["auth"]["local"]["username"] + return self.async_create_entry(title=title, data=user_input) return self.async_show_form( step_id="advanced", diff --git a/homeassistant/components/habitica/const.py b/homeassistant/components/habitica/const.py index 13babdf458ad9e..33258f07cb2e48 100644 --- a/homeassistant/components/habitica/const.py +++ b/homeassistant/components/habitica/const.py @@ -11,7 +11,7 @@ SERVICE_API_CALL = "api_call" ATTR_PATH = CONF_PATH ATTR_ARGS = "args" - +ATTR_CONFIG_ENTRY = "config_entry" # event constants EVENT_API_CALL_SUCCESS = f"{DOMAIN}_{SERVICE_API_CALL}_success" ATTR_DATA = "data" diff --git a/homeassistant/components/habitica/services.yaml b/homeassistant/components/habitica/services.yaml index a7ef39eb5299f6..d1cbf02a03f58f 100644 --- a/homeassistant/components/habitica/services.yaml +++ b/homeassistant/components/habitica/services.yaml @@ -1,11 +1,11 @@ # Describes the format for Habitica service api_call: fields: - name: + config_entry: required: true - example: "xxxNotAValidNickxxx" selector: - text: + config_entry: + integration: habitica path: required: true example: '["tasks", "user", "post"]' diff --git a/homeassistant/components/habitica/strings.json b/homeassistant/components/habitica/strings.json index 58c5e6a5d2bc7c..0247eb8a7b6802 100644 --- a/homeassistant/components/habitica/strings.json +++ b/homeassistant/components/habitica/strings.json @@ -78,9 +78,9 @@ "name": "API name", "description": "Calls Habitica API.", "fields": { - "name": { - "name": "[%key:common::config_flow::data::name%]", - "description": "Habitica's username to call for." + "config_entry": { + "name": "Habitica user", + "description": "The Habitica user to call for" }, "path": { "name": "[%key:common::config_flow::data::path%]",