Skip to content

Commit a01eb48

Browse files
erwindounaNoRi2909
authored andcommitted
Portainer switch terminology to API token (home-assistant#152958)
Co-authored-by: Norbert Rittel <[email protected]>
1 parent eb103a8 commit a01eb48

File tree

7 files changed

+75
-26
lines changed

7 files changed

+75
-26
lines changed

homeassistant/components/portainer/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
from pyportainer import Portainer
66

77
from homeassistant.config_entries import ConfigEntry
8-
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_VERIFY_SSL, Platform
8+
from homeassistant.const import (
9+
CONF_API_KEY,
10+
CONF_API_TOKEN,
11+
CONF_HOST,
12+
CONF_URL,
13+
CONF_VERIFY_SSL,
14+
Platform,
15+
)
916
from homeassistant.core import HomeAssistant
1017
from homeassistant.helpers.aiohttp_client import async_create_clientsession
1118

@@ -20,8 +27,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: PortainerConfigEntry) ->
2027
"""Set up Portainer from a config entry."""
2128

2229
client = Portainer(
23-
api_url=entry.data[CONF_HOST],
24-
api_key=entry.data[CONF_API_KEY],
30+
api_url=entry.data[CONF_URL],
31+
api_key=entry.data[CONF_API_TOKEN],
2532
session=async_create_clientsession(
2633
hass=hass, verify_ssl=entry.data[CONF_VERIFY_SSL]
2734
),
@@ -39,3 +46,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: PortainerConfigEntry) ->
3946
async def async_unload_entry(hass: HomeAssistant, entry: PortainerConfigEntry) -> bool:
4047
"""Unload a config entry."""
4148
return await hass.config_entries.async_unload_platforms(entry, _PLATFORMS)
49+
50+
51+
async def async_migrate_entry(hass: HomeAssistant, entry: PortainerConfigEntry) -> bool:
52+
"""Migrate old entry."""
53+
54+
if entry.version < 2:
55+
data = dict(entry.data)
56+
data[CONF_URL] = data.pop(CONF_HOST)
57+
data[CONF_API_TOKEN] = data.pop(CONF_API_KEY)
58+
hass.config_entries.async_update_entry(entry=entry, data=data, version=2)
59+
60+
return True

homeassistant/components/portainer/config_flow.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import voluptuous as vol
1515

1616
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
17-
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_VERIFY_SSL
17+
from homeassistant.const import CONF_API_TOKEN, CONF_URL, CONF_VERIFY_SSL
1818
from homeassistant.core import HomeAssistant
1919
from homeassistant.exceptions import HomeAssistantError
2020
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@@ -24,8 +24,8 @@
2424
_LOGGER = logging.getLogger(__name__)
2525
STEP_USER_DATA_SCHEMA = vol.Schema(
2626
{
27-
vol.Required(CONF_HOST): str,
28-
vol.Required(CONF_API_KEY): str,
27+
vol.Required(CONF_URL): str,
28+
vol.Required(CONF_API_TOKEN): str,
2929
vol.Optional(CONF_VERIFY_SSL, default=True): bool,
3030
}
3131
)
@@ -35,9 +35,11 @@ async def _validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None:
3535
"""Validate the user input allows us to connect."""
3636

3737
client = Portainer(
38-
api_url=data[CONF_HOST],
39-
api_key=data[CONF_API_KEY],
40-
session=async_get_clientsession(hass=hass, verify_ssl=data[CONF_VERIFY_SSL]),
38+
api_url=data[CONF_URL],
39+
api_key=data[CONF_API_TOKEN],
40+
session=async_get_clientsession(
41+
hass=hass, verify_ssl=data.get(CONF_VERIFY_SSL, True)
42+
),
4143
)
4244
try:
4345
await client.get_endpoints()
@@ -48,19 +50,21 @@ async def _validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None:
4850
except PortainerTimeoutError as err:
4951
raise PortainerTimeout from err
5052

51-
_LOGGER.debug("Connected to Portainer API: %s", data[CONF_HOST])
53+
_LOGGER.debug("Connected to Portainer API: %s", data[CONF_URL])
5254

5355

5456
class PortainerConfigFlow(ConfigFlow, domain=DOMAIN):
5557
"""Handle a config flow for Portainer."""
5658

