Files
kano-wand-smarthome/kano-wand-2/shop.py
T
Lucas Oskorep 5df2dd6b47 Refactoring Code to mesh with the architecture set by https://github.com/GammaGames/kano_wand
Overall - moving from bluez to bleak will give us universal usage, and the rest of the ML side of this project can go on without a hitch.
2020-02-11 16:29:03 -06:00

98 lines
3.5 KiB
Python

from bleak import discover
from wand import Wand
class Shop(object):
"""A scanner class to connect to wands
"""
def __init__(self, wand_class=Wand, debug=False):
"""Create a new scanner
Keyword Arguments:
wand_class {class} -- Class to use when connecting to wand (default: {Wand})
debug {bool} -- Print debug messages (default: {False})
"""
self.wand_class = wand_class
self.debug = debug
self._name = None
self._prefix = None
self._mac = None
async def scan(self, name=None, prefix="Kano-Wand", mac=None, timeout=2.0, connect=False):
"""Scan for devices
Keyword Arguments:
name {str} -- Name of the device to scan for (default: {None})
prefix {str} -- Prefix of name of device to scan for (default: {"Kano-Wand"})
mac {str} -- MAC Address of the device to scan for (default: {None})
timeout {float} -- Timeout before returning from scan (default: {1.0})
connect {bool} -- Connect to the wands automatically (default: {False})
Returns {Wand[]} -- Array of wand objects
"""
if self.debug:
print("Scanning for {} seconds...".format(timeout))
try:
name_check = not (name is None)
prefix_check = not (prefix is None)
mac_check = not (mac is None)
assert name_check or prefix_check or mac_check
except AssertionError as e:
print("Either a name, prefix, or mac address must be provided to find a wand")
raise e
if name is not None:
self._name = name
elif prefix is not None:
self._prefix = prefix
elif mac is not None:
self._mac = mac
self.wands = []
devices = await discover(timeout= timeout)
#TODO: Add in wand filtering.
# if isNewDev:
# # Perform initial detection attempt
# mode = 0
# found = 0
# name = device.getValueText(9)
# if self._name is not None:
# mode += 1
# if name == self._name:
# found += 1
# if self._prefix is not None:
# mode += 1
# if name is not None and name.startswith(self._prefix):
# found += 1
# if self._mac is not None:
# mode += 1
# if device.addr == self._mac:
# found += 1
#
# if found >= mode:
# self.wands.append(self.wand_class(device, debug=self.debug))
# elif self.debug:
# if name != "None":
# print("Mac: {}\tCommon Name: {}".format(device.addr, name))
# else:
# print("Mac: {}".format(device.addr))
print(devices)
devices = list(filter(lambda x : x.name.startswith("kano"), devices))
print(devices)
self.wands = [Wand(d.address, d.name) for d in devices]
# for d in devices:
# #Convert to device making here.
# print(d)
# print(d.name, d.address)
print(self.wands)
#TODO: Process device list here for the shop - > not all BLE devices are necessary
# client = BleakClient(self.wand_address, loop=self.loop)
if connect:
for wand in self.wands:
wand.connect()
return self.wands