1
0
mirror of https://github.com/pommi/python-itho-wpu.git synced 2024-10-31 10:42:17 +01:00

feat(itho-wpu): getsetting support

Action "getsetting" retrieves the value of a setting byi the provided id.

Example usage:
> ./itho-wpu.py --action getsetting --settingid 1
> 1. Hardware Configuratie: 70 (min: 0, max: 65535, step: 1)
> ./itho-wpu.py --action getsetting --settingid 2
> 2. Jaar Inbedrijfstelling: 2010 (min: 2004, max: 2099, step: 1)
This commit is contained in:
Pim van den Berg 2023-06-05 21:35:34 +02:00
parent 620cdaf880
commit d28acae756
3 changed files with 135 additions and 9 deletions

View File

@ -99,6 +99,12 @@ See the [pislave](https://github.com/ootjersb/pislave#wiring) project
... ...
``` ```
* Retrieve a single setting from the WPU
```
# ./itho-wpu.py --action getsetting --settingid 1
1. Hardware Configuratie: 70 (min: 0, max: 65535, step: 1)
```
# Exporting measurements # Exporting measurements
## InfluxDB ## InfluxDB

View File

@ -23,6 +23,7 @@ actions = {
"getserial": [0x90, 0xE1], "getserial": [0x90, 0xE1],
"getdatatype": [0xA4, 0x00], "getdatatype": [0xA4, 0x00],
"getdatalog": [0xA4, 0x01], "getdatalog": [0xA4, 0x01],
"getsetting": [0xA4, 0x10],
} }
@ -36,6 +37,12 @@ def parse_args():
choices=actions.keys(), choices=actions.keys(),
help="Execute an action", help="Execute an action",
) )
parser.add_argument(
"--settingid",
nargs="?",
type=int,
help="Setting identifier",
)
parser.add_argument( parser.add_argument(
"--loglevel", "--loglevel",
nargs="?", nargs="?",
@ -75,7 +82,7 @@ class IthoWPU:
self.datatype = self.get("getdatatype") self.datatype = self.get("getdatatype")
self.heatpump_db = db.sqlite("heatpump.sqlite") self.heatpump_db = db.sqlite("heatpump.sqlite")
def get(self, action): def get(self, action, identifier=None):
if not self.no_cache: if not self.no_cache:
response = self.cache.get(action.replace("get", "")) response = self.cache.get(action.replace("get", ""))
if response is not None: if response is not None:
@ -93,7 +100,7 @@ class IthoWPU:
if not self.slave_only: if not self.slave_only:
master = I2CMaster(address=0x41, bus=1, queue=self._q) master = I2CMaster(address=0x41, bus=1, queue=self._q)
if action: if action:
response = master.execute_action(action) response = master.execute_action(action, identifier)
logger.debug(f"Response: {response}") logger.debug(f"Response: {response}")
master.close() master.close()
@ -150,6 +157,22 @@ class IthoWPU:
return datalog return datalog
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}"
)
if len(setting_details) != 1:
return None
return setting_details[0]
class IthoWPUCache: class IthoWPUCache:
def __init__(self): def __init__(self):
@ -219,6 +242,8 @@ def process_response(action, response, args, wpu):
from itho_export import export_to_influxdb from itho_export import export_to_influxdb
export_to_influxdb(action, measurements) export_to_influxdb(action, measurements)
elif action == "getsetting":
process_setting(response, wpu)
elif action == "getnodeid": elif action == "getnodeid":
process_nodeid(response) process_nodeid(response)
elif action == "getserial": elif action == "getserial":
@ -272,6 +297,48 @@ def process_datalog(response, wpu):
return measurements return measurements
def parse_setting(response, wpu):
message = response[5:]
settingid = int(message[17], 0)
setting = wpu.get_setting_by_id(settingid)
if setting is None:
logger.error(f"Setting '{settingid}' is invalid")
return
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)
return value, minimum, maximum, step
def process_setting(response, wpu):
message = response[5:]
settingid = int(message[17], 0)
setting = wpu.get_setting_by_id(settingid)
if setting is None:
logger.error(f"Setting '{settingid}' is invalid")
return
value, minimum, maximum, step = parse_setting(response, wpu)
logger.info(
"{}. {}{}: {} (min: {}, max: {}, step: {})".format(
settingid,
setting["title"].title(),
f' ({setting["unit"]})' if setting["unit"] is not None else "",
value,
minimum,
maximum,
step,
)
)
def format_datatype(name, m, dt): def format_datatype(name, m, dt):
""" """
Transform a list of bytes to a readable number based on the datatype. Transform a list of bytes to a readable number based on the datatype.
@ -288,14 +355,46 @@ def format_datatype(name, m, dt):
if dt == 0x0 or dt == 0xC: if dt == 0x0 or dt == 0xC:
num = int(m[-1], 0) num = int(m[-1], 0)
elif dt == 0x1:
num = round(int(m[-1], 0) / 10, 1)
elif dt == 0x2:
num = round(int(m[-1], 0) / 100, 2)
elif dt == 0x10: elif dt == 0x10:
num = (int(m[-2], 0) << 8) + int(m[-1], 0) num = (int(m[-2], 0) << 8) + int(m[-1], 0)
elif dt == 0x12: elif dt == 0x12:
num = round((int(m[-2], 0) << 8) + int(m[-1], 0) / 100, 2) num = round((int(m[-2], 0) << 8) + int(m[-1], 0) / 100, 2)
elif dt == 0x13:
num = round((int(m[-2], 0) << 8) + int(m[-1], 0) / 1000, 3)
elif dt == 0x14:
num = round((int(m[-2], 0) << 8) + int(m[-1], 0) / 10000, 4)
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: elif dt == 0x90:
num = (int(m[-2], 0) << 8) + int(m[-1], 0) num = (int(m[-2], 0) << 8) + int(m[-1], 0)
if num >= 32768: if num >= 32768:
num -= 65536 num -= 65536
elif dt == 0x91:
num = (int(m[-2], 0) << 8) + int(m[-1], 0)
if num >= 32768:
num -= 65536
num = round(num / 10, 2)
elif dt == 0x92: elif dt == 0x92:
num = (int(m[-2], 0) << 8) + int(m[-1], 0) num = (int(m[-2], 0) << 8) + int(m[-1], 0)
if num >= 32768: if num >= 32768:
@ -308,7 +407,7 @@ def format_datatype(name, m, dt):
return num return num
if __name__ == "__main__": def main():
args = parse_args() args = parse_args()
if args.loglevel: if args.loglevel:
@ -319,7 +418,15 @@ if __name__ == "__main__":
logging.Formatter("%(asctime)-15s %(levelname)s: %(message)s") logging.Formatter("%(asctime)-15s %(levelname)s: %(message)s")
) )
if args.action == "getsetting" and args.settingid is None:
logger.error("`--settingid` is required with `--action getsetting`")
return
wpu = IthoWPU(args.master_only, args.slave_only, args.slave_timeout, args.no_cache) 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: if response is not None:
process_response(args.action, response, args, wpu) process_response(args.action, response, args, wpu)
if __name__ == "__main__":
main()

View File

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