Add API interface to request a complete device config as JSON. (#3911)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
RoboMagus 2022-10-20 05:39:34 +02:00 committed by GitHub
parent a21c3e8e2d
commit e87edcc77a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,6 +29,7 @@ import tornado.web
import tornado.websocket
from esphome import const, platformio_api, util, yaml_util
from esphome.core import EsphomeError
from esphome.helpers import mkdir_p, get_bool_env, run_system_command
from esphome.storage_json import (
EsphomeStorageJSON,
@ -927,6 +928,27 @@ class SecretKeysRequestHandler(BaseHandler):
self.write(json.dumps(secret_keys))
class JsonConfigRequestHandler(BaseHandler):
@authenticated
@bind_config
def get(self, configuration=None):
filename = settings.rel_path(configuration)
if not os.path.isfile(filename):
self.send_error(404)
return
try:
content = yaml_util.load_yaml(filename, clear_secrets=False)
json_content = json.dumps(
content, default=lambda o: {"__type": str(type(o)), "repr": repr(o)}
)
self.set_header("content-type", "application/json")
self.write(json_content)
except EsphomeError as err:
_LOGGER.warning("Error translating file %s to JSON: %s", filename, err)
self.send_error(500)
def get_base_frontend_path():
if ENV_DEV not in os.environ:
import esphome_dashboard
@ -1031,6 +1053,7 @@ def make_app(debug=get_bool_env(ENV_DEV)):
(f"{rel}devices", ListDevicesHandler),
(f"{rel}import", ImportRequestHandler),
(f"{rel}secret_keys", SecretKeysRequestHandler),
(f"{rel}json-config", JsonConfigRequestHandler),
(f"{rel}rename", EsphomeRenameHandler),
(f"{rel}prometheus-sd", PrometheusServiceDiscoveryHandler),
],