Skip to content

Commit 6467a7c

Browse files
committed
Start and stop bcast msg task per transport
1 parent ed1d357 commit 6467a7c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

yeelib/discover.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __init__(self, bulb_class=Bulb, loop=None):
4545
self.loop = loop or asyncio.get_event_loop()
4646

4747
def connection_made(self, transport):
48+
self.transport = transport
4849
ucast_socket = transport.get_extra_info('socket')
4950
try:
5051
ucast_socket.bind(('', MCAST_PORT))
@@ -57,6 +58,8 @@ def connection_made(self, transport):
5758
except socket.error as e:
5859
ucast_socket.close()
5960
self.connection_lost(exc=e)
61+
else:
62+
self.bcast_msg_task = asyncio.Task(send_search_broadcast(transport))
6063

6164
@classmethod
6265
def header_to_kwargs(cls, headers):
@@ -97,6 +100,8 @@ def register_bulb(self, **kwargs):
97100

98101
def connection_lost(self, exc):
99102
logger.exception("connection error")
103+
self.bcast_msg_task.cancel()
104+
self.transport.close()
100105

101106
async def _restart():
102107
await asyncio.sleep(10)
@@ -111,5 +116,4 @@ async def search_bulbs(bulb_class=Bulb, loop=None):
111116
unicast_connection = loop.create_datagram_endpoint(
112117
lambda: YeelightProtocol(bulb_class), family=socket.AF_INET)
113118
ucast_transport, _ = await unicast_connection
114-
loop.create_task(send_search_broadcast(ucast_transport))
115119
return bulbs

0 commit comments

Comments
 (0)