Speed up connecting to legacy flux_led devices (#62614)

This commit is contained in:
J. Nick Koston 2021-12-22 14:27:03 -07:00 committed by GitHub
parent e593377fba
commit 91a8b1e7b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View File

@ -8,6 +8,7 @@ from typing import Any, Final, cast
from flux_led import DeviceType from flux_led import DeviceType
from flux_led.aio import AIOWifiLedBulb from flux_led.aio import AIOWifiLedBulb
from flux_led.const import ATTR_ID from flux_led.const import ATTR_ID
from flux_led.scanner import FluxLEDDiscovery
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STARTED, Platform from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STARTED, Platform
@ -52,9 +53,11 @@ REQUEST_REFRESH_DELAY: Final = 1.5
@callback @callback
def async_wifi_bulb_for_host(host: str) -> AIOWifiLedBulb: def async_wifi_bulb_for_host(
host: str, discovery: FluxLEDDiscovery | None
) -> AIOWifiLedBulb:
"""Create a AIOWifiLedBulb from a host.""" """Create a AIOWifiLedBulb from a host."""
return AIOWifiLedBulb(host) return AIOWifiLedBulb(host, discovery=discovery)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
@ -78,7 +81,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Flux LED/MagicLight from a config entry.""" """Set up Flux LED/MagicLight from a config entry."""
host = entry.data[CONF_HOST] host = entry.data[CONF_HOST]
device: AIOWifiLedBulb = async_wifi_bulb_for_host(host) directed_discovery = None
if discovery := async_get_discovery(hass, host):
directed_discovery = False
device: AIOWifiLedBulb = async_wifi_bulb_for_host(host, discovery=discovery)
signal = SIGNAL_STATE_UPDATED.format(device.ipaddr) signal = SIGNAL_STATE_UPDATED.format(device.ipaddr)
@callback @callback
@ -94,10 +100,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) from ex ) from ex
# UDP probe after successful connect only # UDP probe after successful connect only
directed_discovery = None if not discovery and (discovery := await async_discover_device(hass, host)):
if discovery := async_get_discovery(hass, host):
directed_discovery = False
elif discovery := await async_discover_device(hass, host):
directed_discovery = True directed_discovery = True
if discovery: if discovery:

View File

@ -225,7 +225,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# identifying the device as the chip model number # identifying the device as the chip model number
# AKA `HF-LPB100-ZJ200` # AKA `HF-LPB100-ZJ200`
return device return device
bulb = async_wifi_bulb_for_host(host) bulb = async_wifi_bulb_for_host(host, discovery=device)
try: try:
await bulb.async_setup(lambda: None) await bulb.async_setup(lambda: None)
finally: finally: