From 0fbbc5099048caa8d10e2450be7a1eb8df495f0e Mon Sep 17 00:00:00 2001 From: felix schwenzel Date: Fri, 1 Mar 2019 11:07:37 +0100 Subject: [PATCH] Add worker for Oral-B connected toothbrush (#55) * create toothbrush.py add oral-b connected toothbrushs (based on https://github.com/rfaelens/domotica/blob/master/mqtt-toothbrush.py, see https://community.home-assistant.io/t/oral-b-bluetooth-connected-toothbrush/) * add configuration example for oral-b toothbrush * Update README.md --- README.md | 1 + config.yaml.example | 7 +++++++ workers/toothbrush.py | 46 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 workers/toothbrush.py diff --git a/README.md b/README.md index ad9339e..cbae0ee 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ See [Wiki](https://github.com/zewelor/bt-mqtt-gateway/wiki) for more information * [Xiaomi Mi Flora plant sensor](https://xiaomi-mi.com/sockets-and-sensors/xiaomi-huahuacaocao-flower-care-smart-monitor/) via [miflora](https://github.com/open-homeautomation/miflora) * Xiaomi Aqara thermometer via [mithermometer](https://github.com/hobbypunk90/mithermometer) * Bluetooth Low Power devices (BLE) +* Oral-B connected toothbrushs ## Getting Started diff --git a/config.yaml.example b/config.yaml.example index 4a288d0..50b2585 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -55,3 +55,10 @@ manager: smartwath: 00:11:22:33:44:55 topic_prefix: blescan update_interval: 60 + toothbrush: + args: + devices: + ix: 00:11:22:33:44:55 + ia: 11:22:33:44:55:66 + topic_prefix: toothbrush + update_interval: 10 diff --git a/workers/toothbrush.py b/workers/toothbrush.py new file mode 100644 index 0000000..00f13df --- /dev/null +++ b/workers/toothbrush.py @@ -0,0 +1,46 @@ +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 + +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