Config entry options (#18929)
Add support for options flow for config entries
This commit is contained in:
parent
caa3b123ae
commit
d9712027e8
5 changed files with 365 additions and 24 deletions
|
@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch
|
|||
import pytest
|
||||
|
||||
from homeassistant import config_entries, loader, data_entry_flow
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt
|
||||
|
@ -544,6 +545,31 @@ async def test_updating_entry_data(manager):
|
|||
}
|
||||
|
||||
|
||||
async def test_update_entry_options_and_trigger_listener(hass, manager):
|
||||
"""Test that we can update entry options and trigger listener."""
|
||||
entry = MockConfigEntry(
|
||||
domain='test',
|
||||
options={'first': True},
|
||||
)
|
||||
entry.add_to_manager(manager)
|
||||
|
||||
async def update_listener(hass, entry):
|
||||
"""Test function."""
|
||||
assert entry.options == {
|
||||
'second': True
|
||||
}
|
||||
|
||||
entry.add_update_listener(update_listener)
|
||||
|
||||
manager.async_update_entry(entry, options={
|
||||
'second': True
|
||||
})
|
||||
|
||||
assert entry.options == {
|
||||
'second': True
|
||||
}
|
||||
|
||||
|
||||
async def test_setup_raise_not_ready(hass, caplog):
|
||||
"""Test a setup raising not ready."""
|
||||
entry = MockConfigEntry(domain='test')
|
||||
|
@ -588,3 +614,39 @@ async def test_setup_retrying_during_unload(hass):
|
|||
|
||||
assert entry.state == config_entries.ENTRY_STATE_NOT_LOADED
|
||||
assert len(mock_call.return_value.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_entry_options(hass, manager):
|
||||
"""Test that we can set options on an entry."""
|
||||
entry = MockConfigEntry(
|
||||
domain='test',
|
||||
data={'first': True},
|
||||
options=None
|
||||
)
|
||||
entry.add_to_manager(manager)
|
||||
|
||||
class TestFlow:
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(config, options):
|
||||
class OptionsFlowHandler(data_entry_flow.FlowHandler):
|
||||
def __init__(self, config, options):
|
||||
pass
|
||||
return OptionsFlowHandler(config, options)
|
||||
|
||||
config_entries.HANDLERS['test'] = TestFlow()
|
||||
flow = await manager.options._async_create_flow(
|
||||
entry.entry_id, context={'source': 'test'}, data=None)
|
||||
|
||||
flow.handler = entry.entry_id # Used to keep reference to config entry
|
||||
|
||||
await manager.options._async_finish_flow(
|
||||
flow, {'data': {'second': True}})
|
||||
|
||||
assert entry.data == {
|
||||
'first': True
|
||||
}
|
||||
|
||||
assert entry.options == {
|
||||
'second': True
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue