parisbutterfield / restswitchmate

Control Switchmate devices via REST
MIT License
10 stars 2 forks source link

Switchmate Bright Firmware Requirement #3

Closed kianrafiee closed 6 years ago

kianrafiee commented 6 years ago

The only firmware I see listed in the app for the toggle switches I have (Switchmate Bright) are 2.9.15. Is this the one your referring to that needs to be below 2.99.9?

Also, is your code only compatible with certain models of the Switchmate switches? The Bright is the newest slim version.

kianrafiee commented 6 years ago

Also, https://github.com/brianpeiris/switchmate/issues/6#issuecomment-370205123 seems to indicate that the original code you base this on now works with the newest switchmate firmware(which includes the one I have). Could you possibly update your codebase to these changes? I would be happy to test them out. Just waiting on your response to my other issue regarding hardware requirements to start.

parisbutterfield commented 6 years ago

@kianrafiee

Yes, I should be able to test this out within the next few days. I have a Switchmate Bright in my laundry room. It just prompted me to update to 2.9.15.

I looked at his new code and it no longer requires auth tokens. I'm probably going to add a url parameter to the request that distinguishes between the old and new firmware.

It might look something like:

/device/<macaddress>?legacyFirmware=false

Also you won't need to run the AuthServer. So the number of required bluetooth dongles can go down to two. :)

kianrafiee commented 6 years ago

Awesome. I ordered the IOGear bluetooth adapter you recommend. The Pi3 comes with a bluetooth radio built in. So I should be set once I get that one. The Pi3 also comes with 4 usb ports so I dont even need a usb hub! :) Thanks so much for the update, can't wait to try it out!

parisbutterfield commented 6 years ago

Hey @kianrafiee

Bright support is checked int. My bright has been working reliably with the new code for the past few hours. I updated the documentation. So please make API calls to the bright, with the URL parameter. Keep me posted :)

kianrafiee commented 6 years ago

I'm guessing it still needs 3 bluetooth radios? I can't test then because I only have two at the moment. One on the pi and one dongle.

kianrafiee commented 6 years ago

Also thanks so much for getting it supported!

parisbutterfield commented 6 years ago

Nope. Two are fine. You can comment out the authserver in the docker-compose.yml. Or it can start and it won't be used. As long as you can run hcitool dev on the command line. And see hci0 and hci1 it'll work.

kianrafiee commented 6 years ago

Sweet! Will test as soon as I can. Hopefully later today! Thank you.

kianrafiee commented 6 years ago

Hmm, not working so far. Here is the problem in terminal:

pi@raspberrypi:~/restswitchmate $ sudo docker-compose build
Building switchserver
Step 1/7 : FROM parisbutterfield/restswitch:switchserver
switchserver: Pulling from parisbutterfield/restswitch
0864efeeb5cb: Pull complete
3cda69762aee: Pull complete
08013f67c1d1: Pull complete
0936fb4cf08d: Pull complete
3e09e2d1b7e6: Pull complete
41779a7f2909: Pull complete
Digest: sha256:dab5fdd5c66d71d07f06ba6bfbd1dd7e9ecc0bdc4315a9aa78ae0ebe01d23c63
Status: Downloaded newer image for parisbutterfield/restswitch:switchserver
 ---> eaee7661fad2
Step 2/7 : RUN mkdir /db
 ---> Running in 198b08bf69a7
Removing intermediate container 198b08bf69a7
 ---> 219c5456d87b
Step 3/7 : ADD . /code
 ---> 79790b14a29e
Step 4/7 : WORKDIR /code
Removing intermediate container e86288a5e933
 ---> 0f84077f9ad7
Step 5/7 : RUN pip install -r requirements.txt
 ---> Running in 03e46f968acd
Collecting requests==2.18.4 (from -r requirements.txt (line 1))
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
Collecting urllib3<1.23,>=1.21.1 (from requests==2.18.4->-r requirements.txt (line 1))
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
Collecting idna<2.7,>=2.5 (from requests==2.18.4->-r requirements.txt (line 1))
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
Collecting chardet<3.1.0,>=3.0.2 (from requests==2.18.4->-r requirements.txt (line 1))
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting certifi>=2017.4.17 (from requests==2.18.4->-r requirements.txt (line 1))
  Downloading certifi-2018.1.18-py2.py3-none-any.whl (151kB)
