mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Small speed up to listing config entries in the websocket api (#108892)
This commit is contained in:
parent
9de8409f48
commit
dff5e45761
@ -516,7 +516,7 @@ async def async_matching_config_entries(
|
|||||||
if not type_filter:
|
if not type_filter:
|
||||||
return [entry_json(entry) for entry in entries]
|
return [entry_json(entry) for entry in entries]
|
||||||
|
|
||||||
integrations = {}
|
integrations: dict[str, Integration] = {}
|
||||||
# Fetch all the integrations so we can check their type
|
# Fetch all the integrations so we can check their type
|
||||||
domains = {entry.domain for entry in entries}
|
domains = {entry.domain for entry in entries}
|
||||||
for domain_key, integration_or_exc in (
|
for domain_key, integration_or_exc in (
|
||||||
@ -531,35 +531,32 @@ async def async_matching_config_entries(
|
|||||||
# when only helpers are requested, also filter out entries
|
# when only helpers are requested, also filter out entries
|
||||||
# from unknown integrations. This prevent them from showing
|
# from unknown integrations. This prevent them from showing
|
||||||
# up in the helpers UI.
|
# up in the helpers UI.
|
||||||
entries = [
|
filter_is_not_helper = type_filter != ["helper"]
|
||||||
entry
|
filter_set = set(type_filter)
|
||||||
|
return [
|
||||||
|
entry_json(entry)
|
||||||
for entry in entries
|
for entry in entries
|
||||||
if (type_filter != ["helper"] and entry.domain not in integrations)
|
# If the filter is not 'helper', we still include the integration
|
||||||
or (
|
# even if its not returned from async_get_integrations for backwards
|
||||||
entry.domain in integrations
|
# compatibility.
|
||||||
and integrations[entry.domain].integration_type in type_filter
|
if (
|
||||||
|
(integration := integrations.get(entry.domain))
|
||||||
|
and integration.integration_type in filter_set
|
||||||
)
|
)
|
||||||
|
or (filter_is_not_helper and entry.domain not in integrations)
|
||||||
]
|
]
|
||||||
|
|
||||||
return [entry_json(entry) for entry in entries]
|
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def entry_json(entry: config_entries.ConfigEntry) -> dict[str, Any]:
|
def entry_json(entry: config_entries.ConfigEntry) -> dict[str, Any]:
|
||||||
"""Return JSON value of a config entry."""
|
"""Return JSON value of a config entry."""
|
||||||
handler = config_entries.HANDLERS.get(entry.domain)
|
|
||||||
# work out if handler has support for options flow
|
|
||||||
supports_options = handler is not None and handler.async_supports_options_flow(
|
|
||||||
entry
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"entry_id": entry.entry_id,
|
"entry_id": entry.entry_id,
|
||||||
"domain": entry.domain,
|
"domain": entry.domain,
|
||||||
"title": entry.title,
|
"title": entry.title,
|
||||||
"source": entry.source,
|
"source": entry.source,
|
||||||
"state": entry.state.value,
|
"state": entry.state.value,
|
||||||
"supports_options": supports_options,
|
"supports_options": entry.supports_options,
|
||||||
"supports_remove_device": entry.supports_remove_device or False,
|
"supports_remove_device": entry.supports_remove_device or False,
|
||||||
"supports_unload": entry.supports_unload or False,
|
"supports_unload": entry.supports_unload or False,
|
||||||
"pref_disable_new_entities": entry.pref_disable_new_entities,
|
"pref_disable_new_entities": entry.pref_disable_new_entities,
|
||||||
|
@ -54,6 +54,7 @@ if TYPE_CHECKING:
|
|||||||
from .components.zeroconf import ZeroconfServiceInfo
|
from .components.zeroconf import ZeroconfServiceInfo
|
||||||
from .helpers.service_info.mqtt import MqttServiceInfo
|
from .helpers.service_info.mqtt import MqttServiceInfo
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
SOURCE_BLUETOOTH = "bluetooth"
|
SOURCE_BLUETOOTH = "bluetooth"
|
||||||
@ -238,6 +239,7 @@ class ConfigEntry:
|
|||||||
"_integration_for_domain",
|
"_integration_for_domain",
|
||||||
"_tries",
|
"_tries",
|
||||||
"_setup_again_job",
|
"_setup_again_job",
|
||||||
|
"_supports_options",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -318,6 +320,9 @@ class ConfigEntry:
|
|||||||
# Supports remove device
|
# Supports remove device
|
||||||
self.supports_remove_device: bool | None = None
|
self.supports_remove_device: bool | None = None
|
||||||
|
|
||||||
|
# Supports options
|
||||||
|
self._supports_options: bool | None = None
|
||||||
|
|
||||||
# Listeners to call on update
|
# Listeners to call on update
|
||||||
self.update_listeners: list[UpdateListenerType] = []
|
self.update_listeners: list[UpdateListenerType] = []
|
||||||
|
|
||||||
@ -351,6 +356,14 @@ class ConfigEntry:
|
|||||||
f"title={self.title} state={self.state} unique_id={self.unique_id}>"
|
f"title={self.title} state={self.state} unique_id={self.unique_id}>"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def supports_options(self) -> bool:
|
||||||
|
"""Return if entry supports config options."""
|
||||||
|
if self._supports_options is None and (handler := HANDLERS.get(self.domain)):
|
||||||
|
# work out if handler has support for options flow
|
||||||
|
self._supports_options = handler.async_supports_options_flow(self)
|
||||||
|
return self._supports_options or False
|
||||||
|
|
||||||
async def async_setup(
|
async def async_setup(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
@ -734,6 +734,7 @@ async def test_as_dict(snapshot: SnapshotAssertion) -> None:
|
|||||||
"_integration_for_domain",
|
"_integration_for_domain",
|
||||||
"_tries",
|
"_tries",
|
||||||
"_setup_again_job",
|
"_setup_again_job",
|
||||||
|
"_supports_options",
|
||||||
}
|
}
|
||||||
|
|
||||||
entry = MockConfigEntry(entry_id="mock-entry")
|
entry = MockConfigEntry(entry_id="mock-entry")
|
||||||
@ -1176,6 +1177,7 @@ async def test_create_entry_options(
|
|||||||
|
|
||||||
entries = hass.config_entries.async_entries("comp")
|
entries = hass.config_entries.async_entries("comp")
|
||||||
assert len(entries) == 1
|
assert len(entries) == 1
|
||||||
|
assert entries[0].supports_options is False
|
||||||
assert entries[0].data == {"example": "data"}
|
assert entries[0].data == {"example": "data"}
|
||||||
assert entries[0].options == {"example": "option"}
|
assert entries[0].options == {"example": "option"}
|
||||||
|
|
||||||
@ -1202,6 +1204,10 @@ async def test_entry_options(
|
|||||||
|
|
||||||
return OptionsFlowHandler()
|
return OptionsFlowHandler()
|
||||||
|
|
||||||
|
def async_supports_options_flow(self, entry: MockConfigEntry) -> bool:
|
||||||
|
"""Test options flow."""
|
||||||
|
return True
|
||||||
|
|
||||||
config_entries.HANDLERS["test"] = TestFlow()
|
config_entries.HANDLERS["test"] = TestFlow()
|
||||||
flow = await manager.options.async_create_flow(
|
flow = await manager.options.async_create_flow(
|
||||||
entry.entry_id, context={"source": "test"}, data=None
|
entry.entry_id, context={"source": "test"}, data=None
|
||||||
@ -1216,6 +1222,7 @@ async def test_entry_options(
|
|||||||
|
|
||||||
assert entry.data == {"first": True}
|
assert entry.data == {"first": True}
|
||||||
assert entry.options == {"second": True}
|
assert entry.options == {"second": True}
|
||||||
|
assert entry.supports_options is True
|
||||||
|
|
||||||
|
|
||||||
async def test_entry_options_abort(
|
async def test_entry_options_abort(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user