Files
pv-controller/src/pv_controller/ToDevices.py
Wolfgang Hottgenroth 6e50654d00
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
lower case
2025-12-05 13:45:00 +01:00

39 lines
1.5 KiB
Python

from MqttBase import AbstractMqttPublisher
from loguru import logger
from time import sleep
class ToDevices(AbstractMqttPublisher):
def __init__(self, config, modbusHandler):
super().__init__(config)
self.modbusHandler = modbusHandler
def localLoop(self):
while not self.killBill:
sleep(60.0)
def onMessage(self, topic, payload):
try:
logger.debug("mqtt message received: {} -> {}".format(topic, str(payload)))
for device in self.config.input:
if topic != device.subscribe_topic:
continue
logger.debug(f"{topic=} matches {device.subscribe_topic=}, processing")
if not device.enabled:
logger.debug(f" device disabled, skipping")
continue
if device.register_type != 'coil':
raise Exception(f"Unsupported register type {device.register_type} for input device {device.name}")
value = payload == b'on'
self.modbusHandler.writeCoil(device.slave_id, device.address, value)
except Exception as e:
logger.error(f"Caught exception in onMessage: {str(e)}")
def onConnect(self):
logger.info("mqtt connected")
for device in self.config.input:
self.client.subscribe(device.subscribe_topic)
logger.info(f"subscribed to topic: {device.subscribe_topic}")
logger.info("subscribed")