Installing collected packages: urllib3, idna, chardet, certifi, requests
Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
Removing intermediate container 03e46f968acd
 ---> 373b2481d732
Step 6/7 : RUN chmod +x run.sh
 ---> Running in a6f125c67966
Removing intermediate container a6f125c67966
 ---> 3659e39f035b
Step 7/7 : CMD ["/code/run.sh"]
 ---> Running in 1651403b5ac1
Removing intermediate container 1651403b5ac1
 ---> 5478f7a62cea
Successfully built 5478f7a62cea
Successfully tagged restswitchmate_switchserver:latest
Building statusserver
Step 1/6 : FROM parisbutterfield/restswitch:statusserver
statusserver: Pulling from parisbutterfield/restswitch
0864efeeb5cb: Already exists
3cda69762aee: Already exists
c846cffbb1a1: Pull complete
48b867ed0669: Pull complete
28d8de2eab68: Pull complete
91114c7a3486: Pull complete
Digest: sha256:39408c669f6faedb797949dda7f66f3751623c0c4dc2693ef440abe1698a0011
Status: Downloaded newer image for parisbutterfield/restswitch:statusserver
 ---> 999c33b965f5
Step 2/6 : RUN mkdir /db
 ---> Running in bda49205770a
Removing intermediate container bda49205770a
 ---> be304087925e
Step 3/6 : ADD . /code
 ---> 564416dc9ed8
Step 4/6 : WORKDIR /code
Removing intermediate container 916653c6f215
 ---> 3432baf3e45a
Step 5/6 : RUN chmod +x run.sh
 ---> Running in 77004f43fe02
Removing intermediate container 77004f43fe02
 ---> 585f61ef16e7
Step 6/6 : CMD ["/code/run.sh"]
 ---> Running in c0bcfab752a5
Removing intermediate container c0bcfab752a5
 ---> 918127b3eaf0
Successfully built 918127b3eaf0
Successfully tagged restswitchmate_statusserver:latest
Building authserver
Step 1/6 : FROM parisbutterfield/restswitch:authserver
authserver: Pulling from parisbutterfield/restswitch
0864efeeb5cb: Already exists
3cda69762aee: Already exists
2ce94cad3bdc: Pull complete
5a9f4cbc4236: Pull complete
cb3315e0ae26: Pull complete
8939774ef936: Pull complete
Digest: sha256:861159f7598f1ad08b8a88a5122dda6ee2f07f88c404f945f14b94147a4f0509
Status: Downloaded newer image for parisbutterfield/restswitch:authserver
 ---> ebc6bef3a136
Step 2/6 : RUN mkdir /db
 ---> Running in a0ca2a02b1e8
Removing intermediate container a0ca2a02b1e8
 ---> 5aafcef60be3
Step 3/6 : ADD . /code
 ---> 41fdebf8e950
Step 4/6 : WORKDIR /code
Removing intermediate container 7665d2066671
 ---> f70643ec08e6
Step 5/6 : RUN chmod +x run.sh
 ---> Running in fb614432822a
Removing intermediate container fb614432822a
 ---> 71dce744cccf
Step 6/6 : CMD ["/code/run.sh"]
 ---> Running in 861f5ad68424
Removing intermediate container 861f5ad68424
 ---> 6ab1667607c0
Successfully built 6ab1667607c0
Successfully tagged restswitchmate_authserver:latest
pi@raspberrypi:~/restswitchmate $ sudo docker-compose start
Starting switchserver ... done
Starting statusserver ... done
Starting authserver   ... done
ERROR: No containers to start
kianrafiee commented 6 years ago

So I cannot access web interface on port 5000.

parisbutterfield commented 6 years ago

Hmm. This seems similar to https://github.com/docker/compose/issues/5469

Try running docker-compose up

kianrafiee commented 6 years ago

Hmm well I restarted from scratch and installed hass.io image on sd card. But after enabling ssh I cannot install docker(the first step in your readme) as its unsupported. I guess this is because hassio is seen as a different platform.

