Parcourir la source

docs: Document single toggle latch

jamesread il y a 2 jours
Parent
commit
ef3c69b2b9

+ 30 - 0
docs/modules/ROOT/examples/solutions/single-button-toggle/config.yaml

@@ -0,0 +1,30 @@
+entities:
+  - file: /etc/OliveTin/entities/water_valve.yaml
+    name: water_valve
+
+actions:
+  - title: Toggle valve
+    icon: '<img src="/custom-webui/icons/{{ .CurrentEntity.state }}.png" width="48px" />'
+    entity: water_valve
+    shell: |
+      set -e
+      if [ "{{ .CurrentEntity.state }}" = "open" ]; then
+        next_state=closed
+      else
+        next_state=open
+      fi
+      echo "Setting {{ .CurrentEntity.title }} to $next_state"
+      printf -- '- title: %s\n  state: %s\n' "{{ .CurrentEntity.title }}" "$next_state" > /etc/OliveTin/entities/water_valve.yaml
+
+dashboards:
+  - title: Water Valve
+    contents:
+      - title: "{{ water_valve.title }}"
+        type: fieldset
+        entity: water_valve
+        contents:
+          - type: display
+            title: |
+              State: <strong>{{ water_valve.state }}</strong>
+
+          - title: Toggle valve

+ 2 - 0
docs/modules/ROOT/examples/solutions/single-button-toggle/water_valve.yaml

@@ -0,0 +1,2 @@
+- title: Garden water valve
+  state: closed

+ 3 - 0
docs/modules/ROOT/images/solutions/single-button-toggle/.gitignore

@@ -0,0 +1,3 @@
+custom-webui/
+__pycache__/
+water_valve.yaml

+ 23 - 0
docs/modules/ROOT/images/solutions/single-button-toggle/Makefile

@@ -0,0 +1,23 @@
+CONFIGDIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
+ENTITY_SEED := $(CONFIGDIR)../../../examples/solutions/single-button-toggle/water_valve.yaml
+
+# The screenshot config reads and writes this fixture, so captures never touch
+# a real /etc/OliveTin install.
+export WATER_VALVE_ENTITY_FILE := $(CONFIGDIR)water_valve.yaml
+
+include ../../screenshots.mk
+
+start: seed-entity seed-icons
+
+.DEFAULT_GOAL := screenshots
+.PHONY: screenshots seed-entity seed-icons
+
+seed-entity:
+	cp "$(ENTITY_SEED)" "$(WATER_VALVE_ENTITY_FILE)"
+
+screenshots: update-screenshots
+
+# OliveTin serves the action icons from custom-webui/ next to config.yaml.
+seed-icons:
+	mkdir -p "$(CONFIGDIR)custom-webui/icons"
+	cp "$(CONFIGDIR)icons/"*.png "$(CONFIGDIR)custom-webui/icons/"

+ 37 - 0
docs/modules/ROOT/images/solutions/single-button-toggle/config.yaml

@@ -0,0 +1,37 @@
+---
+listenAddressSingleHTTPFrontend: 127.0.0.1:11337
+
+logLevel: "WARN"
+checkForUpdates: false
+showFooter: false
+
+entities:
+  - file: ${{ WATER_VALVE_ENTITY_FILE }}
+    name: water_valve
+
+actions:
+  - title: Toggle valve
+    icon: '<img src="/custom-webui/icons/{{ .CurrentEntity.state }}.png" width="48px" />'
+    entity: water_valve
+    shell: |
+      set -e
+      if [ "{{ .CurrentEntity.state }}" = "open" ]; then
+        next_state=closed
+      else
+        next_state=open
+      fi
+      echo "Setting {{ .CurrentEntity.title }} to $next_state"
+      printf -- '- title: %s\n  state: %s\n' "{{ .CurrentEntity.title }}" "$next_state" > "${{ WATER_VALVE_ENTITY_FILE }}"
+
+dashboards:
+  - title: Water Valve
+    contents:
+      - title: "{{ water_valve.title }}"
+        type: fieldset
+        entity: water_valve
+        contents:
+          - type: display
+            title: |
+              State: <strong>{{ water_valve.state }}</strong>
+
+          - title: Toggle valve

BIN
docs/modules/ROOT/images/solutions/single-button-toggle/dashboard-closed.png


BIN
docs/modules/ROOT/images/solutions/single-button-toggle/dashboard-open.png


BIN
docs/modules/ROOT/images/solutions/single-button-toggle/icons/closed.png


