diff --git a/homeassistant/components/lifx/coordinator.py b/homeassistant/components/lifx/coordinator.py index d01fb266c6f..37e753c27a3 100644 --- a/homeassistant/components/lifx/coordinator.py +++ b/homeassistant/components/lifx/coordinator.py @@ -25,6 +25,7 @@ from .const import ( from .util import async_execute_lifx, get_real_mac_addr, lifx_features REQUEST_REFRESH_DELAY = 0.35 +LIFX_IDENTIFY_DELAY = 3.0 class LIFXUpdateCoordinator(DataUpdateCoordinator): @@ -92,7 +93,7 @@ class LIFXUpdateCoordinator(DataUpdateCoordinator): # Turn the bulb on first, flash for 3 seconds, then turn off await self.async_set_power(state=True, duration=1) await self.async_set_waveform_optional(value=IDENTIFY_WAVEFORM) - await asyncio.sleep(3) + await asyncio.sleep(LIFX_IDENTIFY_DELAY) await self.async_set_power(state=False, duration=1) async def _async_update_data(self) -> None: diff --git a/homeassistant/components/lifx/light.py b/homeassistant/components/lifx/light.py index fe17dd95788..36d3b480f74 100644 --- a/homeassistant/components/lifx/light.py +++ b/homeassistant/components/lifx/light.py @@ -39,7 +39,7 @@ from .manager import ( ) from .util import convert_8_to_16, convert_16_to_8, find_hsbk, lifx_features, merge_hsbk -COLOR_ZONE_POPULATE_DELAY = 0.3 +LIFX_STATE_SETTLE_DELAY = 0.3 SERVICE_LIFX_SET_STATE = "set_state" @@ -231,6 +231,9 @@ class LIFXLight(LIFXEntity, LightEntity): if power_off: await self.set_power(False, duration=fade) + # Avoid state ping-pong by holding off updates as the state settles + await asyncio.sleep(LIFX_STATE_SETTLE_DELAY) + # Update when the transition starts and ends await self.update_during_transition(fade) @@ -338,7 +341,7 @@ class LIFXStrip(LIFXColor): # Zone brightness is not reported when powered off if not self.is_on and hsbk[HSBK_BRIGHTNESS] is None: await self.set_power(True) - await asyncio.sleep(COLOR_ZONE_POPULATE_DELAY) + await asyncio.sleep(LIFX_STATE_SETTLE_DELAY) await self.update_color_zones() await self.set_power(False) diff --git a/tests/components/lifx/conftest.py b/tests/components/lifx/conftest.py index 326c4f75413..a243132dc65 100644 --- a/tests/components/lifx/conftest.py +++ b/tests/components/lifx/conftest.py @@ -1,5 +1,4 @@ """Tests for the lifx integration.""" - from unittest.mock import AsyncMock, MagicMock, patch import pytest diff --git a/tests/components/lifx/test_button.py b/tests/components/lifx/test_button.py index abc91128e25..b166aa05d66 100644 --- a/tests/components/lifx/test_button.py +++ b/tests/components/lifx/test_button.py @@ -1,4 +1,8 @@ """Tests for button platform.""" +from unittest.mock import patch + +import pytest + from homeassistant.components import lifx from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN from homeassistant.components.lifx.const import DOMAIN @@ -21,6 +25,13 @@ from . import ( from tests.common import MockConfigEntry +@pytest.fixture(autouse=True) +def mock_lifx_coordinator_sleep(): + """Mock out lifx coordinator sleeps.""" + with patch("homeassistant.components.lifx.coordinator.LIFX_IDENTIFY_DELAY", 0): + yield + + async def test_button_restart(hass: HomeAssistant) -> None: """Test that a bulb can be restarted.""" config_entry = MockConfigEntry( diff --git a/tests/components/lifx/test_light.py b/tests/components/lifx/test_light.py index 5b641e850f2..6229e130a40 100644 --- a/tests/components/lifx/test_light.py +++ b/tests/components/lifx/test_light.py @@ -50,6 +50,13 @@ from . import ( from tests.common import MockConfigEntry, async_fire_time_changed +@pytest.fixture(autouse=True) +def patch_lifx_state_settle_delay(): + """Set asyncio.sleep for state settles to zero.""" + with patch("homeassistant.components.lifx.light.LIFX_STATE_SETTLE_DELAY", 0): + yield + + async def test_light_unique_id(hass: HomeAssistant) -> None: """Test a light unique id.""" already_migrated_config_entry = MockConfigEntry( @@ -98,7 +105,6 @@ async def test_light_unique_id_new_firmware(hass: HomeAssistant) -> None: assert device.identifiers == {(DOMAIN, SERIAL)} -@patch("homeassistant.components.lifx.light.COLOR_ZONE_POPULATE_DELAY", 0) async def test_light_strip(hass: HomeAssistant) -> None: """Test a light strip.""" already_migrated_config_entry = MockConfigEntry(