core-ssh:~# curl -sSL https://get.docker.com | sh

# Executing docker install script, commit: 02d7c3c

Either your platform is not easily detectable or is not supported by this
installer script.
Please visit the following URL for more detailed installation instructions:

https://docs.docker.com/engine/installation/

The reason I installed hass.io is because it allows add-ons like emulated_hue. And since your readme states it supports emulated_hue, I assumed you need hass.io to use emulated_hue add-on. Is that not the case?

parisbutterfield commented 6 years ago

That's the case. For emulated hue, I have a Pi 3 running Hass.IO. The restswichmate runs on a Pi Zero W. Not sure if you can run both on one Pi. HassIO runs in a docker container itself. So what's why you couldn't install docker. When you enable ssh, you're connecting to the docker container.

Sent with GitHawk

kianrafiee commented 6 years ago

How do you get restswitchmate to interact with hassio on your pi3? I'm going to install hassio on my Synology nas using docker and then use the pi3 for restswitchmate and pi-hole

kianrafiee commented 6 years ago

Oh duh, by IP address. :P Will let you know how it goes

kianrafiee commented 6 years ago

Er dang. The docker image of hass also doesnt have addons. Not sure I can get this running now. Argh.

parisbutterfield commented 6 years ago

Which add-ons would you need? I have the ssh and SMB add-on for HassIO. Those aren't needed for it to run.

Sent with GitHawk

kianrafiee commented 6 years ago

Well originally I just needed emulated_hue. I just want to have the switchmates work with things like ifttt that hass allows.

kianrafiee commented 6 years ago

Well I have restswitchmate working. And I'm still not sure if I can run emulated_hue without hassio image(docker based) so I edited my configuartion.yaml file to this:

# Amazon Echo example configuration.yaml entry
emulated_hue:

switch:
  - platform: rest
     resource: http://192.168.1.67:5000/device/F1ABE82EEDDF
     name: 'Kitchen Light'
     method: 'put'
     body_on: '{"on": true}'
     body_off: '{"on": false}'
     is_on_template: '{{value_json.status}}'

switch:
  - platform: rest
     resource: http://192.168.1.67:5000/device/D01CCB9A9E82
     name: 'Door Light'
     method: 'put'
     body_on: '{"on": true}'
     body_off: '{"on": false}'
     is_on_template: '{{value_json.status}}

And it seems to give an error in hass. It says:

ERROR:homeassistant.util.yaml:mapping values are not allowed here in "/home/homeassistant/.homeassistant/configuration.yaml", line 79, column 14

So I went to http://www.yamllint.com/ and input that and it says that this is the line thats giving problems:

resource: http://192.168.1.67:5000/device/F1ABE82EEDDF

It seems it doesn't like the characters in that line? But thats strange since Im assuming it works for you.

kianrafiee commented 6 years ago

Hmm ok it was a formatting issue. But now it says:

Testing configuration at /home/homeassistant/.homeassistant
Failed config
  General Errors: 
    - Component not found: doorlight
    - Component not found: kitchenlight

This is my yaml file:

# Amazon Echo example configuration.yaml entry
emulated_hue:

kitchenlight:
  - platform: rest
    resource: http://192.168.1.67:5000/device/F1ABE82EEDDF
    name: 'Kitchen Light'
    method: 'put'
    body_on: '{"on": true}'
    body_off: '{"on": false}'
    is_on_template: '{{value_json.status}}'

doorlight:
  - platform: rest
    resource: http://192.168.1.67:5000/device/D01CCB9A9E82
    name: 'Door Light'
    method: 'put'
    body_on: '{"on": true}'
    body_off: '{"on": false}'
    is_on_template: '{{value_json.status}}'
kianrafiee commented 6 years ago

Heh. I think I'm getting closer? :P

I removed one of the switches, and changed the name back to switch and added type: alexa:

# Amazon Echo example configuration.yaml entry
emulated_hue:
  type: alexa

switch:
  - platform: rest
    resource: http://192.168.1.67:5000/device/F1ABE82EEDDF
    method: 'put'
    body_on: '{"on": true}'
    body_off: '{"on": false}'
    is_on_template: '{{value_json.status}}'