BIN
docs/modules/ROOT/images/solutions/single-button-toggle/icons/open.png


+ 16 - 0
docs/modules/ROOT/images/solutions/single-button-toggle/screenshots.ini

@@ -0,0 +1,16 @@
+[DEFAULT]
+base_url = http://localhost:11337/
+dir = .
+width = 900
+height = 260
+post_script_sleep = 0.5
+
+[closed]
+url = /dashboards/Water%20Valve
+name = dashboard-closed
+script = setup_closed.py
+
+[open]
+url = /dashboards/Water%20Valve
+name = dashboard-open
+script = setup_open.py

+ 29 - 0
docs/modules/ROOT/images/solutions/single-button-toggle/setup_closed.py

@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+"""Open the Water Valve dashboard with the valve closed."""
+
+import time
+
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support.ui import WebDriverWait
+
+
+def wait_for_dashboard(driver, timeout=30):
+    WebDriverWait(driver, timeout).until(
+        lambda d: bool(d.find_element(By.TAG_NAME, "body").get_attribute("loaded-dashboard"))
+    )
+
+
+def wait_for_state(driver, state, timeout=30):
+    def ready(d):
+        try:
+            return f"State: {state}" in d.find_element(By.TAG_NAME, "main").text
+        except Exception:
+            return False
+
+    WebDriverWait(driver, timeout).until(ready)
+
+
+def run(driver):
+    wait_for_dashboard(driver)
+    wait_for_state(driver, "closed")
+    time.sleep(0.2)

+ 37 - 0
docs/modules/ROOT/images/solutions/single-button-toggle/setup_open.py

@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+"""Click Toggle valve and wait for the state and icon to show the valve open."""
+
+import time
+
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support.ui import WebDriverWait
+
+
+def wait_for_dashboard(driver, timeout=30):
+    WebDriverWait(driver, timeout).until(
+        lambda d: bool(d.find_element(By.TAG_NAME, "body").get_attribute("loaded-dashboard"))
+    )
+
+
+def wait_for_state(driver, state, timeout=30):
+    def ready(d):
+        try:
+            return f"State: {state}" in d.find_element(By.TAG_NAME, "main").text
+        except Exception:
+            return False
+
+    WebDriverWait(driver, timeout).until(ready)
+
+
+def run(driver):
+    wait_for_dashboard(driver)
+    wait_for_state(driver, "closed")
+    driver.find_element(By.CSS_SELECTOR, 'button[title="Toggle valve"]').click()
+    wait_for_state(driver, "open")
+    WebDriverWait(driver, 10).until(
+        lambda d: d.find_elements(By.CSS_SELECTOR, 'button[title="Toggle valve"] img[src$="/open.png"]')
+    )
+    WebDriverWait(driver, 15).until(
+        lambda d: "Toggle valve" in d.find_element(By.CSS_SELECTOR, 'button[title="Toggle valve"]').text
+    )
+    time.sleep(0.5)

+ 1 - 0
docs/modules/ROOT/nav.adoc

@@ -139,6 +139,7 @@
 ** xref:solutions/heating-control-panel/index.adoc[Heating Control Panel]
 ** xref:solutions/heating-control-panel/index.adoc[Heating Control Panel]
 ** xref:solutions/human-in-the-control-loop/index.adoc[Human in the Control Loop]
 ** xref:solutions/human-in-the-control-loop/index.adoc[Human in the Control Loop]
 ** xref:solutions/latching-toggle/index.adoc[Latching Toggle]
 ** xref:solutions/latching-toggle/index.adoc[Latching Toggle]
+** xref:solutions/single-button-toggle/index.adoc[Single Button Toggle]
 ** xref:solutions/k8s-control-panel-hosted/index.adoc[Kubernetes Control Panel (hosted)]
 ** xref:solutions/k8s-control-panel-hosted/index.adoc[Kubernetes Control Panel (hosted)]
 ** xref:solutions/primitive-password/index.adoc[Primitive Password Protection]
 ** xref:solutions/primitive-password/index.adoc[Primitive Password Protection]
 ** xref:solutions/wol/index.adoc[Wake on LAN]
 ** xref:solutions/wol/index.adoc[Wake on LAN]

+ 1 - 0
docs/modules/ROOT/pages/solutions/latching-toggle/index.adoc

