mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Add reauth flow to the Traccar Server integration (#148236)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Josef Zweck <josef@zweck.dev>
This commit is contained in:
parent
26de1ea37b
commit
70e9c4e2d0
@ -2,9 +2,15 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pytraccar import ApiClient, ServerModel, TraccarException
|
from pytraccar import (
|
||||||
|
ApiClient,
|
||||||
|
ServerModel,
|
||||||
|
TraccarAuthenticationException,
|
||||||
|
TraccarException,
|
||||||
|
)
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
@ -160,6 +166,65 @@ class TraccarServerConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_step_reauth(
|
||||||
|
self, _entry_data: Mapping[str, Any]
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Handle configuration by re-auth."""
|
||||||
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
|
async def async_step_reauth_confirm(
|
||||||
|
self,
|
||||||
|
user_input: dict[str, Any] | None = None,
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Handle reauth flow."""
|
||||||
|
reauth_entry = self._get_reauth_entry()
|
||||||
|
errors: dict[str, str] = {}
|
||||||
|
|
||||||
|
if user_input is not None:
|
||||||
|
test_data = {
|
||||||
|
**reauth_entry.data,
|
||||||
|
**user_input,
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
await self._get_server_info(test_data)
|
||||||
|
except TraccarAuthenticationException:
|
||||||
|
LOGGER.error("Invalid credentials for Traccar Server")
|
||||||
|
errors["base"] = "invalid_auth"
|
||||||
|
except TraccarException as exception:
|
||||||
|
LOGGER.error("Unable to connect to Traccar Server: %s", exception)
|
||||||
|
errors["base"] = "cannot_connect"
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
LOGGER.exception("Unexpected exception")
|
||||||
|
errors["base"] = "unknown"
|
||||||
|
else:
|
||||||
|
return self.async_update_reload_and_abort(
|
||||||
|
reauth_entry,
|
||||||
|
data_updates=user_input,
|
||||||
|
)
|
||||||
|
username = (
|
||||||
|
user_input[CONF_USERNAME]
|
||||||
|
if user_input
|
||||||
|
else reauth_entry.data[CONF_USERNAME]
|
||||||
|
)
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="reauth_confirm",
|
||||||
|
data_schema=vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_USERNAME, default=username): TextSelector(
|
||||||
|
TextSelectorConfig(type=TextSelectorType.EMAIL)
|
||||||
|
),
|
||||||
|
vol.Required(CONF_PASSWORD): TextSelector(
|
||||||
|
TextSelectorConfig(type=TextSelectorType.PASSWORD)
|
||||||
|
),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
errors=errors,
|
||||||
|
description_placeholders={
|
||||||
|
CONF_HOST: reauth_entry.data[CONF_HOST],
|
||||||
|
CONF_PORT: reauth_entry.data[CONF_PORT],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(
|
def async_get_options_flow(
|
||||||
|
@ -13,11 +13,13 @@ from pytraccar import (
|
|||||||
GeofenceModel,
|
GeofenceModel,
|
||||||
PositionModel,
|
PositionModel,
|
||||||
SubscriptionData,
|
SubscriptionData,
|
||||||
|
TraccarAuthenticationException,
|
||||||
TraccarException,
|
TraccarException,
|
||||||
)
|
)
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
@ -90,6 +92,8 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat
|
|||||||
self.client.get_positions(),
|
self.client.get_positions(),
|
||||||
self.client.get_geofences(),
|
self.client.get_geofences(),
|
||||||
)
|
)
|
||||||
|
except TraccarAuthenticationException:
|
||||||
|
raise ConfigEntryAuthFailed from None
|
||||||
except TraccarException as ex:
|
except TraccarException as ex:
|
||||||
raise UpdateFailed(f"Error while updating device data: {ex}") from ex
|
raise UpdateFailed(f"Error while updating device data: {ex}") from ex
|
||||||
|
|
||||||
@ -236,6 +240,8 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat
|
|||||||
"""Subscribe to events."""
|
"""Subscribe to events."""
|
||||||
try:
|
try:
|
||||||
await self.client.subscribe(self.handle_subscription_data)
|
await self.client.subscribe(self.handle_subscription_data)
|
||||||
|
except TraccarAuthenticationException:
|
||||||
|
raise ConfigEntryAuthFailed from None
|
||||||
except TraccarException as ex:
|
except TraccarException as ex:
|
||||||
if self._should_log_subscription_error:
|
if self._should_log_subscription_error:
|
||||||
self._should_log_subscription_error = False
|
self._should_log_subscription_error = False
|
||||||
|
@ -14,14 +14,23 @@
|
|||||||
"host": "The hostname or IP address of your Traccar Server",
|
"host": "The hostname or IP address of your Traccar Server",
|
||||||
"username": "The username (email) you use to log in to your Traccar Server"
|
"username": "The username (email) you use to log in to your Traccar Server"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"reauth_confirm": {
|
||||||
|
"description": "The authentication credentials for {host}:{port} need to be updated.",
|
||||||
|
"data": {
|
||||||
|
"username": "[%key:common::config_flow::data::username%]",
|
||||||
|
"password": "[%key:common::config_flow::data::password%]"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
"unknown": "[%key:common::config_flow::error::unknown%]",
|
||||||
|
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]"
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||||
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"options": {
|
"options": {
|
||||||
|
@ -4,7 +4,7 @@ from collections.abc import Generator
|
|||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from pytraccar import TraccarException
|
from pytraccar import TraccarAuthenticationException, TraccarException
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.traccar_server.const import (
|
from homeassistant.components.traccar_server.const import (
|
||||||
@ -175,3 +175,98 @@ async def test_abort_already_configured(
|
|||||||
|
|
||||||
assert result["type"] is FlowResultType.ABORT
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reauth_flow(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
mock_traccar_api_client: Generator[AsyncMock],
|
||||||
|
) -> None:
|
||||||
|
"""Test reauth flow."""
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={
|
||||||
|
"source": config_entries.SOURCE_REAUTH,
|
||||||
|
"entry_id": mock_config_entry.entry_id,
|
||||||
|
},
|
||||||
|
data=mock_config_entry.data,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
CONF_USERNAME: "new-username",
|
||||||
|
CONF_PASSWORD: "new-password",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "reauth_successful"
|
||||||
|
|
||||||
|
# Verify the config entry was updated
|
||||||
|
assert mock_config_entry.data[CONF_USERNAME] == "new-username"
|
||||||
|
assert mock_config_entry.data[CONF_PASSWORD] == "new-password"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("side_effect", "error"),
|
||||||
|
[
|
||||||
|
(TraccarAuthenticationException, "invalid_auth"),
|
||||||
|
(TraccarException, "cannot_connect"),
|
||||||
|
(Exception, "unknown"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_reauth_flow_errors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
mock_traccar_api_client: Generator[AsyncMock],
|
||||||
|
side_effect: Exception,
|
||||||
|
error: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test reauth flow with errors."""
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={
|
||||||
|
"source": config_entries.SOURCE_REAUTH,
|
||||||
|
"entry_id": mock_config_entry.entry_id,
|
||||||
|
},
|
||||||
|
data=mock_config_entry.data,
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_traccar_api_client.get_server.side_effect = side_effect
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
CONF_USERNAME: "new-username",
|
||||||
|
CONF_PASSWORD: "new-password",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["errors"] == {"base": error}
|
||||||
|
|
||||||
|
# Test recovery after error
|
||||||
|
mock_traccar_api_client.get_server.side_effect = None
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
CONF_USERNAME: "new-username",
|
||||||
|
CONF_PASSWORD: "new-password",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "reauth_successful"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user