Now I can access the emulated_hue web interface! So I guess its working?

BUT when I toggle the switch off from hass web interface, it doesn't do anything and just toggles back to on.

parisbutterfield commented 6 years ago

Ok, cool. The switch in HASS looks good. Can you try changing the url to

http://192.168.1.67:5000/device/F1ABE82EEDDF?newFirmware=true

kianrafiee commented 6 years ago

More progress. Now when I physically toggle the switch, hass sees the correct toggle state. But when I toggle through hass, the toggle indicator reverts back to off it was off and on if was on and nothing happens with the switchmate.

parisbutterfield commented 6 years ago

Are the Switchmates near the Pi?

try running,

cd restswitchmate
sudo docker-compose logs --tail="100"  switchserver

And paste the output here.

kianrafiee commented 6 years ago

They were within 10 feet and behind a bedroom wall. But I moved the brights within 3 feet in the same room and the problem still occurs. Here is the log:

switchserver_1  | [2018-03-11 06:53:51,715] ERROR in app: Exception on /device/F1ABE82EEDDF [PUT]
switchserver_1  | Traceback (most recent call last):
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
switchserver_1  |     response = self.full_dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
switchserver_1  |     rv = self.handle_user_exception(e)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
switchserver_1  |     return cors_after_request(app.make_response(f(*args, **kwargs)))
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
switchserver_1  |     reraise(exc_type, exc_value, tb)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
switchserver_1  |     rv = self.dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
switchserver_1  |     return self.view_functions[rule.endpoint](**req.view_args)
switchserver_1  |   File "/code/app.py", line 61, in device
switchserver_1  |     results.update({'newFirmware': False})
switchserver_1  | AttributeError: 'NoneType' object has no attribute 'update'
switchserver_1  | 127.0.0.1 - - [11/Mar/2018 06:53:51] "PUT /device/F1ABE82EEDDF HTTP/1.1" 500 -
switchserver_1  | [2018-03-11 06:56:14,066] ERROR in app: Exception on /device/F1ABE82EEDDF [PUT]
switchserver_1  | Traceback (most recent call last):
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
switchserver_1  |     response = self.full_dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
switchserver_1  |     rv = self.handle_user_exception(e)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
switchserver_1  |     return cors_after_request(app.make_response(f(*args, **kwargs)))
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
switchserver_1  |     reraise(exc_type, exc_value, tb)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
switchserver_1  |     rv = self.dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
switchserver_1  |     return self.view_functions[rule.endpoint](**req.view_args)
switchserver_1  |   File "/code/app.py", line 61, in device
switchserver_1  |     results.update({'newFirmware': False})
switchserver_1  | AttributeError: 'NoneType' object has no attribute 'update'
switchserver_1  | 127.0.0.1 - - [11/Mar/2018 06:56:14] "PUT /device/F1ABE82EEDDF HTTP/1.1" 500 -
parisbutterfield commented 6 years ago

Ok, make sure the URL parameter is correct. Restart HassIO or reload the config. The parameter is missing so the request is failing.

http://192.168.1.67:5000/device/F1ABE82EEDDF?newFirmware=true

kianrafiee commented 6 years ago

I rebooted the pi and realized you have to restart reswitchmate. So I tried to start it again and got this:

pi@raspberrypi:~/restswitchmate $ docker-compose start
Starting switchserver ... done
Starting statusserver ... error
Starting authserver   ... error

ERROR: for authserver  Cannot start service authserver: container "9d62fbf3b7e9c53f0a287665ec8056631a1ef8bc952f37d70115243fc53444f8": already exists

ERROR: for statusserver  Cannot start service statusserver: container "11e81280976bd02c3e5d98157046701e5f38a9d1dd38d26083ed1ddc22790da5": already exists
pi@raspberrypi:~/restswitchmate $ docker-compose up
restswitchmate_switchserver_1 is up-to-date
Starting restswitchmate_authserver_1 ... 
Starting restswitchmate_authserver_1 ... error

ERROR: for restswitchmate_authserver_1  Cannot start service authserver: container "9d62fbf3b7e9c53f0a287665ec8056631a1ef8bc952f37d70115243fc53444f8": already eStarting restswitchmate_statusserver_1 ... error