@@ -56,3 +56,4 @@ include::example$solutions/latching-toggle/config.yaml[]
 * xref:action_customization/enabledExpression.adoc[Enabled expressions]
 * xref:action_customization/enabledExpression.adoc[Enabled expressions]
 * xref:solutions/heating-control-panel/index.adoc[Heating Control Panel]
 * xref:solutions/heating-control-panel/index.adoc[Heating Control Panel]
 * xref:solutions/human-in-the-control-loop/index.adoc[Human in the Control Loop]
 * xref:solutions/human-in-the-control-loop/index.adoc[Human in the Control Loop]
+* xref:solutions/single-button-toggle/index.adoc[Single Button Toggle]

+ 72 - 0
docs/modules/ROOT/pages/solutions/single-button-toggle/index.adoc

@@ -0,0 +1,72 @@
+[#single-button-toggle]
+= Single Button Toggle
+
+A single button toggle controls something that has two states, using just one action. Each press flips the state, and the button's icon changes to show the current state - a "closed" icon when the state is `closed`, and an "open" icon when the state is `open`.
+
+Unlike the xref:solutions/latching-toggle/index.adoc[Latching Toggle], which uses two interlocked actions, the button here is never disabled. It can always be pressed, and it always does the opposite of the current state.
+
+In OliveTin, the state is stored in an xref:entities/intro.adoc[entity file], which the action updates itself, and the icon is chosen with an xref:action_customization/icons.adoc[image icon] whose file name comes from the entity's state.
+
+The example on this page controls a garden water valve.
+
+[cols="1,1"]
+|===
+| Valve closed | Valve open
+
+a| image::solutions/single-button-toggle/dashboard-closed.png[]
+a| image::solutions/single-button-toggle/dashboard-open.png[]
+|===
+
+== How it works
+
+* The entity file `/etc/OliveTin/entities/water_valve.yaml` holds the state of the valve (`open` or `closed`). It is the single source of truth for the dashboard.
+* The **Toggle valve** action's icon is an `<img>` tag that uses `{{ .CurrentEntity.state }}` as the file name, so state `closed` shows `closed.png` and state `open` shows `open.png`.
+* The action has no `enabledExpression`, so it is never disabled.
+* When it runs, the action reads the current state, works out the opposite state, and writes that back to the entity file. OliveTin watches entity files, so the dashboard and the icon update as soon as the file changes.
+
+== Icons
+
+Save the two icons into the `custom-webui/icons/` directory next to your `config.yaml`, so that OliveTin serves them at `/custom-webui/icons/closed.png` and `/custom-webui/icons/open.png`. See xref:action_customization/icons.adoc[Icons] for more about serving your own icons.
+
+[cols="1,1"]
+|===
+| `closed.png` | `open.png`
+
+a| image::solutions/single-button-toggle/icons/closed.png[closed.png,48]
+a| image::solutions/single-button-toggle/icons/open.png[open.png,48]
+|===
+
+You can use any images you like, as long as each one is named after the state it represents.
+
+== Entity file
+
+[source,yaml]
+.`/etc/OliveTin/entities/water_valve.yaml`
+----
+include::example$solutions/single-button-toggle/water_valve.yaml[]
+----
+
+== Configuration
+
+[source,yaml]
+.`config.yaml`
+----
+include::example$solutions/single-button-toggle/config.yaml[]
+----
+
+== Customising this pattern
+
+* The same pattern works for anything with two states, such as lights, relays, services, or a maintenance mode. Add more icons if your entity has more than two states, and change the action to cycle through them.
+* Replace the `echo` command with your real hardware control, for example a GPIO command, an MQTT publish, or a call to a smart plug API.
+* Only write the new state to the entity file once the hardware command has succeeded. If the command fails, the shell exits before the state changes and the dashboard keeps showing the real state.
+* If something else can change the device (a physical switch, a schedule), have a script update the entity file too. The icon will then follow it.
+* The icon path starts with `/`, so it works from any dashboard. If OliveTin is served under a sub-path behind a reverse proxy, include that sub-path in the icon's `src`.
+* This action rewrites the whole entity file, so to control several valves, give each one its own entity file (or update the file in place with a tool such as `yq`).
+* Add ACLs if only certain users should be able to operate the valve.
+
+== See also
+
+* xref:entities/intro.adoc[Entities]
+* xref:action_customization/icons.adoc[Icons]
+* xref:solutions/latching-toggle/index.adoc[Latching Toggle]
+* xref:solutions/human-in-the-control-loop/index.adoc[Human in the Control Loop]