8000 Revert to sleeping until no messages but with a timeout · home-assistant/core@88ddc83 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88ddc83

Browse files
committed
Revert to sleeping until no messages but with a timeout
Signed-off-by: Avi Miller <me@dje.li>
1 parent 049a822 commit 88ddc83

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

homeassistant/components/lifx/coordinator.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,9 @@ async def _async_update_data(self) -> None:
210210

211211
await asyncio.gather(*tasks)
212212

213-
events = []
214-
for _response, event, _callb in self.device.message.values():
215-
if isinstance(event, asyncio.Event):
216-
events.append(event.wait())
217-
if events:
218-
async with asyncio_timeout(MESSAGE_TIMEOUT):
219-
await asyncio.gather(*events)
213+
async with asyncio_timeout(MESSAGE_TIMEOUT):
214+
while len(self.device.message) > 0:
215+
await asyncio.sleep(0)
220216

221217
if self._update_rssi is True:
222218
await self.async_update_rssi()

tests/components/lifx/test_light.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""Tests for the lifx integration light platform."""
2-
import asyncio
32
from datetime import timedelta
43
from unittest.mock import patch
54

6-
from aiolifx.msgtypes import Acknowledgement, StateHostFirmware
5+
from aiolifx.message import Message
76
import aiolifx_effects
87
import pytest
98

@@ -136,17 +135,29 @@ async def test_updates_wait_for_pending_messages(hass: HomeAssistant) -> None:
136135
)
137136
already_migrated_config_entry.add_to_hass(hass)
138137
bulb = _mocked_bulb()
139-
mock_event = asyncio.Event()
140-
bulb.message = {
141-
0: (Acknowledgement, None, None),
142-
1: (StateHostFirmware, mock_event, None),
143-
}
138+
139+
class MockMessages(dict):
140+
"""Mocked message dictionary."""
141+
142+
def __init__(self) -> None:
143+
"""Init a mock message queue."""
144+
self.values: dict[int, tuple[Message, None, None]] = {
145+
0: (Message, None, None),
146+
1: (Message, None, None),
147+
}
148+
149+
def __getitem__(self, __key: int) -> tuple[Message, None, None]:
150+
"""Remove a message when it is requested."""
151+
item = self.values[__key]
152+
del self.values[__key]
153+
return item
154+
144155
entity_id = "light.my_bulb"
156+
_mock_messages = MockMessages()
145157

146-
with _patch_discovery(device=bulb), _patch_config_flow_try_connect(
158+
with patch.dict(bulb.message, _mock_messages, clear=True), _patch_discovery(
147159
device=bulb
148-
), _patch_device(device=bulb):
149-
mock_event.set()
160+
), _patch_config_flow_try_connect(device=bulb), _patch_device(device=bulb):
150161
await async_setup_component(hass, lifx.DOMAIN, {lifx.DOMAIN: {}})
151162
await hass.async_block_till_done()
152163

0 commit comments

Comments
 (0)
0