Skip to content

Commit 1727443

Browse files
committed
Sed: remove unneeded code, implement suggested lock-fixes
1 parent 94d8774 commit 1727443

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

plugwise_usb/nodes/sed.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,10 @@ async def set_awake_duration(self, seconds: int) -> bool:
236236
raise ValueError(
237237
f"Invalid awake duration ({seconds}). It must be between 1 and 255 seconds."
238238
)
239+
239240
if self._battery_config.awake_duration == seconds:
240-
self._new_battery_config = replace(
241-
self._new_battery_config, awake_duration=seconds
242-
)
243-
return False
241+
return False
242+
244243
self._new_battery_config = replace(
245244
self._new_battery_config, awake_duration=seconds
246245
)
@@ -251,6 +250,7 @@ async def set_awake_duration(self, seconds: int) -> bool:
251250
"set_awake_duration | Device %s | config scheduled",
252251
self.name,
253252
)
253+
254254
return True
255255

256256
@raise_not_loaded
@@ -266,11 +266,10 @@ async def set_clock_interval(self, minutes: int) -> bool:
266266
raise ValueError(
267267
f"Invalid clock interval ({minutes}). It must be between 1 and 65535 minutes."
268268
)
269+
269270
if self.battery_config.clock_interval == minutes:
270-
self._new_battery_config = replace(
271-
self._new_battery_config, clock_interval=minutes
272-
)
273271
return False
272+
274273
self._new_battery_config = replace(
275274
self._new_battery_config, clock_interval=minutes
276275
)
@@ -281,6 +280,7 @@ async def set_clock_interval(self, minutes: int) -> bool:
281280
"set_clock_interval | Device %s | config scheduled",
282281
self.name,
283282
)
283+
284284
return True
285285

286286
@raise_not_loaded
@@ -293,10 +293,8 @@ async def set_clock_sync(self, sync: bool) -> bool:
293293
sync,
294294
)
295295
if self._battery_config.clock_sync == sync:
296-
self._new_battery_config = replace(
297-
self._new_battery_config, clock_sync=sync
298-
)
299296
return False
297+
300298
self._new_battery_config = replace(self._new_battery_config, clock_sync=sync)
301299
if not self._sed_config_task_scheduled:
302300
await self.schedule_task_when_awake(self._configure_sed_task())
@@ -305,6 +303,7 @@ async def set_clock_sync(self, sync: bool) -> bool:
305303
"set_clock_sync | Device %s | config scheduled",
306304
self.name,
307305
)
306+
308307
return True
309308

310309
@raise_not_loaded
@@ -320,11 +319,10 @@ async def set_maintenance_interval(self, minutes: int) -> bool:
320319
raise ValueError(
321320
f"Invalid maintenance interval ({minutes}). It must be between 1 and 1440 minutes."
322321
)
322+
323323
if self.battery_config.maintenance_interval == minutes:
324-
self._new_battery_config = replace(
325-
self._new_battery_config, maintenance_interval=minutes
326-
)
327324
return False
325+
328326
self._new_battery_config = replace(
329327
self._new_battery_config, maintenance_interval=minutes
330328
)
@@ -335,6 +333,7 @@ async def set_maintenance_interval(self, minutes: int) -> bool:
335333
"set_maintenance_interval | Device %s | config scheduled",
336334
self.name,
337335
)
336+
338337
return True
339338

340339
@raise_not_loaded
@@ -353,11 +352,10 @@ async def set_sleep_duration(self, minutes: int) -> bool:
353352
raise ValueError(
354353
f"Invalid sleep duration ({minutes}). It must be between 1 and 65535 minutes."
355354
)
355+
356356
if self._battery_config.sleep_duration == minutes:
357-
self._new_battery_config = replace(
358-
self._new_battery_config, sleep_duration=minutes
359-
)
360357
return False
358+
361359
self._new_battery_config = replace(
362360
self._new_battery_config, sleep_duration=minutes
363361
)
@@ -368,6 +366,7 @@ async def set_sleep_duration(self, minutes: int) -> bool:
368366
"set_sleep_duration | Device %s | config scheduled",
369367
self.name,
370368
)
369+
371370
return True
372371

373372
# endregion
@@ -639,25 +638,24 @@ async def _send_tasks(self) -> None:
639638
"""Send all tasks in queue."""
640639
if len(self._send_task_queue) == 0:
641640
return
642-
await self._send_task_lock.acquire()
643-
task_result = await gather(*self._send_task_queue)
644-
if not all(task_result):
645-
_LOGGER.warning(
646-
"Executed %s tasks (result=%s) for %s",
647-
len(self._send_task_queue),
648-
task_result,
649-
self.name,
650-
)
651-
self._send_task_queue = []
652-
self._send_task_lock.release()
641+
642+
async with self._send_task_lock:
643+
task_result = await gather(*self._send_task_queue)
644+
if not all(task_result):
645+
_LOGGER.warning(
646+
"Executed %s tasks (result=%s) for %s",
647+
len(self._send_task_queue),
648+
task_result,
649+
self.name,
650+
)
651+
self._send_task_queue = []
653652

654653
async def schedule_task_when_awake(
655654
self, task_fn: Coroutine[Any, Any, bool]
656655
) -> None:
657656
"""Add task to queue to be executed when node is awake."""
658-
await self._send_task_lock.acquire()
659-
self._send_task_queue.append(task_fn)
660-
self._send_task_lock.release()
657+
async with self._send_task_lock:
658+
self._send_task_queue.append(task_fn)
661659

662660
async def sed_configure( # pylint: disable=too-many-arguments
663661
self,

0 commit comments

Comments
 (0)