Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTful switch failing to initialize (GET request validation failing on a POST resource) #7431

Closed
jdireen opened this issue May 4, 2017 · 23 comments

Comments

@jdireen
Copy link

jdireen commented May 4, 2017

Home Assistant release (hass --version):
Currently running 0.43.2 -- looks like issue came with 0.42 (commit aiohttp 2 #6835)

Python release (python3 --version):
python3.4

Component/platform:
switch/rest

Description of problem:
I have a few rest switchs configured where the resource is a POST to an lirc_web server. After updating to 0.43.2 those switches no longer get initialized after a reboot.

Seeing that there is a new request validation at https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/switch/rest.py#L64
I went into my local installation and commented out the return False to keep it from failing the initialization.

Expected:
Need more specific validation to allow a POST resource, or perhaps a way to specify it in the configuration.

Problem-relevant configuration.yaml entries and steps to reproduce:

switch:
   platform: rest
  resource: http://192.168.1.150:3000/remotes/samsung/KEY_POWER
  name: "TV Power"

Traceback (if applicable):

ERROR (MainThread) [homeassistant.components.switch.rest] Got non-ok response from resource: 404

Additional info:
I hope I explained everything correctly. I'd attempt to fix it myself, but am unsure of the correct way to handle it. Thanks!

@JshWright
Copy link
Contributor

Your resource is returning a 404 (Not Found) error. It seems to be that Home Assistant is doing the right thing here...?

@jdireen
Copy link
Author

jdireen commented May 6, 2017

Yeah, not sure, I'm somewhat out of my depth here.
It may not be common, but lirc_web is splitting its GET vs POST requests to separate resource paths (link to their RESTful API: https://github.com/alexbain/lirc_web#using-the-json-api)

When a GET request is done on the resource path it returns a 404, send it a POST and it will return 200 OK.

So it's not to say this Home Assistant component isn't doing things correct, but I'm hoping for a way that I as the user can configure it to pass on the validation in setup; perhaps specify a second "status" URL for it to do its GET request on...?

@cooljimy84
Copy link

cooljimy84 commented May 9, 2017

So i have this issue since the update as well.
Used the following guide to setup LIRC with a website on my PI.
http://alexba.in/blog/2013/01/06/setting-up-lirc-on-the-raspberrypi/
The website works fine on the PI and I have not upgrade/updated the PI, just Home Assistant.

I take it the Home Assistant is now checking this platform/feature/api rather than before it was just accepting it and not testing for a response ?
Are you able to add an ignore/fallback switch/option i the YAML config for this ?

@cooljimy84
Copy link

cooljimy84 commented May 9, 2017

As a note i have link here from the other guide i followed on how to set this up HASS side.
https://community.home-assistant.io/t/automating-unsmart-ir-devices/1273/51

@JshWright
Copy link
Contributor

How does HASS get the state of the switch if LIRC doesn't respond to GET requests?

@JshWright
Copy link
Contributor

Well... I guess in this case, it just doesn't, eh? It's firing an IR command and assuming the state updates. There is no state to retrieve.

This validation obviously won't work for these sorts of cases.

@JshWright
Copy link
Contributor

@balloob

The issue here is that not all REST switches support GETs for the state. Some of them (like LIRC) operate in de facto 'optimistic' mode. It's not a safe assumption that an otherwise functional switch will support GETs.

I see several paths forwards here:

  • Downgrade this failing check to a warning and let the component setup continue
  • Remove the check entirely
  • Add an 'optimistic' mode to REST switches which skips this check

Thoughts?

@cooljimy84
Copy link

My vote is for optimistic mode,
the other two kinda defeat the work that has been put into making it check.

@cooljimy84
Copy link

as a side note i can snapshot my Home Assistant (it's running under a hyper-v virtual) and test a fix if you require.

@jdireen
Copy link
Author

jdireen commented May 9, 2017

An optimistic option would work well for me.

huh, that or we are shoehorning this lirc setup into the wrong type of thing? (I'm fairly new to Home Assistant) is there a better component type to make this work? A single fire, button type that would show up in the front end?

@balloob
Copy link
Member

balloob commented May 10, 2017

Potential solution: #4937

Optimistic mode is a good alternative that is obviously less work.

@Odianosen25
Copy link

Please I am new to home assistant and having a problem I have been unable to fix for days as regards the restful switch.

I have added the switch to my configuration file, but it doesn't show up on the UI and this shows on the status "ERROR (MainThread) [homeassistant.components.switch.rest] Got non-ok response from resource: 404".

Please anyway to solve this?

Regards

@cooljimy84
Copy link

I can't find where i found the fix, must have been on anther post. I have back tracked my fix i found from someone on here.

If you want to ignore the error and setup the item any way on the terminal edit this file.
/usr/local/lib/python3.4/dist-packages/homeassistant/components/switch/rest.py
scroll down to line 66 in the config. It should read
return False
add a # at the start of that line.
Save and exit
restart home assistant.

I used VIM as it has a handy line counter at the bottom right, tho it's not as simple to use as nano.
sudo vim /usr/local/lib/python3.4/dist-packages/homeassistant/components/switch/rest.py
once on the line, press I to goto insert mode.
press # to type a #
press escape or ESC on your keyboard.
then type :x! to exit saving the changes (you should notice it typeing that at the very bottom of the screen)

@cooljimy84
Copy link

        if req.status >= 400:
            _LOGGER.error("Got non-ok response from resource: %s", req.status)
            return False

This get changed to

        if req.status >= 400:
            _LOGGER.error("Got non-ok response from resource: %s", req.status)
#            return False

note that after an upgrade this get over written so you will need to redo it, if required.

@Odianosen25
Copy link

Hello,

Many thanks for the help, and I really appreciate it.

It worked and my problem of several weeks have been solved 👯‍♂️

Kind regards

@balloobbot
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@cooljimy84
Copy link

I'm unable to test as i no longer have my PI running this.

@malcolmrigg
Copy link

malcolmrigg commented Sep 14, 2017

Just thought I'd share a workaround/different way of doing this - use the RESTful Command instead of a switch:
https://home-assistant.io/components/rest_command/

This lets you create a service that can POST to a URL without having to be able to GET from it. I think doing it this way also makes a bit more sense in this situation, and saves you from having to customise the HA code...

@mildredmonday
Copy link

Just updated from 48.1 to 54.0 and have the same issue. rest.py has been changed a bit so I'm unable to try the workaround by commenting out the false return.

@mildredmonday
Copy link

Used the rest.py from commit #8004 before HTTP auth was added and was able to find and comment out the line which is now on line 73 for anyone else running into this.

@AriZuu
Copy link

AriZuu commented Dec 5, 2017

Transient network outage can also cause GET request during init to fail. This leads into situation where restful switch is not initialized at all and only way to recover is to restart hass. It might be better to consider the situation as warning, not error.

@balloobbot
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@balloobbot
Copy link

This issue will be auto-closed because there hasn't been any activity for a few months. Feel free to open a new one if you still experience this problem 👍

@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants