i hacked together a worker for bt-mqtt-gateway that collects the info that this script collects from my oral-b-toothbrushes. i hope to file a pull request there soon. this is the bt-mqtt-gateway-worker that works fine for me so far. needs a bit of polishing …
(@molobrakos implementation got me into dependency hell which i couldn’t solve on raspbian.)
import time
from interruptingcow import timeout
from bluepy.btle import Scanner, DefaultDelegate
from mqtt import MqttMessage
from workers.base import BaseWorker
from logger import _LOGGER
import sys
from datetime import datetime
import codecs
REQUIREMENTS = ['bluepy']
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
_LOGGER.debug("Discovered new device: %s" % dev.addr)
class ToothbrushWorker(BaseWorker):
def searchmac(self, devices, mac):
for dev in devices:
if dev.addr == mac.lower():
return dev
return None
def status_update(self):
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(5.0)
ret = []
for name, mac in self.devices.items():
device = self.searchmac(devices, mac)
if device is None:
ret.append(MqttMessage(topic=self.format_topic(name+'/presence'), payload="0"))
else:
ret.append(MqttMessage(topic=self.format_topic(name+'/presence/rssi'), payload=device.rssi))
ret.append(MqttMessage(topic=self.format_topic(name+'/presence'), payload="1"))
_LOGGER.debug("text: %s" % device.getValueText(255) )
bytes_ = bytearray(bytes.fromhex(device.getValueText(255)))
ret.append(MqttMessage(topic=self.format_topic(name+'/running'), payload=bytes_[5] ))
ret.append(MqttMessage(topic=self.format_topic(name+'/pressure'), payload=bytes_[6] ))
ret.append(MqttMessage(topic=self.format_topic(name+'/time'), payload=bytes_[7]*60 + bytes_[8] ))
ret.append(MqttMessage(topic=self.format_topic(name+'/mode'), payload=bytes_[9] ))
ret.append(MqttMessage(topic=self.format_topic(name+'/quadrant'), payload=bytes_[10] ))
return ret
configuration:
toothbrush:
args:
devices:
ix: 5c:f8:xx:xx:xx:xx
ia: 2C:6B:xx:xx:xx:xx
topic_prefix: toothbrush
update_interval: 10