Only apply OptionsFlowWithConfigEntry deprecation to core (#130054)

* Only apply OptionsFlowWithConfigEntry deprecation to core

* Fix match string in pytest.raises

* Improve coverage
This commit is contained in:
epenet 2024-11-08 15:55:19 +01:00 committed by GitHub
parent 6b90d8ff1a
commit 353ccf3ea7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 14 deletions

View file

@ -63,7 +63,7 @@ from .helpers.event import (
RANDOM_MICROSECOND_MIN,
async_call_later,
)
from .helpers.frame import report
from .helpers.frame import ReportBehavior, report, report_usage
from .helpers.json import json_bytes, json_bytes_sorted, json_fragment
from .helpers.typing import UNDEFINED, ConfigType, DiscoveryInfoType, UndefinedType
from .loader import async_suggest_report_issue
@ -3168,17 +3168,21 @@ class OptionsFlow(ConfigEntryBaseFlow):
class OptionsFlowWithConfigEntry(OptionsFlow):
"""Base class for options flows with config entry and options."""
"""Base class for options flows with config entry and options.
This class is being phased out, and should not be referenced in new code.
It is kept only for backward compatibility, and only for custom integrations.
"""
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self._config_entry = config_entry
self._options = deepcopy(dict(config_entry.options))
report(
"inherits from OptionsFlowWithConfigEntry, which is deprecated "
"and will stop working in 2025.12",
error_if_integration=False,
error_if_core=True,
report_usage(
"inherits from OptionsFlowWithConfigEntry",
core_behavior=ReportBehavior.ERROR,
core_integration_behavior=ReportBehavior.ERROR,
custom_integration_behavior=ReportBehavior.IGNORE,
)
@property

View file

@ -5040,6 +5040,24 @@ async def test_async_wait_component_startup(hass: HomeAssistant) -> None:
assert "test" in hass.config.components
@pytest.mark.parametrize(
"integration_frame_path",
["homeassistant/components/my_integration", "homeassistant.core"],
)
@pytest.mark.usefixtures("mock_integration_frame")
async def test_options_flow_with_config_entry_core() -> None:
"""Test that OptionsFlowWithConfigEntry cannot be used in core."""
entry = MockConfigEntry(
domain="hue",
data={"first": True},
options={"sub_dict": {"1": "one"}, "sub_list": ["one"]},
)
with pytest.raises(RuntimeError, match="inherits from OptionsFlowWithConfigEntry"):
_ = config_entries.OptionsFlowWithConfigEntry(entry)
@pytest.mark.parametrize("integration_frame_path", ["custom_components/my_integration"])
@pytest.mark.usefixtures("mock_integration_frame")
@patch.object(frame, "_REPORTED_INTEGRATIONS", set())
async def test_options_flow_with_config_entry(caplog: pytest.LogCaptureFixture) -> None:
@ -5051,15 +5069,17 @@ async def test_options_flow_with_config_entry(caplog: pytest.LogCaptureFixture)
)
options_flow = config_entries.OptionsFlowWithConfigEntry(entry)
assert (
"Detected that integration 'hue' inherits from OptionsFlowWithConfigEntry,"
" which is deprecated and will stop working in 2025.12" in caplog.text
)
assert caplog.text == "" # No deprecation warning for custom components
options_flow._options["sub_dict"]["2"] = "two"
options_flow._options["sub_list"].append("two")
# Ensure available at startup
assert options_flow.config_entry is entry
assert options_flow.options == entry.options
assert options_flow._options == {
options_flow.options["sub_dict"]["2"] = "two"
options_flow.options["sub_list"].append("two")
# Ensure it does not mutate the entry options
assert options_flow.options == {
"sub_dict": {"1": "one", "2": "two"},
"sub_list": ["one", "two"],
}