home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.55k stars 30.73k forks source link

Zengge needs migration from bluepy to bleak library #74631

Closed donparlor closed 1 year ago

donparlor commented 2 years ago

The problem

Zengge integration rely on the bluepy and pybluez libraries, which no longer work in newer versions of Python. bluepy has seen its last update in December 2018 (https://pypi.org/project/bluepy/#history) and hasn’t kept up with changes in the Python world. Similar story with pybluez.

I am using Zengge integration and @pvizeli saw no backward compatible path or other solutions to aid this.

If @emontnemery would like to help fix I would be grateful. @pvizeli recommends migrating Zengge onto the Bleak library instead.

#73830) (documentation)

What version of Home Assistant Core has the issue?

2022.7

What was the last working version of Home Assistant Core?

2022.6

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

Zengge

Link to integration documentation on our website

https://www.home-assistant.io/integrations/zengge

Diagnostics information

Platform error light.zengge - Integration 'zengge' not found.

Example YAML snippet

light:
  - platform: zengge
    devices:
      "F8:1D:78:66:2B:E7":
        name: W1
      "F8:1D:78:66:3D:6C":
        name: W2
      "F8:1D:78:66:36:19":
        name: W3
      "F8:1D:78:66:47:01":
        name: W4
      "F8:1D:78:66:32:DD":
        name: W5

Anything in the logs that might be useful for us?

Platform error light.zengge - Integration 'zengge' not found.

Additional information

No response

probot-home-assistant[bot] commented 2 years ago

recorder documentation recorder source (message by IssueLinks)

probot-home-assistant[bot] commented 2 years ago

Hey there @emontnemery, mind taking a look at this issue as it has been labeled with an integration (zengge) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)


zengge documentation zengge source (message by IssueLinks)

probot-home-assistant[bot] commented 2 years ago

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (recorder) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)

emontnemery commented 2 years ago

@donparlor I added myself as code owner for zengge because I did some changes to zenge light a few releases back, i don't have any zengge hardware myself. If you have the time and interest, a PR which migrates zengge would be welcome.

The easiest way forward would be to port https://github.com/mjg59/python-zengge to not use bluepy

donparlor commented 2 years ago

@emontnemery, I'm not a programmer thus I don't know exactly how to do it. I still have a Zengge recessed light hanging around, if you want to have one to test, I can send you one. Or I can try to migrate Zengge but I would need some help on how to tell Zengge to not use bluepy.

emontnemery commented 2 years ago

If you don't have any Python knowledge, it might be tricky.

Otherwise, based on how simplistic the zengge package is, it's probably not that big of a deal to modify it to use bleak: https://github.com/hbldh/bleak https://github.com/hbldh/bleak/tree/develop/examples

Plug the address of your zengge light in to the bleak examples, and it should be simple to connect to the light and control it.

donparlor commented 2 years ago

I'm not a complete noob with python, I'm getting better and better with all the YAML configuration I'm doing with Home Assistant, I'll give it a try and might double check with you here. I'll try to find some time for this in the coming weeks.

Thank you so much @emontnemery

bdraco commented 2 years ago

I'm working on overhauling bluetooth support with a new bt integration.

I have a lot of zengee test devices lying around so I might end up fixing this because I need another test target. Will know in a few weeks

donparlor commented 2 years ago

Wonderful @bdraco! I'll stay in the loop as I have 18 Zengge devices already installed! I'd like to offer some help with this but I might be the third wheel on this job!

donparlor commented 2 years ago

@bdraco I've seen you reopened the ticket. If you succeed with this modification, my whole smart home will finally be integrated into Home Assistant! I'm all with you on this! Thanks

bdraco commented 2 years ago

I have it working, I just haven't had time to clean it up and put it in the library, as I've been busy with the Bluetooth roll out.

donparlor commented 2 years ago

Good news!! Take your time and let me know when you had time to roll it out!

bdraco commented 2 years ago

