Skip to content

Commit

Permalink
add a prohibition to combine scan filters of the same type #25
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b committed Dec 15, 2021
1 parent 14bafc2 commit b66defe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions able/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@

@dataclass
class Filter:
"""Base class for BLE scanning fiters.
>>> # Filters of different kinds could be ANDed to set multiple conditions.
>>> # Both device name and address required:
>>> combined_filter = DeviceNameFilter("Example") & DeviceAddressFilter("01:02:03:AB:CD:EF")
>>> DeviceNameFilter("Example1") & DeviceNameFilter("Example2")
Traceback (most recent call last):
ValueError: cannot combine filters of the same type
"""

def __post_init__(self):
self.filters = [self]

def __and__(self, other):
if type(self) in (type(f) for f in other.filters):
raise ValueError('cannot combine filters of the same type')
self.filters.extend(other.filters)
return self

Expand Down
8 changes: 8 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ def test_filters_combined(java_builder):
("setManufacturerData", ("test-id", [1, 2, 3])),
("build", )
]


def test_combine_same_type_exception(java_builder):
with pytest.raises(ValueError, match="cannot combine filters of the same type"):
f = filters.DeviceNameFilter("test") & (
filters.DeviceAddressFilter("AA:AA:AA:AA:AA:AA") &
filters.DeviceNameFilter("test2")
)

0 comments on commit b66defe

Please sign in to comment.