Skip to content

Commit 96a242f

Browse files
authored
Add function to get current status (awaits response) (#5)
1 parent 1f07702 commit 96a242f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

letpot/deviceclient.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class LetPotDeviceClient:
4949

5050
_update_status: LetPotDeviceStatus | None = None
5151
_update_clear: asyncio.Task | None = None
52+
_status_event: asyncio.Event | None = None
5253
last_status: LetPotDeviceStatus | None = None
5354

5455
def __init__(self, info: AuthenticationInfo, device_serial: str) -> None:
@@ -118,6 +119,8 @@ def _handle_message(
118119
self._update_status = None
119120
self.last_status = status
120121
callback(status)
122+
if self._status_event is not None and not self._status_event.is_set():
123+
self._status_event.set()
121124

122125
async def _publish(self, message: list[int]) -> None:
123126
"""Publish a message to the device command topic."""
@@ -246,6 +249,13 @@ async def request_status_update(self) -> None:
246249
raise LetPotException("Missing converter to build request message")
247250
await self._publish(self._converter.get_current_status_message())
248251

252+
async def get_current_status(self) -> LetPotDeviceStatus | None:
253+
"""Request an update of and return the current device status."""
254+
self._status_event = asyncio.Event()
255+
await self.request_status_update()
256+
await self._status_event.wait()
257+
return self.last_status
258+
249259
async def set_light_brightness(self, level: int) -> None:
250260
"""Set the light brightness for this device (brightness level)."""
251261
if level not in self.get_light_brightness_levels():

0 commit comments

Comments
 (0)