snowdd1 / homebridge-knx

KNX platform shim for homebridge
https://github.com/nfarina/homebridge
GNU General Public License v2.0
97 stars 56 forks source link

Reverse with Switch does not work #108

Closed WindnSalt closed 6 years ago

WindnSalt commented 6 years ago

While trying to integrate a reversed switch I ran into an issue. From the logs it looks like the reverse flag is read during configuration parsing, but while switching the flag is not considered. See config and logs attached.

knx_config.json - Reverse: true

        {
            "DeviceName": "Nachtmodus",
            "Services": [
                {
                    "ServiceType": "Switch",
                    "ServiceName": "Nachtmodus",
                    "Characteristics": [
                        {
                            "Type": "On",
                            "Set": [
                                "0/0/5"
                            ],
                            "Listen": [
                                "0/0/5"
                            ],
                            "Reverse": "true"
                        }
                    ],
                    "subtype": "SUB_e3a27fd2-eb9f-4080-8e50-d492cba9f74f"
                }
            ],
            "UUID": "acac0a61-4a49-44d0-8e44-3648204cbdeb"
        }

homebridge.log: startup - Reverse: true

[2017-11-5 13:34:09] [homebridge-knx.KNX] Matched an accessory: Nachtmodus === Nachtmodus
[2017-11-5 13:34:09] [homebridge-knx.KNX] Reused platformAccessory instance: Nachtmodus
[2017-11-5 13:34:09] [homebridge-knx.KNX] Preparing Services: 1
[2017-11-5 13:34:09] [homebridge-knx.KNX] Preparing Service: #0 with name [Nachtmodus] of ServiceType [Switch]
[2017-11-5 13:34:09] [homebridge-knx.KNX] Service constructor called
[2017-11-5 13:34:09] [homebridge-knx.KNX] ServiceType defined
[2017-11-5 13:34:09] [homebridge-knx.KNX] ServiceType known as function
[2017-11-5 13:34:09] [homebridge-knx.KNX] Found restored service: Nachtmodus
[2017-11-5 13:34:09] [homebridge-knx.KNX] Preparing Characteristics: 1
[2017-11-5 13:34:09] [homebridge-knx.KNX] On
[2017-11-5 13:34:09] [homebridge-knx.KNX] Adding characteristic...
[2017-11-5 13:34:09] [homebridge-knx.KNX] CharacteristicKNX.Constructor
---iterating--------------------
Type: On
Set.0: 0/0/5
Listen.0: 0/0/5
Reverse: true
================================
INFO registerSingleGA 0/0/5
[2017-11-5 13:34:09] [homebridge-knx.KNX] KNX Service created
[2017-11-5 13:34:09] [homebridge-knx.KNX] Done with [Nachtmodus] accessory

homebridge.log: Device off / on - Reverse: true

[2017-11-5 13:28:00] [homebridge-knx.KNX]  (reverse)
[2017-11-5 13:28:00] [homebridge-knx.KNX] DEBUG knxwrite Address conversion, converted 0/0/5 to 5
[2017-11-5 13:28:00] [homebridge-knx.KNX] knxAccess.knxwrite: knx data sent: Value 0 for GA 0/0/5
[2017-11-5 13:28:00] [homebridge-knx.KNX] knxAccess.writeValueHK(0,On,DPT1,false)
[2017-11-5 13:28:00] [homebridge-knx.KNX] knxAccess.writeValueHK: Format bool
[2017-11-5 13:28:00] [homebridge-knx.KNX] BOOL:[On]: Received value from KNX handler:0 of type DPT1
[2017-11-5 13:28:00] [homebridge-knx.KNX] Value changed, updating homebridge
[2017-11-5 13:28:00] [homebridge-knx.KNX] exiting writeValueHK()
[2017-11-5 13:28:01] [homebridge-knx.KNX]  (reverse)
[2017-11-5 13:28:01] [homebridge-knx.KNX] DEBUG knxwrite Address conversion, converted 0/0/5 to 5
[2017-11-5 13:28:01] [homebridge-knx.KNX] knxAccess.knxwrite: knx data sent: Value 1 for GA 0/0/5
[2017-11-5 13:28:01] [homebridge-knx.KNX] knxAccess.writeValueHK(1,On,DPT1,false)
[2017-11-5 13:28:01] [homebridge-knx.KNX] knxAccess.writeValueHK: Format bool
[2017-11-5 13:28:01] [homebridge-knx.KNX] BOOL:[On]: Received value from KNX handler:1 of type DPT1
[2017-11-5 13:28:01] [homebridge-knx.KNX] Value changed, updating homebridge
[2017-11-5 13:28:01] [homebridge-knx.KNX] exiting writeValueHK()

knx_config.json - Reverse: false

        {
            "DeviceName": "Nachtmodus",
            "Services": [
                {
                    "ServiceType": "Switch",
                    "ServiceName": "Nachtmodus",
                    "Characteristics": [
                        {
                            "Type": "On",
                            "Set": [
                                "0/0/5"
                            ],
                            "Listen": [
                                "0/0/5"
                            ]
                        }
                    ],
                    "subtype": "SUB_e3a27fd2-eb9f-4080-8e50-d492cba9f74f"
                }
            ],
            "UUID": "acac0a61-4a49-44d0-8e44-3648204cbdeb"
        }

