mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 09:17:46 +01:00
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:
parent
a21c3e8e2d
commit
e87edcc77a
1 changed files with 23 additions and 0 deletions
|
@ -29,6 +29,7 @@ import tornado.web
|
||||||
import tornado.websocket
|
import tornado.websocket
|
||||||
|
|
||||||
from esphome import const, platformio_api, util, yaml_util
|
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.helpers import mkdir_p, get_bool_env, run_system_command
|
||||||
from esphome.storage_json import (
|
from esphome.storage_json import (
|
||||||
EsphomeStorageJSON,
|
EsphomeStorageJSON,
|
||||||
|
@ -927,6 +928,27 @@ class SecretKeysRequestHandler(BaseHandler):
|
||||||
self.write(json.dumps(secret_keys))
|
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():
|
def get_base_frontend_path():
|
||||||
if ENV_DEV not in os.environ:
|
if ENV_DEV not in os.environ:
|
||||||
import esphome_dashboard
|
import esphome_dashboard
|
||||||
|
@ -1031,6 +1053,7 @@ def make_app(debug=get_bool_env(ENV_DEV)):
|
||||||
(f"{rel}devices", ListDevicesHandler),
|
(f"{rel}devices", ListDevicesHandler),
|
||||||
(f"{rel}import", ImportRequestHandler),
|
(f"{rel}import", ImportRequestHandler),
|
||||||
(f"{rel}secret_keys", SecretKeysRequestHandler),
|
(f"{rel}secret_keys", SecretKeysRequestHandler),
|
||||||
|
(f"{rel}json-config", JsonConfigRequestHandler),
|
||||||
(f"{rel}rename", EsphomeRenameHandler),
|
(f"{rel}rename", EsphomeRenameHandler),
|
||||||
(f"{rel}prometheus-sd", PrometheusServiceDiscoveryHandler),
|
(f"{rel}prometheus-sd", PrometheusServiceDiscoveryHandler),
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in a new issue