Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
20a064dc1f
|
|||
8417454f5b
|
|||
ee0efb6c19
|
|||
dbdd24822e
|
|||
78a68f9009
|
|||
fbb9aa6665
|
|||
51995fc489
|
|||
e6b4733a60
|
|||
62ce6f1b9c
|
@ -10,6 +10,7 @@ data:
|
||||
MQTT_BOX_TOPIC_PREFIXES: |
|
||||
{
|
||||
"high_temp": "heating/config/high_temp/",
|
||||
"overwrite_window": "heating/overwrite_window/",
|
||||
"cmd": "heating/command/"
|
||||
}
|
||||
MQTT_CENTRAL_TOPICS: |
|
||||
@ -19,72 +20,81 @@ data:
|
||||
"status": "heating/system/status"
|
||||
}
|
||||
MQTT_STATUS_TOPIC: "heating/status"
|
||||
MQTT_CONTEXT_TOPIC_PREFIX: "heating/context/"
|
||||
OFF_TEMPERATURE: "5.0"
|
||||
LOW_TEMPERATURE: "15.0"
|
||||
DEFAULT_HIGH_TEMPERATURE: "21.0"
|
||||
MAINTENANCE_TEMPERATURE: "30.0"
|
||||
BOXES: |
|
||||
{
|
||||
"box1": {
|
||||
"patty": {
|
||||
"label": "patty",
|
||||
"windows": [
|
||||
{ "topic": "homegear/instance1/plain/18/1/STATE", "label": "Garten rechts" },
|
||||
{ "topic": "homegear/instance1/plain/22/1/STATE", "label": "Garten links" }
|
||||
{ "topic": "homegear/instance1/plain/18/1/STATE", "label": "Garten rechts", "converter": "max" },
|
||||
{ "topic": "homegear/instance1/plain/22/1/STATE", "label": "Garten links", "converter": "max" }
|
||||
],
|
||||
"output_topic": "heating/homegear/instance1/set/39/1/SET_TEMPERATURE"
|
||||
"output_topic": "heating/homegear/instance1/set/39/1/SET_TEMPERATURE",
|
||||
"output_converter": "max"
|
||||
},
|
||||
"box2": {
|
||||
"kueche": {
|
||||
"label": "kueche",
|
||||
"windows": [
|
||||
{ "topic": "homegear/instance1/plain/37/1/STATE", "label": "Garten Fenster" },
|
||||
{ "topic": "homegear/instance1/plain/36/1/STATE", "label": "Garten Tuer" },
|
||||
{ "topic": "homegear/instance1/plain/38/1/STATE", "label": "Strasse rechts" },
|
||||
{ "topic": "homegear/instance1/plain/13/1/STATE", "label": "Strasse links" }
|
||||
{ "topic": "homegear/instance1/plain/37/1/STATE", "label": "Garten Fenster", "converter": "max" },
|
||||
{ "topic": "homegear/instance1/plain/36/1/STATE", "label": "Garten Tuer", "converter": "max" },
|
||||
{ "topic": "homegear/instance1/plain/38/1/STATE", "label": "Strasse rechts", "converter": "max" },
|
||||
{ "topic": "homegear/instance1/plain/13/1/STATE", "label": "Strasse links", "converter": "max" }
|
||||
],
|
||||
"output_topic": "heating/homegear/instance1/set/40/1/SET_TEMPERATURE"
|
||||
"output_topic": "heating/homegear/instance1/set/40/1/SET_TEMPERATURE",
|
||||
"output_converter": "max"
|
||||
},
|
||||
"box3": {
|
||||
"bad_oben": {
|
||||
"label": "bad_oben",
|
||||
"windows": [
|
||||
],
|
||||
"output_topic": "heating/homegear/instance1/set/41/1/SET_TEMPERATURE"
|
||||
"output_topic": "heating/homegear/instance1/set/41/1/SET_TEMPERATURE",
|
||||
"output_converter": "max"
|
||||
},
|
||||
"box4": {
|
||||
"schlafzimmer": {
|
||||
"label": "schlafzimmer",
|
||||
"windows": [
|
||||
{ "topic": "homegear/instance1/plain/52/1/STATE", "label": "Strasse" }
|
||||
{ "topic": "homegear/instance1/plain/52/1/STATE", "label": "Strasse", "converter": "max" }
|
||||
],
|
||||
"output_topic": "heating/homegear/instance1/set/42/1/SET_TEMPERATURE"
|
||||
"output_topic": "heating/homegear/instance1/set/42/1/SET_TEMPERATURE",
|
||||
"output_converter": "max"
|
||||
},
|
||||
"box5": {
|
||||
"wolfgang": {
|
||||
"label": "wolfgang",
|
||||
"windows": [
|
||||
{ "topic": "homegear/instance1/plain/24/1/STATE", "label": "Garten" }
|
||||
{ "topic": "homegear/instance1/plain/24/1/STATE", "label": "Garten", "converter": "max" }
|
||||
],
|
||||
"output_topic": "heating/homegear/instance1/set/43/1/SET_TEMPERATURE"
|
||||
"output_topic": "heating/homegear/instance1/set/43/1/SET_TEMPERATURE",
|
||||
"output_converter": "max"
|
||||
},
|
||||
"box6": {
|
||||
"esszimmer": {
|
||||
"label": "esszimmer",
|
||||
"windows": [
|
||||
{ "topic": "homegear/instance1/plain/26/1/STATE", "label": "Strasse rechts" },
|
||||
{ "topic": "homegear/instance1/plain/27/1/STATE", "label": "Strasse links" }
|
||||
{ "topic": "homegear/instance1/plain/26/1/STATE", "label": "Strasse rechts", "converter": "max" },
|
||||
{ "topic": "homegear/instance1/plain/27/1/STATE", "label": "Strasse links", "converter": "max" }
|
||||
],
|
||||
"output_topic": "heating/homegear/instance1/set/45/1/SET_TEMPERATURE"
|
||||
"output_topic": "heating/homegear/instance1/set/45/1/SET_TEMPERATURE",
|
||||
"output_converter": "max"
|
||||
},
|
||||
"box7": {
|
||||
"wohnzimmer": {
|
||||
"label": "wohnzimmer",
|
||||
"windows": [
|
||||
{ "topic": "homegear/instance1/plain/28/1/STATE", "label": "Garten rechts" },
|
||||
{ "topic": "homegear/instance1/plain/29/1/STATE", "label": "Garten links" }
|
||||
{ "topic": "homegear/instance1/plain/28/1/STATE", "label": "Garten rechts", "converter": "max" },
|
||||
{ "topic": "homegear/instance1/plain/29/1/STATE", "label": "Garten links", "converter": "max" }
|
||||
],
|
||||
"output_topic": "heating/homegear/instance1/set/46/1/SET_TEMPERATURE"
|
||||
"output_topic": "heating/homegear/instance1/set/46/1/SET_TEMPERATURE",
|
||||
"output_converter": "max"
|
||||
},
|
||||
"box8": {
|
||||
"bad_unten": {
|
||||
"label": "bad_unten",
|
||||
"windows": [
|
||||
{ "topic": "homegear/instance1/plain/44/1/STATE", "label": "Strasse" }
|
||||
{ "topic": "homegear/instance1/plain/44/1/STATE", "label": "Strasse", "converter": "max" }
|
||||
],
|
||||
"output_topic": "heating/homegear/instance1/set/48/1/SET_TEMPERATURE"
|
||||
"output_topic": "heating/homegear/instance1/set/48/1/SET_TEMPERATURE",
|
||||
"output_converter": "max"
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ LOW_TEMPERATURE = os.getenv("LOW_TEMPERATURE", "15.0")
|
||||
DEFAULT_HIGH_TEMPERATURE = os.getenv("DEFAULT_HIGH_TEMPERATURE", "21.0")
|
||||
MAINTENANCE_TEMPERATURE = os.getenv("MAINTENANCE_TEMPERATURE", "30.0")
|
||||
STATUS_TOPIC = os.getenv("MQTT_STATUS_TOPIC")
|
||||
CONTEXT_TOPIC_PREFIX = os.getenv("MQTT_CONTEXT_TOPIC_PREFIX")
|
||||
|
||||
# Check if required environment variables are set
|
||||
missing_vars = []
|
||||
@ -32,6 +33,8 @@ if not CENTRAL_TOPICS_CONFIG:
|
||||
missing_vars.append('MQTT_CENTRAL_TOPICS')
|
||||
if not STATUS_TOPIC:
|
||||
missing_vars.append('MQTT_STATUS_TOPIC')
|
||||
if not CONTEXT_TOPIC_PREFIX:
|
||||
missing_vars.append('MQTT_CONTEXT_TOPIC_PREFIX')
|
||||
|
||||
if missing_vars:
|
||||
logger.error(f"Error: The following environment variables are not set: {', '.join(missing_vars)}")
|
||||
@ -46,6 +49,7 @@ context['low_temperature'] = LOW_TEMPERATURE
|
||||
context['default_high_temperature'] = DEFAULT_HIGH_TEMPERATURE
|
||||
context['maintenance_temperature'] = MAINTENANCE_TEMPERATURE
|
||||
context['status_topic'] = STATUS_TOPIC
|
||||
context['context_topic_prefix'] = CONTEXT_TOPIC_PREFIX
|
||||
|
||||
# Load box configurations from JSON
|
||||
try:
|
||||
@ -66,7 +70,7 @@ try:
|
||||
box_topic_prefixes = json.loads(BOX_TOPIC_PREFIXES_CONFIG)
|
||||
|
||||
# Validation: Check if the required keys are present
|
||||
required_keys = {'high_temp', 'cmd'}
|
||||
required_keys = {'high_temp', 'cmd', 'overwrite_window'}
|
||||
missing_keys = required_keys - box_topic_prefixes.keys()
|
||||
|
||||
if missing_keys:
|
||||
|
@ -1,6 +1,17 @@
|
||||
from loguru import logger
|
||||
import json
|
||||
|
||||
|
||||
|
||||
CONVERTERS = {
|
||||
"target_temperature_output": {
|
||||
"max": lambda x: x["output_temperature"],
|
||||
},
|
||||
"window_contact_input": {
|
||||
"max": lambda x: 'closed' if (x.lower() in ('false', 'close', 'closed')) else 'open'
|
||||
}
|
||||
}
|
||||
|
||||
# context
|
||||
# boxes: structure of boxes
|
||||
# client: MQTT client
|
||||
@ -20,6 +31,7 @@ def prepare_context(box_name, context):
|
||||
|
||||
local_context['general_off'] = False
|
||||
local_context['maintenance_mode'] = False
|
||||
local_context['overwrite_window'] = False
|
||||
|
||||
local_context['window_state'] = {}
|
||||
for w in context['boxes'][box_name]['windows']:
|
||||
@ -35,8 +47,8 @@ def process_message(box_name, topic_key, payload, context):
|
||||
try:
|
||||
box = context['boxes'][box_name]
|
||||
local_context = box['context']
|
||||
logger.info(f"Local context before: {local_context}")
|
||||
logger.info(f"[{box_name}, {box['label']}] Processing message for '{topic_key}': {payload}")
|
||||
logger.info(f"[{box_name}] Local context before: {local_context}")
|
||||
logger.info(f"[{box_name}] Processing message for '{topic_key}': {payload}")
|
||||
|
||||
match topic_key.split('/'):
|
||||
case [ primary_key, sub_key ] if primary_key == 'window':
|
||||
@ -45,6 +57,8 @@ def process_message(box_name, topic_key, payload, context):
|
||||
result = process_high_temp(box_name, context, local_context, payload)
|
||||
case [ primary_key ] if primary_key == 'cmd':
|
||||
result = process_cmd(box_name, context, local_context, payload)
|
||||
case [ primary_key ] if primary_key == 'overwrite_window':
|
||||
result = process_overwrite_window(box_name, context, local_context, payload)
|
||||
case [ primary_key ] if primary_key == 'general_off':
|
||||
result = process_general_off(box_name, context, local_context, payload)
|
||||
case [ primary_key ] if primary_key == 'maintenance_mode':
|
||||
@ -55,12 +69,15 @@ def process_message(box_name, topic_key, payload, context):
|
||||
raise Error(f"Unexcepted topic_key: {topic_key}, {payload}")
|
||||
|
||||
if result:
|
||||
(result_message, status) = result
|
||||
publish_topic = box["output_topic"] if not status else context['status_topic']
|
||||
result_message = CONVERTERS["target_temperature_output"][box["output_converter"]](local_context)
|
||||
publish_topic = box["output_topic"]
|
||||
context['client'].publish(publish_topic, result_message)
|
||||
logger.info(f"[{box_name}] Result published on '{publish_topic}': {status} {result_message}")
|
||||
logger.info(f"[{box_name}] Result published on '{publish_topic}': {result_message}")
|
||||
|
||||
logger.info(f"Local context after: {local_context}")
|
||||
context_topic = f"{context['context_topic_prefix']}{box['label']}"
|
||||
context['client'].publish(context_topic, json.dumps(local_context))
|
||||
|
||||
logger.info(f"[{box_name}] Local context after: {local_context}")
|
||||
except Exception as e:
|
||||
logger.error(f"[{box_name}] Error processing '{topic_key}': {e}")
|
||||
|
||||
@ -74,10 +91,11 @@ def _calculate_output_temperature(local_context):
|
||||
local_context['output_temperature'] = local_context['off_temperature']
|
||||
return
|
||||
# an open window shuts off the heating
|
||||
for w in local_context['window_state'].values():
|
||||
if w == 'open':
|
||||
local_context['output_temperature'] = local_context['off_temperature']
|
||||
return
|
||||
if not local_context['overwrite_window']:
|
||||
for w in local_context['window_state'].values():
|
||||
if w == 'open':
|
||||
local_context['output_temperature'] = local_context['off_temperature']
|
||||
return
|
||||
# finally evaluate the mode
|
||||
if local_context['mode'] == 'off':
|
||||
local_context['output_temperature'] = local_context['off_temperature']
|
||||
@ -93,17 +111,17 @@ def _calculate_output_temperature(local_context):
|
||||
return
|
||||
|
||||
def process_status(box_name, context, local_context, payload):
|
||||
return (f"{local_context}", True)
|
||||
return False
|
||||
|
||||
def process_general_off(box_name, context, local_context, payload):
|
||||
local_context['general_off'] = (payload.lower() in ('true'))
|
||||
_calculate_output_temperature(local_context)
|
||||
return (local_context['output_temperature'], False)
|
||||
return True
|
||||
|
||||
def process_maintenance_mode(box_name, context, local_context, payload):
|
||||
local_context['maintenance_mode'] = (payload.lower() in ('true'))
|
||||
_calculate_output_temperature(local_context)
|
||||
return (local_context['output_temperature'], False)
|
||||
return True
|
||||
|
||||
def process_cmd(box_name, context, local_context, payload):
|
||||
if payload.lower() in ('high', 'low', 'off'):
|
||||
@ -111,15 +129,26 @@ def process_cmd(box_name, context, local_context, payload):
|
||||
_calculate_output_temperature(local_context)
|
||||
else:
|
||||
logger.error(f"Invalid cmd for {box_name} received: {payload}")
|
||||
return (local_context['output_temperature'], False)
|
||||
return True
|
||||
|
||||
def process_overwrite_window(box_name, context, local_context, payload):
|
||||
local_context['overwrite_window'] = (payload.lower() in ('true'))
|
||||
_calculate_output_temperature(local_context)
|
||||
return True
|
||||
|
||||
def process_high_temp(box_name, context, local_context, payload):
|
||||
local_context['high_temperature'] = payload
|
||||
_calculate_output_temperature(local_context)
|
||||
return (local_context['output_temperature'], False)
|
||||
return True
|
||||
|
||||
def process_window(box_name, context, local_context, sub_key, payload):
|
||||
local_context['window_state'][sub_key] = 'closed' if (payload.lower() in ('false', 'close', 'closed')) else 'open'
|
||||
// default converter
|
||||
converter = lambda x:x
|
||||
for sk in box["windows"]:
|
||||
if sk["label"] == sub_key:
|
||||
converter = CONVERTERS["window_contact_input"][sk["converter"]]
|
||||
break
|
||||
local_context['window_state'][sub_key] = converter(payload)
|
||||
_calculate_output_temperature(local_context)
|
||||
return (local_context['output_temperature'], False)
|
||||
return True
|
||||
|
||||
|
Reference in New Issue
Block a user