From 4579fa0d9dcec100529b4c0cffae307cd7b8bf36 Mon Sep 17 00:00:00 2001 From: Nicholas Wiersma Date: Sun, 11 Aug 2024 11:06:39 +0200 Subject: [PATCH] feat: add water warning (#10) --- README.md | 5 +++++ assets/index.html | 8 ++++++++ assets/style.css | 4 ++++ main.go | 19 +++++++++++++++---- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2896dc6..2ba7e51 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ modules: sensorIds: geyserPct: sensor.geyser_hot_water tankPct: sensor.reservoir_percentage + waterConnected: binary_sensor.water_connected geyser: warning: 50 low: 30 @@ -33,6 +34,10 @@ The Home Assistant geyser percentage sensor ID. The Home Assistant water tank percentage sensor ID. +### Water Connection Sensor ID (sensorIds.waterConnected) + +The Home Assistant binary sensor ID indicating if the mains water has pressure. + ### Geyser Warning Percentage (geyser.warning) The Geyser percentage for the hot water bar to display in warning style. diff --git a/assets/index.html b/assets/index.html index 4a7f908..321e59c 100644 --- a/assets/index.html +++ b/assets/index.html @@ -16,6 +16,14 @@ % + + + + diff --git a/assets/style.css b/assets/style.css index 9e72989..6b66d45 100644 --- a/assets/style.css +++ b/assets/style.css @@ -22,6 +22,10 @@ stroke: rgb(242, 73, 92); } +#icons .off { + display: none; +} + .tank { r: var(--radius); fill: none; diff --git a/main.go b/main.go index e7481b5..73edf0a 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ package main import ( _ "embed" "fmt" + "slices" "strconv" "strings" "time" @@ -26,8 +27,9 @@ type Config struct { URL string `yaml:"url"` Token string `yaml:"token"` SensorIDs struct { - GeyserPct string `yaml:"geyserPct"` - TankPct string `yaml:"tankPct"` + GeyserPct string `yaml:"geyserPct"` + TankPct string `yaml:"tankPct"` + WaterConnected string `yaml:"waterConnected"` } `yaml:"sensorIds"` Geyser struct { Warning int `yaml:"warning"` @@ -117,7 +119,7 @@ func (m *Module) setup() error { } func (m *Module) syncStates() error { - states, err := m.ha.FilterStates("sensor") + states, err := m.ha.FilterStates("sensor", "binary_sensor") if err != nil { return fmt.Errorf("getting states: %w", err) } @@ -144,7 +146,8 @@ func (m *Module) listenStates() error { if event.EventType != "state_changed" { continue } - if strings.TrimSuffix(strings.SplitAfter(event.Data.EntityID, ".")[0], ".") != "sensor" { + prefix := strings.TrimSuffix(strings.SplitAfter(event.Data.EntityID, ".")[0], ".") + if !slices.Contains([]string{"sensor", "binary_sensor"}, prefix) { continue } @@ -206,5 +209,13 @@ func (m *Module) updateState(id, state string) { elem.Class().Add(class) } } + case m.cfg.SensorIDs.WaterConnected: + connected := state == "on" + if elem := m.mod.Element().QuerySelector("#water-disconnect"); elem != nil { + elem.Class().Remove("off") + if connected { + elem.Class().Add("off") + } + } } }