python-qube-heatpump 1.0.1__tar.gz → 1.2.1__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/.gitignore +139 -0
  2. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/PKG-INFO +1 -1
  3. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/pyproject.toml +1 -1
  4. python_qube_heatpump-1.2.1/src/python_qube_heatpump/client.py +105 -0
  5. python_qube_heatpump-1.2.1/src/python_qube_heatpump/const.py +71 -0
  6. python_qube_heatpump-1.2.1/src/python_qube_heatpump/models.py +37 -0
  7. python_qube_heatpump-1.0.1/src/python_qube_heatpump/client.py +0 -166
  8. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
  9. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  10. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
  11. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/.github/workflows/ci.yml +0 -0
  12. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/.github/workflows/python-publish.yml +0 -0
  13. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/LICENSE +0 -0
  14. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/README.md +0 -0
  15. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/pytest.ini +0 -0
  16. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/src/python_qube_heatpump/__init__.py +0 -0
  17. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/tests/conftest.py +0 -0
  18. {python_qube_heatpump-1.0.1 → python_qube_heatpump-1.2.1}/tests/test_client.py +0 -0
@@ -0,0 +1,139 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+
77
+ # Jupyter Notebook
78
+ .ipynb_checkpoints
79
+
80
+ # IPython
81
+ profile_default/
82
+ ipython_config.py
83
+
84
+ # pyenv
85
+ .python-version
86
+
87
+ # pipenv
88
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
90
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
91
+ # install all needed dependencies.
92
+ #Pipfile.lock
93
+
94
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95
+ __pypackages__/
96
+
97
+ # Celery stuff
98
+ celerybeat-schedule
99
+ celerybeat.pid
100
+
101
+ # SageMath parsed files
102
+ *.sage.py
103
+
104
+ # Environments
105
+ .env
106
+ .venv
107
+ env/
108
+ venv/
109
+ ENV/
110
+ env.bak/
111
+ venv.bak/
112
+
113
+ # Spyder project settings
114
+ .spyderproject
115
+ .spyproject
116
+
117
+ # Rope project settings
118
+ .ropeproject
119
+
120
+ # mkdocs documentation
121
+ /site
122
+
123
+ # mypy
124
+ .mypy_cache/
125
+ .dmypy.json
126
+ dmypy.json
127
+
128
+ # Pyre type checker
129
+ .pyre/
130
+
131
+ # pytype static type analyzer
132
+ .pytype/
133
+
134
+ # Cython debug symbols
135
+ cython_debug/
136
+
137
+ # IDEs
138
+ .idea/
139
+ .vscode/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-qube-heatpump
3
- Version: 1.0.1
3
+ Version: 1.2.1
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.0.1"
7
+ version = "1.2.1"
8
8
  authors = [
9
9
  { name="MattieGit", email="6250046+MattieGit@users.noreply.github.com" },
10
10
  ]
