Move async_add_entities back to event loop for tplink component (#42454)
parent
5a460e609f
commit
68d2938c6b
|
@ -131,7 +131,7 @@ def get_static_devices(config_data) -> SmartDevices:
|
||||||
return SmartDevices(lights, switches)
|
return SmartDevices(lights, switches)
|
||||||
|
|
||||||
|
|
||||||
def add_available_devices(hass, device_type, device_class, async_add_entities):
|
def add_available_devices(hass, device_type, device_class):
|
||||||
"""Get sysinfo for all devices."""
|
"""Get sysinfo for all devices."""
|
||||||
|
|
||||||
devices = hass.data[TPLINK_DOMAIN][device_type]
|
devices = hass.data[TPLINK_DOMAIN][device_type]
|
||||||
|
@ -140,20 +140,18 @@ def add_available_devices(hass, device_type, device_class, async_add_entities):
|
||||||
devices = hass.data[TPLINK_DOMAIN][f"{device_type}_remaining"]
|
devices = hass.data[TPLINK_DOMAIN][f"{device_type}_remaining"]
|
||||||
|
|
||||||
entities_ready = []
|
entities_ready = []
|
||||||
entities_unavailable = []
|
devices_unavailable = []
|
||||||
for device in devices:
|
for device in devices:
|
||||||
try:
|
try:
|
||||||
device.get_sysinfo()
|
device.get_sysinfo()
|
||||||
entities_ready.append(device_class(device))
|
entities_ready.append(device_class(device))
|
||||||
except SmartDeviceException as ex:
|
except SmartDeviceException as ex:
|
||||||
entities_unavailable.append(device)
|
devices_unavailable.append(device)
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Unable to communicate with device %s: %s",
|
"Unable to communicate with device %s: %s",
|
||||||
device.host,
|
device.host,
|
||||||
ex,
|
ex,
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.data[TPLINK_DOMAIN][f"{device_type}_remaining"] = entities_unavailable
|
hass.data[TPLINK_DOMAIN][f"{device_type}_remaining"] = devices_unavailable
|
||||||
|
return entities_ready
|
||||||
if entities_ready:
|
|
||||||
async_add_entities(entities_ready, update_before_add=True)
|
|
||||||
|
|
|
@ -61,10 +61,13 @@ SLEEP_TIME = 2
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistantType, config_entry, async_add_entities):
|
async def async_setup_entry(hass: HomeAssistantType, config_entry, async_add_entities):
|
||||||
"""Set up lights."""
|
"""Set up lights."""
|
||||||
await hass.async_add_executor_job(
|
entities = await hass.async_add_executor_job(
|
||||||
add_available_devices, hass, CONF_LIGHT, TPLinkSmartBulb, async_add_entities
|
add_available_devices, hass, CONF_LIGHT, TPLinkSmartBulb
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if entities:
|
||||||
|
async_add_entities(entities, update_before_add=True)
|
||||||
|
|
||||||
if hass.data[TPLINK_DOMAIN][f"{CONF_LIGHT}_remaining"]:
|
if hass.data[TPLINK_DOMAIN][f"{CONF_LIGHT}_remaining"]:
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
||||||
|
|
|
@ -31,10 +31,13 @@ SLEEP_TIME = 2
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistantType, config_entry, async_add_entities):
|
async def async_setup_entry(hass: HomeAssistantType, config_entry, async_add_entities):
|
||||||
"""Set up switches."""
|
"""Set up switches."""
|
||||||
await hass.async_add_executor_job(
|
entities = await hass.async_add_executor_job(
|
||||||
add_available_devices, hass, CONF_SWITCH, SmartPlugSwitch, async_add_entities
|
add_available_devices, hass, CONF_SWITCH, SmartPlugSwitch
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if entities:
|
||||||
|
async_add_entities(entities, update_before_add=True)
|
||||||
|
|
||||||
if hass.data[TPLINK_DOMAIN][f"{CONF_SWITCH}_remaining"]:
|
if hass.data[TPLINK_DOMAIN][f"{CONF_SWITCH}_remaining"]:
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
"""Tests for light platform."""
|
"""Tests for light platform."""
|
||||||
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import Callable, NamedTuple
|
from typing import Callable, NamedTuple
|
||||||
|
|
||||||
|
@ -30,8 +31,10 @@ from homeassistant.const import (
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from tests.async_mock import Mock, PropertyMock, patch
|
from tests.async_mock import Mock, PropertyMock, patch
|
||||||
|
from tests.common import async_fire_time_changed
|
||||||
|
|
||||||
|
|
||||||
class LightMockData(NamedTuple):
|
class LightMockData(NamedTuple):
|
||||||
|
@ -605,5 +608,9 @@ async def test_async_setup_entry_unavailable(
|
||||||
)
|
)
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert "Unable to communicate with device 123.123.123.123" in caplog.text
|
assert not hass.states.get("light.light1")
|
||||||
assert len(hass.data[tplink.DOMAIN][f"{CONF_LIGHT}_remaining"]) == 1
|
|
||||||
|
future = utcnow() + timedelta(seconds=30)
|
||||||
|
async_fire_time_changed(hass, future)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get("light.light1")
|
||||||
|
|
Loading…
Reference in New Issue