|
38 | 38 | background_tasks = set() |
39 | 39 |
|
40 | 40 |
|
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): |
42 | 48 | """Ensure background task is running.""" |
43 | | - task = asyncio.ensure_future(coro) |
| 49 | + task = asyncio.ensure_future(coro, loop=loop) |
44 | 50 | background_tasks.add(task) |
45 | | - task.add_done_callback(background_tasks.discard) |
| 51 | + task.add_done_callback(clean_background_task) |
46 | 52 | return task |
47 | 53 |
|
48 | 54 |
|
@@ -78,7 +84,7 @@ def connection_made(self, transport: asyncio.BaseTransport): |
78 | 84 | """Asyncio callback for a successful connection.""" |
79 | 85 | _LOGGER.debug("Connected to IntesisBox") |
80 | 86 | self._transport = transport |
81 | | - ensure_background_task(self.query_initial_state()) |
| 87 | + ensure_background_task(self.query_initial_state(), self._eventLoop) |
82 | 88 |
|
83 | 89 | async def keep_alive(self): |
84 | 90 | """Send a keepalive command to reset it's watchdog timer.""" |
@@ -137,8 +143,8 @@ def data_received(self, data): |
137 | 143 | if cmd == "ID": |
138 | 144 | self._parse_id_received(args) |
139 | 145 | 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) |
142 | 148 | elif cmd == "CHN,1": |
143 | 149 | self._parse_change_received(args) |
144 | 150 | statusChanged = True |
@@ -225,7 +231,7 @@ def connect(self): |
225 | 231 | _LOGGER.debug( |
226 | 232 | "Opening connection to IntesisBox %s:%s", self._ip, self._port |
227 | 233 | ) |
228 | | - ensure_background_task(coro) |
| 234 | + ensure_background_task(coro, self._eventLoop) |
229 | 235 | else: |
230 | 236 | _LOGGER.debug("Missing IP address or port.") |
231 | 237 | self._connectionStatus = API_DISCONNECTED |
|
0 commit comments