[FEATURE,TEST] integrated mqtt support

This commit is contained in:
smokyflex
2023-07-24 15:51:41 +02:00
parent ac28344106
commit 681184973c
3 changed files with 55 additions and 54 deletions

View File

@@ -3,10 +3,10 @@ from setuptools import setup, find_packages
setup(
name='pe1-modbus',
version='0.0.0',
#packages=find_packages(include=['src.ambilite.*', 'test.ambilite.*']),
packages=find_packages(),
install_requires=[
"pymodbus"
"pymodbusTCP",
"paho-mqtt"
]
)

View File

@@ -4,67 +4,68 @@ from datetime import datetime
import time
import subprocess
import paho.mqtt.client as mqtt
import json
from paho.mqtt import publish
import logging
from register import InputRegisters
from modbusclient import LTModbusClient, RegisterResponse
LOGGER = logging.getLogger(__name__)
#mqtt_username = "smoky"
#mqtt_password = "smoky"
mqtt_host = "192.168.0.99"
mqtt_port = 1883
#******************************************
# Callback function when the client successfully connects to the MQTT broker
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
LOGGER.debug("Connected with result code " + str(rc))
selected_regs = [
InputRegisters.BOILER_TEMPERATURE_TOP,
InputRegisters.FILL_LEVEL_PELLETS_CONTAINER,
InputRegisters.TOTAL_PELLETS_CONSUMPTION,
InputRegisters.HEATING_FLOW_TEMPERATURE_ACTUAL,
InputRegisters.HEATING_FLOW_TEMPERATURE_TARGET,
InputRegisters.BUFFER_TEMPERATURE_TOP,
InputRegisters.BUFFER_TEMPERATURE_BOTTOM,
InputRegisters.BUFFER_CHARGING_STATE
]
while True:
item1 = 1000
item2 = 2000
# Publish config??
config_payload = {
"name": "Power Use General",
"state_topic": "homeassistant/sensor/house/power_use1/state",
"state_class": "measurement",
"unit_of_measurement": "kWh",
"device_class": "energy",
"value_template": "{{ value }}",
"unique_id": "power_use",
"device": {
"identifiers": [
"thesensor"
],
"name": "Power Use Sensors",
"model": "None",
"manufacturer": "None"
},
"icon": "mdi:home-lightning-bolt-outline",
"platform": "mqtt"
}
#client.publish(topic="homeassistant/sensor/house/power_use1/config", payload=str(config_payload), qos=2, retain=False)
# Publish State1
topic1 = "homeassistant/sensor/house/power_use1/state"
client.publish(topic=topic1, payload=str(item1), qos=2, retain=False)
# TCP auto connect on first modbus request
lt_client = LTModbusClient(
host="192.168.0.222",
port=502,
unit_id=2
)
status = lt_client.open()
if status:
for ir in selected_regs:
register_response: RegisterResponse = lt_client.get_register_value(ir)
LOGGER.info(f"{register_response.name}: {register_response.value} {register_response.unit}")
publish.single(f"homeassistant/pe1/{register_response.name}", payload=json.dumps({"value": register_response.value}), qos=2, hostname="localhost", port=1883)
else:
LOGGER.error("Cannot connect to Lambdatronic via Modbus")
#raise Exception("Cannot connect to Lambdatronic via Modbus")
lt_client.close()
time.sleep(30)
# Publish State2
#topic2 = "homeassistant/sensor/house/power_use2/state"
#client.publish(topic=topic2, payload=str(item2), qos=2, retain=False)
#print("Published '{0}' to '{1}' Published '{2}' to '{3}'".format(str(item1), topic1, str(item2), topic2))
print("PAck sent")
time.sleep(2)
#******************************************
#-------------------------------------------------------------------------------------------------------
# main function
def main():
client = mqtt.Client()
#client.username_pw_set(mqtt_username, mqtt_password)
client.on_connect = on_connect
client.connect(mqtt_host, mqtt_port)
client.loop_forever()
#---------------------------------------------------------------------------------------------------------------------------------
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("pymodbus").setLevel(logging.DEBUG)
mqtt_client = mqtt.Client()
mqtt_client.username_pw_set("smoky", "smoky")
mqtt_client.on_connect = on_connect
mqtt_client.connect("127.0.0.1", 1883)
mqtt_client.loop_forever()
if __name__=="__main__":
main()
#---------------------------------------------------------------------------------------------------------------------------------

View File

@@ -19,7 +19,7 @@ class InputRegisters(Enum):
TOTAL_PELLETS_CONSUMPTION = (30084, 30001, 10, 1, "t", "TOTAL_PELLETS_CONSUMPTION", "Pelletverbrauch Gesamt")
REMAINING_HOURS_UNTIL_ASH_REMOVAL = (30087, 30001, 1, 0, "h", "REMAINING_HOURS_UNTIL_ASH_REMOVAL", "Verbleibende Heizstunden bis zur Asche entleeren Warnung")
COUNT_CLEANING_CYCLES = (30102, 30001, 1, 0, "#", "COUNT_CLEANING_CYCLES", "Anzahl der Reinigungen")
MINUTES_UNTIL_NEXT_CLENAING_CYLCLE = (30103, 30001, 1, 0, "min", "MINUTES_UNTIL_NEXT_CLENAING_CYLCLE", "Zeit bis zur nächsten Reinigung")
MINUTES_UNTIL_NEXT_CLEANING_CYLCLE = (30103, 30001, 1, 0, "min", "MINUTES_UNTIL_NEXT_CLEANING_CYLCLE", "Zeit bis zur nächsten Reinigung")
RETURN_FLOW_TEMPERATURE_AT_CIRCULATION_LINE = (30712, 30001, 2, 0, "°C", "RETURN_FLOW_TEMPERATURE_AT_CIRCULATION_LINE", "Rücklauftemperatur an der Zirkulations Leitung")
OUTSIDE_TEMPERATURE = (31001, 30001, 2, 0, "°C", "OUTSIDE_TEMPERATURE", "Außentemperatur")
HEATING_FLOW_TEMPERATURE_ACTUAL = (31031, 30001, 2, 0, "°C", "HEATING_FLOW_TEMPERATURE_ACTUAL", "HK1 - Vorlauf-Isttemperatur")