Skip to content

Commit f24d39d

Browse files
authored
fix, set event loop correctly when deferring actions (#74)
1 parent b6f629d commit f24d39d

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

custom_components/intesisbox/intesisbox.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@
3838
background_tasks = set()
3939

4040

41-
def ensure_background_task(coro):
41+
def clean_background_task(task):
42+
"""Handle background task completion."""
43+
background_tasks.discard(task)
44+
_ = task.result() # to propagate exceptions
45+
46+
47+
def ensure_background_task(coro, loop):
4248
"""Ensure background task is running."""
43-
task = asyncio.ensure_future(coro)
49+
task = asyncio.ensure_future(coro, loop=loop)
4450
background_tasks.add(task)
45-
task.add_done_callback(background_tasks.discard)
51+
task.add_done_callback(clean_background_task)
4652
return task
4753

4854

@@ -78,7 +84,7 @@ def connection_made(self, transport: asyncio.BaseTransport):
7884
"""Asyncio callback for a successful connection."""
7985
_LOGGER.debug("Connected to IntesisBox")
8086
self._transport = transport
81-
ensure_background_task(self.query_initial_state())
87+
ensure_background_task(self.query_initial_state(), self._eventLoop)
8288

8389
async def keep_alive(self):
8490
"""Send a keepalive command to reset it's watchdog timer."""
@@ -137,8 +143,8 @@ def data_received(self, data):
137143
if cmd == "ID":
138144
self._parse_id_received(args)
139145
self._connectionStatus = API_AUTHENTICATED
140-
ensure_background_task(self.poll_status())
141-
ensure_background_task(self.poll_ambtemp())
146+
ensure_background_task(self.poll_status(), self._eventLoop)
147+
ensure_background_task(self.poll_ambtemp(), self._eventLoop)
142148
elif cmd == "CHN,1":
143149
self._parse_change_received(args)
144150
statusChanged = True
@@ -225,7 +231,7 @@ def connect(self):
225231
_LOGGER.debug(
226232
"Opening connection to IntesisBox %s:%s", self._ip, self._port
227233
)
228-
ensure_background_task(coro)
234+
ensure_background_task(coro, self._eventLoop)
229235
else:
230236
_LOGGER.debug("Missing IP address or port.")
231237
self._connectionStatus = API_DISCONNECTED

0 commit comments

Comments
 (0)