Skip to content

Commit

Permalink
remove username from entry
Browse files Browse the repository at this point in the history
  • Loading branch information
tr4nt0r committed May 1, 2024
1 parent adbaee2 commit d9323fb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 25 deletions.
21 changes: 8 additions & 13 deletions homeassistant/components/habitica/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions homeassistant/components/habitica/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/habitica/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/habitica/services.yaml
Original file line number Diff line number Diff line change
@@ -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"]'
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/habitica/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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%]",
Expand Down

0 comments on commit d9323fb

Please sign in to comment.