mirror of
https://github.com/pommi/python-itho-wpu.git
synced 2024-12-21 18:43:27 +01:00
Compare commits
5 Commits
482e6c8c60
...
381dd4c6ec
Author | SHA1 | Date | |
---|---|---|---|
381dd4c6ec | |||
1ffd5a72d7 | |||
7a1140e26c | |||
d28acae756 | |||
620cdaf880 |
11
README.md
11
README.md
@ -121,6 +121,17 @@ See the [pislave](https://github.com/ootjersb/pislave#wiring) project
|
||||
...
|
||||
```
|
||||
|
||||
* Change a setting of the WPU
|
||||
```
|
||||
# ./itho-wpu.py --action setsetting --settingid 139 --value 48
|
||||
Current setting:
|
||||
139. Blokkade Tijd Van Verwarmen Naar Koelen (uur): 24 (min: 0, max: 168, step: 1)
|
||||
Setting `139` will be changed to `48`? [y/N] y
|
||||
Updating setting 139 to `48`
|
||||
Are you really sure? (Type uppercase yes): YES
|
||||
139. Blokkade Tijd Van Verwarmen Naar Koelen (uur): 48 (min: 0, max: 168, step: 1)
|
||||
```
|
||||
|
||||
# Exporting measurements
|
||||
|
||||
## InfluxDB
|
||||
|
89
itho-wpu.py
89
itho-wpu.py
@ -24,6 +24,7 @@ actions = {
|
||||
"getdatatype": [0xA4, 0x00],
|
||||
"getdatalog": [0xA4, 0x01],
|
||||
"getsetting": [0xA4, 0x10],
|
||||
"setsetting": [0xA4, 0x10],
|
||||
}
|
||||
|
||||
|
||||
@ -43,6 +44,11 @@ def parse_args():
|
||||
type=int,
|
||||
help="Setting identifier",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--value",
|
||||
nargs="?",
|
||||
help="Setting value",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--loglevel",
|
||||
nargs="?",
|
||||
@ -82,7 +88,7 @@ class IthoWPU:
|
||||
self.datatype = self.get("getdatatype")
|
||||
self.heatpump_db = db.sqlite("heatpump.sqlite")
|
||||
|
||||
def get(self, action, identifier=None):
|
||||
def get(self, action, identifier=None, value=None):
|
||||
if not self.no_cache:
|
||||
response = self.cache.get(action.replace("get", ""))
|
||||
if response is not None:
|
||||
@ -100,7 +106,7 @@ class IthoWPU:
|
||||
if not self.slave_only:
|
||||
master = I2CMaster(address=0x41, bus=1, queue=self._q)
|
||||
if action:
|
||||
response = master.execute_action(action, identifier)
|
||||
response = master.execute_action(action, identifier, value)
|
||||
logger.debug(f"Response: {response}")
|
||||
master.close()
|
||||
|
||||
@ -311,7 +317,7 @@ def process_datalog(response, wpu):
|
||||
return measurements
|
||||
|
||||
|
||||
def process_setting(response, wpu):
|
||||
def parse_setting(response, wpu):
|
||||
message = response[5:]
|
||||
|
||||
settingid = int(message[17], 0)
|
||||
@ -325,6 +331,21 @@ def process_setting(response, wpu):
|
||||
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,
|
||||
@ -346,7 +367,62 @@ def process_settings(wpu, args):
|
||||
process_response("getsetting", response, args, wpu)
|
||||
|
||||
|
||||
def process_setsetting(wpu, args):
|
||||
logger.info("Current setting:")
|
||||
response = wpu.get("getsetting", int(args.settingid))
|
||||
if response is None:
|
||||
return
|
||||
process_response("getsetting", response, args, wpu)
|
||||
message = response[5:]
|
||||
datatype = message[16]
|
||||
|
||||
if args.value is None:
|
||||
value = input("Provide a new value: ")
|
||||
else:
|
||||
value = args.value
|
||||
|
||||
# TODO: add support for negative values
|
||||
if float(value) < 0:
|
||||
logger.error("Negative values are not supported yet.")
|
||||
return
|
||||
|
||||
logger.debug(f"New setting datatype: {datatype}")
|
||||
logger.debug(f"New setting (input): {value}")
|
||||
normalized_value = int(value.replace(".", ""))
|
||||
logger.debug(f"New setting (normalized): {normalized_value}")
|
||||
hex_list_value = [hex(v) for v in list(normalized_value.to_bytes(4, byteorder="big"))]
|
||||
logger.debug(f"New setting (hex): {hex_list_value}")
|
||||
parsed_value = format_datatype(args.settingid, hex_list_value, datatype)
|
||||
logger.debug(f"New setting (parsed): {parsed_value}")
|
||||
|
||||
_, minimum, maximum, _ = parse_setting(response, wpu)
|
||||
if parsed_value < minimum or parsed_value > maximum:
|
||||
logger.error(f"New value `{parsed_value}` is not between `{minimum}` and `{maximum}`")
|
||||
return
|
||||
|
||||
sure = input(f"Setting `{args.settingid}` will be changed to `{parsed_value}`? [y/N] ")
|
||||
if sure in ["y", "Y"]:
|
||||
logger.info(f"Updating setting {args.settingid} to `{parsed_value}`")
|
||||
else:
|
||||
logger.error("Aborted")
|
||||
return
|
||||
|
||||
response = wpu.get("setsetting", args.settingid, normalized_value)
|
||||
if response is None:
|
||||
return
|
||||
process_response("getsetting", response, args, wpu)
|
||||
|
||||
|
||||
def format_datatype(name, m, dt):
|
||||
"""
|
||||
Transform a list of bytes to a readable number based on the datatype.
|
||||
|
||||
:param str name: Name/label of the data
|
||||
:param list[str] m: List of bytes in hexadecimal string format
|
||||
:param dt: Datatype
|
||||
:type dt: str or int
|
||||
"""
|
||||
|
||||
num = None
|
||||
if type(dt) is str:
|
||||
dt = int(dt, 0)
|
||||
@ -410,13 +486,14 @@ def main():
|
||||
|
||||
if args.loglevel:
|
||||
logger.setLevel(args.loglevel.upper())
|
||||
logging.getLogger("itho_i2c").setLevel(args.loglevel.upper())
|
||||
|
||||
if args.timestamp:
|
||||
stdout_log_handler.setFormatter(
|
||||
logging.Formatter("%(asctime)-15s %(levelname)s: %(message)s")
|
||||
)
|
||||
|
||||
if args.action == "getsetting" and args.settingid is None:
|
||||
if args.action in ["getsetting", "setsetting"] and args.settingid is None:
|
||||
logger.error("`--settingid` is required with `--action getsetting`")
|
||||
return
|
||||
|
||||
@ -426,6 +503,10 @@ def main():
|
||||
process_settings(wpu, args)
|
||||
return
|
||||
|
||||
if args.action == "setsetting":
|
||||
process_setsetting(wpu, args)
|
||||
return
|
||||
|
||||
response = wpu.get(args.action, args.settingid)
|
||||
if response is not None:
|
||||
process_response(args.action, response, args, wpu)
|
||||
|
58
itho_i2c.py
58
itho_i2c.py
@ -4,8 +4,13 @@ import logging
|
||||
import pigpio
|
||||
import struct
|
||||
import time
|
||||
import sys
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.INFO)
|
||||
stdout_log_handler = logging.StreamHandler(sys.stdout)
|
||||
stdout_log_handler.setFormatter(logging.Formatter("%(message)s"))
|
||||
logger.addHandler(stdout_log_handler)
|
||||
|
||||
actions = {
|
||||
"getnodeid": [0x90, 0xE0],
|
||||
@ -13,6 +18,7 @@ actions = {
|
||||
"getdatatype": [0xA4, 0x00],
|
||||
"getdatalog": [0xA4, 0x01],
|
||||
"getsetting": [0xA4, 0x10],
|
||||
"setsetting": [0xA4, 0x10],
|
||||
}
|
||||
|
||||
|
||||
@ -46,34 +52,29 @@ class I2CMaster:
|
||||
self.i = I2CRaw(address=address, bus=bus)
|
||||
self.queue = queue
|
||||
|
||||
def compose_request(self, action, identifier):
|
||||
def compose_request(self, action, identifier, value):
|
||||
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,
|
||||
]
|
||||
+ [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]
|
||||
)
|
||||
elif action == "setsetting":
|
||||
byte_list_value = list(value.to_bytes(4, byteorder="big"))
|
||||
request = (
|
||||
[0x80]
|
||||
+ actions[action]
|
||||
+ [0x06, 0x13] # write, length
|
||||
+ byte_list_value # new
|
||||
+ [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
|
||||
@ -90,11 +91,16 @@ class I2CMaster:
|
||||
checksum = 0
|
||||
return checksum
|
||||
|
||||
def execute_action(self, action, identifier):
|
||||
request = self.compose_request(action, identifier)
|
||||
def execute_action(self, action, identifier, value):
|
||||
request = self.compose_request(action, identifier, value)
|
||||
request_in_hex = [hex(c) for c in request]
|
||||
logger.debug(f"Request: {request_in_hex}")
|
||||
result = None
|
||||
if action == "setsetting":
|
||||
sure = input("Are you really sure? (Type uppercase yes): ")
|
||||
if sure != "YES":
|
||||
logger.error("Aborted")
|
||||
return
|
||||
for i in range(0, 20):
|
||||
logger.debug(f"Executing action: {action}")
|
||||
self.i.write_i2c_block_data(request)
|
||||
|
Loading…
Reference in New Issue
Block a user