Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog/highlights/320.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"de": {
"*": "Home Assistant Ports werden auf dem Node installiert und betrieben"
},
"en": {
"*": "Install and run Home Assistant ports on the node"
}
}
7 changes: 6 additions & 1 deletion dashboard/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Changelog

## v0.12.1-pre.2 (2026-12-04)
## v0.12.1-pre.3 (2026-12-06)

### Features

- **dashboard:** A compact header whose status chip names the cause of a problem (#316) (6f5aa61)
- **overview:** add a device map card to the overview (#318) (1557b48)
- **dashboard:** allow Home Assistant port sections in the config schema (02c1387)
- **dashboard:** list Home Assistant port units in diagnostics and service logs (4ab3000)
- **dashboard:** let the system action helper read and stop Home Assistant port units (9e8f14c)
- **ha:** install and run Home Assistant ports on the node (#320)
- **overview:** add the device_map card type to the layout settings (a226f60)
- **overview:** add viewport gestures and fit spring for map cards (62c9456)
- **overview:** add the device map card component (a99435a)
Expand All @@ -22,6 +26,7 @@

### Fixes

- **dashboard:** show port sections in the system settings form (1856b7d)
- **energy:** show role edges with the data layer on the live core (124761f)
- **overview:** make viewport attach idempotent (7d40301)
- **overview:** clear the refresh timer when a live map is destroyed (d6ab607)
Expand Down
2 changes: 1 addition & 1 deletion dashboard/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.12.1-pre.2
v0.12.1-pre.3
13 changes: 12 additions & 1 deletion dashboard/cmd/schemagen/core.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,18 @@
"title": "Services",
"additionalProperties": false,
"required": [],
"properties": {}
"properties": {},
"patternProperties": {
"^ha_[a-z0-9_]+$": {
"title": "Home Assistant port",
"type": "object",
"additionalProperties": false,
"required": ["service_id"],
"properties": {
"service_id": {"type": "string", "pattern": "^ha_[a-z0-9_]+$", "title": "Service ID", "description": "Identifies the port in its MQTT topics."}
}
}
}
},
"installed_services": {
"type": "object",
Expand Down
58 changes: 51 additions & 7 deletions dashboard/energy-node-dashboard-system-action
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ APP_CONFIG_OWNER="energynode"
# replaces this line with a stub, so keep the assignment on one line.
JOURNALCTL="/usr/bin/journalctl"

# Manifest directory of the Home Assistant ports. The test
# scripts/tests/test_bootstrap_dashboard_read_journal.sh replaces this line
# with a temp dir, so keep the assignment on one line.
HA_MANIFEST_DIR="/etc/energy-node/manifests"

# systemctl binary for stop-services and start-services. The test
# scripts/tests/test_system_action_services.sh replaces this line with a
# stub, so keep the assignment on one line.
Expand Down Expand Up @@ -178,12 +183,25 @@ apply_app_config() {
exit 0
}

# ha_domain_ok succeeds only for a Home Assistant port domain: 1 to 40
# characters of a-z, 0-9 and underscore. The pattern is a case glob with a
# negated class, so a newline, a slash, a dot, a semicolon or an uppercase
# letter always fails.
ha_domain_ok() {
case "$1" in
''|*[!a-z0-9_]*) return 1 ;;
esac
[ "${#1}" -le 40 ]
}

# read_journal prints journal entries of one allowlisted unit as JSON lines.
# Arguments: key (dashboard|tailscale), since in minutes (15|60|360|1440|10080),
# lines (100|200|500|1000) and an optional cursor. The key is one of the
# eleven allowlisted service keys in the case below. The cursor must start with
# "s=", be at most 256 characters, and contain only [A-Za-z0-9=;], so it can
# never be read as a journalctl option. Exit codes of journalctl pass through.
# Arguments: key (dashboard|tailscale|ha-<domain>), since in minutes
# (15|60|360|1440|10080), lines (100|200|500|1000) and an optional cursor. The
# key is one of the eleven allowlisted service keys in the case below, or
# ha-<domain> for a Home Assistant port whose manifest exists. The cursor must
# start with "s=", be at most 256 characters, and contain only [A-Za-z0-9=;], so
# it can never be read as a journalctl option. Exit codes of journalctl pass
# through.
read_journal() {
key="$1"
since="$2"
Expand All @@ -202,6 +220,12 @@ read_journal() {
mosquitto) unit="mosquitto.service" ;;
caddy) unit="caddy.service" ;;
tailscale) unit="tailscaled.service" ;;
ha-*)
domain="${key#ha-}"
ha_domain_ok "$domain" || exit 65
[ -f "$HA_MANIFEST_DIR/ha_${domain}.json" ] || exit 65
unit="energy-node-ha@${domain}.service"
;;
*) exit 65 ;;
esac
case "$since" in
Expand Down Expand Up @@ -274,11 +298,31 @@ storage_helper() {
esac
}