ERROR: for restswitchmate_statusserver_1  Cannot start service statusserver: container "11e81280976bd02c3e5d98157046701e5f38a9d1dd38d26083ed1ddc22790da5": already exists

ERROR: for authserver  Cannot start service authserver: container "9d62fbf3b7e9c53f0a287665ec8056631a1ef8bc952f37d70115243fc53444f8": already exists

ERROR: for statusserver  Cannot start service statusserver: container "11e81280976bd02c3e5d98157046701e5f38a9d1dd38d26083ed1ddc22790da5": already exists
ERROR: Encountered errors while bringing up the project.
pi@raspberrypi:~/restswitchmate $ sudo docker-compose start
Starting switchserver ... done
Starting statusserver ... error
Starting authserver   ... error

ERROR: for authserver  Cannot start service authserver: container "9d62fbf3b7e9c53f0a287665ec8056631a1ef8bc952f37d70115243fc53444f8": already exists

ERROR: for statusserver  Cannot start service statusserver: container "11e81280976bd02c3e5d98157046701e5f38a9d1dd38d26083ed1ddc22790da5": already exists
pi@raspberrypi:~/restswitchmate $ sudo docker-compose up
Starting restswitchmate_statusserver_1 ... 
Starting restswitchmate_authserver_1 ... 
Starting restswitchmate_statusserver_1 ... error

ERROR: for restswitchmate_statusserver_1  Cannot start service statusserver: container "11e81280976bd02c3e5d98157046701e5f38a9d1dd38d26083ed1ddc22790da5": alreaStarting restswitchmate_authserver_1 ... error

ERROR: for restswitchmate_authserver_1  Cannot start service authserver: container "9d62fbf3b7e9c53f0a287665ec8056631a1ef8bc952f37d70115243fc53444f8": already exists

ERROR: for authserver  Cannot start service authserver: container "9d62fbf3b7e9c53f0a287665ec8056631a1ef8bc952f37d70115243fc53444f8": already exists

ERROR: for statusserver  Cannot start service statusserver: container "11e81280976bd02c3e5d98157046701e5f38a9d1dd38d26083ed1ddc22790da5": already exists
ERROR: Encountered errors while bringing up the project.
pi@raspberrypi:~/restswitchmate $ 

I couldnt access the status server web interface before or after trying to start it.

kianrafiee commented 6 years ago

If this is of any help:

pi@raspberrypi:~/restswitchmate $ docker ps
CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS              PORTS               NAMES
c0862637a7ec        restswitchmate_switchserver   "/code/run.sh"      4 hours ago         Up 12 minutes                           restswitchmate_switchserver_1
pi@raspberrypi:~/restswitchmate $ docker ps -a
CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS                        PORTS               NAMES
c0862637a7ec        restswitchmate_switchserver   "/code/run.sh"      4 hours ago         Up 13 minutes                                     restswitchmate_switchserver_1
11e81280976b        restswitchmate_statusserver   "/code/run.sh"      4 hours ago         Exited (255) 13 minutes ago                       restswitchmate_statusserver_1
9d62fbf3b7e9        restswitchmate_authserver     "/code/run.sh"      4 hours ago         Exited (255) 13 minutes ago                       restswitchmate_authserver_1
parisbutterfield commented 6 years ago

I starting to think Docker wasn't a good idea.

https://github.com/moby/moby/issues/36145

I had this issue the other day. Please run

docker-containerd-ctr --namespace moby --address /run/docker/containerd/docker-containerd.sock c rm 9d62fbf3b7e9c53f0a287665ec8056631a1ef8bc952f37d70115243fc53444f8 

docker-containerd-ctr --namespace moby --address /run/docker/containerd/docker-containerd.sock c rm 11e81280976bd02c3e5d98157046701e5f38a9d1dd38d26083ed1ddc22790da5 

And then docker-compose up

kianrafiee commented 6 years ago

Well I know for sure the legacy part of the url is in there because before i restarted the servers by removing old containers, I looked at hass logs and it said something along the lines of not being able to connect to the url and it gave the switch url of http://192.168.1.67:5000/device/F1ABE82EEDDF?newFirmware=true

