Skip to content

Commit 12f0be4

Browse files
Use whitelisting instead of blacklisting
1 parent bd9805a commit 12f0be4

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

flowercare_exporter/main.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,44 @@ async def export_metrics(miflora: MiFlora):
5252
async with asyncio.timeout(args.timeout):
5353
async for miflora in mifloramanager.scan_mifloras():
5454
log.debug(f"Added {miflora}")
55-
print(miflora)
5655
if miflora.address in metrics_received:
5756
log.info(f"Not connecting to {miflora.address} (metric already collected)")
58-
elif miflora.address in args.ignore:
59-
log.info(f"Ignoring {miflora}")
57+
elif args.only_alias and miflora.address not in mifloramanager.alias_mapping:
58+
log.info(f"Not connecting to {miflora.address} (only connecting to aliased devices)")
6059
elif await miflora.connect():
6160
while not miflora._services_discovered:
6261
await asyncio.sleep(1) # FIXME: Polling, use Condition variable?
6362
log.debug("Waiting for services")
6463
log.debug("Service discovered")
6564
await export_metrics(miflora)
65+
if args.only_alias and len(metrics_received) == len(mifloramanager.alias_mapping):
66+
log.info("All metrics received")
67+
return
6668
except TimeoutError:
67-
print(f"Received data from {len(metrics_received)} MiFloras!")
69+
log.info(f"Received data from {len(metrics_received)} MiFloras!")
6870

6971

7072
async def blink(args: argparse.Namespace):
7173
blinked: set[str] = set() # set of macs
72-
alias_mapping = _get_alias_mapping(args)
7374
mifloramanager = MiFloraManager(_get_alias_mapping(args))
7475
await mifloramanager.setup_adapter()
7576
try:
7677
async with asyncio.timeout(args.timeout):
7778
async for miflora in mifloramanager.scan_mifloras():
7879
if miflora.address in blinked:
7980
log.debug(f"Already blinked {miflora}")
80-
elif miflora.address in args.ignore:
81-
log.info(f"Ignoring {miflora}")
81+
elif args.only_alias and miflora.address not in mifloramanager.alias_mapping:
82+
log.info(f"Not connecting to {miflora.address} (only connecting to aliased devices)")
8283
elif await miflora.connect():
8384
while not miflora._services_discovered:
8485
await asyncio.sleep(1) # FIXME: Polling, use Condition variable?
8586
log.debug(f"Waiting for services on {miflora}")
8687
await miflora.blink()
8788
log.debug(f"Blinked {miflora}")
8889
blinked.add(miflora.address)
90+
if args.only_alias and len(blinked) == len(mifloramanager.alias_mapping):
91+
log.info("All devices blinked")
92+
return
8993
except TimeoutError:
9094
print(f"Blinked {len(blinked)} MiFloras!")
9195

@@ -131,6 +135,14 @@ def main():
131135
help="Ignore MAC address matching this string. Can be repeated ",
132136
)
133137
parser.add_argument("-t", "--timeout", type=int, default=60, help="Scan timeout in seconds")
138+
parser.add_argument(
139+
"-o",
140+
"--only-alias",
141+
type=bool,
142+
default=False,
143+
help="Only connect to aliases devices (if not provided, set to False)",
144+
)
145+
134146
subparsers = parser.add_subparsers(
135147
title="subcommands", required=True, dest="command", description="valid subcommands", help="Operation mode"
136148
)

0 commit comments

Comments
 (0)