liqi0816 / ble2mqtt

Bluetooth Low Energy to MQTT bridge (like zigbee2mqtt)
1 stars 1 forks source link

A bit more info would be appreciated :-) #3

Open andypnz opened 1 year ago

andypnz commented 1 year ago

Hi. This is a great addition to HA - the Adaprox fingerbots are really cheap :-)

I am using HAOS. I have not run any Python in HA - should this run under something like AppDaemon?

And can the script to get the device id, etc - can that be run under HA? Or do you need a separate Python environment to run it under (like a Linux box / VM / etc)?

liqi0816 commented 1 year ago

Hi,

This is a standalone program and does not depend on AppDaemon. In fact we are not using any HA python API at all. The program interacts with HASS just like zigbee2mqtt. Everything is done via mqtt.

Sorry but I don't quite understand your second question. This program sends the desired device id to HA via mqtt. On HAOS python should be preinstalled, and it should be fine to just use that installation. For MQTT there is an addon.

I chose to write a standalone program because it's easier to restart a small program than the entire HASS.

andypnz commented 1 year ago

Thanks... I think I need a Tuya hub, right?

Or will this work direct to the fingerbot via RPi BT?

liqi0816 commented 1 year ago

You don't need a Tuya Hub. The purpose of this program is to use conventional bluetooth adapters, and to work completely offline.

If you have a Tuya Hub, you can use the official HA integration.

I'm using an HP T630 from ebay which is just a generic linux box. I don't have a RPi on hand, but it should also work as a generic linux box.

andypnz commented 1 year ago

Thanks for your help! I have registered with Tuya Smart Life + Tuya iot platform.... and successfully run tuya-local-key-extractor (I was able to do this within my HAOS using SSH + Web Terminal) - so I have the required keys, ID and MAC address etc

Which directory do I copy your fingerbot.py + util.py to? Are these the only files I need from your github repo?

What triggers fingerbot.py to run?

Thanks again for your help!

liqi0816 commented 1 year ago

If you just want to integrate with HA, and don't want to bother with details like "what triggers fingerbot.py to run", my suggestion would be:

  1. The HA bluetooth requirements for Linux systems still apply. For HAOS it means upgrading to version 9.0 or later.
  2. install MQTT addon. There are plenty of tutorial online, such as this one.
  3. SSH into HAOS, install the python dependencies listed in README.
  4. Clone the repository to any directory you like. It's a standalone program so it doesn't really matter.
  5. Write config.yaml, save it to anywhere
    homeassistant: true
    mqtt:
    base_topic: homeassistant
    server: mqtt://HAOS_IP_ADDRESS:1883
    user: SAME_AS_IN_HAOS_MQTT_ADDON
    password: SAME_AS_IN_HAOS_MQTT_ADDON
    controller:
    capacity: 6
    devices:
    '01:02:03:04:05:08':
      type: tuya.fingerbot
      device_id: abc123456
      uuid: tuya1234567890
      local_key: abc123456
      down_percent: 80
  6. cd into ble2mqtt, then python main.py --config [path-to-config.yaml]

On success it should print

bluetooth.Client.event.connect 01:02:03:04:05:08
initialized with 1 devices
(after a while; Tuya devices go to sleep very fast) bluetooth.Client.event.disconnect 01:02:03:04:05:08

And a button device should be auto discovered in HA.

liqi0816 commented 1 year ago

Maybe I should make a video tutorial when I get the chance :)

andypnz commented 1 year ago

Thanks!

I am using HAOS and I think perhaps things are not quite so straight forward? For example, 'import' is not allowed in a Python script.

Using SSH + Web terminal I first run the command docker exec -it homeassistant bash which gives access to the actual HA 'container'. I then have installed the dependencies, cloned the git repository, etc.

When I run python main.py --config /config/ble2mqtt/config.yaml I get this error as below. I think I need to use appdaemon or pyscript to run main.py - but I am really not sure.....

image

liqi0816 commented 1 year ago

It is running fingerbot.py as sexpect, but complaining about a bluetooth timeout.

There can be several reasons for a bluetooth timeout:

This is a plain old python program that does not really depends on HA or appdaemon or pyscript. You don't need to run it inside the HA container, and I don't know what can happen inside that container. The only thing matters is the MQTT configuration, i.e. base_topic/server/user/password.

andypnz commented 1 year ago

That command gave me this response: image Same response if I run it outside or inside the HA container (actually, I am not even sure if I am using the correct terminology here....)

I realized I did not have Bluetooth: In my configuration.yaml - so added that.... BTW, should I have 'passive scanning' ticked under the BT integration? Probably it does not effect this?

Now running python main.py --config /config/ble2mqtt/config.yaml results in: image

I think because I am running HAOS things may be a bit different - do you think I should try the command in the last post in this thread? https://community.home-assistant.io/t/rpi4-bluetooth-on-2022-8-1/447202/6 I am using a RPI 4 with no external BT module (although I hope it will eventually be able to use BT from ESP32's?). But really I am clutching at straws......

liqi0816 commented 1 year ago

We are getting closer!

Bluetooth is complaining about "No discovery started" so let's try start it manually.

Open two ssh sessions. In the first one, type:

bluetoothctl scan on

This should print all MAC addresses nearby.

Keep the first ssh session open, and then retry in the second one:

bluetoothctl connect [01:02:03:04:05:08 <- MAC of fingerbot]

On success it should print

Attempting to connect to 01:02:03:04:05:08
[CHG] Device 01:02:03:04:05:08 Connected: yes
Connection successful
andypnz commented 1 year ago

bluetoothctl scan on image

Then in a second ssh session: image (same result as previous)

In the first ssh terminal, if I just type bluetoothctl it says AdvertisementMonitor Path Registered and I get a long rolling list of MAC addresses (including the fingerbot) with RSSI values, etc..... If I then type scan on it says Discovery Started and the list contines in much the same way....

image

If I then type connect [01:02:03:04:05:08 <- MAC of fingerbot] it connects Connection Successful

It looks like I need to use bluetoothctl as a separate command from connect or scan on etc..... (and it looks like my two ssh terminals are into different containers? Because bluetoothctl in one did not allow scan on in the other..... but I am very much guessing here!)

andypnz commented 1 year ago

Hi.... do you think there is anything else I can try here? Thank you.......

andypnz commented 1 year ago

BTW, I found this and it is working :-) https://github.com/PlusPlus-ua/ha_tuya_ble

Thanks for getting me looking at this :-)

liqi0816 commented 1 year ago

While I still don't know why my program doesn't work with your setup (sad), I'm glad you find another solution! :)

andypnz commented 1 year ago

Happy to keep trying your solution if you have any other ideas?