From 89fe488b7ccee0cf368e284ae0afb812edb9772d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 13 Apr 2020 17:38:39 -0700 Subject: [PATCH] Add websocket API to update config entry title (#34155) --- .../components/config/config_entries.py | 75 +++++++++++++------ .../components/config/test_config_entries.py | 23 ++++++ 2 files changed, 74 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/config/config_entries.py b/homeassistant/components/config/config_entries.py index f80d63d437a..584255764a3 100644 --- a/homeassistant/components/config/config_entries.py +++ b/homeassistant/components/config/config_entries.py @@ -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, + } diff --git a/tests/components/config/test_config_entries.py b/tests/components/config/test_config_entries.py index d944ebb6eb6..e56580bceb0 100644 --- a/tests/components/config/test_config_entries.py +++ b/tests/components/config/test_config_entries.py @@ -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", {})