converters
This commit is contained in:
@ -2,6 +2,16 @@ 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
|
||||
@ -59,13 +69,13 @@ 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}")
|
||||
|
||||
context_topic = f"{context['context_topic_prefix']}{box['label']}"
|
||||
context['client'].publish(context_topic, json.dumps(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:
|
||||
@ -101,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'):
|
||||
@ -119,20 +129,20 @@ 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 (local_context['output_temperature'], False)
|
||||
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'
|
||||
_calculate_output_temperature(local_context)
|
||||
return (local_context['output_temperature'], False)
|
||||
return True
|
||||
|
||||
|
Reference in New Issue
Block a user