homebridge.log: startup - Reverse: false

[2017-11-5 13:31:25] [homebridge-knx.KNX] Matched an accessory: Nachtmodus === Nachtmodus
[2017-11-5 13:31:25] [homebridge-knx.KNX] Reused platformAccessory instance: Nachtmodus
[2017-11-5 13:31:25] [homebridge-knx.KNX] Preparing Services: 1
[2017-11-5 13:31:25] [homebridge-knx.KNX] Preparing Service: #0 with name [Nachtmodus] of ServiceType [Switch]
[2017-11-5 13:31:25] [homebridge-knx.KNX] Service constructor called
[2017-11-5 13:31:25] [homebridge-knx.KNX] ServiceType defined
[2017-11-5 13:31:25] [homebridge-knx.KNX] ServiceType known as function
[2017-11-5 13:31:25] [homebridge-knx.KNX] Found restored service: Nachtmodus
[2017-11-5 13:31:25] [homebridge-knx.KNX] Preparing Characteristics: 1
[2017-11-5 13:31:25] [homebridge-knx.KNX] On
[2017-11-5 13:31:25] [homebridge-knx.KNX] Adding characteristic...
[2017-11-5 13:31:25] [homebridge-knx.KNX] CharacteristicKNX.Constructor
---iterating--------------------
Type: On
Set.0: 0/0/5
Listen.0: 0/0/5
================================
INFO registerSingleGA 0/0/5
[2017-11-5 13:31:25] [homebridge-knx.KNX] KNX Service created
[2017-11-5 13:31:25] [homebridge-knx.KNX] Done with [Nachtmodus] accessory

homebridge.log: Device off / on - Reverse: false


[2017-11-5 13:30:05] [homebridge-knx.KNX]  (reverse)
[2017-11-5 13:30:05] [homebridge-knx.KNX] DEBUG knxwrite Address conversion, converted 0/0/5 to 5
[2017-11-5 13:30:05] [homebridge-knx.KNX] knxAccess.knxwrite: knx data sent: Value 0 for GA 0/0/5
[2017-11-5 13:30:05] [homebridge-knx.KNX] knxAccess.writeValueHK(0,On,DPT1,false)
[2017-11-5 13:30:05] [homebridge-knx.KNX] knxAccess.writeValueHK: Format bool
[2017-11-5 13:30:05] [homebridge-knx.KNX] BOOL:[On]: Received value from KNX handler:0 of type DPT1
[2017-11-5 13:30:05] [homebridge-knx.KNX] Value changed, updating homebridge
[2017-11-5 13:30:05] [homebridge-knx.KNX] exiting writeValueHK()
[2017-11-5 13:30:06] [homebridge-knx.KNX]  (reverse)
[2017-11-5 13:30:06] [homebridge-knx.KNX] DEBUG knxwrite Address conversion, converted 0/0/5 to 5
[2017-11-5 13:30:06] [homebridge-knx.KNX] knxAccess.knxwrite: knx data sent: Value 1 for GA 0/0/5
[2017-11-5 13:30:06] [homebridge-knx.KNX] knxAccess.writeValueHK(1,On,DPT1,false)
[2017-11-5 13:30:06] [homebridge-knx.KNX] knxAccess.writeValueHK: Format bool
[2017-11-5 13:30:06] [homebridge-knx.KNX] BOOL:[On]: Received value from KNX handler:1 of type DPT1
[2017-11-5 13:30:06] [homebridge-knx.KNX] Value changed, updating homebridge
[2017-11-5 13:30:06] [homebridge-knx.KNX] exiting writeValueHK()
snowdd1 commented 6 years ago

@CptMW which version do you use? Could you, for debugging, uncomment https://github.com/snowdd1/homebridge-knx/blob/6e17c099857a0efc2ee191ca6a90df8608ad90f7/lib/knxaccess.js#L211-L215 to produce debug output? I think there might be something in the parser. Reverse is not so commonly used, it's tending to break quite unnoticed.

WindnSalt commented 6 years ago

Turned on the debugging as you suggested and couldn't resist to dig deeper. The debugging output was in sync with the observed behavior. So I looked into how the characteristics object is created and finally realized I did not pay attention to how the JSON parser interprets types. In my knx_config.json I had:

"Reverse": "true"

With

"Reverse": true

everything worked just fine.

In order to make this a bit more robust, I tried converting the Reverse member of the config object into a Boolean in the constructor of the CharacteristicKNX object. I changed lines 116 and 160 in characteristic-knx.js into

reversed: (Boolean(this.config.Reverse)===true),

In case it is noted as a Boolean in the knx_config.json that does not do much. In case it is noted as a string it would convert Reverse into a Boolean and therefore Reverse would be interpreted as intended. If you want I can prepare a pull request.

snowdd1 commented 6 years ago

Problem with (Boolean(this.config.Reverse)===true) is that it would make the string "false" become true also, because any non-empty string is true-ish AFAIK. I'd better put an extra hint into the READMEs.