Skip to content

Commit

Permalink
Add reconfigure flow to Airgradient (#136324)
Browse files Browse the repository at this point in the history
* Add reconfigure flow to Airgradient

* Update homeassistant/components/airgradient/strings.json

---------

Co-authored-by: Robert Resch <[email protected]>
  • Loading branch information
joostlek and edenhaus authored Jan 23, 2025
1 parent 093c41c commit 132f418
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 6 deletions.
28 changes: 24 additions & 4 deletions homeassistant/components/airgradient/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Config flow for Airgradient."""

from collections.abc import Mapping
from typing import Any

from airgradient import (
Expand All @@ -11,7 +12,12 @@
from awesomeversion import AwesomeVersion
import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import (
SOURCE_RECONFIGURE,
SOURCE_USER,
ConfigFlow,
ConfigFlowResult,
)
from homeassistant.const import CONF_HOST, CONF_MODEL
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
Expand Down Expand Up @@ -95,14 +101,28 @@ async def async_step_user(
await self.async_set_unique_id(
current_measures.serial_number, raise_on_progress=False
)
self._abort_if_unique_id_configured()
if self.source == SOURCE_USER:
self._abort_if_unique_id_configured()
if self.source == SOURCE_RECONFIGURE:
self._abort_if_unique_id_mismatch()
await self.set_configuration_source()
return self.async_create_entry(
title=current_measures.model,
if self.source == SOURCE_USER:
return self.async_create_entry(
title=current_measures.model,
data={CONF_HOST: user_input[CONF_HOST]},
)
return self.async_update_reload_and_abort(
self._get_reconfigure_entry(),
data={CONF_HOST: user_input[CONF_HOST]},
)
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
errors=errors,
)

async def async_step_reconfigure(
self, user_input: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle reconfiguration."""
return await self.async_step_user()
2 changes: 1 addition & 1 deletion homeassistant/components/airgradient/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ rules:
entity-translations: done
exception-translations: done
icon-translations: done
reconfiguration-flow: todo
reconfiguration-flow: done
repair-issues:
status: exempt
comment: |
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/airgradient/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]",
"invalid_version": "This firmware version is unsupported. Please upgrade the firmware of the device to at least version 3.1.1."
"invalid_version": "This firmware version is unsupported. Please upgrade the firmware of the device to at least version 3.1.1.",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
"unique_id_mismatch": "Please ensure you reconfigure against the same device."
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
Expand Down
96 changes: 96 additions & 0 deletions tests/components/airgradient/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,99 @@ async def test_user_flow_works_discovery(

# Verify the discovery flow was aborted
assert not hass.config_entries.flow.async_progress(DOMAIN)


async def test_reconfigure_flow(
hass: HomeAssistant,
mock_new_airgradient_client: AsyncMock,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test reconfigure flow."""
mock_config_entry.add_to_hass(hass)

result = await mock_config_entry.start_reconfigure_flow(hass)

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "10.0.0.131"},
)

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.data == {
CONF_HOST: "10.0.0.131",
}


async def test_reconfigure_flow_errors(
hass: HomeAssistant,
mock_new_airgradient_client: AsyncMock,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test reconfigure flow."""
mock_config_entry.add_to_hass(hass)
mock_new_airgradient_client.get_current_measures.side_effect = (
AirGradientConnectionError()
)

result = await mock_config_entry.start_reconfigure_flow(hass)

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "10.0.0.132"},
)

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "cannot_connect"}

mock_new_airgradient_client.get_current_measures.side_effect = None

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "10.0.0.132"},
)

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.data == {
CONF_HOST: "10.0.0.132",
}


async def test_reconfigure_flow_unique_id_mismatch(
hass: HomeAssistant,
mock_new_airgradient_client: AsyncMock,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test reconfigure flow aborts with unique id mismatch."""
mock_config_entry.add_to_hass(hass)

mock_new_airgradient_client.get_current_measures.return_value.serial_number = (
"84fce612f5b9"
)

result = await mock_config_entry.start_reconfigure_flow(hass)

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "10.0.0.132"},
)

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unique_id_mismatch"
assert mock_config_entry.data == {
CONF_HOST: "10.0.0.131",
}

0 comments on commit 132f418

Please sign in to comment.