@@ -47,7 +47,6 @@ def __init__(
47
47
sensor_debounce_period : int ,
48
48
light_transition_period : float ,
49
49
light_notification_period : float ,
50
- loop : ty .Optional [aio .AbstractEventLoop ] = None ,
51
50
) -> None :
52
51
self .dev_id = device_id
53
52
self ._topic_root = topic_root
@@ -71,11 +70,9 @@ def __init__(
71
70
self ._light_transition_period = light_transition_period
72
71
self ._light_notification_period = light_notification_period
73
72
self ._light_last_sent = None
74
-
75
73
self ._reconnection_interval = reconnection_interval
76
- self ._loop = loop or aio .get_event_loop ()
77
74
self ._client = aio_mqtt .Client (
78
- loop = self . _loop ,
75
+ loop = aio . get_running_loop () ,
79
76
client_id_prefix = 'lumimqtt_' ,
80
77
)
81
78
self ._tasks : ty .List [aio .Future ] = []
@@ -89,33 +86,19 @@ def __init__(
89
86
90
87
async def start (self ):
91
88
self ._tasks = [
92
- self . _loop .create_task (self ._connect_forever ()),
93
- self . _loop .create_task (self ._handle_messages ()),
94
- self . _loop .create_task (self ._periodic_publish ()),
95
- self . _loop .create_task (self ._handle_buttons ()),
89
+ aio .create_task (self ._connect_forever ()),
90
+ aio .create_task (self ._handle_messages ()),
91
+ aio .create_task (self ._periodic_publish ()),
92
+ aio .create_task (self ._handle_buttons ()),
96
93
]
97
- finished , unfinished = await aio .wait (
94
+ finished , _ = await aio .wait (
98
95
self ._tasks ,
99
96
return_when = aio .FIRST_COMPLETED ,
100
97
)
101
- for t in unfinished :
102
- t .cancel ()
103
- try :
104
- await t
105
- except aio .CancelledError :
106
- pass
107
98
for t in finished :
108
99
t .result ()
109
100
110
101
async def close (self ) -> None :
111
- for task in self ._tasks :
112
- if task .done ():
113
- continue
114
- task .cancel ()
115
- try :
116
- await task
117
- except aio .CancelledError :
118
- pass
119
102
if self ._client .is_connected ():
120
103
await self ._client .disconnect ()
121
104
@@ -208,7 +191,7 @@ async def _handle_messages(self) -> None:
208
191
except ValueError as e :
209
192
logger .exception (str (e ))
210
193
continue
211
- new_light_task = self . _loop .create_task (self ._light_handler (light , value ))
194
+ new_light_task = aio .create_task (self ._light_handler (light , value ))
212
195
running_message_tasks .append (new_light_task )
213
196
continue
214
197
command : ty .Optional [Command ] = None
@@ -222,7 +205,7 @@ async def _handle_messages(self) -> None:
222
205
value = json .loads (message .payload )
223
206
except ValueError :
224
207
value = message .payload .decode ()
225
- new_command_task = self . _loop .create_task (self ._command_handler (command , value ))
208
+ new_command_task = aio .create_task (self ._command_handler (command , value ))
226
209
running_message_tasks .append (new_command_task )
227
210
except aio .CancelledError :
228
211
for task in running_message_tasks :
0 commit comments