@@ -0,0 +1,105 @@
1
+ """Client for Qube Heat Pump."""
2
+
3
+ import logging
4
+ from typing import Optional
5
+
6
+ from pymodbus.client import AsyncModbusTcpClient
7
+ from pymodbus.payload import BinaryPayloadDecoder
8
+ from pymodbus.constants import Endian
9
+
10
+ from . import const
11
+ from .models import QubeState
12
+
13
+ _LOGGER = logging.getLogger(__name__)
14
+
15
+
16
+ class QubeClient:
17
+ """Qube Modbus Client."""
18
+
19
+ def __init__(self, host: str, port: int = 502, unit_id: int = 1):
20
+ """Initialize."""
21
+ self.host = host
22
+ self.port = port
23
+ self.unit = unit_id
24
+ self._client = AsyncModbusTcpClient(host, port=port)
25
+ self._connected = False
26
+
27
+ async def connect(self) -> bool:
28
+ """Connect to the Modbus server."""
29
+ if not self._connected:
30
+ self._connected = await self._client.connect()
31
+ return self._connected
32
+
33
+ async def close(self) -> None:
34
+ """Close connection."""
35
+ self._client.close()
36
+ self._connected = False
37
+
38
+ async def get_all_data(self) -> QubeState:
39
+ """Fetch all definition data and return a state object."""
40
+ # Note: In a real implementation you might want to optimize this
41
+ # by reading contiguous blocks instead of one-by-one.
42
+ # For now, we wrap the individual reads for abstraction.
43
+
44
+ state = QubeState()
45
+
46
+ # Helper to read and assign
47
+ async def _read(const_def):
48
+ return await self.read_value(const_def)
49
+
50
+ # Fetch basic sensors
51
+ state.temp_supply = await _read(const.TEMP_SUPPLY)
52
+ state.temp_return = await _read(const.TEMP_RETURN)
53
+ state.temp_outside = await _read(const.TEMP_OUTSIDE)
54
+ state.temp_dhw = await _read(const.TEMP_DHW)
55
+
56
+ return state
57
+
58
+ async def read_value(self, definition: tuple) -> Optional[float]:
59
+ """Read a single value based on the constant definition."""
60
+ address, reg_type, data_type, scale, offset = definition
61
+
62
+ count = (
63
+ 2
64
+ if data_type
65
+ in (const.DataType.FLOAT32, const.DataType.UINT32, const.DataType.INT32)
66
+ else 1
67
+ )
68
+
69
+ try:
70
+ if reg_type == const.ModbusType.INPUT:
71
+ result = await self._client.read_input_registers(
72
+ address, count, slave=self.unit
73
+ )
74
+ else:
75
+ result = await self._client.read_holding_registers(
76
+ address, count, slave=self.unit
77
+ )
78
+
79
+ if result.isError():
80
+ _LOGGER.warning("Error reading address %s", address)
81
+ return None
82
+
83
+ decoder = BinaryPayloadDecoder.fromRegisters(
84
+ result.registers, byteorder=Endian.Big, wordorder=Endian.Little
85
+ )
86
+
87
+ if data_type == const.DataType.FLOAT32:
88
+ val = decoder.decode_32bit_float()
89
+ elif data_type == const.DataType.INT16:
90
+ val = decoder.decode_16bit_int()
91
+ elif data_type == const.DataType.UINT16:
92
+ val = decoder.decode_16bit_uint()
93
+ else:
94
+ val = 0
95
+
96
+ if scale is not None:
97
+ val *= scale
98
+ if offset is not None:
99
+ val += offset
100
+
101
+ return val
102
+
103
+ except Exception as e:
104
+ _LOGGER.error("Exception reading address %s: %s", address, e)
105
+ return None
@@ -0,0 +1,71 @@
1
+ """Constants for Qube Heat Pump."""
2
+
3
+ from enum import Enum
4
+
5
+
6
+ class ModbusType(str, Enum):
7
+ """Modbus register type."""
8
+
9
+ HOLDING = "holding"
10
+ INPUT = "input"
11
+
12
+
13
+ class DataType(str, Enum):
14
+ """Data type."""
15
+
16
+ FLOAT32 = "float32"
17
+ INT16 = "int16"
18
+ UINT16 = "uint16"
19
+
20
+
21
+ # Register definitions (Address, Type, Data Type, Scale, Offset)
22
+ # Scale/Offset are None if not used.
23
+ # Format: KEY = (Address, ModbusType, DataType, Scale, Offset)
24
+
25
+ # --- Sensors (Input Registers) ---
26
+ PCT_USER_PUMP = (4, ModbusType.INPUT, DataType.FLOAT32, -1, 100)
27
+ PCT_SOURCE_PUMP = (6, ModbusType.INPUT, DataType.FLOAT32, -1, 100)
28
+ PCT_SOURCE_VALVE = (8, ModbusType.INPUT, DataType.FLOAT32, None, None)
29
+ REQ_DHW = (14, ModbusType.INPUT, DataType.FLOAT32, None, None)
30
+ REQ_COMPRESSOR = (16, ModbusType.INPUT, DataType.FLOAT32, None, None)
31
+ FLOW_RATE = (18, ModbusType.INPUT, DataType.FLOAT32, None, None)
32
+ TEMP_SUPPLY = (20, ModbusType.INPUT, DataType.FLOAT32, None, None)
33
+ TEMP_RETURN = (22, ModbusType.INPUT, DataType.FLOAT32, None, None)
34
+ TEMP_SOURCE_IN = (24, ModbusType.INPUT, DataType.FLOAT32, None, None)
35
+ TEMP_SOURCE_OUT = (26, ModbusType.INPUT, DataType.FLOAT32, None, None)
36
+ TEMP_ROOM = (28, ModbusType.INPUT, DataType.FLOAT32, None, None)
37
+ TEMP_DHW = (30, ModbusType.INPUT, DataType.FLOAT32, None, None)
38
+ TEMP_OUTSIDE = (32, ModbusType.INPUT, DataType.FLOAT32, None, None)
39
+ COP_CALC = (34, ModbusType.INPUT, DataType.FLOAT32, None, None)
40
+ POWER_THERMIC = (36, ModbusType.INPUT, DataType.FLOAT32, None, None)
41
+ STATUS_CODE = (38, ModbusType.INPUT, DataType.UINT16, None, None)
42
+ TEMP_REG_SETPOINT = (39, ModbusType.INPUT, DataType.FLOAT32, None, None)
43
+ TEMP_COOL_SETPOINT = (41, ModbusType.INPUT, DataType.FLOAT32, None, None)
44
+ TEMP_HEAT_SETPOINT = (43, ModbusType.INPUT, DataType.FLOAT32, None, None)
45
+ COMPRESSOR_SPEED = (45, ModbusType.INPUT, DataType.FLOAT32, 60, None) # RPM
46
+ TEMP_DHW_SETPOINT = (47, ModbusType.INPUT, DataType.FLOAT32, None, None)
47
+ HOURS_DHW = (50, ModbusType.INPUT, DataType.INT16, None, None)
48
+ HOURS_HEAT = (52, ModbusType.INPUT, DataType.INT16, None, None)
49
+ HOURS_COOL = (54, ModbusType.INPUT, DataType.INT16, None, None)
50
+ HOURS_HEATER_1 = (56, ModbusType.INPUT, DataType.INT16, None, None)
51
+ HOURS_HEATER_2 = (58, ModbusType.INPUT, DataType.INT16, None, None)
52
+ HOURS_HEATER_3 = (60, ModbusType.INPUT, DataType.INT16, None, None)
53
+ POWER_ELECTRIC_CALC = (61, ModbusType.INPUT, DataType.FLOAT32, None, None)
54
+ TEMP_PLANT_SETPOINT = (65, ModbusType.INPUT, DataType.FLOAT32, None, None)
55
+ ENERGY_ELECTRIC_TOTAL = (69, ModbusType.INPUT, DataType.FLOAT32, None, None)
56
+ ENERGY_THERMIC_TOTAL = (71, ModbusType.INPUT, DataType.FLOAT32, None, None)
57
+ TEMP_ROOM_MODBUS = (75, ModbusType.INPUT, DataType.FLOAT32, None, None)
58
+
59
+ # --- Configuration (Holding Registers) ---
60
+ SETPOINT_HEAT_DAY = (27, ModbusType.HOLDING, DataType.FLOAT32, None, None)
61
+ SETPOINT_HEAT_NIGHT = (29, ModbusType.HOLDING, DataType.FLOAT32, None, None)
62
+ SETPOINT_COOL_DAY = (31, ModbusType.HOLDING, DataType.FLOAT32, None, None)
63
+ SETPOINT_COOL_NIGHT = (33, ModbusType.HOLDING, DataType.FLOAT32, None, None)
64
+ DT_DHW = (43, ModbusType.HOLDING, DataType.INT16, None, None)
65
+ MIN_TEMP_DHW = (44, ModbusType.HOLDING, DataType.FLOAT32, None, None)
66
+ TEMP_DHW_PROG = (46, ModbusType.HOLDING, DataType.FLOAT32, None, None)
67
+ MIN_SETPOINT_BUFFER = (99, ModbusType.HOLDING, DataType.FLOAT32, None, None)
68
+ USER_HEAT_SETPOINT = (101, ModbusType.HOLDING, DataType.FLOAT32, None, None)
69
+ USER_COOL_SETPOINT = (103, ModbusType.HOLDING, DataType.FLOAT32, None, None)
70
+ MAX_SETPOINT_BUFFER = (169, ModbusType.HOLDING, DataType.FLOAT32, None, None)
71
+ USER_DHW_SETPOINT = (173, ModbusType.HOLDING, DataType.FLOAT32, None, None)
@@ -0,0 +1,37 @@
1
+ """Models for Qube Heat Pump."""
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Optional
5
+
6
+
7
+ @dataclass
8
+ class QubeState:
9
+ """Representation of the Qube Heat Pump state."""
10
+
11
+ # Temperatures
12
+ temp_supply: Optional[float] = None
13
+ temp_return: Optional[float] = None
14
+ temp_source_in: Optional[float] = None
15
+ temp_source_out: Optional[float] = None
16
+ temp_room: Optional[float] = None
17
+ temp_dhw: Optional[float] = None
18
+ temp_outside: Optional[float] = None
19
+
20
+ # Power/Energy
21
+ power_thermic: Optional[float] = None
22
+ power_electric: Optional[float] = None
23
+ energy_total_electric: Optional[float] = None
24
+ energy_total_thermic: Optional[float] = None
25
+ cop_calc: Optional[float] = None
26
+
27
+ # Operation
28
+ status_code: Optional[int] = None
29
+ compressor_speed: Optional[float] = None
30
+ flow_rate: Optional[float] = None
31
+
32
+ # Setpoints (Read/Write)
33
+ setpoint_room_heat_day: Optional[float] = None
34
+ setpoint_room_heat_night: Optional[float] = None
35
+ setpoint_room_cool_day: Optional[float] = None
36
+ setpoint_room_cool_night: Optional[float] = None
37
+ setpoint_dhw: Optional[float] = None
@@ -1,166 +0,0 @@
1
- """Qube Heat Pump Client Library."""
2
-
3
- from __future__ import annotations
4
-
5
- import asyncio
6
- import contextlib
7
- import ipaddress
8
- import logging
9
- import socket
10
- import struct
11
- from typing import Any
12
-
13
- from pymodbus.client import AsyncModbusTcpClient
14
- from pymodbus.exceptions import ModbusException
15
- from pymodbus.pdu import ExceptionResponse
16
-
17
-
18
- class QubeClient:
19
- """Qube Heat Pump Client."""
20
-
21
- def __init__(
22
- self,
23
- host: str,
24
- port: int,
25
- unit_id: int = 1,
26
- ) -> None:
27
- """Initialize the client."""
28
- self._host = host
29
- self._port = port
30
- self._unit = unit_id
31
- self._client: AsyncModbusTcpClient | None = None
32
- self._io_timeout_s: float = 3.0
33
-
34
- @property
35
- def host(self) -> str:
36
- """Return host."""
37
- return self._host
38
-
39
- @property
40
- def port(self) -> int:
41
- """Return port."""
42
- return self._port
43
-
44
- @property
45
- def unit(self) -> int:
46
- """Return unit ID."""
47
- return self._unit
48
-
49
- def set_unit_id(self, unit_id: int) -> None:
50
- """Set unit ID."""
51
- self._unit = int(unit_id)
52
-
53
- async def connect(self) -> bool:
54
- """Connect to the Modbus server."""
55
- if self._client is None:
56
- self._client = AsyncModbusTcpClient(self._host, port=self._port)
57
-
58
- if self.is_connected:
59
- return True
60
-
61
- try:
62
- return await asyncio.wait_for(
63
- self._client.connect(), timeout=self._io_timeout_s
64
- )
65
- except Exception as exc: # pylint: disable=broad-except # noqa: BLE001
66
- logging.getLogger(__name__).debug("Failed to connect: %s", exc)
67
- return False
68
-
69
- @property
70
- def is_connected(self) -> bool:
71
- """Return True if connected."""
72
- return bool(self._client and getattr(self._client, "connected", False))
73
-
74
- async def close(self) -> None:
75
- """Close the connection."""
76
- if self._client:
77
- with contextlib.suppress(Exception):
78
- self._client.close()
79
- self._client = None
80
-
81
- async def _call(self, method: str, **kwargs: Any) -> Any:
82
- if self._client is None:
83
- raise ModbusException("Client not connected")
84
- func = getattr(self._client, method)
85
- # Try with 'slave' then 'unit', finally without either
86
- try:
87
- resp = await asyncio.wait_for(
88
- func(**{**kwargs, "slave": self._unit}), timeout=self._io_timeout_s
89
- )
90
- except TypeError:
91
- try:
92
- resp = await asyncio.wait_for(
93
- func(**{**kwargs, "unit": self._unit}), timeout=self._io_timeout_s
94
- )
95
- except TypeError:
96
- resp = await asyncio.wait_for(
97
- func(**kwargs), timeout=self._io_timeout_s
98
- )
99
- # Normalize error checking
100
- if isinstance(resp, ExceptionResponse) or (
101
- hasattr(resp, "isError") and resp.isError()
102
- ):
103
- raise ModbusException(f"Modbus error on {method} with {kwargs}")
104
- return resp
105
-
106
- async def read_registers(
107
- self, address: int, count: int, input_type: str = "holding"
108
- ) -> list[int]:
109
- """Read registers from the device."""
110
- if input_type == "input":
111
- rr = await self._call("read_input_registers", address=address, count=count)
112
- else:
113
- rr = await self._call(
114
- "read_holding_registers", address=address, count=count
115
- )
116
-
117
- regs = getattr(rr, "registers", None)
118
- if regs is None:
119
- raise ModbusException("No registers returned")
120
- return list(regs)
121
-
122
- @staticmethod
123
- def decode_registers(regs: list[int], data_type: str | None) -> float | int:
124
- """Decode registers to a value."""
125
- # All decoding assumes big-endian word and byte order.
126
- if data_type == "float32":
127
- raw = struct.pack(">HH", int(regs[0]) & 0xFFFF, int(regs[1]) & 0xFFFF)
128
- return float(struct.unpack(">f", raw)[0])
129
- if data_type == "int16":
130
- v = int(regs[0]) & 0xFFFF
131
- return v - 0x10000 if v & 0x8000 else v
132
- if data_type == "uint16":
133
- return int(regs[0]) & 0xFFFF
134
- if data_type == "uint32":
135
- return ((int(regs[0]) & 0xFFFF) << 16) | (int(regs[1]) & 0xFFFF)
136
- if data_type == "int32":
137
- u = ((int(regs[0]) & 0xFFFF) << 16) | (int(regs[1]) & 0xFFFF)
138
- return u - 0x1_0000_0000 if u & 0x8000_0000 else u
139
- # Fallback to first register as unsigned 16-bit
140
- return int(regs[0]) & 0xFFFF
141
-
142
- async def resolve_ip(self) -> str | None:
143
- """Resolve the host to an IP address."""
144
- with contextlib.suppress(ValueError):
145
- return str(ipaddress.ip_address(self._host))
146
-
147
- try:
148
- infos = await asyncio.get_running_loop().getaddrinfo(
149
- self._host,
150
- None,
151
- type=socket.SOCK_STREAM,
152
- )
153
- except OSError:
154
- return None
155
-
156
- for family, _, _, _, sockaddr in infos:
157
- if not sockaddr:
158
- continue
159
- addr = sockaddr[0]
160
- if not isinstance(addr, str):
161
- continue
162
- if family == socket.AF_INET6 and addr.startswith("::ffff:"):
163
- addr = addr.removeprefix("::ffff:")
164
- return addr
165
-
166
- return None