Add MQTT subscrition for modbus experiences

This commit is contained in:
Stefan Allius
2024-05-03 00:05:34 +02:00
parent 5fdad484f4
commit 30dc802fb2

View File

@@ -1,22 +1,15 @@
import asyncio import asyncio
import logging import logging
import aiomqtt import aiomqtt
import traceback
from modbus import Modbus
from messages import Message
from config import Config from config import Config
from singleton import Singleton
logger_mqtt = logging.getLogger('mqtt') logger_mqtt = logging.getLogger('mqtt')
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
logger_mqtt.debug('singleton: __call__')
if cls not in cls._instances:
cls._instances[cls] = super(Singleton,
cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Mqtt(metaclass=Singleton): class Mqtt(metaclass=Singleton):
__client = None __client = None
__cb_MqttIsUp = None __cb_MqttIsUp = None
@@ -65,6 +58,9 @@ class Mqtt(metaclass=Singleton):
password=mqtt['passwd']) password=mqtt['passwd'])
interval = 5 # Seconds interval = 5 # Seconds
ha_status_topic = f"{ha['auto_conf_prefix']}/status"
inv_cnf_topic = "tsun/+/test"
while True: while True:
try: try:
async with self.__client: async with self.__client:
@@ -74,16 +70,32 @@ class Mqtt(metaclass=Singleton):
await self.__cb_MqttIsUp() await self.__cb_MqttIsUp()
# async with self.__client.messages() as messages: # async with self.__client.messages() as messages:
await self.__client.subscribe( await self.__client.subscribe(ha_status_topic)
f"{ha['auto_conf_prefix']}" await self.__client.subscribe(inv_cnf_topic)
"/status")
async for message in self.__client.messages: async for message in self.__client.messages:
status = message.payload.decode("UTF-8") if message.topic.matches(ha_status_topic):
logger_mqtt.info('Home-Assistant Status:' status = message.payload.decode("UTF-8")
f' {status}') logger_mqtt.info('Home-Assistant Status:'
if status == 'online': f' {status}')
self.ha_restarts += 1 if status == 'online':
await self.__cb_MqttIsUp() self.ha_restarts += 1
await self.__cb_MqttIsUp()
if message.topic.matches(inv_cnf_topic):
topic = str(message.topic)
node_id = topic.split('/')[1] + '/'
payload = message.payload.decode("UTF-8")
logger_mqtt.info(f'InvCnf: {node_id}:{payload}')
for m in Message:
if m.server_side and m.node_id == node_id:
logger_mqtt.info(f'Found: {node_id}')
fnc = getattr(m, "send_modbus_cmd", None)
if callable(fnc):
# await fnc(Modbus.MB_WRITE_SINGLE_REG,
# 0x2008, 2)
await fnc(Modbus.MB_READ_SINGLE_REG,
0x2008, 1)
except aiomqtt.MqttError: except aiomqtt.MqttError:
if Config.is_default('mqtt'): if Config.is_default('mqtt'):
@@ -101,3 +113,8 @@ class Mqtt(metaclass=Singleton):
logger_mqtt.debug("MQTT task cancelled") logger_mqtt.debug("MQTT task cancelled")
self.__client = None self.__client = None
return return
except Exception:
# self.inc_counter('SW_Exception')
logger_mqtt.error(
f"Exception:\n"
f"{traceback.format_exc()}")