mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 18:54:08 +00:00
Revise device initialization and subsequent updates (#807)
This improves the initial update cycle to fetch the information as early as possible and avoid requesting unnecessary information (like the child component listing) in every subsequent call of `update()`. The initial update performs the following steps: 1. `component_nego` (for components) and `get_device_info` (for common device info) are requested as first, and their results are stored in the internal state to allow individual modules (like colortemp) to access the data during the initialization later on. 2. If `child_device` component is available, the child device list and their components is requested separately to initialize the children. 3. The modules are initialized based on component lists, making the queries available for the regular `update()`. 4. Finally, a query requesting all module-defined queries is executed, including also those that we already did above, like the device info. All subsequent updates will only involve queries that are defined by the supported modules. This also means that we do not currently support adding & removing child devices on the fly. The internal state contains now only the responses for the most recent update (i.e., no component information is directly available anymore, but needs to be accessed separately if needed). If component information is wanted from homeassistant users via diagnostics reports, the diagnostic platform needs to be adapted to acquire this separately.
This commit is contained in:
@@ -24,7 +24,7 @@ def test_childdevice_init(dev, dummy_protocol, mocker):
|
||||
@strip_smart
|
||||
async def test_childdevice_update(dev, dummy_protocol, mocker):
|
||||
"""Test that parent update updates children."""
|
||||
child_info = dev._last_update["child_info"]
|
||||
child_info = dev.internal_state["get_child_device_list"]
|
||||
child_list = child_info["child_device_list"]
|
||||
|
||||
assert len(dev.children) == child_info["sum"]
|
||||
|
@@ -1,8 +1,9 @@
|
||||
"""Tests for SMART devices."""
|
||||
import logging
|
||||
from unittest.mock import patch
|
||||
from typing import Any, Dict
|
||||
|
||||
import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from kasa import KasaException
|
||||
from kasa.exceptions import SmartErrorCode
|
||||
@@ -25,13 +26,79 @@ async def test_try_get_response(dev: SmartDevice, caplog):
|
||||
|
||||
|
||||
@device_smart
|
||||
async def test_update_no_device_info(dev: SmartDevice):
|
||||
async def test_update_no_device_info(dev: SmartDevice, mocker: MockerFixture):
|
||||
mock_response: dict = {
|
||||
"get_device_usage": {},
|
||||
"get_device_time": {},
|
||||
}
|
||||
msg = f"get_device_info not found in {mock_response} for device 127.0.0.123"
|
||||
with patch.object(dev.protocol, "query", return_value=mock_response), pytest.raises(
|
||||
KasaException, match=msg
|
||||
):
|
||||
with mocker.patch.object(
|
||||
dev.protocol, "query", return_value=mock_response
|
||||
), pytest.raises(KasaException, match=msg):
|
||||
await dev.update()
|
||||
|
||||
|
||||
@device_smart
|
||||
async def test_initial_update(dev: SmartDevice, mocker: MockerFixture):
|
||||
"""Test the initial update cycle."""
|
||||
# As the fixture data is already initialized, we reset the state for testing
|
||||
dev._components_raw = None
|
||||
dev._features = {}
|
||||
|
||||
negotiate = mocker.spy(dev, "_negotiate")
|
||||
initialize_modules = mocker.spy(dev, "_initialize_modules")
|
||||
initialize_features = mocker.spy(dev, "_initialize_features")
|
||||
|
||||
# Perform two updates and verify that initialization is only done once
|
||||
await dev.update()
|
||||
await dev.update()
|
||||
|
||||
negotiate.assert_called_once()
|
||||
assert dev._components_raw is not None
|
||||
initialize_modules.assert_called_once()
|
||||
assert dev.modules
|
||||
initialize_features.assert_called_once()
|
||||
assert dev.features
|
||||
|
||||
|
||||
@device_smart
|
||||
async def test_negotiate(dev: SmartDevice, mocker: MockerFixture):
|
||||
"""Test that the initial negotiation performs expected steps."""
|
||||
# As the fixture data is already initialized, we reset the state for testing
|
||||
dev._components_raw = None
|
||||
dev._children = {}
|
||||
|
||||
query = mocker.spy(dev.protocol, "query")
|
||||
initialize_children = mocker.spy(dev, "_initialize_children")
|
||||
await dev._negotiate()
|
||||
|
||||
# Check that we got the initial negotiation call
|
||||
query.assert_any_call({"component_nego": None, "get_device_info": None})
|
||||
assert dev._components_raw
|
||||
|
||||
# Check the children are created, if device supports them
|
||||
if "child_device" in dev._components:
|
||||
initialize_children.assert_called_once()
|
||||
query.assert_any_call(
|
||||
{
|
||||
"get_child_device_component_list": None,
|
||||
"get_child_device_list": None,
|
||||
}
|
||||
)
|
||||
assert len(dev.children) == dev.internal_state["get_child_device_list"]["sum"]
|
||||
|
||||
|
||||
@device_smart
|
||||
async def test_update_module_queries(dev: SmartDevice, mocker: MockerFixture):
|
||||
"""Test that the regular update uses queries from all supported modules."""
|
||||
query = mocker.spy(dev.protocol, "query")
|
||||
|
||||
# We need to have some modules initialized by now
|
||||
assert dev.modules
|
||||
|
||||
await dev.update()
|
||||
full_query: Dict[str, Any] = {}
|
||||
for mod in dev.modules.values():
|
||||
full_query |= mod.query()
|
||||
|
||||
query.assert_called_with(full_query)
|
||||
|
Reference in New Issue
Block a user