mariusmotea / diyHue

Philips Hue emulator that is able to control multiple types of lights
Other
627 stars 107 forks source link

Python Cleanup #262

Closed titilambert closed 6 years ago

titilambert commented 6 years ago

Hello ! I just found your project, and this is really awesome ! The only issue is the python code structure. So I'm creating this issue to know if you're open to a huge python code cleaning:

What do you think about this ?

mariusmotea commented 6 years ago

Hi, Is only one issue, i'm not really python expert, never did the things you mention. I start to play with python after i saw the mysql/php implementation drawbacks and when somebody suggeste in one issue to take in consideration the programming language replacement. Now because the number of lines is too big i start to perform some seggragation, but is not easy to do this with the functions that use the bridge_config dict. If you know how to clean my code i will be happy to merge your pull requests.

titilambert commented 6 years ago

Thanks for your answer ! Sorry I was not clear, I can do that ! My question was more about are you opened to that kind of thing ? If yes, I can start a fork/branch and make some merge requests ;)

mariusmotea commented 6 years ago

Yes, this will be an improvement for sure.

Marius.

titilambert commented 6 years ago

@mariusmotea I would suggest split this repository in two repositories:

  1. a Github repository with only the python source (with only the BridgeEmulator folder as root)
  2. a Github repository with all the DIY source code What do you think about this ?

Maybe you should also move the web-ui source, but I didn't read the code yet

mariusmotea commented 6 years ago

Hi,

But what will be the benefit of this split? I'm thinking about the current issues, people will not know exactly if one issue is related to bridge or the lights, etc.

Marius.

titilambert commented 6 years ago

I see ! So I just changed the folder structure. You can see it here: https://github.com/titilambert/diyHue What do you think about this ?

mariusmotea commented 6 years ago

Is ok, only issue is to correct the actual links to images folder.

titilambert commented 6 years ago

@mariusmotea Hello, I started splitting the HueEmulator3.py file in the huebridgeemulator folder. My main issue today is to understand how this is working to validating I didn't break anything. So for now, the daemon is starting, I can go the web page but it's kind of empty: 3f1a4fd9-8ce7-4a5b-8562-38680cdb4c69

What did I miss ? Thanks !

mariusmotea commented 6 years ago

start HueEmulator3.py script manually, i believe there are some errors when you open this page.

titilambert commented 6 years ago

What do yo mean "some errors" ? In fact, I'm just wondering what I should get ? (my configuration is empty and I never used hueEmulator before ) I started the daemon, then what I have to do ? I have a yeelight bulb and a hue bridge. How can I add them to hueemulator config ? Thanks !

mariusmotea commented 6 years ago

Ohh. I was believed that your lights are not displayed. Is fine to not have any lights displayed if there are no lights/rooms.

Marius.

titilambert commented 6 years ago

Perfect, How can I add my yeelight bulb ?

mariusmotea commented 6 years ago

Enable lan mode on the bulb, i write how in wiki. Then once the bulb is connected to your wifi network perform a light search from hue application.

titilambert commented 6 years ago

OK so, the bulb is configured correctly, but how i can perform a light search ? I don't see any button in the UI

mariusmotea commented 6 years ago

You need the android/iphone hue application

titilambert commented 6 years ago

ho ! ok ! thanks ! This is really helpful !

titilambert commented 6 years ago

How can I simulate a hue button click ? The hue app can not join because the button was never join ?

mariusmotea commented 6 years ago

Access emulatorip/touchlink to allow 30seconds for apps to pair with the emulator

Default credentials Hue/Hue

titilambert commented 6 years ago

Where did you define this credentials ?

juanesf commented 6 years ago

Example, in browser: 192.168.8.10/hue/linkbutton Hue is the user and password

titilambert commented 6 years ago

Perfect ! Thansk !

titilambert commented 6 years ago

I can see yeelight bulb ! Now I will try to control it

juanesf commented 6 years ago

Good luck

titilambert commented 6 years ago

Success ! I can control my yeelight bulb ! Now I will try to really clean the code !

juanesf commented 6 years ago

I have a fork with a branch called OpenWrt which works on lede 17.01 perfectly in GL-MT300A

titilambert commented 6 years ago

Hello ! I got yeelight and Hue working ! I don't have any other devices continue the development :/ I just put a small documentation here to test it: https://github.com/titilambert/diyHue#new-folder-structure As you can see I tried clean up this wonderful project by:

