Skip to content

Commit

Permalink
Search and remove missing bulbs
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Apr 6, 2019
1 parent 57b525a commit 4a9120e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion yeelib/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,23 @@ def connection_lost(self, exc):

async def _restart():
await asyncio.sleep(10)
await search_bulbs(self.bulb_class, self.loop)
await search_bulbs(self.bulb_class, self.loop, kwargs=self.bulb_class)

asyncio.Task(_restart())


async def remove_missing_bulbs(timeout=60):
while True:
missing_bulbs = (
bulb.id
for bulb in bulbs
if bulb.last_seen < time.time() - timeout
)
for idx in missing_bulbs:
del bulbs[idx]
await asyncio.sleep(timeout/2)


async def search_bulbs(bulb_class=Bulb, loop=None, kwargs=None):
if loop is None:
loop = asyncio.get_event_loop()
Expand All @@ -119,4 +131,5 @@ async def search_bulbs(bulb_class=Bulb, loop=None, kwargs=None):
unicast_connection = loop.create_datagram_endpoint(
lambda: YeelightProtocol(bulb_class, **kwargs), family=socket.AF_INET)
ucast_transport, _ = await unicast_connection
loop.Task(remove_missing_bulbs())
return bulbs

0 comments on commit 4a9120e

Please sign in to comment.