python-qube-heatpump 1.2.1__tar.gz → 1.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (18) hide show
  1. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/PKG-INFO +1 -1
  2. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/pyproject.toml +1 -1
  3. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/src/python_qube_heatpump/client.py +39 -8
  4. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/src/python_qube_heatpump/const.py +2 -0
  5. python_qube_heatpump-1.2.2/tests/test_client.py +73 -0
  6. python_qube_heatpump-1.2.1/tests/test_client.py +0 -49
  7. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
  8. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  9. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
  10. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/.github/workflows/ci.yml +0 -0
  11. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/.github/workflows/python-publish.yml +0 -0
  12. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/.gitignore +0 -0
  13. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/LICENSE +0 -0
  14. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/README.md +0 -0
  15. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/pytest.ini +0 -0
  16. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/src/python_qube_heatpump/__init__.py +0 -0
  17. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/src/python_qube_heatpump/models.py +0 -0
  18. {python_qube_heatpump-1.2.1 → python_qube_heatpump-1.2.2}/tests/conftest.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-qube-heatpump
3
- Version: 1.2.1
3
+ Version: 1.2.2
4
4
  Summary: Async Modbus client for Qube Heat Pumps
5
5
  Project-URL: Homepage, https://github.com/MattieGit/python-qube-heatpump
6
6
  Project-URL: Bug Tracker, https://github.com/MattieGit/python-qube-heatpump/issues
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "python-qube-heatpump"
7
- version = "1.2.1"
7
+ version = "1.2.2"
8
8
  authors = [
9
9
  { name="MattieGit", email="6250046+MattieGit@users.noreply.github.com" },
10
10
  ]
@@ -1,11 +1,10 @@
1
1
  """Client for Qube Heat Pump."""
2
2
 
3
3
  import logging
4
+ import struct
4
5
  from typing import Optional
5
6
 
6
7
  from pymodbus.client import AsyncModbusTcpClient
7
- from pymodbus.payload import BinaryPayloadDecoder
8
- from pymodbus.constants import Endian
9
8
 
10
9
  from . import const
11
10
  from .models import QubeState
@@ -80,16 +79,48 @@ class QubeClient:
80
79
  _LOGGER.warning("Error reading address %s", address)
81
80
  return None
82
81
 
83
- decoder = BinaryPayloadDecoder.fromRegisters(
84
- result.registers, byteorder=Endian.Big, wordorder=Endian.Little
85
- )
82
+ regs = result.registers
83
+ val = 0
84
+
85
+ # Manual decoding to avoid pymodbus.payload dependencies
86
+ # Assuming Little Endian Word Order for 32-bit values [LSW, MSW] per standard Modbus often used
87
+ # But the original code used Endian.Little WordOrder.
88
+ # Decoder: byteorder=Endian.Big, wordorder=Endian.Little
89
+ # Big Endian Bytes: [H, L]
90
+ # Little Endian Words: [Reg0, Reg1] -> [LSW, MSW]
91
+ #
92
+ # Example Float32: 123.456
93
+ # Reg0 (LSW)
94
+ # Reg1 (MSW)
95
+ # Full 32-bit int: (Reg1 << 16) | Reg0
96
+ # Then pack as >I (Big Endian 32-bit int) and unpack as >f (Big Endian float)?
97
+ #
98
+ # Wait, PyModbus BinaryPayloadDecoder.fromRegisters(registers, byteorder=Endian.Big, wordorder=Endian.Little)
99
+ # ByteOrder Big: Normal network byte order per register.
100
+ # WordOrder Little: The first register is the least significant word.
101
+ #
102
+ # So:
103
+ # 32-bit value = (regs[1] << 16) | regs[0]
104
+ # Then interpret that 32-bit integer as a float.
105
+ # To interpret int bits as float in Python: struct.unpack('!f', struct.pack('!I', int_val))[0]
86
106
 
87
107
  if data_type == const.DataType.FLOAT32:
88
- val = decoder.decode_32bit_float()
108
+ # Combine 2 registers, Little Endian Word Order
109
+ int_val = (regs[1] << 16) | regs[0]
110
+ val = struct.unpack(">f", struct.pack(">I", int_val))[0]
89
111
  elif data_type == const.DataType.INT16:
90
- val = decoder.decode_16bit_int()
112
+ val = regs[0]
113
+ # Signed 16-bit
114
+ if val > 32767:
115
+ val -= 65536
91
116
  elif data_type == const.DataType.UINT16:
92
- val = decoder.decode_16bit_uint()
117
+ val = regs[0]
118
+ elif data_type == const.DataType.UINT32:
119
+ val = (regs[1] << 16) | regs[0]
120
+ elif data_type == const.DataType.INT32:
121
+ val = (regs[1] << 16) | regs[0]
122
+ if val > 2147483647:
123
+ val -= 4294967296
93
124
  else:
94
125
  val = 0
95
126
 
@@ -16,6 +16,8 @@ class DataType(str, Enum):
16
16
  FLOAT32 = "float32"
17
17
  INT16 = "int16"
18
18
  UINT16 = "uint16"
19
+ INT32 = "int32"
20
+ UINT32 = "uint32"
19
21
 
20
22
 
21
23
  # Register definitions (Address, Type, Data Type, Scale, Offset)