kianrafiee commented 6 years ago

And after restarting the servers, I tried again and it still doesnt do anything to the switchmate and just toggles back to original state.

kianrafiee commented 6 years ago

Sorry here is the log:

pi@raspberrypi:~/restswitchmate $ sudo docker-compose logs --tail="100" switchserver
Attaching to restswitchmate_switchserver_1
switchserver_1  | Switchmate SwitchServer Running
switchserver_1  |  * Running on http://0.0.0.0:5002/ (Press CTRL+C to quit)
switchserver_1  | [2018-03-11 07:33:15,597] ERROR in app: Exception on /device/F1ABE82EEDDF [PUT]
switchserver_1  | Traceback (most recent call last):
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
switchserver_1  |     response = self.full_dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
switchserver_1  |     rv = self.handle_user_exception(e)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
switchserver_1  |     return cors_after_request(app.make_response(f(*args, **kwargs)))
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
switchserver_1  |     reraise(exc_type, exc_value, tb)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
switchserver_1  |     rv = self.dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
switchserver_1  |     return self.view_functions[rule.endpoint](**req.view_args)
switchserver_1  |   File "/code/app.py", line 61, in device
switchserver_1  |     results.update({'newFirmware': False})
switchserver_1  | AttributeError: 'NoneType' object has no attribute 'update'
switchserver_1  | 127.0.0.1 - - [11/Mar/2018 07:33:15] "PUT /device/F1ABE82EEDDF HTTP/1.1" 500 -
parisbutterfield commented 6 years ago

Do you have a rest client handy? HassIO seems to be using the old url still.

https://ibb.co/jjJs2n

I'd like you to test the PUT with your ip/macaddress.

kianrafiee commented 6 years ago

Your in my head ;)

https://ibb.co/hamehn

parisbutterfield commented 6 years ago

:) Ok, did that fail? If so send me the logs.

kianrafiee commented 6 years ago

yep:

switchserver_1  | [2018-03-11 07:50:51,990] ERROR in app: Exception on /device/F1ABE82EEDDF [PUT]
switchserver_1  | Traceback (most recent call last):
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
switchserver_1  |     response = self.full_dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
switchserver_1  |     rv = self.handle_user_exception(e)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
switchserver_1  |     return cors_after_request(app.make_response(f(*args, **kwargs)))
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
switchserver_1  |     reraise(exc_type, exc_value, tb)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
switchserver_1  |     rv = self.dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
switchserver_1  |     return self.view_functions[rule.endpoint](**req.view_args)
switchserver_1  |   File "/code/app.py", line 61, in device
switchserver_1  |     results.update({'newFirmware': False})
switchserver_1  | AttributeError: 'NoneType' object has no attribute 'update'
switchserver_1  | 127.0.0.1 - - [11/Mar/2018 07:50:51] "PUT /device/F1ABE82EEDDF HTTP/1.1" 500 -
parisbutterfield commented 6 years ago

Ok, found a bug. Try to connect to :5002 with your rest client. That should work. When the request is being proxied, it's losing the url parameter. Let me figure out how to fix it.

kianrafiee commented 6 years ago

It sort of worked:

switchserver_1  | Request added to queue locally
switchserver_1  | 192.168.1.77 - - [11/Mar/2018 07:55:48] "PUT /device/F1ABE82EEDDF?newFirmware=true HTTP/1.1" 200 -
switchserver_1  | Failed to connect to peripheral f1:ab:e8:2e:ed:df, addr type: random
pi@raspberrypi:~/restswitchmate $ 

I wonder if it thinks it needs to authorize it?

parisbutterfield commented 6 years ago

Nope. That looks like a bluetooth error.

Run hcitool dev on the Pi and send me the output. Try brining the switch next to the PI.

kianrafiee commented 6 years ago

Hmmm. http://192.168.1.67:5000/status is not picking up toggle changes.

kianrafiee commented 6 years ago

Yeah I ran it right afterwards and it shows:

pi@raspberrypi:~/restswitchmate $ hcitool dev
Devices:
    hci1    B8:27:EB:D3:78:F0
    hci0    5C:F3:70:87:70:BF