# service_units prints the units that stop-services and start-services act on.
# Keep the names in step with the units of the read_journal case statement.
# service_units prints the units that stop-services and start-services act on:
# the fixed list, then every enabled Home Assistant port unit. Keep the fixed
# names in step with the units of the read_journal case statement.
service_units() {
printf '%s\n' automation.service battery-soc.service shelly-rpc.service \
trucki-http.service tuya.service apsystems-ez1.service
ha_port_units
}

# ha_port_units prints every enabled energy-node-ha@<domain>.service whose
# domain passes ha_domain_ok. Any other name, including the template unit
# itself, is dropped.
ha_port_units() {
"$SYSTEMCTL" list-unit-files 'energy-node-ha@*.service' --state=enabled --no-legend 2>/dev/null \
| while read -r unit _; do
case "$unit" in
energy-node-ha@*.service) : ;;
*) continue ;;
esac
domain="${unit#energy-node-ha@}"
domain="${domain%.service}"
if ha_domain_ok "$domain"; then
printf '%s\n' "energy-node-ha@${domain}.service"
fi
done
}

# service_loop VERB runs "systemctl VERB UNIT" for every enabled unit of
Expand Down
27 changes: 27 additions & 0 deletions dashboard/internal/appconfig/appconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,30 @@ func TestSchemaAcceptsConfigWithoutRuntimeDir(t *testing.T) {
t.Fatalf("Load ohne runtime_dir: %v", err)
}
}

func TestSchemaAcceptsHAPortSection(t *testing.T) {
document := validDocument()
document["services"].(map[string]any)["ha_nordpool"] = map[string]any{"service_id": "ha_nordpool"}
if _, err := appconfig.Load(writeConfig(t, document)); err != nil {
t.Fatalf("Load mit HA-Port-Abschnitt: %v", err)
}
}

func TestSchemaRejectsUnknownService(t *testing.T) {
document := validDocument()
document["services"].(map[string]any)["foo"] = map[string]any{"service_id": "foo"}
if _, err := appconfig.Load(writeConfig(t, document)); err == nil {
t.Fatal("erwartete einen Schema-Fehler fuer den unbekannten Dienst foo")
}
}

