Skip to content

Commit d01089f

Browse files
committed
Python3.12 compat tweaks, simplified scan() result handling.
Updated pyproject config with likely repo location.
1 parent 78163d8 commit d01089f

5 files changed

Lines changed: 18 additions & 14 deletions

File tree

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "powersensor-local"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
description = "Network-local (non-cloud) interface for Powersensor devices"
55
authors = [
66
{ name = "Jade Mattsson", email = "jmattsson@dius.com.au" },
@@ -16,9 +16,9 @@ classifiers = [
1616
]
1717

1818
[project.urls]
19-
Repository = "https://github.com/DiUS/powersensor-local.git"
20-
Homepage = "https://github.com/DiUS/powersensor-local"
21-
Issues = "https://github.com/DiUS/powersensor-local/issues"
19+
Repository = "https://github.com/DiUS/python-powersensor_local.git"
20+
Homepage = "https://github.com/DiUS/python-powersensor_local"
21+
Issues = "https://github.com/DiUS/python-powersensor_local/issues"
2222

2323
[project.scripts]
2424
ps-events = "powersensor_local.events:app"

src/powersensor_local/devices.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ async def yourcallback(event: dict)
126126
}
127127
"""
128128
self._event_cb = async_event_cb
129-
await self._ps.scan(self._on_scanned)
129+
await self._on_scanned(await self._ps.scan())
130130
self._timer = self._Timer(EXPIRY_CHECK_INTERVAL_S, self._on_timer)
131131

132132
async def rescan(self):
133133
"""Performs a fresh scan of the network to discover added devices,
134134
or devices which have changed their IP address for some reason."""
135-
await self._ps.scan(self._on_scanned)
135+
await self._on_scanned(await self._ps.scan())
136136

137137
async def stop(self):
138138
"""Stops the event streaming and disconnects from the devices.
@@ -165,7 +165,7 @@ async def _on_scanned(self, ips):
165165
}
166166
await self._event_cb(ev)
167167

168-
asyncio.create_task(self._ps.subscribe(self._on_msg))
168+
await self._ps.subscribe(self._on_msg)
169169

170170
async def _on_msg(self, obj):
171171
mac = obj.get('mac')
@@ -303,10 +303,11 @@ def __init__(self, interval_s, callback):
303303
self._terminate = False
304304
self._interval = interval_s
305305
self._callback = callback
306-
asyncio.create_task(self._run())
306+
self._task = asyncio.create_task(self._run())
307307

308308
def terminate(self):
309309
self._terminate = True
310+
self._task.cancel()
310311

311312
async def _run(self):
312313
while not self._terminate:

src/powersensor_local/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def handle_sigint(signum, frame):
5252
await asyncio.sleep(1)
5353

5454
def app():
55-
loop = asyncio.get_event_loop()
55+
loop = asyncio.new_event_loop()
5656
loop.run_until_complete(main())
5757
loop.stop()
5858

src/powersensor_local/listener.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ def __init__(self, bcast_addr='<broadcast>'):
1818
self._exiting = False
1919
self._bcast = bcast_addr
2020

21-
async def scan(self, completion_callback):
22-
"""Scans the local network for discoverable devices with a timeout."""
21+
async def scan(self):
22+
"""Scans the local network for discoverable devices with a timeout.
23+
Returns the list of IP addresses of the discovered gateways (plugs).
24+
"""
2325
self._known_addresses.clear()
2426
loop = asyncio.get_running_loop()
2527
transport, _ = await loop.create_datagram_endpoint(
@@ -37,7 +39,7 @@ async def scan(self, completion_callback):
3739
timeout -= 0.5
3840

3941
transport.close()
40-
await completion_callback(set(self._known_addresses.keys()))
42+
return self._known_addresses.keys()
4143

4244
def protocol_factory(self):
4345
return self

src/powersensor_local/rawfirehose.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ def handle_sigint(signum, frame):
4444
signal.signal(signal.SIGINT, handle_sigint)
4545

4646
# Scan for devices and subscribe upon completion
47-
await ps.scan(lambda ips: asyncio.create_task(ps.subscribe(on_msg)))
47+
await ps.scan()
48+
await ps.subscribe(on_msg)
4849

4950
# Keep the event loop running until Ctrl+C is pressed
5051
while not exiting:
5152
await asyncio.sleep(1)
5253

5354
def app():
54-
loop = asyncio.get_event_loop()
55+
loop = asyncio.new_event_loop()
5556
loop.run_until_complete(main())
5657
loop.stop()
5758

0 commit comments

Comments
 (0)