Prevent tplink missing devices and unavailable state (#39762)
* Adds self to codeowners for tplink * Adds retry to update to prevent missing devices * Runs through isort and corrects async commit * Runs through black * Runs through pre-checks * Corrects and matches var names * Re-runs through black * Corrects var name * Removes the retry loop and in favor of async add * Cleanup imports * Removes no longer valid test * Removes is_ready and only log retry once * Corrects switch logging vars * Adds list of entities to add_entities * Consumes exception for attempt_update * Consumes specific exception * Removes unnecessary update * Reducing back to 2 seconds * Removes useless return * Call get_sysinfo for all at once * Formated black * Adds missing docstirng * Corrects docstring * Update homeassistant/components/tplink/light.py Co-authored-by: Anders Melchiorsen <amelchio@nogoto.net> * Corrects sysinfo call * Adds default for host vars * Adds log when device responds again * Revert host alias default * Removes unncessary host var * Removes host var * Get device details from sysinfo * Use host and alias for log msg * Gets hosts from smartbulb * Changes retry logging to debug * Attempts coverage add * Removes unused import * Updates tests for new retry * Runs through isort * Removes unneeded try * Prevents static entries from failing integration * Format black * Forces an update after turn on off * Remove common test * Revert update after turn_on off * Adds patch for sleep_time 0 * Returns False when update fails Co-authored-by: Anders Melchiorsen <amelchio@nogoto.net>
This commit is contained in:
1 parent
366006e0aa
commit
874e1f6103
8 files changed
+213
-263
No files matched your search
@@ -1,82 +0,0 @@
|
||||
"""Common code tests."""
|
||||
from datetime import timedelta
|
||||
|
||||
from pyHS100 import SmartDeviceException
|
||||
|
||||
from homeassistant.components.tplink.common import async_add_entities_retry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from tests.async_mock import MagicMock
|
||||
|
||||
|
||||
async def test_async_add_entities_retry(hass: HomeAssistantType):
|
||||
"""Test interval callback."""
|
||||
async_add_entities_callback = MagicMock()
|
||||
|
||||
# The objects that will be passed to async_add_entities_callback.
|
||||
objects = ["Object 1", "Object 2", "Object 3", "Object 4"]
|
||||
|
||||
# For each call to async_add_entities_callback, the following side effects
|
||||
# will be triggered in order. This set of side effects accurateley simulates
|
||||
# 3 attempts to add all entities while also handling several return types.
|
||||
# To help understand what's going on, a comment exists describing what the
|
||||
# object list looks like throughout the iterations.
|
||||
callback_side_effects = [
|
||||
# OB1, OB2, OB3, OB4
|
||||
False,
|
||||
False,
|
||||
True, # Object 3
|
||||
False,
|
||||
# OB1, OB2, OB4
|
||||
True, # Object 1
|
||||
SmartDeviceException("My error"),
|
||||
False,
|
||||
# OB2, OB4
|
||||
True, # Object 2
|
||||
True, # Object 4
|
||||
]
|
||||
|
||||
callback = MagicMock(side_effect=callback_side_effects)
|
||||
|
||||
await async_add_entities_retry(
|
||||
hass,
|
||||
async_add_entities_callback,
|
||||
objects,
|
||||
callback,
|
||||
interval=timedelta(milliseconds=100),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert callback.call_count == len(callback_side_effects)
|
||||
|
||||
|
||||
async def test_async_add_entities_retry_cancel(hass: HomeAssistantType):
|
||||
"""Test interval callback."""
|
||||
async_add_entities_callback = MagicMock()
|
||||
|
||||
callback_side_effects = [
|
||||
False,
|
||||
False,
|
||||
True, # Object 1
|
||||
False,
|
||||
True, # Object 2
|
||||
SmartDeviceException("My error"),
|
||||
False,
|
||||
True, # Object 3
|
||||
True, # Object 4
|
||||
]
|
||||
|
||||
callback = MagicMock(side_effect=callback_side_effects)
|
||||
|
||||
objects = ["Object 1", "Object 2", "Object 3", "Object 4"]
|
||||
cancel = await async_add_entities_retry(
|
||||
hass,
|
||||
async_add_entities_callback,
|
||||
objects,
|
||||
callback,
|
||||
interval=timedelta(milliseconds=100),
|
||||
)
|
||||
cancel()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert callback.call_count == 4
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Tests for light platform."""
|
||||
import logging
|
||||
from typing import Callable, NamedTuple
|
||||
|
||||
from pyHS100 import SmartDeviceException
|
||||
@@ -20,6 +21,7 @@ from homeassistant.components.tplink.common import (
|
||||
CONF_DISCOVERY,
|
||||
CONF_LIGHT,
|
||||
)
|
||||
from homeassistant.components.tplink.light import SLEEP_TIME
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
CONF_HOST,
|
||||
@@ -474,20 +476,6 @@ async def test_get_light_state_retry(
|
||||
|
||||
light_mock_data.get_sysinfo_mock.side_effect = get_sysinfo_side_effect
|
||||
|
||||
# Setup test for retries of getting state information.
|
||||
get_state_call_count = 0
|
||||
|
||||
def get_light_state_side_effect():
|
||||
nonlocal get_state_call_count
|
||||
get_state_call_count += 1
|
||||
|
||||
if get_state_call_count == 1:
|
||||
raise SmartDeviceException()
|
||||
|
||||
return light_mock_data.light_state
|
||||
|
||||
light_mock_data.get_light_state_mock.side_effect = get_light_state_side_effect
|
||||
|
||||
# Setup test for retries of setting state information.
|
||||
set_state_call_count = 0
|
||||
|
||||
@@ -534,3 +522,58 @@ async def test_get_light_state_retry(
|
||||
assert light_mock_data.get_sysinfo_mock.call_count < 40
|
||||
assert light_mock_data.get_light_state_mock.call_count < 40
|
||||
assert light_mock_data.set_light_state_mock.call_count < 10
|
||||
|
||||
|
||||
async def test_update_failure(
|
||||
hass: HomeAssistant, light_mock_data: LightMockData, caplog
|
||||
):
|
||||
"""Test that update failures are logged."""
|
||||
|
||||
await async_setup_component(hass, HA_DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
tplink.DOMAIN,
|
||||
{
|
||||
tplink.DOMAIN: {
|
||||
CONF_DISCOVERY: False,
|
||||
CONF_LIGHT: [{CONF_HOST: "123.123.123.123"}],
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
caplog.clear()
|
||||
caplog.set_level(logging.WARNING)
|
||||
await hass.helpers.entity_component.async_update_entity("light.light1")
|
||||
assert caplog.text == ""
|
||||
|
||||
with patch("homeassistant.components.tplink.light.MAX_ATTEMPTS", 0):
|
||||
caplog.clear()
|
||||
caplog.set_level(logging.WARNING)
|
||||
await hass.helpers.entity_component.async_update_entity("light.light1")
|
||||
assert "Could not read state for 123.123.123.123|light1" in caplog.text
|
||||
|
||||
get_state_call_count = 0
|
||||
|
||||
def get_light_state_side_effect():
|
||||
nonlocal get_state_call_count
|
||||
get_state_call_count += 1
|
||||
|
||||
if get_state_call_count == 1:
|
||||
raise SmartDeviceException()
|
||||
|
||||
return light_mock_data.light_state
|
||||
|
||||
light_mock_data.get_light_state_mock.side_effect = get_light_state_side_effect
|
||||
|
||||
with patch("homeassistant.components.tplink.light", MAX_ATTEMPTS=2, SLEEP_TIME=0):
|
||||
caplog.clear()
|
||||
caplog.set_level(logging.DEBUG)
|
||||
|
||||
await update_entity(hass, "light.light1")
|
||||
assert (
|
||||
f"Retrying in {SLEEP_TIME} seconds for 123.123.123.123|light1"
|
||||
in caplog.text
|
||||
)
|
||||
assert "Device 123.123.123.123|light1 responded after " in caplog.text
|
||||
Reference in new issue
Block a user