status output

This commit is contained in:
2024-11-06 14:13:37 +01:00
parent f231d42a4f
commit c81ed9644a
3 changed files with 49 additions and 19 deletions

5
env
View File

@ -7,10 +7,13 @@ export MQTT_BOX_TOPIC_PREFIXES='{
}'
export MQTT_CENTRAL_TOPICS='{
"general_off": "heating/system/general_off",
"maintenance_mode": "heating/system/maintenance_mode"
"maintenance_mode": "heating/system/maintenance_mode",
"status": "heating/system/status"
}'
export MQTT_STATUS_TOPIC="heating/status"
export OFF_TEMPERATURE="5.0"
export LOW_TEMPERATURE="15.0"
export DEFAULT_HIGH_TEMPERATURE="21.0"
export MAINTENANCE_TEMPERATURE="30.0"
export BOXES='{
"box1": {

View File

@ -16,7 +16,9 @@ BOX_TOPIC_PREFIXES_CONFIG = os.getenv("MQTT_BOX_TOPIC_PREFIXES") # JSON configu
CENTRAL_TOPICS_CONFIG = os.getenv("MQTT_CENTRAL_TOPICS") # JSON configuration of central topics
OFF_TEMPERATURE = os.getenv("OFF_TEMPERATURE", "5.0")
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")
# Check if required environment variables are set
missing_vars = []
@ -28,6 +30,8 @@ if not BOX_TOPIC_PREFIXES_CONFIG:
missing_vars.append('MQTT_BOX_TOPIC_PREFIXES')
if not CENTRAL_TOPICS_CONFIG:
missing_vars.append('MQTT_CENTRAL_TOPICS')
if not STATUS_TOPIC:
missing_vars.append('MQTT_STATUS_TOPIC')
if missing_vars:
logger.error(f"Error: The following environment variables are not set: {', '.join(missing_vars)}")
@ -39,7 +43,9 @@ context = {}
# configuration values for boxes
context['off_temperature'] = OFF_TEMPERATURE
context['low_temperature'] = LOW_TEMPERATURE
context['default_high_temperature'] = DEFAULT_HIGH_TEMPERATURE
context['maintenance_temperature'] = MAINTENANCE_TEMPERATURE
context['status_topic'] = STATUS_TOPIC
# Load box configurations from JSON
try:

View File

@ -11,6 +11,21 @@ from loguru import logger
def prepare_context(box_name, context):
local_context = {}
local_context['id'] = box_name
local_context['label'] = context['boxes'][box_name]['label']
local_context['high_temperature'] = context['default_high_temperature']
local_context['low_temperature'] = context['low_temperature']
local_context['off_temperature'] = context['off_temperature']
local_context['maintenance_temperature'] = context['maintenance_temperature']
local_context['general_off'] = False
local_context['maintenance_mode'] = False
local_context['window_state'] = {}
for w in context['boxes'][box_name]['windows']:
local_context['window_state'][w['label']] = 'closed'
local_context['output_temperature'] = local_context['high_temperature']
return local_context
@ -23,37 +38,43 @@ def process_message(box_name, topic_key, payload, context):
match topic_key.split('/'):
case [ primary_key, sub_key ] if primary_key == 'window':
result = process_window(box_name, context, sub_key, payload)
result = process_window(box_name, context, local_context, sub_key, payload)
case [ primary_key ] if primary_key == 'high_temp':
result = process_high_temp(box_name, context, payload)
result = process_high_temp(box_name, context, local_context, payload)
case [ primary_key ] if primary_key == 'cmd':
result = process_cmd(box_name, context, payload)
result = process_cmd(box_name, context, local_context, payload)
case [ primary_key ] if primary_key == 'general_off':
result = process_general_off(box_name, context, payload)
result = process_general_off(box_name, context, local_context, payload)
case [ primary_key ] if primary_key == 'maintenance_mode':
result = process_maintenance_mode(box_name, context, payload)
result = process_maintenance_mode(box_name, context, local_context, payload)
case [ primary_key ] if primary_key == 'status':
result = process_status(box_name, context, local_context, payload)
case _:
raise Error(f"Unexcepted topic_key: {topic_key}, {payload}")
if result:
publish_topic = box["output_topic"]
context['client'].publish(publish_topic, result)
logger.info(f"[{box_name}] Result published on '{publish_topic}': {result}")
(result_message, status) = result
publish_topic = box["output_topic"] if not status else context['status_topic']
context['client'].publish(publish_topic, result_message)
logger.info(f"[{box_name}] Result published on '{publish_topic}': {status} {result_message}")
except Exception as e:
logger.error(f"[{box_name}] Error processing '{topic_key}': {e}")
def process_general_off(box_name, context, payload):
return "general off"
def process_general_off(box_name, context, local_context, payload):
return ("general off", False)
def process_maintenance_mode(box_name, context, payload):
return "maintenance mode"
def process_maintenance_mode(box_name, context, local_context, payload):
return ("maintenance mode", False)
def process_cmd(box_name, context, payload):
return f"cmd: {payload}"
def process_status(box_name, context, local_context, payload):
return (f"{local_context}", True)
def process_high_temp(box_name, context, payload):
return f"high_temp: {payload}"
def process_cmd(box_name, context, local_context, payload):
return (f"cmd: {payload}", False)
def process_window(box_name, context, sub_key, payload):
return f"window: {sub_key}, {payload}"
def process_high_temp(box_name, context, local_context, payload):
return (f"high_temp: {payload}", False)
def process_window(box_name, context, local_context, sub_key, payload):
return (f"window: {sub_key}, {payload}", False)