59+
VERSION = 2
60+
5761
async def async_step_user(
5862
self, user_input: dict[str, Any] | None = None
5963
) -> ConfigFlowResult:
6064
"""Handle the initial step."""
6165
errors: dict[str, str] = {}
6266
if user_input is not None:
63-
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
67+
self._async_abort_entries_match({CONF_URL: user_input[CONF_URL]})
6468
try:
6569
await _validate_input(self.hass, user_input)
6670
except CannotConnect:
@@ -73,10 +77,10 @@ async def async_step_user(
7377
_LOGGER.exception("Unexpected exception")
7478
errors["base"] = "unknown"
7579
else:
76-
await self.async_set_unique_id(user_input[CONF_API_KEY])
80+
await self.async_set_unique_id(user_input[CONF_API_TOKEN])
7781
self._abort_if_unique_id_configured()
7882
return self.async_create_entry(
79-
title=user_input[CONF_HOST], data=user_input
83+
title=user_input[CONF_URL], data=user_input
8084
)
8185

8286
return self.async_show_form(

homeassistant/components/portainer/coordinator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pyportainer.models.portainer import Endpoint
1717

1818
from homeassistant.config_entries import ConfigEntry
19-
from homeassistant.const import CONF_HOST
19+
from homeassistant.const import CONF_URL
2020
from homeassistant.core import HomeAssistant
2121
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
2222
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@@ -87,7 +87,7 @@ async def _async_setup(self) -> None:
8787
async def _async_update_data(self) -> dict[int, PortainerCoordinatorData]:
8888
"""Fetch data from Portainer API."""
8989
_LOGGER.debug(
90-
"Fetching data from Portainer API: %s", self.config_entry.data[CONF_HOST]
90+
"Fetching data from Portainer API: %s", self.config_entry.data[CONF_URL]
9191
)
9292

9393
try:

homeassistant/components/portainer/strings.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
"step": {
44
"user": {
55
"data": {
6-
"host": "[%key:common::config_flow::data::host%]",
7-
"api_key": "[%key:common::config_flow::data::api_key%]",
6+
"url": "[%key:common::config_flow::data::url%]",
7+
"api_token": "[%key:common::config_flow::data::api_token%]",
88
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
99
},
1010
"data_description": {
11-
"host": "The host/URL, including the port, of your Portainer instance",
12-
"api_key": "The API key for authenticating with Portainer",
11+
"url": "The URL, including the port, of your Portainer instance",
12+
"api_token": "The API access token for authenticating with Portainer",
1313
"verify_ssl": "Whether to verify SSL certificates. Disable only if you have a self-signed certificate"
1414
},
15-
"description": "You can create an API key in the Portainer UI. Go to **My account > API keys** and select **Add API key**"
15+
"description": "You can create an access token in the Portainer UI. Go to **My account > Access tokens** and select **Add access token**"
1616
}
1717
},
1818
"error": {

tests/components/portainer/conftest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import pytest
99

1010
from homeassistant.components.portainer.const import DOMAIN
11-
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_VERIFY_SSL
11+
from homeassistant.const import CONF_API_TOKEN, CONF_URL, CONF_VERIFY_SSL
1212

1313
from tests.common import MockConfigEntry, load_json_array_fixture
1414

1515
MOCK_TEST_CONFIG = {
16-
CONF_HOST: "https://127.0.0.1:9000/",
17-
CONF_API_KEY: "test_api_key",
16+
CONF_URL: "https://127.0.0.1:9000/",
17+
CONF_API_TOKEN: "test_api_token",
1818
CONF_VERIFY_SSL: True,
1919
}
2020

@@ -61,4 +61,5 @@ def mock_config_entry() -> MockConfigEntry:
6161
title="Portainer test",
6262
data=MOCK_TEST_CONFIG,
6363
entry_id="portainer_test_entry_123",
64+
version=2,
6465
)

tests/components/portainer/test_config_flow.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from homeassistant.components.portainer.const import DOMAIN
1313
from homeassistant.config_entries import SOURCE_USER
14-
from homeassistant.const import CONF_API_KEY, CONF_HOST
14+
from homeassistant.const import CONF_API_TOKEN, CONF_URL, CONF_VERIFY_SSL
1515
from homeassistant.core import HomeAssistant
1616
from homeassistant.data_entry_flow import FlowResultType
1717

@@ -20,8 +20,9 @@
2020
from tests.common import MockConfigEntry
2121

2222
MOCK_USER_SETUP = {
23-
CONF_HOST: "https://127.0.0.1:9000/",
24-
CONF_API_KEY: "test_api_key",
23+
CONF_URL: "https://127.0.0.1:9000/",
24+
CONF_API_TOKEN: "test_api_token",
25+
CONF_VERIFY_SSL: True,
2526
}
2627

2728

tests/components/portainer/test_init.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
)
1010
import pytest
1111

12+
from homeassistant.components.portainer.const import DOMAIN
1213
from homeassistant.config_entries import ConfigEntryState
14+
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_HOST, CONF_URL
1315
from homeassistant.core import HomeAssistant
1416

1517
from . import setup_integration
@@ -36,3 +38,25 @@ async def test_setup_exceptions(
3638
mock_portainer_client.get_endpoints.side_effect = exception
3739
await setup_integration(hass, mock_config_entry)
3840
assert mock_config_entry.state == expected_state
41+
42+
43+
async def test_v1_migration(hass: HomeAssistant) -> None:
44+
"""Test migration from v1 to v2 config entry."""
45+
entry = MockConfigEntry(
46+
domain=DOMAIN,
47+
data={
48+
CONF_HOST: "http://test_host",
49+
CONF_API_KEY: "test_key",
50+
},
51+
unique_id="1",
52+
version=1,
53+
)
54+
entry.add_to_hass(hass)
55+
await hass.config_entries.async_setup(entry.entry_id)
56+
await hass.async_block_till_done()
57+
58+
assert entry.version == 2
59+
assert CONF_HOST not in entry.data
60+
assert CONF_API_KEY not in entry.data
61+
assert entry.data[CONF_URL] == "http://test_host"
62+
assert entry.data[CONF_API_TOKEN] == "test_key"

0 commit comments

Comments
 (0)