danimtb / dasshio

Hass.io add-on to easily use Amazon Dash Buttons with Home Assistant
Apache License 2.0
192 stars 64 forks source link

HassOS, VMDK - Status Code 401, Bad Request #42

Closed JesseWebDotCom closed 5 years ago

JesseWebDotCom commented 6 years ago

Running HassOS VMDK 1.8, I get a status code of 401 status code after each button press.

{
  "timeout": 10,
  "buttons": [
    {
      "name": "Living Room | Accessory",
      "address": "12:34:56:78:90:ab",
      "domain": "switch",
      "service": "toggle",
      "service_data": "{\"entity_id\": \"switch.living_room_accessory\"}"
    }
  ]
}
2018-07-23 22:44:25,412 | INFO | Living Room | Accessory button pressed!
2018-07-23 22:44:25,414 | INFO | Request: http://hassio/homeassistant/api/services/switch/toggle
2018-07-23 22:44:25,431 | INFO | Status Code: 401
2018-07-23 22:44:25,432 | ERROR | Bad request
2018-07-23 22:44:25,450 | INFO | Packet captured, waiting 10s ...
russjr08 commented 6 years ago

I am seeing the same thing here.

{
  "timeout": 5,
  "buttons": [
    {
      "name": "Bathroom",
      "address": "38:F7:3D:7B:91:99",
      "domain": "script",
      "service": "toggle",
      "service_data": "{\"entity_id\": \"script.toggle_bathroom\"}"
    },
    {
      "name": "KimsToggle",
      "address": "00:FC:8B:7C:A8:24",
      "domain": "light",
      "service": "toggle",
      "service_data": "{\"entity_id\": \"light.kims_light\"}"
    },
    {
      "name": "RJToggle",
      "address": "18:74:2E:89:54:BA",
      "domain": "light",
      "service": "toggle",
      "service_data": "{\"entity_id\": \"group.bedroom\"}"
    }
  ]
}

Log

2018-07-23 21:43:14,608 | INFO | Packet captured, waiting 5s ...
2018-07-23 21:43:19,612 | INFO | Starting sniffing...
2018-07-23 23:14:47,797 | INFO | Bathroom button pressed!
2018-07-23 23:14:47,798 | INFO | Request: http://hassio/homeassistant/api/services/script/toggle
2018-07-23 23:14:47,806 | INFO | Status Code: 401
2018-07-23 23:14:47,806 | ERROR | Bad request
2018-07-23 23:14:47,810 | INFO | Packet captured, waiting 5s ...
2018-07-23 23:14:52,815 | INFO | Starting sniffing...
2018-07-23 23:16:42,428 | INFO | RJToggle button pressed!
2018-07-23 23:16:42,428 | INFO | Request: http://hassio/homeassistant/api/services/light/toggle
2018-07-23 23:16:42,440 | INFO | Status Code: 401
2018-07-23 23:16:42,440 | ERROR | Bad request
2018-07-23 23:16:42,443 | INFO | Packet captured, waiting 5s ...
2018-07-23 23:16:47,448 | INFO | Starting sniffing...

I'm still able to manually make a service call to toggle the groups.

Edit: I saw the discussion going on in #40 and here, I've uninstalled and removed the repo, then rebooted HassIO, installed the repo and addon, saved the config, rebooted HassIO again, same problem unfortunately.

Edit 2: Seems like the internal API is rejecting the request for some reason. I've modified my buttons to make the request manually, by passing in the x-ha-access header, and calling the relevant API manually to temporarily fix this. My config now looks like this:

{
  "timeout": 5,
  "buttons": [
    {
      "name": "Bathroom",
      "address": "38:F7:3D:7B:91:99",
      "url": "http://localhost:8123/api/services/script/toggle",
      "headers": "{\"x-ha-access\": \"YOURPWHERE\"}",
      "body": "{\"entity_id\": \"script.toggle_bathroom\"}"
    },
    {
      "name": "KimsToggle",
      "address": "00:FC:8B:7C:A8:24",
      "url": "http://localhost:8123/api/services/light/toggle",
      "headers": "{\"x-ha-access\": \"YOURPWHERE\"}",
      "body": "{\"entity_id\": \"group.kimsroom\"}"
    },
    {
      "name": "RJToggle",
      "address": "18:74:2E:89:54:BA",
      "url": "http://localhost:8123/api/services/light/toggle",
      "headers": "{\"x-ha-access\": \"YOURPWHERE\"}",
      "body": "{\"entity_id\": \"group.bedroom\"}"
    }
  ]
}
barefootpookie8 commented 6 years ago

