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

[FEATURE]: TAPO P100 smart relay support #888

Closed
theyosh opened this issue Jan 25, 2024 · 31 comments
Closed

[FEATURE]: TAPO P100 smart relay support #888

theyosh opened this issue Jan 25, 2024 · 31 comments

Comments

@theyosh
Copy link
Owner

theyosh commented Jan 25, 2024

Support for TAPO P100 smart relays

https://www.tapo.com/en/product/smart-plug/tapo-p100/

https://www.tp-link.com/us/home-networking/smart-plug/tapo-p100/

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

Documentation can be found at: https://theyosh.github.io/TerrariumPI/hardware/relay/tapo-p100-mini-smart-wi-fi-socket/
Pay attention for the address value you need to enter.

In order to get this new relay working, do the following steps:

  1. Stop TP4: sudo service terrariumpi stop
  2. Enter the TP4 folder: cd /home/pi/TerrariumPI
  3. Update the source code: git pull
  4. Rerun the installer: sudo ./install.sh

A reboot is NOT needed

And now we run it in debug mode. In your case you need to be the user root. So do a sudo -i first.
Enter the TP4 folder (again): cd /home/pi/TerrariumPI
Load Python environment: source venv/bin/activate
Run TP4 manual: python terrariumPI.py

Now it should start, and just work normally. I hope you can login with the login admin/password in order to add a new relay. Select the TAPO P100 relay and enter the address as described in the documentation.

When that works, it should start polling the relay every 30 seconds. And that should produce some debug information in the console, which I do need back. If you can try this and post the debug here, I am vert thankful.

@RobinM-MB
Copy link

RobinM-MB commented Jan 25, 2024

2024-01-25 20:55:12,762 - WARNING - hardware.relay - Could not load hardware for relay TAPO P100/5 relay named 'TEST' at address '192.168.1.58,[email protected],password123!': 'result', retrying in 0.5 seconds...

This occurs when just trying to add the relay.

theyosh added a commit that referenced this issue Jan 25, 2024
theyosh added a commit that referenced this issue Jan 25, 2024
@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

Hmm, the error which is just 'result' is not much :( Are you sure that the login is the same as for TPLink? I guess you have an app for it, and you need the same authentication as in the app.

Because the used python library needs to connect to the TP cloud with your credentials. And I get the feeling that password123! is not your password you use in the app.

@RobinM-MB
Copy link

it was the password when i tested it, have changed it now:)

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

Ok, than I think the best way now is to test with a simple script first.

As you have still the python environment loaded, create a new file. Not sure if you have handy with nano or vi?
But paste the following code to a file called p100.py

from PyP100 import PyP100

p100 = PyP100.P100("192.168.X.X", "[email protected]", "Password123") #Creates a P100 plug object

p100.handshake() #Creates the cookies required for further methods
p100.login() #Sends credentials to the plug and creates AES Key and IV for further methods

p100.turnOn() #Turns the connected plug on
p100.getDeviceInfo() #Returns dict with all the device info of the connected plug

And fill the values IP, Email and password with your credentails and IP of the TAPO switch device. Save it to disk and close the file.
Now you can run it with the command: python p100.py and see what happens.

This is easier debugging, because now you have a very small en simple script. And when this works, we know at least the correct ip, email address and password.

@RobinM-MB
Copy link

Traceback (most recent call last):
File "p100.py", line 5, in
p100.handshake() #Creates the cookies required for further methods
File "/home/pi/TerrariumPI/venv/lib/python3.7/site-packages/PyP100/PyP100.py", line 129, in handshake
encryptedKey = r.json()["result"]["key"]
KeyError: 'result'

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

Ah, this looks like a firmware issue. So lets try something. Can you run the following command in your current python environment:

pip install --force-reinstall git+https://github.com/almottier/TapoP100.git@main

And rerun the same script again

@RobinM-MB
Copy link

RobinM-MB commented Jan 25, 2024

yes that worked, socket turned on

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

Nice! so use the same IP, email address and password in TP4 and I guess it should also work now

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

Oh, what was the output? The last command should give some status output which I need

@RobinM-MB
Copy link

it didn't print anythin to the terminal
(venv) root@raspberrypi:/home/pi/TerrariumPI# python p100.py
(venv) root@raspberrypi:/home/pi/TerrariumPI# python p100.py

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

Can you change the last line in the script. It needs a print( in front and a ) on the end.
So it would be: print(p100.getDeviceInfo())

and rerun

@RobinM-MB
Copy link

Its now added in the dashboard and is controllable

here is the output
{'device_id': '8022BEF758034CDCCE4FE13525887E072112F478', 'fw_ver': '1.2.1 Build 230804 Rel.190922', 'hw_ver': '2.0', 'type': 'SMART.TAPOPLUG', 'model': 'P100', 'mac': '48-22-54-FE-12-56', 'hw_id': '4012E37933F469A8790D690E12080BB6', 'fw_id': '00000000000000000000000000000000', 'oem_id': '525FC9C0545B4C8BEF51FA66130E51DE', 'ip': '192.168.1.58', 'time_diff': 0, 'ssid': 'bWFkaG91c2U=', 'rssi': -71, 'signal_level': 1, 'auto_off_status': 'off', 'auto_off_remain_time': 0, 'latitude': 0, 'longitude': 0, 'lang': '', 'avatar': 'plug', 'region': 'Europe/Jersey', 'specs': '', 'nickname': 'dGVzdA==', 'has_set_location_info': False, 'device_on': True, 'on_time': 134, 'default_states': {'type': 'last_states', 'state': {}}, 'overheated': False}

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

