mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Add reconfigure flow to Russound RIO (#133091)
* Add reconfigure flow to Russound RIO * Mark reconfiguration flow as done * Update * Update
This commit is contained in:
parent
c10473844f
commit
fab92d1cf8
@ -9,7 +9,11 @@ from typing import Any
|
|||||||
from aiorussound import RussoundClient, RussoundTcpConnectionHandler
|
from aiorussound import RussoundClient, RussoundTcpConnectionHandler
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import (
|
||||||
|
SOURCE_RECONFIGURE,
|
||||||
|
ConfigFlow,
|
||||||
|
ConfigFlowResult,
|
||||||
|
)
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
|
|
||||||
@ -50,6 +54,12 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
else:
|
else:
|
||||||
await self.async_set_unique_id(controller.mac_address)
|
await self.async_set_unique_id(controller.mac_address)
|
||||||
|
if self.source == SOURCE_RECONFIGURE:
|
||||||
|
self._abort_if_unique_id_mismatch(reason="wrong_device")
|
||||||
|
return self.async_update_reload_and_abort(
|
||||||
|
self._get_reconfigure_entry(),
|
||||||
|
data_updates=user_input,
|
||||||
|
)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
data = {CONF_HOST: host, CONF_PORT: port}
|
data = {CONF_HOST: host, CONF_PORT: port}
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
@ -60,6 +70,17 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_step_reconfigure(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Handle reconfiguration of the integration."""
|
||||||
|
if not user_input:
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="reconfigure",
|
||||||
|
data_schema=DATA_SCHEMA,
|
||||||
|
)
|
||||||
|
return await self.async_step_user(user_input)
|
||||||
|
|
||||||
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
|
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
|
||||||
"""Attempt to import the existing configuration."""
|
"""Attempt to import the existing configuration."""
|
||||||
self._async_abort_entries_match({CONF_HOST: import_data[CONF_HOST]})
|
self._async_abort_entries_match({CONF_HOST: import_data[CONF_HOST]})
|
||||||
|
@ -11,10 +11,7 @@ rules:
|
|||||||
brands: done
|
brands: done
|
||||||
common-modules: done
|
common-modules: done
|
||||||
config-flow-test-coverage: done
|
config-flow-test-coverage: done
|
||||||
config-flow:
|
config-flow: done
|
||||||
status: todo
|
|
||||||
comment: |
|
|
||||||
The data_description fields in translations are missing.
|
|
||||||
dependency-transparency: done
|
dependency-transparency: done
|
||||||
docs-actions:
|
docs-actions:
|
||||||
status: exempt
|
status: exempt
|
||||||
@ -65,7 +62,7 @@ rules:
|
|||||||
diagnostics: done
|
diagnostics: done
|
||||||
exception-translations: done
|
exception-translations: done
|
||||||
icon-translations: todo
|
icon-translations: todo
|
||||||
reconfiguration-flow: todo
|
reconfiguration-flow: done
|
||||||
dynamic-devices: todo
|
dynamic-devices: todo
|
||||||
discovery-update-info: todo
|
discovery-update-info: todo
|
||||||
repair-issues: done
|
repair-issues: done
|
||||||
|
@ -9,6 +9,21 @@
|
|||||||
"host": "[%key:common::config_flow::data::host%]",
|
"host": "[%key:common::config_flow::data::host%]",
|
||||||
"name": "[%key:common::config_flow::data::name%]",
|
"name": "[%key:common::config_flow::data::name%]",
|
||||||
"port": "[%key:common::config_flow::data::port%]"
|
"port": "[%key:common::config_flow::data::port%]"
|
||||||
|
},
|
||||||
|
"data_description": {
|
||||||
|
"host": "The IP address of the Russound controller.",
|
||||||
|
"port": "The port of the Russound controller."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"reconfigure": {
|
||||||
|
"description": "Reconfigure your Russound controller.",
|
||||||
|
"data": {
|
||||||
|
"host": "[%key:common::config_flow::data::host%]",
|
||||||
|
"port": "[%key:common::config_flow::data::port%]"
|
||||||
|
},
|
||||||
|
"data_description": {
|
||||||
|
"host": "[%key:component::russound_rio::config::step::user::data_description::host%]",
|
||||||
|
"port": "[%key:component::russound_rio::config::step::user::data_description::port%]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -17,7 +32,9 @@
|
|||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"cannot_connect": "[%key:component::russound_rio::common::error_cannot_connect%]",
|
"cannot_connect": "[%key:component::russound_rio::common::error_cannot_connect%]",
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||||
|
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
|
||||||
|
"wrong_device": "This Russound controller does not match the existing device id. Please make sure you entered the correct IP address."
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"issues": {
|
"issues": {
|
||||||
|
@ -9,9 +9,10 @@ from aiorussound.util import controller_device_str, zone_device_str
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.russound_rio.const import DOMAIN
|
from homeassistant.components.russound_rio.const import DOMAIN
|
||||||
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import API_VERSION, HARDWARE_MAC, HOST, MOCK_CONFIG, MODEL, PORT
|
from .const import API_VERSION, HARDWARE_MAC, MOCK_CONFIG, MODEL
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, load_json_object_fixture
|
from tests.common import MockConfigEntry, load_json_object_fixture
|
||||||
|
|
||||||
@ -68,7 +69,9 @@ def mock_russound_client() -> Generator[AsyncMock]:
|
|||||||
1, "MCA-C5", client, controller_device_str(1), HARDWARE_MAC, None, zones
|
1, "MCA-C5", client, controller_device_str(1), HARDWARE_MAC, None, zones
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
client.connection_handler = RussoundTcpConnectionHandler(HOST, PORT)
|
client.connection_handler = RussoundTcpConnectionHandler(
|
||||||
|
MOCK_CONFIG[CONF_HOST], MOCK_CONFIG[CONF_PORT]
|
||||||
|
)
|
||||||
client.is_connected = Mock(return_value=True)
|
client.is_connected = Mock(return_value=True)
|
||||||
client.unregister_state_update_callbacks.return_value = True
|
client.unregister_state_update_callbacks.return_value = True
|
||||||
client.rio_version = API_VERSION
|
client.rio_version = API_VERSION
|
||||||
|
@ -3,16 +3,20 @@
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
||||||
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
|
|
||||||
HOST = "127.0.0.1"
|
|
||||||
PORT = 9621
|
|
||||||
MODEL = "MCA-C5"
|
MODEL = "MCA-C5"
|
||||||
HARDWARE_MAC = "00:11:22:33:44:55"
|
HARDWARE_MAC = "00:11:22:33:44:55"
|
||||||
API_VERSION = "1.08.00"
|
API_VERSION = "1.08.00"
|
||||||
|
|
||||||
MOCK_CONFIG = {
|
MOCK_CONFIG = {
|
||||||
"host": HOST,
|
CONF_HOST: "192.168.20.75",
|
||||||
"port": PORT,
|
CONF_PORT: 9621,
|
||||||
|
}
|
||||||
|
|
||||||
|
MOCK_RECONFIGURATION_CONFIG = {
|
||||||
|
CONF_HOST: "192.168.20.70",
|
||||||
|
CONF_PORT: 9622,
|
||||||
}
|
}
|
||||||
|
|
||||||
_CONTROLLER = namedtuple("Controller", ["mac_address", "controller_type"]) # noqa: PYI024
|
_CONTROLLER = namedtuple("Controller", ["mac_address", "controller_type"]) # noqa: PYI024
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
DeviceRegistryEntrySnapshot({
|
DeviceRegistryEntrySnapshot({
|
||||||
'area_id': None,
|
'area_id': None,
|
||||||
'config_entries': <ANY>,
|
'config_entries': <ANY>,
|
||||||
'configuration_url': 'http://127.0.0.1',
|
'configuration_url': 'http://192.168.20.75',
|
||||||
'connections': set({
|
'connections': set({
|
||||||
tuple(
|
tuple(
|
||||||
'mac',
|
'mac',
|
||||||
|
@ -3,11 +3,12 @@
|
|||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from homeassistant.components.russound_rio.const import DOMAIN
|
from homeassistant.components.russound_rio.const import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER, ConfigFlowResult
|
||||||
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
from .const import MOCK_CONFIG, MODEL
|
from .const import MOCK_CONFIG, MOCK_RECONFIGURATION_CONFIG, MODEL
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
@ -117,3 +118,63 @@ async def test_import_cannot_connect(
|
|||||||
|
|
||||||
assert result["type"] is FlowResultType.ABORT
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["reason"] == "cannot_connect"
|
assert result["reason"] == "cannot_connect"
|
||||||
|
|
||||||
|
|
||||||
|
async def _start_reconfigure_flow(
|
||||||
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Initialize a reconfigure flow."""
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
reconfigure_result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||||
|
|
||||||
|
assert reconfigure_result["type"] is FlowResultType.FORM
|
||||||
|
assert reconfigure_result["step_id"] == "reconfigure"
|
||||||
|
|
||||||
|
return reconfigure_result
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reconfigure_flow(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_russound_client: AsyncMock,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test reconfigure flow."""
|
||||||
|
|
||||||
|
reconfigure_result = await _start_reconfigure_flow(hass, mock_config_entry)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
reconfigure_result["flow_id"],
|
||||||
|
MOCK_RECONFIGURATION_CONFIG,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "reconfigure_successful"
|
||||||
|
|
||||||
|
entry = hass.config_entries.async_get_entry(mock_config_entry.entry_id)
|
||||||
|
assert entry
|
||||||
|
assert entry.data == {
|
||||||
|
CONF_HOST: "192.168.20.70",
|
||||||
|
CONF_PORT: 9622,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reconfigure_unique_id_mismatch(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_russound_client: AsyncMock,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Ensure reconfigure flow aborts when the bride changes."""
|
||||||
|
mock_russound_client.controllers[1].mac_address = "different_mac"
|
||||||
|
|
||||||
|
reconfigure_result = await _start_reconfigure_flow(hass, mock_config_entry)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
reconfigure_result["flow_id"],
|
||||||
|
MOCK_RECONFIGURATION_CONFIG,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "wrong_device"
|
||||||
|
@ -59,8 +59,8 @@ async def test_disconnect_reconnect_log(
|
|||||||
|
|
||||||
mock_russound_client.is_connected = Mock(return_value=False)
|
mock_russound_client.is_connected = Mock(return_value=False)
|
||||||
await mock_state_update(mock_russound_client, CallbackType.CONNECTION)
|
await mock_state_update(mock_russound_client, CallbackType.CONNECTION)
|
||||||
assert "Disconnected from device at 127.0.0.1" in caplog.text
|
assert "Disconnected from device at 192.168.20.75" in caplog.text
|
||||||
|
|
||||||
mock_russound_client.is_connected = Mock(return_value=True)
|
mock_russound_client.is_connected = Mock(return_value=True)
|
||||||
await mock_state_update(mock_russound_client, CallbackType.CONNECTION)
|
await mock_state_update(mock_russound_client, CallbackType.CONNECTION)
|
||||||
assert "Reconnected to device at 127.0.0.1" in caplog.text
|
assert "Reconnected to device at 192.168.20.75" in caplog.text
|
||||||
|
Loading…
x
Reference in New Issue
Block a user