hrdasdominik / hue-entertainment-pykit

Pythonic bridge to dynamic lighting via Philips Hue Entertainment API
MIT License
9 stars 0 forks source link

handshake response issues (just sometimes) #17

Closed H852ing closed 2 weeks ago

H852ing commented 1 month ago

Hi hrdasdominik,

I could use your help. It often happens when I start your script that I get the following error:

Traceback (most recent call last):
  File "C:\Python312\Lib\site-packages\mbedtls\tls.py", line 441, in do_handshake
    self._buffer.do_handshake()
  File "src\\mbedtls\\_tls.pyx", line 1147, in mbedtls._tls.MbedTLSBuffer.do_handshake
  File "src\\mbedtls\\_tls.pyx", line 1159, in mbedtls._tls.MbedTLSBuffer._handle_handshake_response
mbedtls._tls.WantReadError: WantReadError()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\_test\entertainment_python_1.py", line 34, in <module>
    streaming.start_stream()
  File "C:\Python312\Lib\site-packages\hue_entertainment_pykit.py", line 237, in start_stream
    self._streaming_service.start_stream()
  File "C:\Python312\Lib\site-packages\services\streaming_service.py", line 122, in start_stream
    self._dtls_service.do_handshake()
  File "C:\Python312\Lib\site-packages\network\dtls.py", line 135, in do_handshake
    self._dtls_socket.do_handshake()
  File "C:\Python312\Lib\site-packages\mbedtls\tls.py", line 444, in do_handshake
    data = self._socket.recv(
           ^^^^^^^^^^^^^^^^^^
TimeoutError: timed out

When I try again three or four times, it eventually works. I am using Python 3.12.1 on Windows 10. Is this phenomenon known, or could it be related to my environment? Do you have any tips for me? When your script has started and runs, it works great, and I’m really impressed with your integration. I just need to figure out how to make sure it starts reliably every time.

I appreciate any tips.

Best regards, H852ing

hrdasdominik commented 1 month ago

Morning and hi again! Yes, this is a well-known bug to me. Well, it's not a bug exactly, but in the redesign, the library should handle stopping the stream if the application fails smh.

What I am doing now is trying and catching the app with general exception so if anything fails and the app is gonna shut down I want it to trigger the .stop_streaming() method from the Streaming class.

I guess that if anything in development breaks and you do not send to the bridge that the stream has stopped, the connection ends up still opening for some time until it times out somehow and the bridge ends the connection on its own.

Hope this helps!

Best regards, Dominik Hrdas

H852ing commented 1 month ago

Hello,

Thank you for the quick response. I hope you manage to find a way to identify and catch this exception in the future.

I’m definitely following your project with great excitement. Your project is the perfect addition for me to integrate the Hue outdoor lights into my Halloween and Christmas lighting shows. You’ve definitely gained a new fan. I also love that your script allows me to use multiple Hue-Bridges, so I can control more than 10 lights in Entertainment mode with millisecond precision across two bridges. That helps a lot.

Thank you again for all your hard work, and I’m looking forward to future updates

hrdasdominik commented 3 weeks ago

Yesterday on October of 12th 2024 I had a whole day of problem with this. Had only one connection success. Not sure what is going on. Checked handshake on Wireshark and captured that bridge is not responding to cookie hello for some reason: asdagqet

this was some weird case also that I almost got connected: sdfhjerzwe45

I'm investigating this further, even wrote to the hue devs on the forum and tagged them. I'm guessing it has to be something bout the bridge firmware update that happened 10th of October 2024

hrdasdominik commented 3 weeks ago

!Update! This smells to me like a firmware bug since I tried JavaScript solution from the article: https://iotech.blog/posts/philips-hue-entertainment-api/ and I'm getting the same error.

P.S. I have probably found something. I'm not sure if this was like this before but the port is closed for the bridge which they defined we should connect to. This port is also closed even if you start the entertainment configuration via POST image

H852ing commented 3 weeks ago

Hello, It's not working for me anymore either. When I start the Python script, I can see in the Hue app on my smartphone that an "Entertainment Session" is active, but the light sequence or the lamps don't light up. It's as if the stream with the color information isn't arriving.

And this happens just 2 weeks before Halloween. I was planning to include all of our outdoor lights.

Thanks a lot, Philips, for the update.

hrdasdominik commented 3 weeks ago

@H852ing sorry to hear about this. I have written on the forum of the philips hue dev community and tagged them. I hope they fix this soon since I'm working on this library. Also something that is interesting to me is that their sync solution for entertainment lights (the spotify and desktop hue version) are working. I'm gonna inspect with wireshark what is going on. Working on it and I'll try to send email to the devs if possible since this is kind of critical bug for us.

hrdasdominik commented 3 weeks ago

Update

I came up with this glorious idea of using their Hue Sync app to sync with PC monitors which is calls Hue Entertainment API and inspect it with Wireshark. 1st 2nd I've found the bug: they're sending 2 client hello messages with the cookie that are replicas on the second confirmation of the bridge which is not normal. I can probably patch the library so it sends two messages but that would probably break the minute bridge firmware comes with the fix. The problem is I'm kind of on a tight schedule and have some family matters to attend to so I'm not sure if it's worth doing a release for that crunching some extra hours just for them to release possibly soon fix for this.

H852ing commented 3 weeks ago

Hi hrdasdominik,

First of all, thank you and huge respect for analyzing the issue and finding the error.

Let me put it this way. Of course, it would be great for me if it worked on October 31st,.... but as you said, it would probably be a waste of your time to implement it now (basically just for me) and then have to undo it once Phillips has patched it.

Let’s just hope Phillips fixes it soon.

If you're busy with family matters right now, it's better to leave it and take your time later if you want.

After all, this is your hobby project, which you were kind enough to share with us. Unfortunately, I’m just an end user with minimal coding knowledge and can't help you, so I hope you continue to enjoy this project for a long time and develop it bit by bit. Just don’t put any pressure on yourself.

I’m looking forward to fully integrating your library Halloween 2025. Maybe by then, you’ll also have addressed those occasional connection issues.

In any case, thanks for all your efforts.

hrdasdominik commented 2 weeks ago

@H852ing Hi, the Philips Hue update 1.67v for the bridge came and the same thing is happening. I've been working on the patch these past 3 days and I'm pretty close to solving it. I hope this weekend I'm gonna release the patch so keep a close eye on this issue for updates.

P.S. Made the solution. I need to work out some Unit test problems. A new release with the patch is incoming. Hopefully, it's enough time for you to continue and finish your work for Halloween.

H852ing commented 2 weeks ago

Thank you so much for your effort. You're truly a hero.

But unfortunately,.... I have to admit, I’m not sure how to install the branch "17-handshake-response-issues-just-sometimes". I installed your main package simply using PIP on Windows. Do I need to compile this branch somehow first? I’m a bit stuck at the moment… unfortunately, I’m just a end user who plays around with this as a hobby. I have never compiled anything in my life. But I’d really love to try out your new version.

What do I need to do?

hrdasdominik commented 2 weeks ago

@H852ing Hi, don't worry. I'm gonna make it as a release so you're gonna update it normally through pip package manager. I'm just bit of stuck on the tests side but I will finish this pretty soon.

hrdasdominik commented 2 weeks ago

@H852ing sorry for the wait but the new release is out -> v0.9.2-beta Hopefully, I haven't taken too much out of your time so you can prepare for Halloween. I'm closing this and if you find a new issue please open a new one. Ty for using the library and hope you will continue enjoy using it!

H852ing commented 2 weeks ago

Hello,

I need your help again. I have now removed the old package via pip uninstall hue-entertainment-pykit and then installed the new version 0.9.2 using pip install hue-entertainment-pykit.

Then I deleted the old Data and Log folders in the script directory and create a new token out the Hue bridge using your discovery example script. I inserted this new token into your example code for testing.

When I run it, I get the following errors:

Traceback (most recent call last): File "D:\hue-python-test\entertainment_python.py", line 5, in bridge = create_bridge( ^^^^^^^^^^^^^^ TypeError: create_bridge() got an unexpected keyword argument 'client_key'

Does this have to do with my Python 3.11.3 installation on Windows? Or do I need to manually delete/clean something beforehand? Are there any leftovers anywhere?

hrdasdominik commented 2 weeks ago

@H852ing Hi, I might have changed the attribute name from client_key to clientkey meaning if you are assigning parameters like this: create_bridge(client_key="jsno27rj...", ...) Then you should do it like this: create_bridge(clientkey="jsno27rj...", ...)

I need to update the example and the docs. But this should solve your problem.

H852ing commented 2 weeks ago

It works,... ❤️ Then I'll spend the next few days adjusting the timing of the Hue lights to match the rest of the show.

Thank you so much!