mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Fix flux_led taking a long time to recover after offline (#72507)
This commit is contained in:
parent
2863c7ee5b
commit
3537fa1dab
@ -15,7 +15,10 @@ from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STARTED, Platform
|
|||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import (
|
||||||
|
async_dispatcher_connect,
|
||||||
|
async_dispatcher_send,
|
||||||
|
)
|
||||||
from homeassistant.helpers.event import (
|
from homeassistant.helpers.event import (
|
||||||
async_track_time_change,
|
async_track_time_change,
|
||||||
async_track_time_interval,
|
async_track_time_interval,
|
||||||
@ -27,6 +30,7 @@ from .const import (
|
|||||||
DISCOVER_SCAN_TIMEOUT,
|
DISCOVER_SCAN_TIMEOUT,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
FLUX_LED_DISCOVERY,
|
FLUX_LED_DISCOVERY,
|
||||||
|
FLUX_LED_DISCOVERY_SIGNAL,
|
||||||
FLUX_LED_EXCEPTIONS,
|
FLUX_LED_EXCEPTIONS,
|
||||||
SIGNAL_STATE_UPDATED,
|
SIGNAL_STATE_UPDATED,
|
||||||
STARTUP_SCAN_TIMEOUT,
|
STARTUP_SCAN_TIMEOUT,
|
||||||
@ -196,6 +200,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
# to avoid a race condition where the add_update_listener is not
|
# to avoid a race condition where the add_update_listener is not
|
||||||
# in place in time for the check in async_update_entry_from_discovery
|
# in place in time for the check in async_update_entry_from_discovery
|
||||||
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
|
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
|
||||||
|
|
||||||
|
async def _async_handle_discovered_device() -> None:
|
||||||
|
"""Handle device discovery."""
|
||||||
|
# Force a refresh if the device is now available
|
||||||
|
if not coordinator.last_update_success:
|
||||||
|
coordinator.force_next_update = True
|
||||||
|
await coordinator.async_refresh()
|
||||||
|
|
||||||
|
entry.async_on_unload(
|
||||||
|
async_dispatcher_connect(
|
||||||
|
hass,
|
||||||
|
FLUX_LED_DISCOVERY_SIGNAL.format(entry_id=entry.entry_id),
|
||||||
|
_async_handle_discovered_device,
|
||||||
|
)
|
||||||
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ from homeassistant.const import CONF_HOST
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.data_entry_flow import AbortFlow, FlowResult
|
from homeassistant.data_entry_flow import AbortFlow, FlowResult
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.typing import DiscoveryInfoType
|
from homeassistant.helpers.typing import DiscoveryInfoType
|
||||||
|
|
||||||
from . import async_wifi_bulb_for_host
|
from . import async_wifi_bulb_for_host
|
||||||
@ -31,6 +32,7 @@ from .const import (
|
|||||||
DEFAULT_EFFECT_SPEED,
|
DEFAULT_EFFECT_SPEED,
|
||||||
DISCOVER_SCAN_TIMEOUT,
|
DISCOVER_SCAN_TIMEOUT,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
FLUX_LED_DISCOVERY_SIGNAL,
|
||||||
FLUX_LED_EXCEPTIONS,
|
FLUX_LED_EXCEPTIONS,
|
||||||
TRANSITION_GRADUAL,
|
TRANSITION_GRADUAL,
|
||||||
TRANSITION_JUMP,
|
TRANSITION_JUMP,
|
||||||
@ -109,12 +111,20 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
and ":" in entry.unique_id
|
and ":" in entry.unique_id
|
||||||
and mac_matches_by_one(entry.unique_id, mac)
|
and mac_matches_by_one(entry.unique_id, mac)
|
||||||
):
|
):
|
||||||
if async_update_entry_from_discovery(
|
if (
|
||||||
self.hass, entry, device, None, allow_update_mac
|
async_update_entry_from_discovery(
|
||||||
|
self.hass, entry, device, None, allow_update_mac
|
||||||
|
)
|
||||||
|
or entry.state == config_entries.ConfigEntryState.SETUP_RETRY
|
||||||
):
|
):
|
||||||
self.hass.async_create_task(
|
self.hass.async_create_task(
|
||||||
self.hass.config_entries.async_reload(entry.entry_id)
|
self.hass.config_entries.async_reload(entry.entry_id)
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
async_dispatcher_send(
|
||||||
|
self.hass,
|
||||||
|
FLUX_LED_DISCOVERY_SIGNAL.format(entry_id=entry.entry_id),
|
||||||
|
)
|
||||||
raise AbortFlow("already_configured")
|
raise AbortFlow("already_configured")
|
||||||
|
|
||||||
async def _async_handle_discovery(self) -> FlowResult:
|
async def _async_handle_discovery(self) -> FlowResult:
|
||||||
|
@ -74,3 +74,5 @@ EFFECT_SPEED_SUPPORT_MODES: Final = {ColorMode.RGB, ColorMode.RGBW, ColorMode.RG
|
|||||||
CONF_CUSTOM_EFFECT_COLORS: Final = "custom_effect_colors"
|
CONF_CUSTOM_EFFECT_COLORS: Final = "custom_effect_colors"
|
||||||
CONF_CUSTOM_EFFECT_SPEED_PCT: Final = "custom_effect_speed_pct"
|
CONF_CUSTOM_EFFECT_SPEED_PCT: Final = "custom_effect_speed_pct"
|
||||||
CONF_CUSTOM_EFFECT_TRANSITION: Final = "custom_effect_transition"
|
CONF_CUSTOM_EFFECT_TRANSITION: Final = "custom_effect_transition"
|
||||||
|
|
||||||
|
FLUX_LED_DISCOVERY_SIGNAL = "flux_led_discovery_{entry_id}"
|
||||||
|
@ -30,6 +30,7 @@ class FluxLedUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
self.device = device
|
self.device = device
|
||||||
self.title = entry.title
|
self.title = entry.title
|
||||||
self.entry = entry
|
self.entry = entry
|
||||||
|
self.force_next_update = False
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
@ -45,6 +46,8 @@ class FluxLedUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
async def _async_update_data(self) -> None:
|
async def _async_update_data(self) -> None:
|
||||||
"""Fetch all device and sensor data from api."""
|
"""Fetch all device and sensor data from api."""
|
||||||
try:
|
try:
|
||||||
await self.device.async_update()
|
await self.device.async_update(force=self.force_next_update)
|
||||||
except FLUX_LED_EXCEPTIONS as ex:
|
except FLUX_LED_EXCEPTIONS as ex:
|
||||||
raise UpdateFailed(ex) from ex
|
raise UpdateFailed(ex) from ex
|
||||||
|
finally:
|
||||||
|
self.force_next_update = False
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"dependencies": ["network"],
|
"dependencies": ["network"],
|
||||||
"documentation": "https://www.home-assistant.io/integrations/flux_led",
|
"documentation": "https://www.home-assistant.io/integrations/flux_led",
|
||||||
"requirements": ["flux_led==0.28.29"],
|
"requirements": ["flux_led==0.28.30"],
|
||||||
"quality_scale": "platinum",
|
"quality_scale": "platinum",
|
||||||
"codeowners": ["@icemanch", "@bdraco"],
|
"codeowners": ["@icemanch", "@bdraco"],
|
||||||
"iot_class": "local_push",
|
"iot_class": "local_push",
|
||||||
|
@ -657,7 +657,7 @@ fjaraskupan==1.0.2
|
|||||||
flipr-api==1.4.2
|
flipr-api==1.4.2
|
||||||
|
|
||||||
# homeassistant.components.flux_led
|
# homeassistant.components.flux_led
|
||||||
flux_led==0.28.29
|
flux_led==0.28.30
|
||||||
|
|
||||||
# homeassistant.components.homekit
|
# homeassistant.components.homekit
|
||||||
# homeassistant.components.recorder
|
# homeassistant.components.recorder
|
||||||
|
@ -466,7 +466,7 @@ fjaraskupan==1.0.2
|
|||||||
flipr-api==1.4.2
|
flipr-api==1.4.2
|
||||||
|
|
||||||
# homeassistant.components.flux_led
|
# homeassistant.components.flux_led
|
||||||
flux_led==0.28.29
|
flux_led==0.28.30
|
||||||
|
|
||||||
# homeassistant.components.homekit
|
# homeassistant.components.homekit
|
||||||
# homeassistant.components.recorder
|
# homeassistant.components.recorder
|
||||||
|
@ -2,10 +2,11 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant import config_entries
|
||||||
from homeassistant.components import flux_led
|
from homeassistant.components import flux_led
|
||||||
from homeassistant.components.flux_led.const import (
|
from homeassistant.components.flux_led.const import (
|
||||||
CONF_REMOTE_ACCESS_ENABLED,
|
CONF_REMOTE_ACCESS_ENABLED,
|
||||||
@ -19,6 +20,8 @@ from homeassistant.const import (
|
|||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
EVENT_HOMEASSISTANT_STARTED,
|
EVENT_HOMEASSISTANT_STARTED,
|
||||||
|
STATE_ON,
|
||||||
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
@ -27,6 +30,7 @@ from homeassistant.util.dt import utcnow
|
|||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
DEFAULT_ENTRY_TITLE,
|
DEFAULT_ENTRY_TITLE,
|
||||||
|
DHCP_DISCOVERY,
|
||||||
FLUX_DISCOVERY,
|
FLUX_DISCOVERY,
|
||||||
FLUX_DISCOVERY_PARTIAL,
|
FLUX_DISCOVERY_PARTIAL,
|
||||||
IP_ADDRESS,
|
IP_ADDRESS,
|
||||||
@ -113,6 +117,70 @@ async def test_config_entry_retry(hass: HomeAssistant) -> None:
|
|||||||
assert config_entry.state == ConfigEntryState.SETUP_RETRY
|
assert config_entry.state == ConfigEntryState.SETUP_RETRY
|
||||||
|
|
||||||
|
|
||||||
|
async def test_config_entry_retry_right_away_on_discovery(hass: HomeAssistant) -> None:
|
||||||
|
"""Test discovery makes the config entry reload if its in a retry state."""
|
||||||
|
config_entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN, data={CONF_HOST: IP_ADDRESS}, unique_id=MAC_ADDRESS
|
||||||
|
)
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
with _patch_discovery(no_device=True), _patch_wifibulb(no_device=True):
|
||||||
|
await async_setup_component(hass, flux_led.DOMAIN, {flux_led.DOMAIN: {}})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert config_entry.state == ConfigEntryState.SETUP_RETRY
|
||||||
|
|
||||||
|
with _patch_discovery(), _patch_wifibulb():
|
||||||
|
await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={"source": config_entries.SOURCE_DHCP},
|
||||||
|
data=DHCP_DISCOVERY,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert config_entry.state == ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
|
||||||
|
async def test_coordinator_retry_right_away_on_discovery_already_setup(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
) -> None:
|
||||||
|
"""Test discovery makes the coordinator force poll if its already setup."""
|
||||||
|
config_entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
data={CONF_HOST: IP_ADDRESS, CONF_NAME: DEFAULT_ENTRY_TITLE},
|
||||||
|
unique_id=MAC_ADDRESS,
|
||||||
|
)
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
bulb = _mocked_bulb()
|
||||||
|
with _patch_discovery(), _patch_wifibulb(device=bulb):
|
||||||
|
await async_setup_component(hass, flux_led.DOMAIN, {flux_led.DOMAIN: {}})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert config_entry.state == ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
entity_id = "light.bulb_rgbcw_ddeeff"
|
||||||
|
entity_registry = er.async_get(hass)
|
||||||
|
assert entity_registry.async_get(entity_id).unique_id == MAC_ADDRESS
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.state == STATE_ON
|
||||||
|
|
||||||
|
now = utcnow()
|
||||||
|
bulb.async_update = AsyncMock(side_effect=RuntimeError)
|
||||||
|
async_fire_time_changed(hass, now + timedelta(seconds=50))
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
bulb.async_update = AsyncMock()
|
||||||
|
|
||||||
|
with _patch_discovery(), _patch_wifibulb():
|
||||||
|
await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={"source": config_entries.SOURCE_DHCP},
|
||||||
|
data=DHCP_DISCOVERY,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.state == STATE_ON
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"discovery,title",
|
"discovery,title",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user