From c9d82f67a0a77981a584393e2acc18af1405ba62 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Mon, 5 Jun 2023 21:35:34 +0200 Subject: [PATCH] WIP: getsetting support --- itho-wpu.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++--- itho_i2c.py | 40 +++++++++++++++++++++++++---- 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/itho-wpu.py b/itho-wpu.py index f857d77..68fcdd0 100755 --- a/itho-wpu.py +++ b/itho-wpu.py @@ -23,6 +23,7 @@ actions = { "getserial": [0x90, 0xE1], "getdatatype": [0xA4, 0x00], "getdatalog": [0xA4, 0x01], + "getsetting": [0xA4, 0x10], } @@ -36,6 +37,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 +82,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 +100,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 +157,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 +240,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": @@ -272,6 +295,29 @@ def process_datalog(response, wpu): 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: @@ -279,10 +325,31 @@ def format_datatype(name, m, dt): 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: @@ -311,6 +378,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) diff --git a/itho_i2c.py b/itho_i2c.py index 00dfd6f..82972a6 100644 --- a/itho_i2c.py +++ b/itho_i2c.py @@ -12,6 +12,7 @@ actions = { "getserial": [0x90, 0xE1], "getdatatype": [0xA4, 0x00], "getdatalog": [0xA4, 0x01], + "getsetting": [0xA4, 0x10], } @@ -45,9 +46,38 @@ class I2CMaster: self.i = I2CRaw(address=address, bus=bus) self.queue = queue - def compose_request(self, action): - # 0x80 = source, 0x04 = msg_type, 0x00 = length - request = [0x80] + actions[action] + [0x04, 0x00] + 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, + ] + ) + else: + # 0x80 = source, 0x04 = msg_type, 0x00 = length + request = [0x80] + actions[action] + [0x04, 0x00] request.append(self.calculate_checksum(request)) return request @@ -60,8 +90,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