diff --git a/mqttcontrol/printerbox_mqtt.py b/mqttcontrol/printerbox_mqtt.py index 712cb08..7148201 100644 --- a/mqttcontrol/printerbox_mqtt.py +++ b/mqttcontrol/printerbox_mqtt.py @@ -69,8 +69,11 @@ def create_discovery_msg(config_entry): topic = "{}/{}/{}/config".format( CONFIG['mqtt_discovery_prefix'], cfg['entity_type'], cfg['unique_id']) del cfg['entity_type'] - return {'topic': topic, 'payload': json.dumps(cfg), 'retain': False} - + if 'min_voltage_percentage' in cfg: + del cfg['min_voltage_percentage'] + msg = {'topic': topic, 'payload': json.dumps(cfg), 'retain': True} + print("sending discovery msg", msg) + return msg def send_autodiscovery_messages(client): client.publish(**create_discovery_msg(CONFIG['devices']['venting_fan'])) @@ -114,24 +117,24 @@ def handle_light_message(client, msg): def handle_fan_message(client, msg): topic = msg.topic - payload = msg.payload.decode() + payload = msg.payload for cfg, fan in [(CONFIG['devices']['venting_fan'], hw.venting_fan), (CONFIG['devices']['filter_fan'], hw.filter_fan)]: if topic == cfg['command_topic']: fan.value = 1 if payload == b"ON" else 0 client.publish(cfg['state_topic'], payload) + client.publish(cfg['percentage_state_topic'], 100) return True elif topic == cfg['percentage_command_topic']: - #print("setting percentage, payload = ", payload) - fan_percentage = float(payload) / 100 + fan_percentage = float(payload.decode()) / 100 min_perc = cfg['min_voltage_percentage'] voltage_percentage = min_perc + (1 - min_perc) * fan_percentage if fan_percentage == 0: voltage_percentage = 0 - #print("setting voltage percentage ", voltage_percentage) fan.value = voltage_percentage client.publish(cfg['percentage_state_topic'], payload) + client.publish(cfg['state_topic'], b"ON" if voltage_percentage > 0 else b"OFF") return True return False