Skip to content

Instantly share code, notes, and snippets.

@bricewge
Last active December 18, 2023 18:12
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bricewge/a38bd4223e407967f6ab78842c3df07e to your computer and use it in GitHub Desktop.
Save bricewge/a38bd4223e407967f6ab78842c3df07e to your computer and use it in GitHub Desktop.
Use iTag Bluetooth BLE tracker as a light switch with Home Assistant
from bluepy import btle
import requests
bleAddr = "ff:ff:10:05:ef:ff"
url = "http://localhost:8123/api/services/light/toggle"
lamp = "light.mylight"
hassPasswd = "password"
class LightToggleDelegate(btle.DefaultDelegate):
def __init__(self):
btle.DefaultDelegate.__init__(self)
def handleNotification(slef, cHandle, data):
requests.post(url, json = {"entity_id": lamp}, headers = {"x-ha-access": hassPasswd, "Content-Type": "application/json"})
print("Lights toggled")
dev = btle.Peripheral()
dev.connect(bleAddr)
print("Connected to:", bleAddr)
dev.setDelegate(LightToggleDelegate())
while True:
dev.waitForNotifications(10)
dev.disconnect()
print("Disconnected...")
bluepy==1.1.4
certifi==2018.1.18
chardet==3.0.4
idna==2.6
requests==2.18.4
urllib3==1.22
#!/usr/bin/env sh
apk update
apk add vim py3-virtualenv glib glib-dev gcc make musl-dev
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment