Skip to content

Commit

Permalink
Added paho-mqtt module to driver
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Feb 17, 2025
1 parent d1d7f5f commit 6f9fab3
Show file tree
Hide file tree
Showing 14 changed files with 6,603 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Changelog

## v0.2.4-dev
## v0.2.4
⚠️ This version is required for Venus OS v3.60~27 or later, but it is also compatible with older versions.
* Added: Make device instance configurable
* Added: Make device name configurable
* Added: paho-mqtt module to driver
* Changed: Broker port missing on reconnect
* Changed: Fixed service not starting sometimes

Expand Down
23 changes: 13 additions & 10 deletions dbus-enphase-envoy/dbus-enphase-envoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
from time import sleep, time
import json
import paho.mqtt.client as mqtt
import configparser # for config/ini file
import _thread
import re
Expand All @@ -20,6 +19,10 @@
# import to request new token
from enphasetoken import getToken

# import external packages
sys.path.insert(1, os.path.join(os.path.dirname(__file__), "ext"))
import paho.mqtt.client as mqtt

# import Victron Energy packages
sys.path.insert(1, os.path.join(os.path.dirname(__file__), "ext", "velib_python"))
from vedbus import VeDbusService # noqa: E402
Expand Down Expand Up @@ -237,13 +240,13 @@


# MQTT
def on_disconnect(client, userdata, rc):
def on_disconnect(client, userdata, flags, reason_code, properties):
global connected
logging.warning("MQTT client: Got disconnected")
if rc != 0:
if reason_code != 0:
logging.warning("MQTT client: Unexpected MQTT disconnection. Will auto-reconnect")
else:
logging.warning("MQTT client: rc value:" + str(rc))
logging.warning("MQTT client: reason_code value:" + str(reason_code))

while connected == 0:
try:
Expand All @@ -262,16 +265,16 @@ def on_disconnect(client, userdata, rc):
sleep(15)


def on_connect(client, userdata, flags, rc):
def on_connect(client, userdata, flags, reason_code, properties):
global connected
if rc == 0:
if reason_code == 0:
logging.info("MQTT client: Connected to MQTT broker!")
connected = 1
else:
logging.error("MQTT client: Failed to connect, return code %d\n", rc)
logging.error("MQTT client: Failed to connect, return code %d\n", reason_code)


def on_publish(client, userdata, rc):
def on_publish(client, userdata, mid, reason_codes, properties):
pass


Expand Down Expand Up @@ -1390,7 +1393,7 @@ def __init__(
self._dbusservice.add_path("/ProductId", 0xFFFF)
self._dbusservice.add_path("/ProductName", productname)
self._dbusservice.add_path("/CustomName", productname)
self._dbusservice.add_path("/FirmwareVersion", "0.2.4-dev (20241030)")
self._dbusservice.add_path("/FirmwareVersion", "0.2.4 (20250217)")
self._dbusservice.add_path("/HardwareVersion", hardware)
self._dbusservice.add_path("/Connected", 1)

Expand Down Expand Up @@ -1556,7 +1559,7 @@ def main():
# MQTT configuration
if MQTT_enabled == 1:
# create new instance
client = mqtt.Client("EnphaseEnvoyPV_" + get_vrm_portal_id())
client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2, client_id="EnphaseEnvoyPV_" + get_vrm_portal_id())
client.on_disconnect = on_disconnect
client.on_connect = on_connect
client.on_publish = on_publish
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions dbus-enphase-envoy/ext/paho/mqtt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__version__ = "2.1.0"


class MQTTException(Exception):
pass
Loading

0 comments on commit 6f9fab3

Please sign in to comment.