Skip to content

Commit 4a9120e

Browse files
committed
Search and remove missing bulbs
1 parent 57b525a commit 4a9120e

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

yeelib/discover.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,23 @@ def connection_lost(self, exc):
106106

107107
async def _restart():
108108
await asyncio.sleep(10)
109-
await search_bulbs(self.bulb_class, self.loop)
109+
await search_bulbs(self.bulb_class, self.loop, kwargs=self.bulb_class)
110110

111111
asyncio.Task(_restart())
112112

113113

114+
async def remove_missing_bulbs(timeout=60):
115+
while True:
116+
missing_bulbs = (
117+
bulb.id
118+
for bulb in bulbs
119+
if bulb.last_seen < time.time() - timeout
120+
)
121+
for idx in missing_bulbs:
122+
del bulbs[idx]
123+
await asyncio.sleep(timeout/2)
124+
125+
114126
async def search_bulbs(bulb_class=Bulb, loop=None, kwargs=None):
115127
if loop is None:
116128
loop = asyncio.get_event_loop()
@@ -119,4 +131,5 @@ async def search_bulbs(bulb_class=Bulb, loop=None, kwargs=None):
119131
unicast_connection = loop.create_datagram_endpoint(
120132
lambda: YeelightProtocol(bulb_class, **kwargs), family=socket.AF_INET)
121133
ucast_transport, _ = await unicast_connection
134+
loop.Task(remove_missing_bulbs())
122135
return bulbs

0 commit comments

Comments
 (0)