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:
Elvio Sebastianelli
2019-01-11 22:02:49 +01:00
committed by zewelor
parent 2e27c9a7e7
commit 345b200113
4 changed files with 50 additions and 0 deletions
+1
View File
@@ -1,3 +1,4 @@
config.yaml
__pycache__/
.idea/
.vscode/
+3
View File
@@ -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
+7
View File
@@ -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
+39
View File
@@ -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