mirror of
https://github.com/lumapu/ahoy.git
synced 2025-04-30 02:36:20 +02:00
Merge pull request #579 from PaeserBastelstube/RPI-function-MQTT-Output-as-class-method
RPi function mqtt output as class method
This commit is contained in:
commit
0225481b29
2 changed files with 20 additions and 9 deletions
|
@ -176,14 +176,16 @@ def poll_inverter(inverter, dtu_ser, do_init, retries):
|
||||||
command_queue[inv_str].append(hoymiles.compose_send_time_payload(InfoCommands.AlarmData, alarm_id=event_message_index[inv_str]))
|
command_queue[inv_str].append(hoymiles.compose_send_time_payload(InfoCommands.AlarmData, alarm_id=event_message_index[inv_str]))
|
||||||
|
|
||||||
if mqtt_client:
|
if mqtt_client:
|
||||||
mqtt_send_status(mqtt_client, inverter_ser, data,
|
# mqtt_send_status(mqtt_client, inverter_ser, data, topic=inverter.get('mqtt', {}).get('topic', None))
|
||||||
topic=inverter.get('mqtt', {}).get('topic', None))
|
mqtt_client.store_status(result, topic=inverter.get('mqtt', {}).get('topic', None))
|
||||||
|
|
||||||
if influx_client:
|
if influx_client:
|
||||||
influx_client.store_status(result)
|
influx_client.store_status(result)
|
||||||
|
|
||||||
if volkszaehler_client:
|
if volkszaehler_client:
|
||||||
volkszaehler_client.store_status(result)
|
volkszaehler_client.store_status(result)
|
||||||
|
|
||||||
|
"""
|
||||||
def mqtt_send_status(broker, inverter_ser, data, topic=None):
|
def mqtt_send_status(broker, inverter_ser, data, topic=None):
|
||||||
"""
|
"""
|
||||||
Publish StatusResponse object
|
Publish StatusResponse object
|
||||||
|
@ -227,6 +229,7 @@ def mqtt_send_status(broker, inverter_ser, data, topic=None):
|
||||||
broker.publish(f'{topic}/temperature', data['temperature'])
|
broker.publish(f'{topic}/temperature', data['temperature'])
|
||||||
if data['energy_total'] is not None:
|
if data['energy_total'] is not None:
|
||||||
broker.publish(f'{topic}/total', data['energy_total']/1000)
|
broker.publish(f'{topic}/total', data['energy_total']/1000)
|
||||||
|
"""
|
||||||
|
|
||||||
def mqtt_on_command(client, userdata, message):
|
def mqtt_on_command(client, userdata, message):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -137,7 +137,7 @@ class MqttOutputPlugin(OutputPluginFactory):
|
||||||
""" Mqtt output plugin """
|
""" Mqtt output plugin """
|
||||||
client = None
|
client = None
|
||||||
|
|
||||||
def __init__(self, *args, **params):
|
def __init__(self, config, **params):
|
||||||
"""
|
"""
|
||||||
Initialize MqttOutputPlugin
|
Initialize MqttOutputPlugin
|
||||||
|
|
||||||
|
@ -158,11 +158,14 @@ class MqttOutputPlugin(OutputPluginFactory):
|
||||||
:param topic: custom mqtt topic prefix (default: hoymiles/{inverter_ser})
|
:param topic: custom mqtt topic prefix (default: hoymiles/{inverter_ser})
|
||||||
:type topic: str
|
:type topic: str
|
||||||
"""
|
"""
|
||||||
super().__init__(*args, **params)
|
super().__init__(**params)
|
||||||
|
|
||||||
mqtt_client = paho.mqtt.client.Client()
|
mqtt_client = paho.mqtt.client.Client()
|
||||||
mqtt_client.username_pw_set(params.get('user', None), params.get('password', None))
|
if config.get('useTLS',False):
|
||||||
mqtt_client.connect(params.get('host', '127.0.0.1'), params.get('port', 1883))
|
mqtt_client.tls_set()
|
||||||
|
mqtt_client.tls_insecure_set(config.get('insecureTLS',False))
|
||||||
|
mqtt_client.username_pw_set(config.get('user', None), config.get('password', None))
|
||||||
|
mqtt_client.connect(config.get('host', '127.0.0.1'), config.get('port', 1883))
|
||||||
mqtt_client.loop_start()
|
mqtt_client.loop_start()
|
||||||
|
|
||||||
self.client = mqtt_client
|
self.client = mqtt_client
|
||||||
|
@ -182,8 +185,11 @@ class MqttOutputPlugin(OutputPluginFactory):
|
||||||
raise ValueError('Data needs to be instance of StatusResponse')
|
raise ValueError('Data needs to be instance of StatusResponse')
|
||||||
|
|
||||||
data = response.__dict__()
|
data = response.__dict__()
|
||||||
|
topic = f'{data.get("inverter_name", "hoymiles")}/{data.get("inverter_ser", None)}'
|
||||||
|
|
||||||
topic = params.get('topic', f'hoymiles/{data["inverter_ser"]}')
|
# Global Head
|
||||||
|
if data['time'] is not None:
|
||||||
|
self.client.publish(f'{topic}/time', data['time'].strftime("%d.%m.%y - %H:%M:%S"))
|
||||||
|
|
||||||
# AC Data
|
# AC Data
|
||||||
phase_id = 0
|
phase_id = 0
|
||||||
|
@ -191,16 +197,18 @@ class MqttOutputPlugin(OutputPluginFactory):
|
||||||
self.client.publish(f'{topic}/emeter/{phase_id}/power', phase['power'])
|
self.client.publish(f'{topic}/emeter/{phase_id}/power', phase['power'])
|
||||||
self.client.publish(f'{topic}/emeter/{phase_id}/voltage', phase['voltage'])
|
self.client.publish(f'{topic}/emeter/{phase_id}/voltage', phase['voltage'])
|
||||||
self.client.publish(f'{topic}/emeter/{phase_id}/current', phase['current'])
|
self.client.publish(f'{topic}/emeter/{phase_id}/current', phase['current'])
|
||||||
|
self.client.publish(f'{topic}/emeter/{phase_id}/Q_AC', phase['reactive_power'])
|
||||||
phase_id = phase_id + 1
|
phase_id = phase_id + 1
|
||||||
|
|
||||||
# DC Data
|
# DC Data
|
||||||
string_id = 0
|
string_id = 0
|
||||||
for string in data['strings']:
|
for string in data['strings']:
|
||||||
self.client.publish(f'{topic}/emeter-dc/{string_id}/total', string['energy_total']/1000)
|
|
||||||
self.client.publish(f'{topic}/emeter-dc/{string_id}/power', string['power'])
|
|
||||||
self.client.publish(f'{topic}/emeter-dc/{string_id}/voltage', string['voltage'])
|
self.client.publish(f'{topic}/emeter-dc/{string_id}/voltage', string['voltage'])
|
||||||
self.client.publish(f'{topic}/emeter-dc/{string_id}/current', string['current'])
|
self.client.publish(f'{topic}/emeter-dc/{string_id}/current', string['current'])
|
||||||
|
self.client.publish(f'{topic}/emeter-dc/{string_id}/YieldDay', string['energy_daily'])
|
||||||
|
self.client.publish(f'{topic}/emeter-dc/{string_id}/YieldTotal', string['energy_total']/1000)
|
||||||
string_id = string_id + 1
|
string_id = string_id + 1
|
||||||
|
|
||||||
# Global
|
# Global
|
||||||
if data['powerfactor'] is not None:
|
if data['powerfactor'] is not None:
|
||||||
self.client.publish(f'{topic}/pf', data['powerfactor'])
|
self.client.publish(f'{topic}/pf', data['powerfactor'])
|
||||||
|
|
Loading…
Add table
Reference in a new issue