1
0
mirror of https://github.com/pommi/python-itho-wpu.git synced 2024-12-21 18:43:27 +01:00

Compare commits

...

3 Commits

2 changed files with 142 additions and 30 deletions

View File

@ -23,6 +23,8 @@ actions = {
"getserial": [0x90, 0xE1],
"getdatatype": [0xA4, 0x00],
"getdatalog": [0xA4, 0x01],
"getconfig": [0xC0, 0x30],
"getsetting": [0xA4, 0x10],
}
@ -36,6 +38,12 @@ def parse_args():
choices=actions.keys(),
help="Execute an action",
)
parser.add_argument(
"--settingid",
nargs="?",
type=int,
help="Setting identifier",
)
parser.add_argument(
"--loglevel",
nargs="?",
@ -75,7 +83,7 @@ class IthoWPU:
self.datatype = self.get("getdatatype")
self.heatpump_db = db.sqlite("heatpump.sqlite")
def get(self, action):
def get(self, action, identifier=None):
if not self.no_cache:
response = self.cache.get(action.replace("get", ""))
if response is not None:
@ -93,7 +101,7 @@ class IthoWPU:
if not self.slave_only:
master = I2CMaster(address=0x41, bus=1, queue=self._q)
if action:
response = master.execute_action(action)
response = master.execute_action(action, identifier)
logger.debug(f"Response: {response}")
master.close()
@ -150,6 +158,20 @@ class IthoWPU:
return datalog
return datalog
def get_setting_by_id(self, settingid):
listversion = self.get_listversion_from_nodeid()
parameterlist_version = self.heatpump_db.execute(
f"SELECT parameterlist FROM versiebeheer WHERE version = {listversion}"
)[0]["parameterlist"]
if parameterlist_version is None or not type(parameterlist_version) == int:
logger.error(f"Parameterlist not found in database for version {listversion}")
return None
setting_details = self.heatpump_db.execute(
"SELECT name, min, max, def, title, description, unit "
+ f"FROM parameterlijst_v{parameterlist_version} WHERE id = {settingid}"
)[0]
return setting_details
class IthoWPUCache:
def __init__(self):
@ -219,6 +241,8 @@ def process_response(action, response, args, wpu):
from itho_export import export_to_influxdb
export_to_influxdb(action, measurements)
elif action == "getsetting":
process_setting(response)
elif action == "getnodeid":
process_nodeid(response)
elif action == "getserial":
@ -259,35 +283,90 @@ def process_datalog(response, wpu):
measurements = {}
for d in datalog:
if d.type == 0x0 or d.type == 0xC:
m = message[d.index : d.index + 1] # noqa: E203
num = int(m[0], 0)
elif d.type == 0x10:
m = message[d.index : d.index + 2] # noqa: E203
num = (int(m[0], 0) << 8) + int(m[1], 0)
elif d.type == 0x12:
m = message[d.index : d.index + 2] # noqa: E203
num = round((int(m[0], 0) << 8) + int(m[1], 0) / 100, 2)
elif d.type == 0x90:
m = message[d.index : d.index + 2] # noqa: E203
num = (int(m[0], 0) << 8) + int(m[1], 0)
if num >= 32768:
num -= 65536
elif d.type == 0x92:
m = message[d.index : d.index + 2] # noqa: E203
num = (int(m[0], 0) << 8) + int(m[1], 0)
if num >= 32768:
num -= 65536
num = round(num / 100, 2)
length = 1
elif d.type == 0x10 or d.type == 0x12 or d.type == 0x90 or d.type == 0x92:
length = 2
elif d.type == 0x20:
m = message[d.index : d.index + 4] # noqa: E203
num = (int(m[0], 0) << 24) + (int(m[1], 0) << 16) + (int(m[2], 0) << 8) + int(m[3], 0)
length = 4
else:
logger.error(f"Unknown message type for datalog {d.name}: {d.type}")
logger.error(f"Unknown message type for datalog {d.label}: {d.type}")
num = format_datatype(d.label, message[d.index : d.index + length], d.type) # noqa: E203
logger.info(f"{d.description}: {num}")
measurements[d.label] = num
return measurements
def process_setting(response):
message = response[5:]
settingid = int(message[17], 0)
setting = wpu.get_setting_by_id(settingid)
datatype = message[16]
value = format_datatype(setting["name"], message[0:4], datatype)
minimum = format_datatype(setting["name"], message[4:8], datatype)
maximum = format_datatype(setting["name"], message[8:12], datatype)
step = format_datatype(setting["name"], message[12:16], datatype)
logger.info(
"{}{}: {} (min: {}, max: {}, step: {})".format(
setting["title"],
f' ({setting["unit"]})' if setting["unit"] is not None else "",
value,
minimum,
maximum,
step,
)
)
def format_datatype(name, m, dt):
num = None
if type(dt) is str:
dt = int(dt, 0)
if dt == 0x0 or dt == 0xC:
num = int(m[-1], 0)
elif dt == 0x1:
num = round(int(m[-1], 0) / 10, 1)
elif dt == 0x10:
num = (int(m[-2], 0) << 8) + int(m[-1], 0)
elif dt == 0x12:
num = round((int(m[-2], 0) << 8) + int(m[-1], 0) / 100, 2)
elif dt == 0x80:
num = int(m[-1], 0)
if num >= 128:
num -= 256
elif dt == 0x81:
num = int(m[-1], 0)
if num >= 128:
num -= 256
num = round(num / 10, 1)
elif dt == 0x82:
num = int(m[-1], 0)
if num >= 128:
num -= 256
num = round(num / 100, 2)
elif dt == 0x8F:
num = int(m[-1], 0)
if num >= 128:
num -= 256
num = round(num / 1000, 3)
elif dt == 0x90:
num = (int(m[-2], 0) << 8) + int(m[-1], 0)
if num >= 32768:
num -= 65536
elif dt == 0x92:
num = (int(m[-2], 0) << 8) + int(m[-1], 0)
if num >= 32768:
num -= 65536
num = round(num / 100, 2)
elif dt == 0x20:
num = (int(m[-4], 0) << 24) + (int(m[-3], 0) << 16) + (int(m[-2], 0) << 8) + int(m[-1], 0)
else:
logger.error(f"Unknown datatype for '{name}': 0x{dt:X}")
return num
if __name__ == "__main__":
args = parse_args()
@ -300,6 +379,6 @@ if __name__ == "__main__":
)
wpu = IthoWPU(args.master_only, args.slave_only, args.slave_timeout, args.no_cache)
response = wpu.get(args.action)
response = wpu.get(args.action, args.settingid)
if response is not None:
process_response(args.action, response, args, wpu)