If you get a chance, would you please send debug logs for homeassistant.components.bluetooth with the advertisements for your devices? I'll need to make sure they match up to make discovery work

donparlor commented 2 years ago

Hello @bdraco! I cannot test beyond adding this code in configuration.yaml

Capture d’écran, le 2022-08-16 à 01 40 51

When I test my YAML, I get the circle running non-stop

Capture d’écran, le 2022-08-16 à 01 41 20

What else can I do to help you?

bdraco commented 2 years ago
# Example configuration.yaml entry
logger:
  default: info
  logs:
    homeassistant.components.bluetooth: debug

This should get the advertisements to show up

donparlor commented 2 years ago

Ok, logger installed. I copied my code for zengge in configuration.yaml, where should I look now?

bdraco commented 2 years ago

Once bluetooth is installed check the log for advertisements matching the mac addresses of the devices

donparlor commented 2 years ago

I searched through the logs with one of my bluetooth MAC to see if anything appears, but nothing:

Capture d’écran, le 2022-08-16 à 08 49 47

Also, I have essentially BT Mesh Downlights working with HaoDeng, and my understanding is that this is manufactured by Zengge.

Capture d’écran, le 2022-08-16 à 08 42 06

These Downlight are synched with Zengge Smart bridge over wifi.

Capture d’écran, le 2022-08-16 à 08 42 49
bdraco commented 2 years ago

The non mesh ones will work with the led-Ble integration releasing later today. This will support all the devices the integration currently supports.

The mesh ones require a key that we don't have so unless we can get the keys we can't add support for them

https://github.com/zhourenjun/BleMeshLib/blob/master/BluetoothLightLib/src/main/java/com/telink/crypto/AES.java

donparlor commented 2 years ago

Are there any known way to retrieve those keys?

bdraco commented 2 years ago

Unfortunately we don't know how to get the keys

donparlor commented 2 years ago

Ok... sadly I guess my BT-Mesh Zengge lights will remain the outsider of my smart home...

Thanks bdraco, you gave me some hope but I am forced to accept there is no way to integrate those lights in Home Assistant

Kerios00 commented 1 year ago

What is the state of migration? I have just bought a Zengge BT controller for my LED strip, but I can't integrate it into the system. Error log: Logger: homeassistant.components.light Source: components/zengge/light.py:66 Integration: Világítás (documentation, issues) First occurred: 22:24:02 (1 occurrences) Last logged: 22:24:02

Error while setting up zengge platform for light Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform await asyncio.shield(task) File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/usr/src/homeassistant/homeassistant/components/zengge/light.py", line 45, in setup_platform light = ZenggeLight(device) File "/usr/src/homeassistant/homeassistant/components/zengge/light.py", line 66, in init if self._bulb.connect() is False: File "/usr/local/lib/python3.9/site-packages/zengge/init.py", line 36, in connect self.device = btle.Peripheral(self.mac, addrType=btle.ADDR_TYPE_PUBLIC) File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 391, in init self._connect(deviceAddr, addrType, iface) File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 439, in _connect raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp) bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral XX:XX:XX:XX:XX:X, addr type: public

What version of Home Assistant Core has the issue? 2022.6.1

I am using Docker container.

bdraco commented 1 year ago

Use led-ble instead. It supports all the devices the original integration supported.

It does not support the mesh devices (this integration never did either)

Kerios00 commented 1 year ago

In my version, the led-ble does not work. https://www.home-assistant.io/integrations/led_ble/ The ADD INTEGRATION button on this link throws an error, and the home assistant interface does not even give the search when adding integration. Also, to my knowledge the Zengge BT LED CONTROLLER is not a mesh device.

Error This integration does not support configuration via the UI. If you followed this link from the Home Assistant website, make sure you run the latest version of Home Assistant.

And I have no update available.

bdraco commented 1 year ago

In my version, the led-ble does not work. https://www.home-assistant.io/integrations/led_ble/ The ADD INTEGRATION button on this link throws an error, and the home assistant interface does not even give the search when adding integration.