Thanks for the output data. Now I can also read out the relay state. So if you change the state of the relay with your phone app. My software will detect and update itself to the current active state. Which I think is a very need feature.

So I made a final update of the code. Could you do once more a git pull in the /home/pi/TerrariumPI folder and than rerun manual? python terrariumPI.py. The code should still work, but just to be sure it would be fine if you can do a final test.

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

No, you need to activate the python environment first. You do not have the (venv) in front of root@

So do first source venv/bin/activate and then python terrariumPI.py

@RobinM-MB
Copy link

source venv/bin/activate

yeah i remebered and deleted comment doh

@RobinM-MB
Copy link

Test works all is working from dashboard still

@RobinM-MB
Copy link

Thankyou :)

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

That is very nice! So that concludes the integration I think and now the software supports TAPO 100 and 105 sockets.

I think for now, you can stop the TP4 with Ctrl-c and just restart the PI sudo reboot. Then everything should just start up nicely.

And than there is one thing left. As I implemented a new feature for you, I would like to request a small favor back. Please post some pictures in #210 when your setup is running. Like to see how people use my software.

@RobinM-MB
Copy link

That is very nice! So that concludes the integration I think and now the software supports TAPO 100 and 105 sockets.

I think for now, you can stop the TP4 with Ctrl-c and just restart the PI sudo reboot. Then everything should just start up nicely.

And than there is one thing left. As I implemented a new feature for you, I would like to request a small favor back. Please post some pictures in #210 when your setup is running. Like to see how people use my software.

Sure thing, its now back up and running, I have some of the NextEvoDimmers on order.
I did start doing this kind of project on my own before finding TerrariumPI, but was now where near the complexity.

You really have gone above and beyond.

First to get it will be our Emerald Swift which will be a good one as it has light, heat and humidity requirements but also a running water fall, all being bio active as well.

@theyosh
Copy link
Owner Author

theyosh commented Jan 25, 2024

Nice, my madagaskar day gecko is now almost 14 years old :). Which is pretty neat according to this data: https://reptiletraining.com/madagascar-day-gecko-faq-guide-on-food-habitat-size-lifespan-and-predators/

I hope my software was part of getting her happy and healthy al those years

@theyosh
Copy link
Owner Author

theyosh commented Jan 26, 2024

So, did you by any change let TP4 run over night? I would like to know how stable it is. So could you turn TP4 on, and try to toggle once. Wait 24 hours or so, and toggle again.

Because there is a login session active with the socket. And I do not know how long the login session is valid. So if you could do some 'endurance' testing, that would by great.

@RobinM-MB
Copy link

So, did you by any change let TP4 run over night? I would like to know how stable it is. So could you turn TP4 on, and try to toggle once. Wait 24 hours or so, and toggle again.

Because there is a login session active with the socket. And I do not know how long the login session is valid. So if you could do some 'endurance' testing, that would by great.

This has been running all weekend now running a timed session ie turning on every morning at 7 and off at 1900.

The only issue I can see is my plug decided it was going to change its IP, so I had to delete the "relay" and set it up again, but I gave it a static IP on my network and its been running fine since

@theyosh
Copy link
Owner Author

theyosh commented Jan 29, 2024

The only issue I can see is my plug decided it was going to change its IP, so I had to delete the "relay" and set it up again, but I gave it a static IP on my network and its been running fine since

Yeah, that is a known issue. But I think I can fix that. I hope to have an update later tonight.

@theyosh
Copy link
Owner Author

theyosh commented Jan 29, 2024

Ok, I did made a change. But you can only test it by deleting the existing relay and add it again once more. When you add it, it will use the MAC address for ID generating. This MAC address will not change.
But in order to make it work, you have to re-add the relay.

So do a git pull and restart TP4. Delete the existing relay, and add it again. Now an IP change should not make a difference anymore.

@Gwigga
Copy link

Gwigga commented Jan 30, 2024

Ok, I did made a change. But you can only test it by deleting the existing relay and add it again once more. When you add it, it will use the MAC address for ID generating. This MAC address will not change. But in order to make it work, you have to re-add the relay.

So do a git pull and restart TP4. Delete the existing relay, and add it again. Now an IP change should not make a difference anymore.

Do I need to do an install again

getting this in debug now
Traceback (most recent call last):
File "terrariumPI.py", line 19, in
terrariumEngine = terrariumEngine(version)
File "/home/pi/TerrariumPI/terrariumEngine.py", line 142, in init
self.__load_existing_relays()
File "/home/pi/TerrariumPI/terrariumEngine.py", line 830, in __load_existing_relays
self.relays[relay.id].set_state(last_value, True)
KeyError: '514ad891676c7b3f982334d9bd806154'

@theyosh
Copy link
Owner Author

theyosh commented Jan 30, 2024

A re-install is not needed. And this looks like that somehow the old relay is still in the database.

If you are just testing, could you delete the database and start over?

cd /home/pi/Terrariumpi
rm data/terrariumpi.db*

Than load the python environment and run in debug mode.

That should remove the database, and when you start TP4 again, it will be recreated. Add the relay again, and see if a restart will work.

As far I can test it, it should work. Sorry for the inconvenience.

@RobinM-MB
Copy link

All working now:)

@theyosh
Copy link
Owner Author

theyosh commented Feb 12, 2024

Still working? Is so, than we can close this issue?

@Gwigga
Copy link

Gwigga commented Feb 12, 2024 via email

@theyosh theyosh closed this as completed Feb 12, 2024
@theyosh
Copy link
Owner Author

theyosh commented Feb 12, 2024

Oh, almost forgot. When I add a new feature or hardware as a request, I expect some 'payment' in return ;)

Please post some pictures of your setup when done at #210 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants