theyosh / TerrariumPI

Home automated terrarium/aquarium or other enclosed environment with a Raspberry Pi
https://terrarium.theyosh.nl
GNU General Public License v3.0
402 stars 97 forks source link

[FEATURE]: TAPO P100 smart relay support #888

Closed theyosh closed 4 months ago

theyosh commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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,robin.morgan8@outlook.com,password123!': 'result', retrying in 0.5 seconds...

This occurs when just trying to add the relay.

theyosh commented 5 months ago

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 commented 5 months ago

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

theyosh commented 5 months ago

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@gmail.com", "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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

yes that worked, socket turned on

theyosh commented 5 months ago

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

theyosh commented 5 months ago

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

RobinM-MB commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

source venv/bin/activate

yeah i remebered and deleted comment doh

RobinM-MB commented 5 months ago

Test works all is working from dashboard still

RobinM-MB commented 5 months ago

Thankyou :)

theyosh commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

All working now:)

theyosh commented 4 months ago

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

Gwigga commented 4 months ago

Yes all good thankyou

Sent from Outlook for iOShttps://aka.ms/o0ukef


From: TheYOSH @.> Sent: Monday, February 12, 2024 9:40:16 PM To: theyosh/TerrariumPI @.> Cc: Gwigga @.>; Comment @.> Subject: Re: [theyosh/TerrariumPI] [FEATURE]: TAPO P100 smart relay support (Issue #888)

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

— Reply to this email directly, view it on GitHubhttps://github.com/theyosh/TerrariumPI/issues/888#issuecomment-1939630867, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AFPKP6CAPWBASWBCFS4CQWLYTKD4BAVCNFSM6AAAAABCLCCEKWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZZGYZTAOBWG4. You are receiving this because you commented.Message ID: @.***>

theyosh commented 4 months ago

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 https://github.com/theyosh/TerrariumPI/issues/210 Thanks!