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

View File

@ -10,7 +10,22 @@ from loguru import logger
def prepare_context(box_name, context): def prepare_context(box_name, context):
local_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 return local_context
@ -23,37 +38,43 @@ def process_message(box_name, topic_key, payload, context):
match topic_key.split('/'): match topic_key.split('/'):
case [ primary_key, sub_key ] if primary_key == 'window': 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': 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': 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': 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': 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 _: case _:
raise Error(f"Unexcepted topic_key: {topic_key}, {payload}") raise Error(f"Unexcepted topic_key: {topic_key}, {payload}")
if result: if result:
publish_topic = box["output_topic"] (result_message, status) = result
context['client'].publish(publish_topic, result) publish_topic = box["output_topic"] if not status else context['status_topic']
logger.info(f"[{box_name}] Result published on '{publish_topic}': {result}") context['client'].publish(publish_topic, result_message)
logger.info(f"[{box_name}] Result published on '{publish_topic}': {status} {result_message}")
except Exception as e: except Exception as e:
logger.error(f"[{box_name}] Error processing '{topic_key}': {e}") logger.error(f"[{box_name}] Error processing '{topic_key}': {e}")
def process_general_off(box_name, context, payload): def process_general_off(box_name, context, local_context, payload):
return "general off" return ("general off", False)
def process_maintenance_mode(box_name, context, payload): def process_maintenance_mode(box_name, context, local_context, payload):
return "maintenance mode" return ("maintenance mode", False)
def process_cmd(box_name, context, payload): def process_status(box_name, context, local_context, payload):
return f"cmd: {payload}" return (f"{local_context}", True)
def process_high_temp(box_name, context, payload): def process_cmd(box_name, context, local_context, payload):
return f"high_temp: {payload}" return (f"cmd: {payload}", False)
def process_window(box_name, context, sub_key, payload): def process_high_temp(box_name, context, local_context, payload):
return f"window: {sub_key}, {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)