Add api to reload config entries (#39068)

This commit is contained in:
J. Nick Koston 2020-08-25 17:59:22 -05:00 committed by GitHub
parent 11f121b008
commit e109b04efe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 129 additions and 8 deletions

View file

@ -7,7 +7,7 @@ from homeassistant import config_entries, data_entry_flow
from homeassistant.auth.permissions.const import CAT_CONFIG_ENTRIES, POLICY_EDIT
from homeassistant.components import websocket_api
from homeassistant.components.http import HomeAssistantView
from homeassistant.const import HTTP_NOT_FOUND
from homeassistant.const import HTTP_FORBIDDEN, HTTP_NOT_FOUND
from homeassistant.core import callback
from homeassistant.exceptions import Unauthorized
import homeassistant.helpers.config_validation as cv
@ -22,6 +22,7 @@ async def async_setup(hass):
"""Enable the Home Assistant views."""
hass.http.register_view(ConfigManagerEntryIndexView)
hass.http.register_view(ConfigManagerEntryResourceView)
hass.http.register_view(ConfigManagerEntryResourceReloadView)
hass.http.register_view(ConfigManagerFlowIndexView(hass.config_entries.flow))
hass.http.register_view(ConfigManagerFlowResourceView(hass.config_entries.flow))
hass.http.register_view(ConfigManagerAvailableFlowView)
@ -92,6 +93,29 @@ class ConfigManagerEntryResourceView(HomeAssistantView):
return self.json(result)
class ConfigManagerEntryResourceReloadView(HomeAssistantView):
"""View to reload a config entry."""
url = "/api/config/config_entries/entry/{entry_id}/reload"
name = "api:config:config_entries:entry:resource:reload"
async def post(self, request, entry_id):
"""Reload a config entry."""
if not request["hass_user"].is_admin:
raise Unauthorized(config_entry_id=entry_id, permission="remove")
hass = request.app["hass"]
try:
result = await hass.config_entries.async_reload(entry_id)
except config_entries.OperationNotAllowed:
return self.json_message("Entry cannot be reloaded", HTTP_FORBIDDEN)
except config_entries.UnknownEntry:
return self.json_message("Invalid entry specified", HTTP_NOT_FOUND)
return self.json({"require_restart": not result})
class ConfigManagerFlowIndexView(FlowManagerIndexView):
"""View to create config flows."""
@ -345,4 +369,5 @@ def entry_json(entry: config_entries.ConfigEntry) -> dict:
"state": entry.state,
"connection_class": entry.connection_class,
"supports_options": supports_options,
"supports_unload": entry.supports_unload,
}