Spanni26 / pyHPSU

Python Script to read and send commands to a Rotex HPSU
26 stars 15 forks source link

value type #48

Open segaura opened 3 years ago

segaura commented 3 years ago

I am looking at value type implementation in CanPI.py. At line 81-83 the value taken from commands_hpsu.json is fitted into a single byte (two hex nibbles)

            if cmd["type"] == "value":
                setValue = int(setValue)
                command = command+" 00 %02X" % (setValue)

this does not work with values bigger than 255. For instance the command

$ pyHPSU -c anti_leg_day:friday

compose the message

32 00 FA 01 01 00 500

breking things, because friday equals 1280 (0x0500).

I tried to solve this with this code

            if cmd["type"] == "value":
                setValue = int(setValue)
                command = command+" %02X %02X" % (setValue >> 8, setValue & 0xff)

but further investigation is needed because I am not exactly aware of how different "value_code" maps to different message bytes.

I also tried merging the "value_code" fix from Spanni26:master in my current HEAD (segaura:logrevision). This is for setting values and I also tried to implement the possible corresponding code for reading values. What I experiment is that the value_code dictionaries are needed in the original order in the read operation.

I am talking of rows around line 500 in pyHPSU.py

if not setValue:
    response = n_hpsu.parseCommand(cmd=c, response=rc, verbose=verbose)
    resp = n_hpsu.umConversion(cmd=c, response=response, verbose=verbose)

    if c["value_code"] is None:
        arrResponse.append({"name":c["name"], "resp":resp, "timestamp":response["timestamp"]})
    else:
        arrResponse.append({"name":c["name"], "resp":resp, "timestamp":response["timestamp"],"desc":dict(map(reversed, c["value_code"].items()))[resp]})

this adds an additional desc field to output JSON for the value description, when applicable, like this

$ pyHPSU.py -c anti_leg_day 
[{'name': 'anti_leg_day', 'resp': '0', 'timestamp': 1616795897.625081, 'desc': 'off'}]

and with the inverted value_code dictionary you introduced

c["value_code"]
{'everyday': '2048', 'friday': '1280', 'monday': '256', 'off': '0', 'saturday': '1536', 'sunday': '1792', 'thursday': '1024', 'tuesday': '512', 'wednesday': '768'}

it is still necessary to have it reversed, like this

dict(map(reversed, c["value_code"].items()))
{'0': 'off', '1024': 'thursday', '1280': 'friday', '1536': 'saturday', '1792': 'sunday', '2048': 'everyday', '256': 'monday', '512': 'tuesday', '768': 'wednesday'}

given this, I think we can consider other things (e.g. readability? something else?) to decide which order is better, because this "set and read" use case works with any order and needs both orders ...so far I don't mind, both orders are equally fine for me.

I can add everything to my current pull request #44, but the new "desc" field in the output can be harmful for someone, maybe breaking automations: for this sake I only have included it in JSON output cases (e.g. the CLI output and the MQTT output when timestamp is added) and also wait for some discussion about this.