Varme: gem komforttemperaturer som defaults via knap på dashboard
This commit is contained in:
@@ -389,6 +389,13 @@ sections:
|
|||||||
action: call-service
|
action: call-service
|
||||||
service: script.varme_recalculate
|
service: script.varme_recalculate
|
||||||
|
|
||||||
|
- type: button
|
||||||
|
name: Gem temperaturer som standard
|
||||||
|
icon: mdi:content-save
|
||||||
|
tap_action:
|
||||||
|
action: perform-action
|
||||||
|
perform_action: script.varme_save_defaults
|
||||||
|
|
||||||
# Ventilposition
|
# Ventilposition
|
||||||
- type: grid
|
- type: grid
|
||||||
cards:
|
cards:
|
||||||
|
|||||||
@@ -223,3 +223,18 @@ varme_recalculate:
|
|||||||
data:
|
data:
|
||||||
hvac_mode: heat
|
hvac_mode: heat
|
||||||
temperature: "{{ ferie_temp }}"
|
temperature: "{{ ferie_temp }}"
|
||||||
|
|
||||||
|
|
||||||
|
varme_save_defaults:
|
||||||
|
alias: Gem varme-standardværdier
|
||||||
|
icon: mdi:content-save
|
||||||
|
sequence:
|
||||||
|
- action: shell_command.varme_save_defaults
|
||||||
|
response_variable: result
|
||||||
|
- action: persistent_notification.create
|
||||||
|
data:
|
||||||
|
title: Varme-standardværdier gemt
|
||||||
|
message: >
|
||||||
|
{{ result.stdout if result.returncode == 0
|
||||||
|
else 'Fejl: ' ~ result.stderr }}
|
||||||
|
notification_id: varme_save_defaults
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
varme_save_defaults: "/usr/local/bin/docker exec homeassistant python3 /config/python_scripts/save_varme_defaults.py"
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Gem nuværende varme-indstillinger som nye 'initial' standardværdier i YAML-filen.
|
||||||
|
Køres inde i homeassistant Docker-containeren.
|
||||||
|
"""
|
||||||
|
import re
|
||||||
|
import json
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
YAML_FILE = "/config/include/input/number/varme.yaml"
|
||||||
|
SECRETS_FILE = "/config/secrets.yaml"
|
||||||
|
HA_URL = "http://localhost:8123"
|
||||||
|
|
||||||
|
ENTITIES = [
|
||||||
|
"varme_komfort_andreas",
|
||||||
|
"varme_komfort_daniel",
|
||||||
|
"varme_komfort_sovevaerelse",
|
||||||
|
"varme_komfort_kontor",
|
||||||
|
"varme_komfort_gang",
|
||||||
|
"varme_komfort_forgang",
|
||||||
|
"varme_komfort_lille_bad",
|
||||||
|
"varme_komfort_badevarelse",
|
||||||
|
"varme_komfort_stue",
|
||||||
|
"varme_nat_saenkning",
|
||||||
|
"varme_vaek_saenkning",
|
||||||
|
"varme_ferie_temp",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def get_token():
|
||||||
|
with open(SECRETS_FILE) as f:
|
||||||
|
for line in f:
|
||||||
|
if line.startswith("ha_token:"):
|
||||||
|
return line.split(":", 1)[1].strip()
|
||||||
|
raise ValueError("ha_token ikke fundet i secrets.yaml")
|
||||||
|
|
||||||
|
|
||||||
|
def get_states(token):
|
||||||
|
req = urllib.request.Request(
|
||||||
|
f"{HA_URL}/api/states",
|
||||||
|
headers={"Authorization": f"Bearer {token}"},
|
||||||
|
)
|
||||||
|
with urllib.request.urlopen(req) as resp:
|
||||||
|
return {d["entity_id"]: d["state"] for d in json.loads(resp.read())}
|
||||||
|
|
||||||
|
|
||||||
|
def format_value(state_str):
|
||||||
|
val = float(state_str)
|
||||||
|
return str(int(val)) if val == int(val) else str(val)
|
||||||
|
|
||||||
|
|
||||||
|
def update_initial(content, entity_name, new_value):
|
||||||
|
"""Erstat initial-værdien for en given entity i YAML-indholdet."""
|
||||||
|
pattern = rf"(^{re.escape(entity_name)}:\n(?: [^\n]*\n)*? initial: )\S+"
|
||||||
|
new_content, count = re.subn(
|
||||||
|
pattern, rf"\g<1>{new_value}", content, flags=re.MULTILINE, count=1
|
||||||
|
)
|
||||||
|
if count == 0:
|
||||||
|
print(f" ADVARSEL: {entity_name} ikke fundet i YAML")
|
||||||
|
return new_content
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
token = get_token()
|
||||||
|
states = get_states(token)
|
||||||
|
|
||||||
|
with open(YAML_FILE) as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
saved = []
|
||||||
|
for name in ENTITIES:
|
||||||
|
entity_id = f"input_number.{name}"
|
||||||
|
state = states.get(entity_id)
|
||||||
|
if state in (None, "unavailable", "unknown"):
|
||||||
|
print(f" SPRING OVER {entity_id}: {state}")
|
||||||
|
continue
|
||||||
|
val_str = format_value(state)
|
||||||
|
content = update_initial(content, name, val_str)
|
||||||
|
saved.append(f"{entity_id} = {val_str}")
|
||||||
|
|
||||||
|
with open(YAML_FILE, "w") as f:
|
||||||
|
f.write(content)
|
||||||
|
|
||||||
|
print(f"Gemt {len(saved)} standardværdier -> {YAML_FILE}")
|
||||||
|
for line in saved:
|
||||||
|
print(f" {line}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 34 KiB |
Reference in New Issue
Block a user