|
@@ -5,15 +5,23 @@ import asyncio
|
|
import signal
|
|
import signal
|
|
import json
|
|
import json
|
|
|
|
|
|
-import aiomqtt
|
|
|
|
|
|
+import paho.mqtt.client as mqtt
|
|
from bleak.exc import BleakError, BleakDeviceNotFoundError
|
|
from bleak.exc import BleakError, BleakDeviceNotFoundError
|
|
|
|
|
|
from bleclient import BleClient
|
|
from bleclient import BleClient
|
|
|
|
|
|
|
|
+client = mqtt.Client()
|
|
send_config = True
|
|
send_config = True
|
|
-reconnect_interval = 5 # In seconds
|
|
|
|
|
|
|
|
-async def mqtt_publish(details: dict[str, any], client: aiomqtt.Client):
|
|
|
|
|
|
+def details_handler(details):
|
|
|
|
+ if details:
|
|
|
|
+ print(f"Battery: {details['battery_percentage']}% ({details['battery_voltage']}V)")
|
|
|
|
+ mqtt_publish(details, client)
|
|
|
|
+ else:
|
|
|
|
+ print("No values recieved")
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def mqtt_publish(details, client):
|
|
global send_config
|
|
global send_config
|
|
# Define the base topic for MQTT Discovery
|
|
# Define the base topic for MQTT Discovery
|
|
base_topic = "homeassistant"
|
|
base_topic = "homeassistant"
|
|
@@ -62,41 +70,47 @@ async def mqtt_publish(details: dict[str, any], client: aiomqtt.Client):
|
|
|
|
|
|
# Publish the MQTT Discovery payload
|
|
# Publish the MQTT Discovery payload
|
|
if send_config:
|
|
if send_config:
|
|
- print(f"Publishing MQTT Discovery payload for {key}")
|
|
|
|
- await client.publish(topic, payload=json.dumps(payload), retain=True)
|
|
|
|
|
|
+ client.publish(topic, payload=json.dumps(payload), retain=True)
|
|
|
|
|
|
# Publish the entity state
|
|
# Publish the entity state
|
|
- await client.publish(state_topic, payload=str(value))
|
|
|
|
|
|
+ client.publish(state_topic, payload=str(value))
|
|
send_config = False
|
|
send_config = False
|
|
|
|
|
|
async def main(address, host, port, username, password):
|
|
async def main(address, host, port, username, password):
|
|
|
|
+ client.username_pw_set(username, password) # Set MQTT username and password
|
|
|
|
+
|
|
async def run_mppt():
|
|
async def run_mppt():
|
|
while True:
|
|
while True:
|
|
try:
|
|
try:
|
|
- async with aiomqtt.Client(hostname=host, port=port, username=username, password=password) as client:
|
|
|
|
- print(f"Connecting to MQTT broker at {host}:{port}")
|
|
|
|
|
|
+ client.connect(host, port) # Connect to the MQTT broker
|
|
|
|
+ break # Connection successful, exit the loop
|
|
|
|
+
|
|
|
|
+ except asyncio.CancelledError:
|
|
|
|
+ raise # Re-raise the CancelledError to stop the task
|
|
|
|
+ except Exception as e:
|
|
|
|
+ print(f"An error occurred while connecting to MQTT broker: {e}")
|
|
|
|
+ await asyncio.sleep(5) # Wait for 5 seconds before retrying
|
|
|
|
+
|
|
|
|
+ while True:
|
|
|
|
+ try:
|
|
|
|
+ async with BleClient(address) as mppt:
|
|
|
|
+ mppt.on_details_received = details_handler
|
|
|
|
+ await mppt.request_details()
|
|
|
|
+
|
|
while True:
|
|
while True:
|
|
|
|
+ await asyncio.sleep(20.0)
|
|
try:
|
|
try:
|
|
- async with BleClient(address) as mppt:
|
|
|
|
- while True:
|
|
|
|
- details = await mppt.request_details()
|
|
|
|
- if details:
|
|
|
|
- print(f"Battery: {details['battery_percentage']}% ({details['battery_voltage']}V)")
|
|
|
|
- await mqtt_publish(details, client)
|
|
|
|
- else:
|
|
|
|
- print("No values recieved")
|
|
|
|
- await asyncio.sleep(20.0)
|
|
|
|
-
|
|
|
|
- except BleakDeviceNotFoundError:
|
|
|
|
- print(f"BLE device with address {address} was not found")
|
|
|
|
- await asyncio.sleep(5) # Wait for 5 seconds before retrying
|
|
|
|
|
|
+ await mppt.request_details()
|
|
except BleakError as e:
|
|
except BleakError as e:
|
|
print(f"BLE error occurred: {e}")
|
|
print(f"BLE error occurred: {e}")
|
|
- except aiomqtt.MqttError as error:
|
|
|
|
- print(f'Error "{error}". Reconnecting in {reconnect_interval} seconds.')
|
|
|
|
- await asyncio.sleep(reconnect_interval)
|
|
|
|
|
|
+ # Handle the BLE error accordingly, e.g., reconnect or terminate the task
|
|
|
|
+ break
|
|
|
|
+
|
|
except asyncio.CancelledError:
|
|
except asyncio.CancelledError:
|
|
raise # Re-raise the CancelledError to stop the task
|
|
raise # Re-raise the CancelledError to stop the task
|
|
|
|
+ except BleakDeviceNotFoundError:
|
|
|
|
+ print(f"BLE device with address {address} was not found")
|
|
|
|
+ await asyncio.sleep(5) # Wait for 5 seconds before retrying
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(f"An error occurred during BLE communication: {e}")
|
|
print(f"An error occurred during BLE communication: {e}")
|
|
await asyncio.sleep(5) # Wait for 5 seconds before retrying
|
|
await asyncio.sleep(5) # Wait for 5 seconds before retrying
|