Also, to my knowledge the Zengge BT LED CONTROLLER is not a mesh device.

Open an issue with logs for led-ble

issue-triage-workflows[bot] commented 1 year ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

SleepyNinja0o commented 1 year ago

Unfortunately we don't know how to get the keys @bdraco

If you were able to retrieve these keys, how hard would it be to get this working? I am using the Hao Deng app with some BLE Mesh Flood Lights.

If you have a rooted Android phone, you can find these keys located in a SQL Lite database under the application's data folder (/data/data/com.zengge.telinkmeshlight/databases/ZGMeshDB)

Also, here's a cool trick. You can add a device using a non-rooted phone and then sign in to your Hao Deng cloud account on a rooted phone/emulator to retrieve the keys.

keys

bdraco commented 1 year ago

There is a fork of an archived library here talk to that mesh https://github.com/bdraco/python-dimond

donparlor commented 1 year ago

If anyone is interested to push a little more with a detailed procedure to retrieve and integrate Hao Deng lights into HA, I am willing to give some help. It's a very specific projet... but Hao Deng needs to be beaten with that project! 🗡️

SleepyNinja0o commented 1 year ago

I'm a little rusty when it comes to Python but I can write up some documentation on how I retrieved my keys.

Basically just need to pair device with your phone, setup Hao Deng cloud account in the app. Then download app and login inside a Bluestack emulator that is rooted to retrieve keys.

donparlor commented 1 year ago

No rush on this, I know time is precious for everyone. I'd be more than happy but it's just a "nice to have" for now. But if you feel like working on it you can be sure I'll follow up and try what you suggest.

SleepyNinja0o commented 1 year ago

So far I am able to communicate with my Zengge BLE Mesh floodlights using the Bleak module and some functions from the python-dimond library mentioned above. I can "pair", execute commands such as change colors, turn off/on, etc....but I am unable to receive notifications from the device using the Bleak start_notify function. This would be a vital piece needed to verify if commands are sent to the device successfully.

If anyone is willing to help that has more experience with BLE GATT Server/Client communications and the Bleak library, this shouldn't take much more to get these lights integrated. For some reason the notifications work fine when I am using the bluepy library but Bleak is returning an error regarding writing to an attribute (The notify UUID of the device).

Command: await client.start_notify("00010203-0405-0607-0809-0a0b0c0d1911", callback) Windows Error: OSError: [WinError -2140864509] The attribute cannot be written Linux Error: bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Operation failed with ATT error: 0x0e (Unlikely Error)

I have Bluetooth packet captures from my cell phone and from my computer (when running the Python Bleak commands) to help diagnose the issue but this issue seems to be with Bleak.

SleepyNinja0o commented 1 year ago

It appears the GATT server on many BLE Mesh lights are misconfigured so the start_notify command in Bleak does not work as it should. I mentioned this issue on Bleak's page and they have an open issue where they mentioned building out their API to allow for subscribing/enabling notifications to characteristics that don't follow specs. https://github.com/hbldh/bleak/issues/972

fsaris commented 1 year ago

The Zengge BLE Mesh looks similar to the Awox BLE mesh. Both based on telink. For more info see also https://github.com/fsaris/home-assistant-awox (not using bleak because of the mentioned issue https://github.com/hbldh/bleak/issues/972) and https://github.com/fsaris/EspHome-AwoX-BLE-mesh-hub

bdraco commented 1 year ago

The Zengge BLE Mesh looks similar to the Awox BLE mesh. Both based on telink. For more info see also https://github.com/fsaris/home-assistant-awox (not using bleak because of the mentioned issue https://github.com/hbldh/bleak/issues/972) and

https://github.com/fsaris/EspHome-AwoX-BLE-mesh-hub

Can you get it to work with an esphome proxy ?

donparlor commented 1 year ago

Just letting you know I follow this discussion with much interest. Thanks for investing time into this!