I am also getting this same message.

{ "timeout": 20, "buttons": [ { "name": "keurig1", "address": "00:FC:8B:63:AC:9A", "domain": "light", "service": "toggle", "service_data": "{\"entity_id\": \"light.ge_appliances_zll_light_0002e915_1" } ] }

HudsonMC16 commented 6 years ago

This has something to do with the authorization settings in Hass. I don't think add-ons may use hard-coded API key anymore. I'm not at home, so cannot test, but I think you need to either update your config to use legacy auth or the dasshio Dev needs to update the add-on to use the new token/key auth method. https://developers.home-assistant.io/blog/2018/07/02/trying-new-auth.html

danimtb commented 6 years ago

@HudsonMC16 Thanks for the information link. Following the instructions there it should be fine setting in the configuration.yaml:

homeassistant:
  auth_providers:
   - type: legacy_api_password

I can guess this issue comes after updating home assistant to 0.74. I havent tried the solution yet (still in 0.73) and I was not aware this was a breaking change in Hass.io

e2m32 commented 6 years ago

I was in HA v0.73.2 and was having this issue

russjr08 commented 6 years ago

Same. HA 0.73.1 and was working great till yesterday. I don’t recall performing and updates which is why it’s a head scratcher that it just flat out stopped.

e2m32 commented 6 years ago