View File

@ -12,6 +12,8 @@ actions = {
"getserial": [0x90, 0xE1],
"getdatatype": [0xA4, 0x00],
"getdatalog": [0xA4, 0x01],
"getconfig": [0xC0, 0x30],
"getsetting": [0xA4, 0x10],
}
@ -45,7 +47,38 @@ class I2CMaster:
self.i = I2CRaw(address=address, bus=bus)
self.queue = queue
def compose_request(self, action):
def compose_request(self, action, identifier):
if action == "getsetting":
request = (
[0x80]
+ actions[action]
+ [
0x04,
0x13,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
identifier,
0x00,
]
)
elif action == "getconfig":
request = [0x80] + actions[action] + [0x04, 0x04, 0x00, 0x00, 0x00, 0x0C]
else:
# 0x80 = source, 0x04 = msg_type, 0x00 = length
request = [0x80] + actions[action] + [0x04, 0x00]
request.append(self.calculate_checksum(request))
@ -60,8 +93,8 @@ class I2CMaster:
checksum = 0
return checksum
def execute_action(self, action):
request = self.compose_request(action)
def execute_action(self, action, identifier):
request = self.compose_request(action, identifier)
request_in_hex = [hex(c) for c in request]
logger.debug(f"Request: {request_in_hex}")
result = None