func TestSchemaRejectsHAPortExtraField(t *testing.T) {
document := validDocument()
document["services"].(map[string]any)["ha_nordpool"] = map[string]any{
"service_id": "ha_nordpool",
"poll_interval_s": 60,
}
if _, err := appconfig.Load(writeConfig(t, document)); err == nil {
t.Fatal("erwartete einen Schema-Fehler fuer ein Zusatzfeld in einem HA-Port-Abschnitt")
}
}
18 changes: 18 additions & 0 deletions dashboard/internal/appconfig/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,24 @@
},
"services": {
"additionalProperties": false,
"patternProperties": {
"^ha_[a-z0-9_]+$": {
"additionalProperties": false,
"properties": {
"service_id": {
"description": "Identifies the port in its MQTT topics.",
"pattern": "^ha_[a-z0-9_]+$",
"title": "Service ID",
"type": "string"
}
},
"required": [
"service_id"
],
"title": "Home Assistant port",
"type": "object"
}
},
"properties": {
"apsystems": {
"additionalProperties": false,
Expand Down
50 changes: 49 additions & 1 deletion dashboard/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func displayName(name string) (key, label string) {
if info, ok := displayNames[name]; ok {
return info.key, info.label
}
// Home Assistant port devices: ha_<domain>_devices.
if rest, ok := strings.CutPrefix(name, "ha_"); ok {
if domain, ok := strings.CutSuffix(rest, "_devices"); ok && domain != "" {
return "", "HA: " + domain
}
}
return "", name
}

Expand Down Expand Up @@ -534,6 +540,13 @@ func checkSchemaKeywords(raw any, path string) error {
}
}
}
if patterns, ok := schema["patternProperties"].(map[string]any); ok {
for key, child := range patterns {
if err := checkSchemaKeywords(child, path+".patternProperties."+key); err != nil {
return err
}
}
}
for _, keyword := range []string{"items", "if", "then", "else"} {
if child, ok := schema[keyword]; ok {
if err := checkSchemaKeywords(child, path+"."+keyword); err != nil {
Expand Down Expand Up @@ -666,9 +679,17 @@ func applySchema(value, rawSchema any, path string) (map[string]bool, error) {
return nil, fmt.Errorf("%s.%s is required", path, required)
}
}
patterns, _ := schema["patternProperties"].(map[string]any)
if additional, ok := schema["additionalProperties"].(bool); ok && !additional {
for _, key := range sortedKeys(object) {
if _, exists := properties[key]; !exists {
if _, exists := properties[key]; exists {
continue
}
matched, err := matchingPatternSchemas(patterns, key)
if err != nil {
return nil, err
}
if len(matched) == 0 {
return nil, fmt.Errorf("%s.%s is not allowed", path, key)
}
}
Expand All @@ -680,6 +701,16 @@ func applySchema(value, rawSchema any, path string) (map[string]bool, error) {
return nil, err
}
}
matched, err := matchingPatternSchemas(patterns, key)
if err != nil {
return nil, err
}
for _, sub := range matched {
evaluated[key] = true
if err := validateValue(object[key], sub, path+"."+key); err != nil {
return nil, err
}
}
}
}
if all, ok := schema["allOf"].([]any); ok {
Expand Down Expand Up @@ -739,6 +770,23 @@ func applySchema(value, rawSchema any, path string) (map[string]bool, error) {
return evaluated, nil
}

// matchingPatternSchemas returns the patternProperties subschemas whose
// regular expression matches key, in pattern order. An invalid pattern is an
// error, so a broken schema cannot silently accept keys.
func matchingPatternSchemas(patterns map[string]any, key string) ([]any, error) {
var matched []any
for _, pattern := range sortedKeys(patterns) {
expr, err := compiledPattern(pattern)
if err != nil {
return nil, fmt.Errorf("invalid patternProperties pattern %q", pattern)
}
if expr.MatchString(key) {
matched = append(matched, patterns[pattern])
}
}
return matched, nil
}

// branchDeclarations collects the properties of every then/else branch,
// active or not, the first declaration winning (as allProperties in
// schema-form.js). Deviating from JSON Schema on purpose: a field of an
Expand Down
30 changes: 30 additions & 0 deletions dashboard/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,33 @@ func TestAutomationRulesSchemaPatternAcceptsCustomCategories(t *testing.T) {
t.Fatal("custom:Sauna accepted")
}
}

const patternPropertiesSchema = `{"type":"object","additionalProperties":false,"properties":{"services":{"type":"object","additionalProperties":false,"properties":{},"patternProperties":{"^ha_[a-z0-9_]+$":{"type":"object","additionalProperties":false,"required":["service_id"],"properties":{"service_id":{"type":"string","pattern":"^ha_[a-z0-9_]+$"}}}}}}}`

func TestPatternPropertiesAcceptsMatchingKey(t *testing.T) {
data := []byte(`{"services":{"ha_nordpool":{"service_id":"ha_nordpool"}}}`)
if err := ValidateDocument(data, []byte(patternPropertiesSchema)); err != nil {
t.Fatalf("matching key rejected: %v", err)
}
}

func TestPatternPropertiesRejectsExtraFieldOnMatchingKey(t *testing.T) {
data := []byte(`{"services":{"ha_nordpool":{"service_id":"ha_nordpool","poll_interval_s":60}}}`)
if err := ValidateDocument(data, []byte(patternPropertiesSchema)); err == nil {
t.Fatal("extra field on a matching key was accepted")
}
}

func TestPatternPropertiesRejectsNonMatchingKey(t *testing.T) {
data := []byte(`{"services":{"foo":{"service_id":"foo"}}}`)
if err := ValidateDocument(data, []byte(patternPropertiesSchema)); err == nil {
t.Fatal("non-matching key was accepted under additionalProperties false")
}
}

func TestDisplayNameHAPort(t *testing.T) {
key, label := displayName("ha_nordpool_devices")
if key != "" || label != "HA: nordpool" {
t.Fatalf("displayName(ha_nordpool_devices) = (%q, %q), want (\"\", \"HA: nordpool\")", key, label)
}
}
15 changes: 13 additions & 2 deletions dashboard/internal/httpapi/servicelogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ import (
"context"
"errors"
"net/http"
"slices"
"strconv"

"github.com/Developer-Simon/energy-node-dashboard/internal/auth"
"github.com/Developer-Simon/energy-node-dashboard/internal/servicelogs"
)

// portManifestDir is where the installed Home Assistant port manifests live.
// A variable so tests can point it at a temporary directory.
var portManifestDir = servicelogs.DefaultPortManifestDir

// portLogUnits returns the Home Assistant port units the log screen may show.
// It is kept apart from the fixed units, so this part can be switched off alone.
func portLogUnits() []string {
return servicelogs.PortUnits(portManifestDir)
}

// ServiceLogReader reads the journal entries of one allowlisted service.
// *servicelogs.Reader implements it.
type ServiceLogReader interface {
Expand Down Expand Up @@ -60,7 +71,7 @@ func handleServiceLogs(manager *auth.Manager, reader ServiceLogReader) http.Hand
}
query.Lines = n
}
if err := query.Validate(); err != nil {
if err := query.ValidateWith(portLogUnits()); err != nil {
writeError(w, http.StatusBadRequest, "log_query_invalid", "Die Log-Abfrage ist ungültig.")
return
}
Expand Down Expand Up @@ -102,7 +113,7 @@ func handleServiceLogUnits(manager *auth.Manager) http.HandlerFunc {
return
}
writeJSON(w, map[string]any{
"units": servicelogs.Units,
"units": slices.Concat(servicelogs.Units, portLogUnits()),
"since_minutes": servicelogs.SinceMinutes,
"lines": servicelogs.LineLimits,
"default_unit": servicelogs.DefaultUnit,
Expand Down
Loading
Loading