Add WS command for getting a config entry (#93387)

* Add WS command for getting a config entry

* Update tests
This commit is contained in:
Erik Montnemery 2023-05-23 10:44:02 +02:00 committed by GitHub
parent 657d285e8f
commit b754f03eb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 41 deletions

View File

@ -41,8 +41,9 @@ async def async_setup(hass):
hass.http.register_view(OptionManagerFlowIndexView(hass.config_entries.options)) hass.http.register_view(OptionManagerFlowIndexView(hass.config_entries.options))
hass.http.register_view(OptionManagerFlowResourceView(hass.config_entries.options)) hass.http.register_view(OptionManagerFlowResourceView(hass.config_entries.options))
websocket_api.async_register_command(hass, config_entries_get) websocket_api.async_register_command(hass, config_entries_get_matching)
websocket_api.async_register_command(hass, config_entry_disable) websocket_api.async_register_command(hass, config_entry_disable)
websocket_api.async_register_command(hass, config_entry_get)
websocket_api.async_register_command(hass, config_entry_update) websocket_api.async_register_command(hass, config_entry_update)
websocket_api.async_register_command(hass, config_entries_subscribe) websocket_api.async_register_command(hass, config_entries_subscribe)
websocket_api.async_register_command(hass, config_entries_progress) websocket_api.async_register_command(hass, config_entries_progress)
@ -284,6 +285,28 @@ def get_entry(
return entry return entry
@websocket_api.require_admin
@websocket_api.websocket_command(
{
"type": "config_entries/get",
"entry_id": str,
}
)
@websocket_api.async_response
async def config_entry_get(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Update config entry."""
entry = get_entry(hass, connection, msg["entry_id"], msg["id"])
if entry is None:
return
result = {"config_entry": entry_json(entry)}
connection.send_result(msg["id"], result)
@websocket_api.require_admin @websocket_api.require_admin
@websocket_api.websocket_command( @websocket_api.websocket_command(
{ {
@ -409,13 +432,13 @@ async def ignore_config_flow(
@websocket_api.websocket_command( @websocket_api.websocket_command(
{ {
vol.Required("type"): "config_entries/get", vol.Required("type"): "config_entries/get_matching",
vol.Optional("type_filter"): vol.All(cv.ensure_list, [str]), vol.Optional("type_filter"): vol.All(cv.ensure_list, [str]),
vol.Optional("domain"): str, vol.Optional("domain"): str,
} }
) )
@websocket_api.async_response @websocket_api.async_response
async def config_entries_get( async def config_entries_get_matching(
hass: HomeAssistant, hass: HomeAssistant,
connection: websocket_api.ActiveConnection, connection: websocket_api.ActiveConnection,
msg: dict[str, Any], msg: dict[str, Any],

View File

@ -36,7 +36,7 @@ async def test_options_flow_disabled_not_setup(
await ws_client.send_json( await ws_client.send_json(
{ {
"id": 5, "id": 5,
"type": "config_entries/get", "type": "config_entries/get_matching",
"domain": "bluetooth", "domain": "bluetooth",
} }
) )
@ -370,7 +370,7 @@ async def test_options_flow_disabled_macos(
await ws_client.send_json( await ws_client.send_json(
{ {
"id": 5, "id": 5,
"type": "config_entries/get", "type": "config_entries/get_matching",
"domain": "bluetooth", "domain": "bluetooth",
} }
) )
@ -403,7 +403,7 @@ async def test_options_flow_enabled_linux(
await ws_client.send_json( await ws_client.send_json(
{ {
"id": 5, "id": 5,
"type": "config_entries/get", "type": "config_entries/get_matching",
"domain": "bluetooth", "domain": "bluetooth",
} }
) )

View File

@ -965,6 +965,55 @@ async def test_options_flow_with_invalid_data(hass: HomeAssistant, client) -> No
} }
async def test_get(hass: HomeAssistant, hass_ws_client: WebSocketGenerator) -> None:
"""Test that we can get a config entry."""
assert await async_setup_component(hass, "config", {})
ws_client = await hass_ws_client(hass)
entry = MockConfigEntry(domain="demo", state=core_ce.ConfigEntryState.LOADED)
entry.add_to_hass(hass)
assert entry.pref_disable_new_entities is False
assert entry.pref_disable_polling is False
await ws_client.send_json_auto_id(
{
"type": "config_entries/get",
"entry_id": entry.entry_id,
}
)
response = await ws_client.receive_json()
assert response["success"]
assert response["result"]["config_entry"] == {
"disabled_by": None,
"domain": "demo",
"entry_id": entry.entry_id,
"pref_disable_new_entities": False,
"pref_disable_polling": False,
"reason": None,
"source": "user",
"state": "loaded",
"supports_options": False,
"supports_remove_device": False,
"supports_unload": False,
"title": "Mock Title",
}
await ws_client.send_json_auto_id(
{
"type": "config_entries/get",
"entry_id": "blah",
}
)
response = await ws_client.receive_json()
assert not response["success"]
assert response["error"] == {
"code": "not_found",
"message": "Config entry not found",
}
async def test_update_prefrences( async def test_update_prefrences(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None: ) -> None:
@ -1209,7 +1258,7 @@ async def test_ignore_flow_nonexisting(
assert response["error"]["code"] == "not_found" assert response["error"]["code"] == "not_found"
async def test_get_entries_ws( async def test_get_matching_entries_ws(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, clear_handlers hass: HomeAssistant, hass_ws_client: WebSocketGenerator, clear_handlers
) -> None: ) -> None:
"""Test get entries with the websocket api.""" """Test get entries with the websocket api."""
@ -1260,14 +1309,8 @@ async def test_get_entries_ws(
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
await ws_client.send_json( await ws_client.send_json_auto_id({"type": "config_entries/get_matching"})
{
"id": 5,
"type": "config_entries/get",
}
)
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response["id"] == 5
assert response["result"] == [ assert response["result"] == [
{ {
"disabled_by": None, "disabled_by": None,
@ -1341,16 +1384,14 @@ async def test_get_entries_ws(
}, },
] ]
await ws_client.send_json( await ws_client.send_json_auto_id(
{ {
"id": 6, "type": "config_entries/get_matching",
"type": "config_entries/get",
"domain": "comp1", "domain": "comp1",
"type_filter": "hub", "type_filter": "hub",
} }
) )
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response["id"] == 6
assert response["result"] == [ assert response["result"] == [
{ {
"disabled_by": None, "disabled_by": None,
@ -1368,15 +1409,13 @@ async def test_get_entries_ws(
} }
] ]
await ws_client.send_json( await ws_client.send_json_auto_id(
{ {
"id": 7, "type": "config_entries/get_matching",
"type": "config_entries/get",
"type_filter": ["service", "device"], "type_filter": ["service", "device"],
} }
) )
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response["id"] == 7
assert response["result"] == [ assert response["result"] == [
{ {
"disabled_by": None, "disabled_by": None,
@ -1408,15 +1447,13 @@ async def test_get_entries_ws(
}, },
] ]
await ws_client.send_json( await ws_client.send_json_auto_id(
{ {
"id": 8, "type": "config_entries/get_matching",
"type": "config_entries/get",
"type_filter": "hub", "type_filter": "hub",
} }
) )
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response["id"] == 8
assert response["result"] == [ assert response["result"] == [
{ {
"disabled_by": None, "disabled_by": None,
@ -1453,16 +1490,14 @@ async def test_get_entries_ws(
"homeassistant.components.config.config_entries.async_get_integrations", "homeassistant.components.config.config_entries.async_get_integrations",
return_value={"any": IntegrationNotFound("any")}, return_value={"any": IntegrationNotFound("any")},
): ):
await ws_client.send_json( await ws_client.send_json_auto_id(
{ {
"id": 9, "type": "config_entries/get_matching",
"type": "config_entries/get",
"type_filter": "hub", "type_filter": "hub",
} }
) )
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response["id"] == 9
assert response["result"] == [ assert response["result"] == [
{ {
"disabled_by": None, "disabled_by": None,
@ -1541,16 +1576,14 @@ async def test_get_entries_ws(
"homeassistant.components.config.config_entries.async_get_integrations", "homeassistant.components.config.config_entries.async_get_integrations",
return_value={"any": IntegrationNotFound("any")}, return_value={"any": IntegrationNotFound("any")},
): ):
await ws_client.send_json( await ws_client.send_json_auto_id(
{ {
"id": 10, "type": "config_entries/get_matching",
"type": "config_entries/get",
"type_filter": ["helper"], "type_filter": ["helper"],
} }
) )
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response["id"] == 10
assert response["result"] == [] assert response["result"] == []
# Verify we raise if something really goes wrong # Verify we raise if something really goes wrong
@ -1559,16 +1592,14 @@ async def test_get_entries_ws(
"homeassistant.components.config.config_entries.async_get_integrations", "homeassistant.components.config.config_entries.async_get_integrations",
return_value={"any": Exception()}, return_value={"any": Exception()},
): ):
await ws_client.send_json( await ws_client.send_json_auto_id(
{ {
"id": 11, "type": "config_entries/get_matching",
"type": "config_entries/get",
"type_filter": ["device", "hub", "service"], "type_filter": ["device", "hub", "service"],
} }
) )
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response["id"] == 11
assert response["success"] is False assert response["success"] is False

View File

@ -858,7 +858,7 @@ async def test_options_flow_disabled_gen_1(
await ws_client.send_json( await ws_client.send_json(
{ {
"id": 5, "id": 5,
"type": "config_entries/get", "type": "config_entries/get_matching",
"domain": "shelly", "domain": "shelly",
} }
) )
@ -879,7 +879,7 @@ async def test_options_flow_enabled_gen_2(
await ws_client.send_json( await ws_client.send_json(
{ {
"id": 5, "id": 5,
"type": "config_entries/get", "type": "config_entries/get_matching",
"domain": "shelly", "domain": "shelly",
} }
) )
@ -900,7 +900,7 @@ async def test_options_flow_disabled_sleepy_gen_2(
await ws_client.send_json( await ws_client.send_json(
{ {
"id": 5, "id": 5,
"type": "config_entries/get", "type": "config_entries/get_matching",
"domain": "shelly", "domain": "shelly",
} }
) )