@danimtb, Just gave this a test. No dice. :-(

homeassistant:
  auth_providers:
   - type: legacy_api_password

It was working fine in 0.73.2 like @russjr08 said, then it just stopped working. I thought maybe I should update to 0.74... obviously that didnt work. :-( Not sure what's going on here with the API.

adonno commented 6 years ago

same issue hassio 0.74.0 on hassos

pyrosmiley commented 6 years ago

Bumping/subscribing for the same problem -- just coming back with "Bad Request." legacy_api_password did nothing, unfortunately.

e2m32 commented 6 years ago

Just attempted rolling back to 0.73.2, 0.73.1, and 0.73.0

All are not working. Same error:

2018-07-24 14:10:21,022 | INFO | Request: http://hassio/homeassistant/api/services/switch/toggle
2018-07-24 14:10:21,066 | INFO | Status Code: 401
2018-07-24 14:10:21,067 | ERROR | Bad request

From the system log: 18-07-24 18:12:29 WARNING (MainThread) [hassio.api.proxy] Unknown HomeAssistant API access!

e2m32 commented 6 years ago

Just looking at this doc: https://developers.home-assistant.io/docs/en/hassio_addon_communication.html

It looks like the api call is missing the HASSIO_TOKEN

" Use this URL: http://hassio/homeassistant/api and internal communication is redirected to the right place. The next stept is to add homeassistant_api: true to config.json and read the environment variable HASSIO_TOKEN and use this as Home-Assistant password."

Could this be related to the SSL issue that they found pre-0.73.2? Maybe they broke this on purpose? Or maybe the HASSIO_TOKEN is there and I just don't know what to look for?

Just tossing out ideas. I'm not sure how HASSIO_TOKEN would be implemented... my guess is that it would have to be passed to the header for the value x-ha-access.

adonno commented 6 years ago

An update for hassio has been released in order to fix the api issue https://twitter.com/pvizeli/status/1021854007546073088?s=21

danimtb commented 6 years ago

Please, update Hass.io supervisor to version 120 to solve this issue. No matter if you are running resinOS or hassOS. Thanks!

JesseWebDotCom commented 6 years ago

After updating hassio my logs are flooded:

18-07-24 22:21:40 WARNING (MainThread) [hassio.api.proxy] Unknown HomeAssistant API access!

JesseWebDotCom commented 6 years ago

But the dash buttons now work!

e2m32 commented 6 years ago

Hrm... Mine's not working. Just updated to Hass.io Supervisor 121 (thats the version that showed up in my UI) and HA 0.74.1. I tried restarting the Dashio Add-on and my rPi. Anyone have any ideas? Roll back to Supervisor 120?

maur54 commented 6 years ago

Also for me the same error

2018-07-25 19:12:29,375 | INFO | Reading config file: /data/options.json 2018-07-25 19:12:29,379 | INFO | Starting sniffing... 2018-07-25 19:12:54,758 | INFO | son_UNO button pressed! 2018-07-25 19:12:54,759 | INFO | Request: http://hassio/homeassistant/api/services/light/turn_on 2018-07-25 19:12:54,818 | INFO | Status Code: 401 2018-07-25 19:12:54,819 | ERROR | Bad request 2018-07-25 19:12:54,857 | INFO | Packet captured, waiting 2s ... 2018-07-25 19:12:56,861 | INFO | Starting sniffing...

Dasshio 0.3.0 Home Assistant 0.74.1 Hass.io supervisor Version 121 System Resin OS 2.3.0+rev1

wwwebrat commented 6 years ago

Same here:

2018-07-25 20:54:41,796 | INFO | Request: http://hassio/homeassistant/api/services/homeassistant/toggle
2018-07-25 20:54:41,855 | INFO | Status Code: 401
2018-07-25 20:54:41,857 | ERROR | Bad request
2018-07-25 20:54:41,892 | INFO | Packet captured, waiting 20s ...
2018-07-25 20:55:01,914 | INFO | Starting sniffing...

Haome Assistant: 0.74.1 (also on the version before) Dasshio: 0.3.0 Supervisor: 121 Host: Resin OS 2.3.0+rev1

balloob commented 6 years ago

Add-ons communicating via http://hassio/homeassistant/ need to send the environment variable HASSIO_TOKEN (docs) as authentication. Not the users password.

jmoery commented 6 years ago

Same issue here. Have everything update to current versions but bad request coming back.

HA 0.74.2 Dasshio: 0.3.0 Supervisor: 121 Host: Resin OS 2.3.0+rev1

JesseWebDotCom commented 6 years ago

broken again for me (HassOS VMDK 1.8, HA 0.74.2, supervisor 121) :(

e2m32 commented 6 years ago

@maur54, @wwwebrat, @jmoery, @JesseWebDotCom here's a work around until the internal api link is fixed.

Thank you @russjr08!

{
  "timeout": 20,
  "buttons": [
    {
      "name": "mybutton1",
      "address": "AB:CD:EF:GH:IJ:00",
      "url": "https://<myhasubdomain>.duckdns.org:8123/api/services/light/toggle",
      "headers": "{\"x-ha-access\": \"my_ha_api_paswd\",\"Content-Type\": \"application/json\"}",
      "body": "{\"entity_id\": \"light.bathroom\"}"
    }
   ]
}

@danimtb set this up so the button can also call external api's, not just the internal one provided for Add-Ins. If you have your HA API exposed, you can utilize that functionality to call it this way. If you're not using a domain name, just use your HA's internal IP address and drop https (assuming your not using SSL). I tried using https://192.168.x.x (internal IP), but I got an error.

Note that if you add users with the new authentication method that is coming soon, this will likely break your buttons. You'd have to update your headers.

balloob commented 6 years ago

For the note about the user system, if you communicate via http://hassio/homeassistant/, you don't have to worry about that, it will be handled for you.

maur54 commented 6 years ago

With @e2m32 suggestion now goes!!! Thanks!!

{ "timeout": 2, "buttons": [ { "name": "son_UNO", "address": "FC:XX:67:XX:32:60", "url": "http://192.168.1.XXX:8123/api/services/light/turn_on", "headers": "{\"x-ha-access\": \"xxxxx my password\",\"Content-Type\": \"application/json\"}", "body": "{\"entity_id\": \"light.son_uno\"}" }, { "name": "son_UNO1", "address": "FC:XX:67:9B:XX:50", "url": "http://192.168.1.XXX:8123/api/services/light/turn_off", "headers": "{\"x-ha-access\": \"xxxxx my password\",\"Content-Type\": \"application/json\"}", "body": "{\"entity_id\": \"light.son_uno\"}" } ] }

forast3r commented 6 years ago

I have upgraded to 0.74.2 I have enabled new auth system on HA configuration.yaml BTW others addons can handle the auth token automatically.

Sample configuration for one of my buttons: { "name": "button2", "address": "xx:xx:xx:xx:xx:xx", "domain": "automation", "service": "trigger", "service_data": "{\"entity_id\": \"automation.button2\"}" }

Result: 2018-07-28 14:42:03,018 | INFO | Reading config file: /data/options.json 2018-07-28 14:42:03,020 | INFO | Starting sniffing... 2018-07-28 14:43:44,199 | INFO | button2 button pressed! 2018-07-28 14:43:44,201 | INFO | Request: http://hassio/homeassistant/api/services/automation/trigger 2018-07-28 14:43:44,228 | INFO | Status Code: 401 2018-07-28 14:43:44,228 | ERROR | Bad request 2018-07-28 14:43:44,249 | INFO | Packet captured, waiting 20s ...

Do we really have to hardcode the url and the password or is there an elegant solution?

e2m32 commented 6 years ago

Ok, I've been playing around with this trying to get it to work. I cloned the code and setup an internal addon for testing. This is what I got so far:

I updated dasshio.py line 56:

           try:
                if "url" in button:
                    request = requests.post(url_request, json=json.loads(
                        button["body"]), headers=json.loads(button["headers"]))
                else:
                    request = requests.post(url_request, json=json.loads(
                        button["service_data"]) , headers={'X-HASSIO-KEY': os.environ.get('HASSIO_TOKEN')})

This was added:", headers={'X-HASSIO-KEY': os.environ.get('HASSIO_TOKEN')}"

I did some logger print statements and it appears to be pulling the HASSIO_TOKEN correctly (as I am getting a 32 character string that looks like a hash), but I am still getting a 401 error.

I tried adding ,"Content-Type": "application/json"} to the header, but got the same result (I expect this isn't needed since we are identifying the body data to be json already json=...). I'm obviously new to using request().

Is there anyone out there smarter than I know why this isn't working?

shoejosh commented 6 years ago

@e2m32 The code is correct, you just used the wrong header key. x-hassio-key is for the Hass.io API. Use x-ha-access for Home Assistant API requests. I just tested and it worked for me.

, headers={'x-ha-access': os.environ.get('HASSIO_TOKEN')}

e2m32 commented 6 years ago

Thanks @shoejosh! All these names, Hassio, Hassos, Home Assistant. Thanks for the explanation.

BertrumUK commented 6 years ago

Let’s hope the dev can update their code soon to save us all having to clone our own. #fingerscrossed

grantalewis commented 6 years ago

Agreed, @BertrumUK. It'd be great if someone dropped in to say that the fix was in process. Or something. It's been more than a week.

russjr08 commented 6 years ago

Shoejosh did open up a PR for this, so looks like it just needs to be merged 👍

e2m32 commented 6 years ago

@grantalewis, I wanted to make sure I could fix it first. Didn't want to make any promises. As you can see, I needed some help.

grantalewis commented 6 years ago

@e2m32 Sure, understandable. To tell you the truth, I wasn't aware that you were taking the lead on this. So thanks!

freekeys commented 6 years ago

Any update on this? Just got home from a 2 week holiday and all my buttons have stopped working! .....made the silly mistake of upgrading HA in the airport :/

russjr08 commented 6 years ago

@freekeys Looks like the issue is just awaiting a PR to be accepted. In the meantime, you can try the workaround I posted earlier!

danimtb commented 5 years ago

Just merged. Sorry guys but I was on holiday. Just released 0.3.1 with the fix. You'll tell how it goes!

BertrumUK commented 5 years ago

Working for me. Thanks Daniel.

russjr08 commented 5 years ago

Confirmed working here as well, thanks a bunch!

wwwebrat commented 5 years ago

Can confirm that it is working again. Thx @shoejosh and @danimtb

freekeys commented 5 years ago

Awesome - how to you upgrade custom addons? I can’t see an update button so just checking what I’m missing...?

grantalewis commented 5 years ago

Up and running once again! Thanks to all --

forast3r commented 5 years ago

Working again!

The 0.3.1 release didn't appear on hassio panel, but a reinstallation did the trick. Hours later the new version showed up.

JesseWebDotCom commented 5 years ago

Sweet Christmas, it works! Thanks