New worker: ble devices scanner (#46)
* Worker for ble devices scanning
Added a worker that scan for ble devices and notify, by mqtt, presence of mac addresses and rssi.
Configuration section example:
....
workers:
blescanmulti:
args:
devices:
nut: xx:xx:xx:xx:xx:xx
ionic: xx:xx:xx:xx:xx:xx
topic_prefix: blescan
update_interval: 60
...
* Update README.md
blescanmulti worker added
* Update config.yaml.example
Added configuration section for blescanmulti worker
* Converted blescanmulti.py indentation to 2 spaces
* changed mqtt message syntax
* Update README.md
Added contributor repos url
* Update .gitignore
Removed .vscode/
* Delete settings.json
* Changes by pull request review 11/01/2019
On branch master
modified: workers/blescanmulti.py
This commit is contained in:
committed by
zewelor
parent
2e27c9a7e7
commit
345b200113
@@ -1,3 +1,4 @@
|
||||
config.yaml
|
||||
__pycache__/
|
||||
.idea/
|
||||
.vscode/
|
||||
|
||||
@@ -21,6 +21,7 @@ See [Wiki](https://github.com/zewelor/bt-mqtt-gateway/wiki) for more information
|
||||
* [MySensors](https://www.mysensors.org/)
|
||||
* [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)
|
||||
|
||||
## Getting Started
|
||||
|
||||
@@ -104,6 +105,8 @@ You need to define the absolute path of `gateway.py` in `bt-mqtt-gateway.service
|
||||
|
||||
* [**zewelor**](https://github.com/zewelor) - *Initial work*
|
||||
* [**bbbenji**](https://github.com/bbbenji) - *Minor contributions*
|
||||
* [**elviosebastianelli**](https://github.com/elviosebastianelli) - *BLEscanmulti*
|
||||
* [**jumping2000**](https://github.com/jumping2000) - *BLEscan*
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -48,3 +48,10 @@ manager:
|
||||
living_room: 00:11:22:33:44:55
|
||||
topic_prefix: mithermometer
|
||||
update_interval: 300
|
||||
blescanmulti:
|
||||
args:
|
||||
devices:
|
||||
beacon: 00:11:22:33:44:55
|
||||
smartwath: 00:11:22:33:44:55
|
||||
topic_prefix: blescan
|
||||
update_interval: 60
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
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 BlescanmultiWorker(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(10.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('presence/'+name), payload="0"))
|
||||
else:
|
||||
ret.append(MqttMessage(topic=self.format_topic('presence/'+name+'/rssi'), payload=device.rssi))
|
||||
ret.append(MqttMessage(topic=self.format_topic('presence/'+name), payload="1"))
|
||||
|
||||
return ret
|
||||
Reference in New Issue
Block a user