custom-components / ble_monitor

BLE monitor for passive BLE sensors
https://community.home-assistant.io/t/passive-ble-monitor-integration/
MIT License
1.9k stars 245 forks source link

Govee support #386

Closed Ernst79 closed 3 years ago

Ernst79 commented 3 years ago

This issue is to discuss on how to integrate the govee integration into BLE monitor and to join forces.

We basically need to write a govee.py in the ble_parser folder, similar like the other parsers. And we need to add a line in ble_parser/init.py that filters on the UUID of govee sensors and calls the parser.

Please not that the xiaomi parser is just rewritten, but isn't in the master yet (it's in 2.8.0-beta now). Better to look at the other parsers (which are also a lot shorter and thus easier to understand).

Ernst79 commented 3 years ago

If you want help let me know. I can set up the basics, to get the advertisement into govee.py if you want.

Ernst79 commented 3 years ago

I've set up the basics and added the Govee H5074. I don't think we need this check on the "flags"as you do in your code, as ble monitor will loop over all individual parts of the advertiement, and will process them all individually (it will e.g. skip the 020106, moves 3 bytes further (02 + 1) and will read e.g. part with the name, will skip that too, and will continue with the actual data),

Most important files are the __init__.py and govee.py in the ble_parser folder. the first will filter out the correct company id or UUID (I saw that the devices use different ID's so we need to add some later. The processing of the data is done in govee.py.

To test, copy all files from here into the ble_monitor folder in custom_components.

https://github.com/custom-components/ble_monitor/tree/Govee

Thrilleratplay commented 3 years ago

@Ernst79 Thank you for getting this started and adding an initial Govee device. Sorry, I have been busy and have not had the time to devote to this. I hope to be able to test this and add other devices in the next few days.

Ernst79 commented 3 years ago

No problem, take your time.

I forgot to mention the const.py file, there you will need to add the sensors as well, with the type of sensors you need for each device.

Ernst79 commented 3 years ago

I have temporary disabled the govee sensor in ble_parser/__init__.py. Just remove the commented out lines. and it will work again.

Ernst79 commented 3 years ago

I had some time over, so I've added the other govee sensors as well, based on your code. The code is in the govee branch. Just copy ble_monitor/ble_parser/__init__.py and ble_monitor/ble_parser/govee.py over the same files (after updating to 4.0.0). I think we only need testing with actual sensors. I found some hcidumps I have tested, but couldn't find one of a H5179, so not sure if this one will work.

Thrilleratplay commented 3 years ago

Sorry for the delay. I only own H5072, H5074 (x3), H5075 (x6), H5102 and H5177. Testing version 4.0.0, all three H5074 are reporting correctly when "Active Scan" is enabled. Even with "Govee" selected for "Report Unknown", no other Govee devices I own are appearing. This is due to the wide range of encodings the Govee devices have. I'll spend some time this evening working on getting the remainder working.

EDIT: I have all of the devices I own working. I have to clean up the code and add checks for negative temperatures. As for the H5051 and H5179, I'll try to either purchase them or try to find some debug data from them.

Ernst79 commented 3 years ago

Sorry, I forgot to update the const.py file. Will do that today, My fault.

Edit: added the missing const.py Govee entries. Feel free to overwrite if you have it different.

I already had something in the code for the negative temperature of the sensors that extract the data from this long integer.

Regarding the negative temperature that you fix with the two_complement code, I have changed that part of your code in the struct.unpack part. In your code you have

temp, hum, batt = struct.unpack_from("<HHB", self.mfg_data, 6)

I BLE monitor, I changed the first H in HHB to a not capitalized letter h

(temp, humi, batt) = unpack("<hHB", data[5:10])

H is an unsigned integer (always positive) extracted from 2 bytes, while h is an integer (positive or negative) extracted from 2 bytes. This should be doing the same as your 2 complement check. See the 2nd answer in the link you refer to in your code. This answer was probably added later 😄

Thrilleratplay commented 3 years ago

If I remember correctly, I skipped the second answer because of the phrase "Unlike Joran's answer this supports arbitrary bit-width" and assumed the more versatile function would be more useful with different decodings. I figured there was a bit shift shortcut for the H5075 but haven't figured it out.

Last night I was adding the other Govee devices but didn't realize the govee branch still had unmerged commits. After rebasing the branch, I saw you had everything already developed plus some. I created a PR with a few label changes. There was also an superfluous MANUFACTURER_DICT entry for H5074 but this may have been intentionally left in as not to break your cache.

Your changes look good to me for the devices I own. The H5179 and H5051 will remain untested until someone with the device can test them out.

Thrilleratplay commented 3 years ago

There is another Govee device, H5178, that has an open PR for sensor.goveetemp_bt_hci, which may not be as easy to integrate. It is a multiple sensor device, there is a display with a temperature humidity sensor then a second temperature humidity sensor that, I believe, communicates via RF. The display broadcast the information via BLE for both sensors but only uses one MAC address.

Do any other the ble_monitor supported devices do anything similar?

Ernst79 commented 3 years ago

Sorry, for working at the same time on the same thing. I had some time left, and decided also give it a try. Unfortunately at the same time. 😄

About the H5178, yes, I had something similar with the kegtron sensor. I used some workaround to make that work. In the end, I ended up with making two sensors, ("volume dispensed port 1" and "volume dispensed port 2").

I propose we first make a release for the sensors we have now and we try to add the H5178 later. this will require some work I guess, to figure out how to distinguish between the two sensors.

Will you add the sensors to the docs, or do you want me to do that?

Thrilleratplay commented 3 years ago

Great. I'll add them to the docs.

Ernst79 commented 3 years ago

Added the docs to info.md as well (this is what is shown in HACS). Also added you to the credits 🎉

I'll release tomorrow, first some sleep

Ernst79 commented 3 years ago

4.1.0-beta has been released.

Ernst79 commented 3 years ago

4.1.0 has been released. I'll create a new issue for the H5178

JackTalisker commented 3 years ago

Today I tried to add my H5179 removing the former integration by Thrilleratplay (which was working flawlessly) and installing ble_monitor. No success. I tried either the UI config or the yaml way. The entities simply do not appear. No errors in HA log. H5179 works well as reported by bluetoothctl scan.

EDIT: I'm opening a new issue #448