andylockran / heatmiserV3

MIT License
12 stars 11 forks source link

Allow for local RS485 adapters to be used #84

Open colinmacgiolla opened 8 months ago

colinmacgiolla commented 8 months ago

Hi,

I'm trying to get it working with a local (rather then via remote IP socket) adapter like this one:

[Mon Feb 26 19:29:29 2024] usb 1-1.3: new full-speed USB device number 6 using dwc_otg
[Mon Feb 26 19:29:29 2024] usb 1-1.3: New USB device found, idVendor=1a86, idProduct=55d3, bcdDevice= 4.45
[Mon Feb 26 19:29:29 2024] usb 1-1.3: New USB device strings: Mfr=0, Product=2, SerialNumber=3
[Mon Feb 26 19:29:29 2024] usb 1-1.3: Product: USB Single Serial
[Mon Feb 26 19:29:29 2024] usb 1-1.3: SerialNumber: AAAA
[Mon Feb 26 19:29:29 2024] cdc_acm 1-1.3:1.0: ttyACM0: USB ACM device
[Mon Feb 26 19:29:29 2024] usbcore: registered new interface driver cdc_acm
[Mon Feb 26 19:29:29 2024] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters

But it looks like its hardcoded to expect a IP:port socket: https://github.com/andylockran/heatmiserV3/blob/5778f5b876d60424bf7d789c3e308e32eee83c6b/heatmiserv3/connection.py#L18C9-L18C84

Testing with the serial library, it seems that the underlying library (and call) has no problem with a local device, but at the moment there is no way to pass that through.

>>> port = serial.serial_for_url("/dev/ttyACM0")
>>> port.open()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/srv/homeassistant/lib/python3.11/site-packages/serial/serialposix.py", line 318, in open
    raise SerialException("Port is already open.")

Happy to help out or test, but I don't know much about the HA side of the plugin...

Thanks

andyloughran commented 8 months ago

Heya, I'm going through a refactor so that it should be possible to send a different connection into the tool.

This should allow you to put your device in; lemme find some time to write you instructions to test - if so that might unblock you.

6by9 commented 8 months ago

I'm not actively using it at present, and my tree is a tad old (1c262a3605f868cbe319de9eaeac32fead0323b7), but I have a Pi connected to a USB RS485 adapter talking to my 10 PRT-N (or similar) stats. Diffs:

-- a/examples/set_temperature_tp_20.py
+++ b/examples/set_temperature_tp_20.py
@@ -15,20 +15,33 @@ PORT = "102"
 logging.basicConfig(level=logging.INFO)

 # Create a HeatmiserUH! connection
-HeatmiserUH1 = connection.HeatmiserUH1(IP_ADDRESS, PORT)
+HeatmiserUH1 = connection.HeatmiserUH1(dev="/dev/ttyUSB0")
diff --git a/heatmiserV3/connection.py b/heatmiserV3/connection.py
index 6e281a8..70c81dc 100644
--- a/heatmiserV3/connection.py
+++ b/heatmiserV3/connection.py
@@ -13,9 +13,15 @@ class HeatmiserUH1(object):
     connection, and can have multiple thermostats
     """

-    def __init__(self, ipaddress, port):
+    def __init__(self, ipaddress = None, port = 0, dev = None):
         self.thermostats = {}
-        self._serport = serial.serial_for_url("socket://" + ipaddress + ":" + port)
+        if (dev != None) :
+            self._serport = serial.serial_for_url(
+                    dev
+            )
+        elif (ipaddress != None) :
+            self._serport = serial.serial_for_url("socket://" + ipaddress + ":" + port)
+
         # Ensures that the serial port has not
         # been left hanging around by a previous process.
         serport_response = self._serport.close()

Everything else I've tried just works if you pass in dev when creating HeatmiserUH1. Hope that helps.

colinmacgiolla commented 8 months ago

Thanks! Was thinking along the same lines this morning!