1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-13 00:32:20 +01:00

[web_server] add entity sorting for v3 (#6445)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
RFDarter
2024-05-30 03:54:20 +02:00
committed by GitHub
parent 854d3f2e4a
commit a7fc1a6298
20 changed files with 802 additions and 503 deletions

View File

@@ -1,6 +1,9 @@
from __future__ import annotations
import gzip
import esphome.codegen as cg
import esphome.config_validation as cv
import esphome.final_validate as fv
from esphome.components import web_server_base
from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID
from esphome.const import (
@@ -19,6 +22,8 @@ from esphome.const import (
CONF_LOG,
CONF_VERSION,
CONF_LOCAL,
CONF_WEB_SERVER_ID,
CONF_WEB_SERVER_SORTING_WEIGHT,
PLATFORM_ESP32,
PLATFORM_ESP8266,
PLATFORM_BK72XX,
@@ -64,6 +69,46 @@ def validate_ota(config):
return config
def _validate_no_sorting_weight(
webserver_version: int, config: dict, path: list[str] | None = None
) -> None:
if path is None:
path = []
if CONF_WEB_SERVER_SORTING_WEIGHT in config:
raise cv.FinalExternalInvalid(
f"Sorting weight on entities is not supported in web_server version {webserver_version}",
path=path + [CONF_WEB_SERVER_SORTING_WEIGHT],
)
for p, value in config.items():
if isinstance(value, dict):
_validate_no_sorting_weight(webserver_version, value, path + [p])
elif isinstance(value, list):
for i, item in enumerate(value):
if isinstance(item, dict):
_validate_no_sorting_weight(webserver_version, item, path + [p, i])
def _final_validate_sorting_weight(config):
if (webserver_version := config.get(CONF_VERSION)) != 3:
_validate_no_sorting_weight(webserver_version, fv.full_config.get())
return config
FINAL_VALIDATE_SCHEMA = _final_validate_sorting_weight
WEBSERVER_SORTING_SCHEMA = cv.Schema(
{
cv.OnlyWith(CONF_WEB_SERVER_ID, "web_server"): cv.use_id(WebServer),
cv.Optional(CONF_WEB_SERVER_SORTING_WEIGHT): cv.All(
cv.requires_component("web_server"),
cv.float_,
),
}
)
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
@@ -108,6 +153,19 @@ CONFIG_SCHEMA = cv.All(
)
def add_entity_to_sorting_list(web_server, entity, config):
sorting_weight = 50
if CONF_WEB_SERVER_SORTING_WEIGHT in config:
sorting_weight = config[CONF_WEB_SERVER_SORTING_WEIGHT]
cg.add(
web_server.add_entity_to_sorting_list(
entity,
sorting_weight,
)
)
def build_index_html(config) -> str:
html = "<!DOCTYPE html><html><head><meta charset=UTF-8><link rel=icon href=data:>"
css_include = config.get(CONF_CSS_INCLUDE)