Supereg / homebridge-http-switch

Powerful http switch for Homebridge: https://github.com/homebridge/homebridge
ISC License
218 stars 36 forks source link

Keep getting `Error 422` upon setting switch state #122

Closed Ostromogilski closed 10 months ago

Ostromogilski commented 10 months ago

Describe the bug For some unknown for me reason, I'm keeping to receive Error 422 sending POST request to SmartThings API to start my vacuum cleaner. I tried the same request in Postman and it works as expected. But once I put the request into plugin config I get Error occurred setting state of switch: HTTP request returned with error code 422 upon triggering the switch

Version (output of npm list -g homebridge homebridge-http-switch)

Configuration

{
    "accessories": [
        {
            "accessory": "HTTP-SWITCH",
            "name": "Samsung Powerbot",
            "switchType": "stateful",
            "onUrl": {
                "url": "https://api.smartthings.com/v1/devices/4076b244-****-****-****-3315d7085113/commands",
                "method": "POST",
                "body": {
                        "commands": [
                            {
                                "component": "main",
                                "capability": "robotCleanerCleaningMode",
                                "command": "setRobotCleanerCleaningMode",
                                "arguments": [
                                    "cleaning"
                                ]
                            }
                        ]
                    },
                "headers": {
                    "Authorization": "Bearer 22afc817-****-****-****-5fda2d54af7c",
                    "Content-Type": "application/json"
                }
            },
            "offUrl": {
                "url": "https://api.smartthings.com/v1/devices/4076b244-****-****-****-3315d7085113/commands",
                "method": "POST",
                "body": {
                        "commands": [
                            {
                                "component": "main",
                                "capability": "robotCleanerCleaningMode",
                                "command": "setRobotCleanerCleaningMode",
                                "arguments": [
                                    "homing"
                                ]
                            }
                        ]
                    },
                "headers": {
                    "Authorization": "Bearer 22afc817-****-****-****-5fda2d54af7cc",
                    "Content-Type": "application/json"
                }
            },
            "pullInterval": 5000,
            "statusUrl": {
                "url": "https://api.smartthings.com/v1/devices/4076b244-****-****-****-3315d7085113/components/main/capabilities/samsungce.robotCleanerOperatingState/status",
                "method": "GET",
                "headers": {
                    "Authorization": "Bearer 22afc817-****-****-****-5fda2d54af7c",
                    "Content-Type": "application/json"
                },
                "statusPattern": "true"
            }
        }
    ],
    "platforms": [
        {
            "platform": "homebridge-http-switch"
        }
    ]
}

Additional context

image

I would greatly appreciate any advise on this matter.

Ostromogilski commented 10 months ago

Postman request with the same body works as expected:

image
Ostromogilski commented 10 months ago

it seems that plugin is adding \ for every line break, that doesn't work with SmartThings API:

{\"commands\":[{\"component\":\"main\",\"capability\":\"robotCleanerCleaningMode\",\"command\":\"setRobotCleanerCleaningMode\",\"arguments\":[\"homing\"]}]}

Is it possible to avoid adding this behavior?

Working format should be like this:

{"commands":[{"component":"main","capability":"robotCleanerMovement","command":"setRobotCleanerMovement","arguments":["homing"]}]}
Ostromogilski commented 10 months ago

My working configuration for Samsung PowerBot :

{
    "accessories": [
        {
            "accessory": "HTTP-SWITCH",
            "name": "Samsung Powerbot",
            "switchType": "stateful",
            "debug": false,
            "onUrl": {
                "url": "https://api.smartthings.com/v1/devices/YOUR_DEVICE_ID/commands",
                "method": "POST",
                "body": {
                    "commands": [
                        {
                            "component": "main",
                            "capability": "robotCleanerMovement",
                            "command": "setRobotCleanerMovement",
                            "arguments": [
                                "cleaning"
                            ]
                        }
                    ]
                },
                "headers": {
                    "Authorization": "Bearer YOUR_TOKEN",
                    "Content-Type": "application/json"
                }
            },
            "offUrl": {
                "url": "https://api.smartthings.com/v1/devices/YOUR_DEVICE_ID/commands",
                "method": "POST",
                "body": {
                    "commands": [
                        {
                            "component": "main",
                            "capability": "robotCleanerMovement",
                            "command": "setRobotCleanerMovement",
                            "arguments": [
                                "homing"
                            ]
                        }
                    ]
                },
                "headers": {
                    "Authorization": "Bearer YOUR_TOKEN",
                    "Content-Type": "application/json"
                }
            },
            "pullInterval": 5000,
            "statusUrl": {
                "url": "https://api.smartthings.com/v1/devices/YOUR_DEVICE_ID/components/main/capabilities/samsungce.robotCleanerOperatingState/status",
                "method": "GET",
                "headers": {
                    "Authorization": "Bearer YOUR_TOKEN",
                    "Content-Type": "application/json"
                }
            },
            "statusPattern": ".*\"value\":\"cleaning\".*"
        }
    ],
    "platforms": [
        {
            "platform": "homebridge-http-switch"
        }
    ]
}