kianrafiee commented 6 years ago

Ok so i reran it and it worked! Seems it may be one of the bluetooth radios arent that great.

kianrafiee commented 6 years ago

But one caveat: Is there a way to first detect the current switch status and base the PUT request on that? That way if you send two off put requests, it wont just toggle it on on the second put. Thats what it does right now and I could see that posing a problem. Maybe not immediately but it could.

But it does work :) So you did smash a bug. Must feel satisfying, I know it did for me lol

kianrafiee commented 6 years ago

Oh also, if I have two switchmates, how do I add both to hass yaml? right now if I add another "switch" line it gives a configuration error and says there are duplicates. i know it probably is as easy as adding some naming scheme but I am not familiar with yaml.

Also, you might want to update your readme to add sudo for the docker-compose lines in the install text. You'll get errors if you do it without because the right user isnt set for docker. You could probably just tell the user to add the user too. It states to do so when you install docker.

parisbutterfield commented 6 years ago
switch:
  - platform: rest
    name: 'Bedroom Fan'
    resource: http://192.168.1.50:5000/device/CB02F------
    method: 'put'
    body_on: '{"on": true}'
    body_off: '{"on": false}'
    is_on_template: '{{value_json.status}}'

  - platform: rest
    name: 'Dining Table'
    resource: http://192.168.1.50:5000/device/D1263F-----
    method: 'put'
    body_on: '{"on": true}'
    body_off: '{"on": false}'
    is_on_template: '{{value_json.status}}'

Yeah it looks like that.

kianrafiee commented 6 years ago

Great. I should have known.

I just realized that maybe the reason it failed to switch on but worked the second try was because were using port 5002 which means the switch server now may have needed 2 bluetooth radios at that moment and the other one was used by status server and the original one for switch server.

parisbutterfield commented 6 years ago

Ok, fixed the proxy issue. The parameter should now get passed. And work in HASS.io.

As for toggling, yeah that's definitely something you can add. You can read the DB before the bluetooth call and make your decision to on/off based on the value. One thing to note is there's a delay in polling.

Also since the Switchmate Bright / new firmware requires you to connect to get the state, there may be an issue if you simultaneously send an on/off. The original firmware doesn't have this issue. May need to redesign things in the future.

kianrafiee commented 6 years ago

Whenever you have time can you show how to read the database for current state and do all that jazz? Or is there a guide for this on hass?

Lastly, if I update the git branch on my pi for reswitchmate, do I have to recreate the containers for the servers?

Thanks for spending part of your weekend on this. You rock man.

kianrafiee commented 6 years ago

Also, docker-compose down will kill and remove all containers. This may be useful for someone that has the same problem and wants to fix it quick and doesnt use other docker containers on there pi(like me so far).

kianrafiee commented 6 years ago

Ok well I updated restswitchmate branch. redid docker-compose up. restarted hass. but when i toggle in hass it just reverts to original state as before. log:

switchserver_1  | [2018-03-11 08:41:30,638] ERROR in app: Exception on /device/F1ABE82EEDDF [PUT]
switchserver_1  | Traceback (most recent call last):
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
switchserver_1  |     response = self.full_dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
switchserver_1  |     rv = self.handle_user_exception(e)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
switchserver_1  |     return cors_after_request(app.make_response(f(*args, **kwargs)))
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
switchserver_1  |     reraise(exc_type, exc_value, tb)
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
switchserver_1  |     rv = self.dispatch_request()
switchserver_1  |   File "/usr/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
switchserver_1  |     return self.view_functions[rule.endpoint](**req.view_args)
switchserver_1  |   File "/code/app.py", line 61, in device
switchserver_1  |     results.update({'newFirmware': False})
switchserver_1  | AttributeError: 'NoneType' object has no attribute 'update'
switchserver_1  | 127.0.0.1 - - [11/Mar/2018 08:41:30] "PUT /device/F1ABE82EEDDF HTTP/1.1" 500 -
pi@raspberrypi:~/restswitchmate $ 

I'm still using port 5000 in my yaml. I didnt think youd want to keep port 5002 or did you?