What next:

@mariusmotea What do you think about this ? If you like this new structure, how do you want to proceed to do think huge change in the main repository ?

mariusmotea commented 6 years ago

I'm in vacation a few days, but now i browse your code and looks much better than mine. I will perform a test when i will arrive home. I made a commit after you started to work on this project, it is included? It is related to sunrise and sunset calculations using the astral module for python.

mariusmotea commented 6 years ago

@titilambert i don't want to look like i don't appreciate your effort and ask too much, but i want to ask you if you know how to create that dtls server with psk for Hue Entertainment in python. Currently i spent hard time with wolfssl library to create this service in C, but i don't have experience and i did not manage to have a successful compile.

titilambert commented 6 years ago

Hello ! No worries ! I don't have any experience with dtls but I'm sure I can help you with that. I guess we can do it un python. I found this: https://gist.github.com/manuels/8852953

What do you want to priorized ? Refectoring or dtls ? I think we should complete the refactoring as soon as possible.

I don't know if I got your last commit, if not, I could try to port it to this new structure.

Let me know when you come back.

mariusmotea commented 6 years ago

Let's finish the current migration then we can focus more on dtls. There are some libraries in python, the problem is that non offer support for DTLS server with PSK and that rare cipher used by hue entertainment

juanesf commented 6 years ago

I'm still researching gnutls and python-gnutls: https://gnutls.org/manual/html_node/gnutls_002dserv-Invocation.html

https://www.apt-browse.org/browse/debian/wheezy/main/i386/python-gnutls/1.2.4-1/file/usr/share/doc/python-gnutls/examples/server.py

mariusmotea commented 6 years ago

The examples from python-gnutls are not made with psk. The requirements for hue entertainment are:

Only DTLS mode version 1.2 with Pre-Shared Key (PSK) Key exchange method with TLS_PSK_WITH_AES_128_GCM_SHA256 set as Cipher Suite is supported.

titilambert commented 6 years ago

@mariusmotea Do you want to create a new branch in your repository where I could push my changes then, you will be able to push also in this branch ?

mariusmotea commented 6 years ago

Yes please. You can use develop branch as well.

titilambert commented 6 years ago

@mariusmotea could you create a new branch in your repo called "refactor" ? will create a PR to this branch (including your last changes)

titilambert commented 6 years ago

@mariusmotea I just applied your last patch (daylight sensor) to my branch.

mariusmotea commented 6 years ago

@titilambert i create the new branch

mariusmotea commented 6 years ago

@titilambert The branch is now created.

https://github.com/mariusmotea/diyHue/tree/refactor

Marius.

titilambert commented 6 years ago

I think we should create a new class: Registry The configuration is in fact a registry where groups/devices/sensors/... are registered and where theirs states are stored. At the end of the refactoring we should (kind of) "never" edit directly the "config.json" dictionnary as we are doing today.

mariusmotea commented 6 years ago

I start to test the installation. The first issue is the lack of https. On official page of hue api was announced that http is not considered legacy and the developers have one year to switch to https. Now both services are running on original hue bridge. Can we start a new thread for https with the certificate from this repo?

titilambert commented 6 years ago

Hello ! Ho ! I didn't know that ! I would suggest to not handle https directly from your source code. It should be handled by a true http server like nginx. Also you should NOT provide a certificate, this imply insecure https connection. We should provide command lines to generate the certificate or use a service like "let's encrypt" to get it. At the end, we should use ngnix running in docker to handle https. I will try to add this today.

mariusmotea commented 6 years ago

Original Hue Bridge use nginx for https relay to http, but i try to keep this standalone and independent as much as possible. By providing the cert + key you are right, is insecure like http, but currently here we are. Original Hue Bridge generate a self signed certificate with serial number as CN (generated from mac address + 4 constant letters in the middle), but i doubt the applications check the CN as the ssl of this certificate will always fail. Hug don't support https to add it as second thread like i did?

TopdRob commented 6 years ago

Hi the efforts from @titilambert looked very promising. Any progress on there, it's quiet for a while?

ghost commented 6 years ago

I'm closing this as there is now an issue for each of the mentioned refactoring categories. Chat can happen in those or on slack. Feel free to open more issue for additional categories @titilambert