Round dashboards: one face at a time, a face per widget type, paged by swipe, dots and keys; slider reading; face row in the editor
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
8b8cb5cda9
commit
84071ee106
@@ -90,12 +90,6 @@ INPUT_WIDGETS = {
|
||||
"color",
|
||||
}
|
||||
|
||||
#: What a round dashboard may draw on its ring: the widgets that are one value
|
||||
#: along a range, which is what turning a knob's ring can set or a circle's
|
||||
#: edge can show. Mirrored in the client
|
||||
#: (``frontend/src/components/Dashboard/DashboardView.tsx``).
|
||||
RING_WIDGETS = {"slider", "dropdown", "color", "gauge"}
|
||||
|
||||
#: What a colour widget puts on the wire, by the format it was configured for.
|
||||
#: The default is what the Node-RED instance this ports already sends its
|
||||
#: DMX encoders — ``[h, s, v]``, hue in degrees and the other two in percent —
|
||||
@@ -231,6 +225,10 @@ class WidgetDef(BaseModel):
|
||||
|
||||
The first two are a ``list`` message, the third a ``str``, which is what
|
||||
``COLOR_DTYPES`` records and the check below holds a binding to.
|
||||
|
||||
A slider may also show what it is setting: ``reading`` binds
|
||||
``{message, dtype, label}`` — a room's temperature beside its setpoint —
|
||||
held to the same numbers its target is.
|
||||
"""
|
||||
|
||||
id: str
|
||||
@@ -297,6 +295,12 @@ class WidgetDef(BaseModel):
|
||||
)
|
||||
return [*outer, *self.inner_bindings]
|
||||
|
||||
@property
|
||||
def reading(self) -> dict[str, Any]:
|
||||
"""What a slider reads beside the value it sets; empty for anything else."""
|
||||
reading = self.config.get("reading") if self.type == "slider" else None
|
||||
return reading if isinstance(reading, dict) else {}
|
||||
|
||||
@property
|
||||
def messages(self) -> list[str]:
|
||||
"""Every message name this widget reads."""
|
||||
@@ -318,8 +322,8 @@ class WidgetDef(BaseModel):
|
||||
return [
|
||||
str(row.get("message")) for row in self.bar_rows if row.get("message")
|
||||
]
|
||||
name = self.config.get("message")
|
||||
return [str(name)] if name else []
|
||||
names = [self.config.get("message"), self.reading.get("message")]
|
||||
return [str(name) for name in names if name]
|
||||
|
||||
@property
|
||||
def target(self) -> str:
|
||||
@@ -358,7 +362,10 @@ class WidgetDef(BaseModel):
|
||||
]
|
||||
if self.type == "bar":
|
||||
return [str(row.get("dtype") or "") for row in self.bar_rows]
|
||||
return [str(self.config.get("dtype") or "")]
|
||||
return [
|
||||
str(self.config.get("dtype") or ""),
|
||||
str(self.reading.get("dtype") or ""),
|
||||
]
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _check_binding(self) -> WidgetDef:
|
||||
@@ -481,14 +488,11 @@ class DashboardDef(BaseModel):
|
||||
#: "unset" and the client falls back to its default.
|
||||
canvas_width: int = Field(default=1920, ge=0, le=7680)
|
||||
canvas_height: int = Field(default=1080, ge=0, le=4320)
|
||||
#: ``round`` draws the dashboard in the circle inscribed in its canvas, for
|
||||
#: a round screen such as a rotary knob. A circle has one size, so a round
|
||||
#: canvas is square: its height is its width.
|
||||
#: ``round`` draws the dashboard on the circle inscribed in its canvas, for
|
||||
#: a round screen such as a rotary knob: one widget at a time, each a whole
|
||||
#: face, in the order of ``widgets`` (``layout`` is not read). A circle has
|
||||
#: one size, so a round canvas is square: its height is its width.
|
||||
shape: Literal["rect", "round"] = "rect"
|
||||
#: The widget a round dashboard draws along its edge instead of in the grid
|
||||
#: — one of ``RING_WIDGETS``, by id — which a knob's ring turns. Empty
|
||||
#: draws no ring.
|
||||
ring: str = ""
|
||||
#: A lucide icon name, drawn on the panel rail; empty falls back to two
|
||||
#: letters of the title.
|
||||
icon: str = ""
|
||||
@@ -550,20 +554,9 @@ class DashboardDef(BaseModel):
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _check_shape(self) -> DashboardDef:
|
||||
"""Square a round canvas, and hold the ring to a widget it can draw."""
|
||||
"""Square a round canvas."""
|
||||
if self.shape == "round":
|
||||
self.canvas_height = self.canvas_width
|
||||
if not self.ring:
|
||||
return self
|
||||
if self.shape != "round":
|
||||
raise ValueError("only a round dashboard has a ring")
|
||||
widget = next((w for w in self.widgets if w.id == self.ring), None)
|
||||
if widget is None:
|
||||
raise ValueError(
|
||||
f"the ring names '{self.ring}', which is not a widget here"
|
||||
)
|
||||
if widget.type not in RING_WIDGETS:
|
||||
raise ValueError(f"a '{widget.type}' widget cannot be drawn on the ring")
|
||||
return self
|
||||
|
||||
@property
|
||||
@@ -581,9 +574,6 @@ class DashboardDef(BaseModel):
|
||||
"""
|
||||
shapes = []
|
||||
for widget in self.widgets:
|
||||
# Drawn along the edge, so it takes no place in the grid.
|
||||
if widget.id == self.ring:
|
||||
continue
|
||||
at = (
|
||||
widget.layout.get("lg")
|
||||
or widget.layout.get("md")
|
||||
|
||||
@@ -252,7 +252,11 @@ PANEL_WIDGETS = [
|
||||
{
|
||||
"id": "w_slider",
|
||||
"type": "slider",
|
||||
"config": {"target": "demo.slider", "dtype": "float"},
|
||||
"config": {
|
||||
"target": "demo.slider",
|
||||
"dtype": "float",
|
||||
"reading": {"message": "demo.slider_reading", "dtype": "float"},
|
||||
},
|
||||
},
|
||||
{"id": "w_input", "type": "input", "config": {"target": "demo.input"}},
|
||||
{"id": "w_dropdown", "type": "dropdown", "config": {"target": "demo.dropdown"}},
|
||||
@@ -333,7 +337,12 @@ def test_a_panel_speaks_only_its_own_widgets_messages(
|
||||
), target
|
||||
|
||||
# And reads the history of everything its widgets bind to.
|
||||
for name in ("demo.temperature", "demo.switch_state", "demo.query_series"):
|
||||
for name in (
|
||||
"demo.temperature",
|
||||
"demo.switch_state",
|
||||
"demo.slider_reading",
|
||||
"demo.query_series",
|
||||
):
|
||||
assert (
|
||||
client.get(f"{messages}/{name}/history", headers=panel_headers).status_code
|
||||
== 200
|
||||
|
||||
@@ -445,25 +445,28 @@ def test_a_dashboard_is_cut_into_a_sane_number_of_columns():
|
||||
DashboardDef(name="house", columns=columns)
|
||||
|
||||
|
||||
def test_a_round_dashboard_is_square_and_rings_one_of_its_own_widgets():
|
||||
dial = WidgetDef(id="dial", type="slider", layout={"lg": Placement(x=1)})
|
||||
stat = WidgetDef(id="stat", type="stat")
|
||||
|
||||
knob = DashboardDef(
|
||||
name="knob",
|
||||
shape="round",
|
||||
canvas_width=360,
|
||||
canvas_height=200,
|
||||
ring="dial",
|
||||
widgets=[dial, stat],
|
||||
)
|
||||
def test_a_round_dashboard_is_square():
|
||||
knob = DashboardDef(name="knob", shape="round", canvas_width=360, canvas_height=200)
|
||||
|
||||
assert knob.canvas_height == 360
|
||||
# Drawn along the edge, so the outline of the grid leaves it out.
|
||||
assert [shape.type for shape in knob.footprint] == ["stat"]
|
||||
for shape, ring in (("rect", "dial"), ("round", "gone"), ("round", "stat")):
|
||||
with pytest.raises(ValueError):
|
||||
DashboardDef(name="knob", shape=shape, ring=ring, widgets=[dial, stat])
|
||||
|
||||
|
||||
def test_a_slider_reads_what_it_shows_beside_its_value():
|
||||
"""The room's temperature beside its setpoint: read, and held to numbers."""
|
||||
setpoint = {"target": "house.setpoint", "dtype": "float"}
|
||||
slider = WidgetDef(
|
||||
id="target",
|
||||
type="slider",
|
||||
config={**setpoint, "reading": {"message": "house.indoor", "dtype": "float"}},
|
||||
)
|
||||
|
||||
assert slider.messages == ["house.indoor"]
|
||||
with pytest.raises(ValueError):
|
||||
WidgetDef(
|
||||
id="target",
|
||||
type="slider",
|
||||
config={**setpoint, "reading": {"message": "house.mode", "dtype": "str"}},
|
||||
)
|
||||
|
||||
|
||||
def test_a_setting_refuses_a_message_it_cannot_carry():
|
||||
|
||||
Reference in New Issue
Block a user