2018-02-16 22:07:38 +00:00
|
|
|
"""Http views to control the config manager."""
|
2019-08-18 04:34:11 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
2018-12-13 14:30:20 +00:00
|
|
|
from homeassistant.auth.permissions.const import CAT_CONFIG_ENTRIES
|
2019-08-18 04:34:11 +00:00
|
|
|
from homeassistant.components import websocket_api
|
2018-02-16 22:07:38 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
2018-12-13 14:30:20 +00:00
|
|
|
from homeassistant.exceptions import Unauthorized
|
2018-04-17 09:44:32 +00:00
|
|
|
from homeassistant.helpers.data_entry_flow import (
|
2019-07-31 19:25:30 +00:00
|
|
|
FlowManagerIndexView,
|
|
|
|
FlowManagerResourceView,
|
|
|
|
)
|
2019-07-08 23:19:37 +00:00
|
|
|
from homeassistant.loader import async_get_config_flows
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def async_setup(hass):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Enable the Home Assistant views."""
|
|
|
|
hass.http.register_view(ConfigManagerEntryIndexView)
|
|
|
|
hass.http.register_view(ConfigManagerEntryResourceView)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.http.register_view(ConfigManagerFlowIndexView(hass.config_entries.flow))
|
|
|
|
hass.http.register_view(ConfigManagerFlowResourceView(hass.config_entries.flow))
|
2018-02-16 22:07:38 +00:00
|
|
|
hass.http.register_view(ConfigManagerAvailableFlowView)
|
2019-08-18 04:34:11 +00:00
|
|
|
|
2019-02-22 16:59:43 +00:00
|
|
|
hass.http.register_view(
|
2019-07-31 19:25:30 +00:00
|
|
|
OptionManagerFlowIndexView(hass.config_entries.options.flow)
|
|
|
|
)
|
2019-02-22 16:59:43 +00:00
|
|
|
hass.http.register_view(
|
2019-07-31 19:25:30 +00:00
|
|
|
OptionManagerFlowResourceView(hass.config_entries.options.flow)
|
|
|
|
)
|
2019-08-18 04:34:11 +00:00
|
|
|
|
|
|
|
hass.components.websocket_api.async_register_command(system_options_list)
|
|
|
|
hass.components.websocket_api.async_register_command(system_options_update)
|
|
|
|
|
2018-02-16 22:07:38 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def _prepare_json(result):
|
|
|
|
"""Convert result for JSON."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if result["type"] != data_entry_flow.RESULT_TYPE_FORM:
|
2018-02-16 22:07:38 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
import voluptuous_serialize
|
|
|
|
|
|
|
|
data = result.copy()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
schema = data["data_schema"]
|
2018-02-16 22:07:38 +00:00
|
|
|
if schema is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["data_schema"] = []
|
2018-02-16 22:07:38 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["data_schema"] = voluptuous_serialize.convert(schema)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigManagerEntryIndexView(HomeAssistantView):
|
|
|
|
"""View to get available config entries."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/config/config_entries/entry"
|
|
|
|
name = "api:config:config_entries:entry"
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def get(self, request):
|
2019-02-22 16:59:43 +00:00
|
|
|
"""List available config entries."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass = request.app["hass"]
|
|
|
|
|
2019-08-16 23:19:19 +00:00
|
|
|
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(
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"domain": entry.domain,
|
|
|
|
"title": entry.title,
|
|
|
|
"source": entry.source,
|
|
|
|
"state": entry.state,
|
|
|
|
"connection_class": entry.connection_class,
|
2019-08-16 23:19:19 +00:00
|
|
|
"supports_options": supports_options,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
2019-08-16 23:19:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return self.json(results)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigManagerEntryResourceView(HomeAssistantView):
|
|
|
|
"""View to interact with a config entry."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/config/config_entries/entry/{entry_id}"
|
|
|
|
name = "api:config:config_entries:entry:resource"
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def delete(self, request, entry_id):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Delete a config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if not request["hass_user"].is_admin:
|
|
|
|
raise Unauthorized(config_entry_id=entry_id, permission="remove")
|
2018-12-13 14:30:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass = request.app["hass"]
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
try:
|
2018-10-01 06:50:05 +00:00
|
|
|
result = await hass.config_entries.async_remove(entry_id)
|
2018-02-16 22:07:38 +00:00
|
|
|
except config_entries.UnknownEntry:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json_message("Invalid entry specified", 404)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
return self.json(result)
|
|
|
|
|
|
|
|
|
2018-04-17 09:44:32 +00:00
|
|
|
class ConfigManagerFlowIndexView(FlowManagerIndexView):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""View to create config flows."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/config/config_entries/flow"
|
|
|
|
name = "api:config:config_entries:flow"
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def get(self, request):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""List flows that are in progress but not started by a user.
|
|
|
|
|
|
|
|
Example of a non-user initiated flow is a discovered Hue hub that
|
|
|
|
requires user interaction to finish setup.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
if not request["hass_user"].is_admin:
|
|
|
|
raise Unauthorized(perm_category=CAT_CONFIG_ENTRIES, permission="add")
|
2018-12-13 14:30:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass = request.app["hass"]
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json(
|
|
|
|
[
|
|
|
|
flw
|
|
|
|
for flw in hass.config_entries.flow.async_progress()
|
|
|
|
if flw["context"]["source"] != config_entries.SOURCE_USER
|
|
|
|
]
|
|
|
|
)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-12-13 14:30:20 +00:00
|
|
|
# pylint: disable=arguments-differ
|
|
|
|
async def post(self, request):
|
|
|
|
"""Handle a POST request."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if not request["hass_user"].is_admin:
|
|
|
|
raise Unauthorized(perm_category=CAT_CONFIG_ENTRIES, permission="add")
|
2018-12-13 14:30:20 +00:00
|
|
|
|
|
|
|
# pylint: disable=no-value-for-parameter
|
|
|
|
return await super().post(request)
|
|
|
|
|
2019-03-15 14:41:34 +00:00
|
|
|
def _prepare_result_json(self, result):
|
|
|
|
"""Convert result to JSON."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if result["type"] != data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
|
2019-03-15 14:41:34 +00:00
|
|
|
return super()._prepare_result_json(result)
|
|
|
|
|
|
|
|
data = result.copy()
|
2019-07-31 19:25:30 +00:00
|
|
|
data["result"] = data["result"].entry_id
|
|
|
|
data.pop("data")
|
2019-03-15 14:41:34 +00:00
|
|
|
return data
|
|
|
|
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-04-17 09:44:32 +00:00
|
|
|
class ConfigManagerFlowResourceView(FlowManagerResourceView):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""View to interact with the flow manager."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/config/config_entries/flow/{flow_id}"
|
|
|
|
name = "api:config:config_entries:flow:resource"
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-12-13 14:30:20 +00:00
|
|
|
async def get(self, request, flow_id):
|
|
|
|
"""Get the current state of a data_entry_flow."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if not request["hass_user"].is_admin:
|
|
|
|
raise Unauthorized(perm_category=CAT_CONFIG_ENTRIES, permission="add")
|
2018-12-13 14:30:20 +00:00
|
|
|
|
|
|
|
return await super().get(request, flow_id)
|
|
|
|
|
|
|
|
# pylint: disable=arguments-differ
|
|
|
|
async def post(self, request, flow_id):
|
|
|
|
"""Handle a POST request."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if not request["hass_user"].is_admin:
|
|
|
|
raise Unauthorized(perm_category=CAT_CONFIG_ENTRIES, permission="add")
|
2018-12-13 14:30:20 +00:00
|
|
|
|
|
|
|
# pylint: disable=no-value-for-parameter
|
|
|
|
return await super().post(request, flow_id)
|
|
|
|
|
2019-03-15 14:41:34 +00:00
|
|
|
def _prepare_result_json(self, result):
|
|
|
|
"""Convert result to JSON."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if result["type"] != data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
|
2019-03-15 14:41:34 +00:00
|
|
|
return super()._prepare_result_json(result)
|
|
|
|
|
|
|
|
data = result.copy()
|
2019-07-31 19:25:30 +00:00
|
|
|
data["result"] = data["result"].entry_id
|
|
|
|
data.pop("data")
|
2019-03-15 14:41:34 +00:00
|
|
|
return data
|
|
|
|
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
class ConfigManagerAvailableFlowView(HomeAssistantView):
|
|
|
|
"""View to query available flows."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/config/config_entries/flow_handlers"
|
|
|
|
name = "api:config:config_entries:flow_handlers"
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def get(self, request):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""List available flow handlers."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass = request.app["hass"]
|
2019-07-08 23:19:37 +00:00
|
|
|
return self.json(await async_get_config_flows(hass))
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OptionManagerFlowIndexView(FlowManagerIndexView):
|
|
|
|
"""View to create option flows."""
|
|
|
|
|
2019-08-15 21:11:55 +00:00
|
|
|
url = "/api/config/config_entries/options/flow"
|
|
|
|
name = "api:config:config_entries:option:flow"
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
# pylint: disable=arguments-differ
|
|
|
|
async def post(self, request):
|
|
|
|
"""Handle a POST request.
|
|
|
|
|
|
|
|
handler in request is entry_id.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
if not request["hass_user"].is_admin:
|
|
|
|
raise Unauthorized(perm_category=CAT_CONFIG_ENTRIES, permission="edit")
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
# pylint: disable=no-value-for-parameter
|
|
|
|
return await super().post(request)
|
|
|
|
|
|
|
|
|
2019-03-15 14:41:34 +00:00
|
|
|
class OptionManagerFlowResourceView(FlowManagerResourceView):
|
2019-02-22 16:59:43 +00:00
|
|
|
"""View to interact with the option flow manager."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/config/config_entries/options/flow/{flow_id}"
|
|
|
|
name = "api:config:config_entries:options:flow:resource"
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
async def get(self, request, flow_id):
|
|
|
|
"""Get the current state of a data_entry_flow."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if not request["hass_user"].is_admin:
|
|
|
|
raise Unauthorized(perm_category=CAT_CONFIG_ENTRIES, permission="edit")
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
return await super().get(request, flow_id)
|
|
|
|
|
|
|
|
# pylint: disable=arguments-differ
|
|
|
|
async def post(self, request, flow_id):
|
|
|
|
"""Handle a POST request."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if not request["hass_user"].is_admin:
|
|
|
|
raise Unauthorized(perm_category=CAT_CONFIG_ENTRIES, permission="edit")
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
# pylint: disable=no-value-for-parameter
|
|
|
|
return await super().post(request, flow_id)
|
2019-08-18 04:34:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.async_response
|
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{"type": "config_entries/system_options/list", "entry_id": str}
|
|
|
|
)
|
|
|
|
async def system_options_list(hass, connection, msg):
|
|
|
|
"""List all system options for a config entry."""
|
|
|
|
entry_id = msg["entry_id"]
|
|
|
|
entry = hass.config_entries.async_get_entry(entry_id)
|
|
|
|
|
|
|
|
if entry:
|
|
|
|
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/system_options/update",
|
|
|
|
"entry_id": str,
|
|
|
|
vol.Optional("disable_new_entities"): bool,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
async def system_options_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)
|
|
|
|
|
2019-08-19 23:45:17 +00:00
|
|
|
if entry is None:
|
|
|
|
connection.send_error(
|
|
|
|
msg["id"], websocket_api.const.ERR_NOT_FOUND, "Config entry not found"
|
|
|
|
)
|
|
|
|
return
|
2019-08-18 04:34:11 +00:00
|
|
|
|
2019-08-19 23:45:17 +00:00
|
|
|
hass.config_entries.async_update_entry(entry, system_options=changes)
|
|
|
|
connection.send_result(msg["id"], entry.system_options.as_dict())
|