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,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