Fix flux_led reloading too quickly after an operating mode change (#64591)

pull/64741/head
J. Nick Koston 2022-01-22 18:56:38 -10:00 committed by GitHub
parent cdf7742c3e
commit d5211c12b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 15 deletions

View File

@ -1,9 +1,15 @@
"""Support for Magic Home select."""
from __future__ import annotations
import asyncio
from flux_led.aio import AIOWifiLedBulb
from flux_led.base_device import DeviceType
from flux_led.const import DEFAULT_WHITE_CHANNEL_TYPE, WhiteChannelType
from flux_led.const import (
DEFAULT_WHITE_CHANNEL_TYPE,
STATE_CHANGE_LATENCY,
WhiteChannelType,
)
from flux_led.protocol import PowerRestoreState, RemoteConfig
from homeassistant import config_entries
@ -23,6 +29,14 @@ NAME_TO_POWER_RESTORE_STATE = {
}
async def _async_delayed_reload(
hass: HomeAssistant, entry: config_entries.ConfigEntry
) -> None:
"""Reload after making a change that will effect the operation of the device."""
await asyncio.sleep(STATE_CHANGE_LATENCY)
hass.async_create_task(hass.config_entries.async_reload(entry.entry_id))
async def async_setup_entry(
hass: HomeAssistant,
entry: config_entries.ConfigEntry,
@ -137,10 +151,7 @@ class FluxICTypeSelect(FluxConfigSelect):
async def async_select_option(self, option: str) -> None:
"""Change the ic type."""
await self._device.async_set_device_config(ic_type=option)
# reload since we need to reinit the device
self.hass.async_create_task(
self.hass.config_entries.async_reload(self.coordinator.entry.entry_id)
)
await _async_delayed_reload(self.hass, self.coordinator.entry)
class FluxWiringsSelect(FluxConfigSelect):
@ -181,10 +192,7 @@ class FluxOperatingModesSelect(FluxConfigSelect):
async def async_select_option(self, option: str) -> None:
"""Change the ic type."""
await self._device.async_set_device_config(operating_mode=option)
# reload since we need to reinit the device
self.hass.async_create_task(
self.hass.config_entries.async_reload(self.coordinator.entry.entry_id)
)
await _async_delayed_reload(self.hass, self.coordinator.entry)
class FluxRemoteConfigSelect(FluxConfigSelect):
@ -244,10 +252,8 @@ class FluxWhiteChannelSelect(FluxConfigAtStartSelect):
async def async_select_option(self, option: str) -> None:
"""Change the white channel type."""
entry = self.entry
hass = self.hass
hass.config_entries.async_update_entry(
entry, data={**entry.data, CONF_WHITE_CHANNEL_TYPE: option.lower()}
self.hass.config_entries.async_update_entry(
self.entry,
data={**self.entry.data, CONF_WHITE_CHANNEL_TYPE: option.lower()},
)
# reload since we need to reinit the device
hass.async_create_task(hass.config_entries.async_reload(entry.entry_id))
await _async_delayed_reload(self.hass, self.entry)

View File

@ -31,6 +31,13 @@ from . import (
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True)
def no_wait_on_state_change():
"""Disable waiting for state change in tests."""
with patch("homeassistant.components.flux_led.select.STATE_CHANGE_LATENCY", 0):
yield
async def test_switch_power_restore_state(hass: HomeAssistant) -> None:
"""Test a smart plug power restore state."""
config_entry = MockConfigEntry(