@@ -0,0 +1,73 @@
1
+ """Test the Qube Heat Pump client."""
2
+
3
+ from unittest.mock import AsyncMock, MagicMock
4
+ import pytest
5
+ from python_qube_heatpump import QubeClient
6
+
7
+
8
+ @pytest.mark.asyncio
9
+ async def test_connect(mock_modbus_client):
10
+ """Test connection."""
11
+ client = QubeClient("1.2.3.4", 502)
12
+ mock_instance = mock_modbus_client.return_value
13
+ mock_instance.connect.return_value = True
14
+ mock_instance.connected = False
15
+ assert await client.connect() is True
16
+ mock_modbus_client.assert_called_with("1.2.3.4", port=502)
17
+
18
+
19
+ @pytest.mark.asyncio
20
+ async def test_read_value(mock_modbus_client):
21
+ """Test reading values."""
22
+ client = QubeClient("1.2.3.4", 502)
23
+ mock_instance = mock_modbus_client.return_value
24
+ mock_instance.connected = True
25
+
26
+ # Mock response for reading holding registers (FLOAT32)
27
+ # 24.5 = 0x41C40000 -> 16836 (0x41C4), 0 (0x0000) (Big Endian)
28
+ # Our decoder expects [0, 16836] for Little Endian Word Order?
29
+ # Logic in client.py: int_val = (regs[1] << 16) | regs[0]
30
+ # To get 0x41C40000: regs[1]=0x41C4, regs[0]=0x0000
31
+ mock_resp = MagicMock()
32
+ mock_resp.isError.return_value = False
33
+ mock_resp.registers = [0, 16836]
34
+
35
+ mock_instance.read_holding_registers = AsyncMock(return_value=mock_resp)
36
+ client._client = mock_instance
37
+
38
+ # Test reading a FLOAT32 holding register
39
+ # definition = (address, reg_type, data_type, scale, offset)
40
+ # We use a dummy definition
41
+ from python_qube_heatpump import const
42
+
43
+ definition = (10, const.ModbusType.HOLDING, const.DataType.FLOAT32, None, None)
44
+
45
+ result = await client.read_value(definition)
46
+
47
+ # Verify result is approximately 24.5
48
+ assert result is not None
49
+ assert round(result, 1) == 24.5
50
+
51
+ mock_instance.read_holding_registers.assert_called_once()
52
+
53
+
54
+ @pytest.mark.asyncio
55
+ async def test_read_value_int16(mock_modbus_client):
56
+ """Test reading INT16 value."""
57
+ client = QubeClient("1.2.3.4", 502)
58
+ mock_instance = mock_modbus_client.return_value
59
+
60
+ # Mock response for -10 (0xFFF6 = 65526)
61
+ mock_resp = MagicMock()
62
+ mock_resp.isError.return_value = False
63
+ mock_resp.registers = [65526]
64
+
65
+ mock_instance.read_input_registers = AsyncMock(return_value=mock_resp)
66
+ client._client = mock_instance
67
+
68
+ from python_qube_heatpump import const
69
+
70
+ definition = (20, const.ModbusType.INPUT, const.DataType.INT16, None, None)
71
+
72
+ result = await client.read_value(definition)
73
+ assert result == -10
@@ -1,49 +0,0 @@
1
- """Test the Qube Heat Pump client."""
2
-
3
- from unittest.mock import AsyncMock, MagicMock
4
- import pytest
5
- from python_qube_heatpump import QubeClient
6
-
7
-
8
- @pytest.mark.asyncio
9
- async def test_connect(mock_modbus_client):
10
- """Test connection."""
11
- client = QubeClient("1.2.3.4", 502)
12
- mock_instance = mock_modbus_client.return_value
13
- mock_instance.connect.return_value = True
14
- mock_instance.connected = False
15
- assert await client.connect() is True
16
- mock_modbus_client.assert_called_with("1.2.3.4", port=502)
17
-
18
-
19
- @pytest.mark.asyncio
20
- async def test_read_registers(mock_modbus_client):
21
- """Test reading registers."""
22
- client = QubeClient("1.2.3.4", 502)
23
- mock_instance = mock_modbus_client.return_value
24
- mock_instance.connected = True
25
- # Mock response
26
- mock_resp = MagicMock()
27
- mock_resp.isError.return_value = False
28
- mock_resp.registers = [123]
29
- # Setup the read_holding_registers method on the mock
30
- mock_instance.read_holding_registers = AsyncMock(return_value=mock_resp)
31
- # We need to manually set the client on the wrapper if we bypass connect
32
- client._client = mock_instance
33
- result = await client.read_registers(10, 1)
34
- assert result == [123]
35
- mock_instance.read_holding_registers.assert_called_once()
36
-
37
-
38
- @pytest.mark.asyncio
39
- async def test_decode_registers():
40
- """Test Register Decoding."""
41
- # float32: 24.5 = 0x41C40000 -> 16836, 0 (Big Endian)
42
- # struct.unpack('>f', struct.pack('>HH', 16836, 0)) -> 24.5
43
- regs = [16836, 0]
44
- val = QubeClient.decode_registers(regs, "float32")
45
- assert round(val, 1) == 24.5
46
- # int16 (negative): -10 = 0xFFF6 = 65526
47
- regs = [65526]
48
- val = QubeClient.decode_registers(regs, "int16")
49
- assert val == -10