mirror of
https://github.com/home-assistant/core
synced 2024-10-04 07:58:43 +02:00
Add websocket API to update config entry title (#34155)
This commit is contained in:
parent
5ddcc22583
commit
89fe488b7c
@ -8,6 +8,7 @@ from homeassistant.auth.permissions.const import CAT_CONFIG_ENTRIES
|
||||
from homeassistant.components import websocket_api
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.const import HTTP_NOT_FOUND
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import Unauthorized
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.data_entry_flow import (
|
||||
@ -28,6 +29,7 @@ async def async_setup(hass):
|
||||
hass.http.register_view(OptionManagerFlowIndexView(hass.config_entries.options))
|
||||
hass.http.register_view(OptionManagerFlowResourceView(hass.config_entries.options))
|
||||
|
||||
hass.components.websocket_api.async_register_command(config_entry_update)
|
||||
hass.components.websocket_api.async_register_command(config_entries_progress)
|
||||
hass.components.websocket_api.async_register_command(system_options_list)
|
||||
hass.components.websocket_api.async_register_command(system_options_update)
|
||||
@ -64,30 +66,9 @@ class ConfigManagerEntryIndexView(HomeAssistantView):
|
||||
"""List available config entries."""
|
||||
hass = request.app["hass"]
|
||||
|
||||
results = []
|
||||
|
||||
for entry in hass.config_entries.async_entries():
|
||||
handler = config_entries.HANDLERS.get(entry.domain)
|
||||
supports_options = (
|
||||
# Guard in case handler is no longer registered (custom compnoent etc)
|
||||
handler is not None
|
||||
# pylint: disable=comparison-with-callable
|
||||
and handler.async_get_options_flow
|
||||
!= config_entries.ConfigFlow.async_get_options_flow
|
||||
)
|
||||
results.append(
|
||||
{
|
||||
"entry_id": entry.entry_id,
|
||||
"domain": entry.domain,
|
||||
"title": entry.title,
|
||||
"source": entry.source,
|
||||
"state": entry.state,
|
||||
"connection_class": entry.connection_class,
|
||||
"supports_options": supports_options,
|
||||
}
|
||||
)
|
||||
|
||||
return self.json(results)
|
||||
return self.json(
|
||||
[entry_json(entry) for entry in hass.config_entries.async_entries()]
|
||||
)
|
||||
|
||||
|
||||
class ConfigManagerEntryResourceView(HomeAssistantView):
|
||||
@ -287,6 +268,30 @@ async def system_options_update(hass, connection, msg):
|
||||
connection.send_result(msg["id"], entry.system_options.as_dict())
|
||||
|
||||
|
||||
@websocket_api.require_admin
|
||||
@websocket_api.async_response
|
||||
@websocket_api.websocket_command(
|
||||
{"type": "config_entries/update", "entry_id": str, vol.Optional("title"): str}
|
||||
)
|
||||
async def config_entry_update(hass, connection, msg):
|
||||
"""Update config entry system options."""
|
||||
changes = dict(msg)
|
||||
changes.pop("id")
|
||||
changes.pop("type")
|
||||
entry_id = changes.pop("entry_id")
|
||||
|
||||
entry = hass.config_entries.async_get_entry(entry_id)
|
||||
|
||||
if entry is None:
|
||||
connection.send_error(
|
||||
msg["id"], websocket_api.const.ERR_NOT_FOUND, "Config entry not found"
|
||||
)
|
||||
return
|
||||
|
||||
hass.config_entries.async_update_entry(entry, **changes)
|
||||
connection.send_result(msg["id"], entry_json(entry))
|
||||
|
||||
|
||||
@websocket_api.require_admin
|
||||
@websocket_api.async_response
|
||||
@websocket_api.websocket_command({"type": "config_entries/ignore_flow", "flow_id": str})
|
||||
@ -319,3 +324,25 @@ async def ignore_config_flow(hass, connection, msg):
|
||||
data={"unique_id": flow["context"]["unique_id"]},
|
||||
)
|
||||
connection.send_result(msg["id"])
|
||||
|
||||
|
||||
@callback
|
||||
def entry_json(entry: config_entries.ConfigEntry) -> dict:
|
||||
"""Return JSON value of a config entry."""
|
||||
handler = config_entries.HANDLERS.get(entry.domain)
|
||||
supports_options = (
|
||||
# Guard in case handler is no longer registered (custom compnoent etc)
|
||||
handler is not None
|
||||
# pylint: disable=comparison-with-callable
|
||||
and handler.async_get_options_flow
|
||||
!= config_entries.ConfigFlow.async_get_options_flow
|
||||
)
|
||||
return {
|
||||
"entry_id": entry.entry_id,
|
||||
"domain": entry.domain,
|
||||
"title": entry.title,
|
||||
"source": entry.source,
|
||||
"state": entry.state,
|
||||
"connection_class": entry.connection_class,
|
||||
"supports_options": supports_options,
|
||||
}
|
||||
|
@ -614,6 +614,29 @@ async def test_update_system_options(hass, hass_ws_client):
|
||||
assert entry.system_options.disable_new_entities
|
||||
|
||||
|
||||
async def test_update_entry(hass, hass_ws_client):
|
||||
"""Test that we can update entry."""
|
||||
assert await async_setup_component(hass, "config", {})
|
||||
ws_client = await hass_ws_client(hass)
|
||||
|
||||
entry = MockConfigEntry(domain="demo", title="Initial Title")
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
await ws_client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config_entries/update",
|
||||
"entry_id": entry.entry_id,
|
||||
"title": "Updated Title",
|
||||
}
|
||||
)
|
||||
response = await ws_client.receive_json()
|
||||
|
||||
assert response["success"]
|
||||
assert response["result"]["title"] == "Updated Title"
|
||||
assert entry.title == "Updated Title"
|
||||
|
||||
|
||||
async def test_ignore_flow(hass, hass_ws_client):
|
||||
"""Test we can ignore a flow."""
|
||||
assert await async_setup_component(hass, "config", {})
|
||||
|
Loading…
Reference in New Issue
Block a user