-
-
Notifications
You must be signed in to change notification settings - Fork 239
Add childsetup module to smartcam hubs #1469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
027d9b4
Add childsetup module to smartcam hubs
sdb9696 8e2edf9
Apply suggestions from code review
sdb9696 03cd513
Merge branch 'master' into feat/smartcam_childsetup
sdb9696 def2af4
Address review comments
sdb9696 d3471b4
Merge remote-tracking branch 'upstream/master' into feat/smartcam_chi…
sdb9696 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| """Implementation for child device setup. | ||
|
|
||
| This module allows pairing and disconnecting child devices. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import logging | ||
|
|
||
| from ...feature import Feature | ||
| from ..smartcammodule import SmartCamModule | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ChildSetup(SmartCamModule): | ||
| """Implementation for child device setup.""" | ||
|
|
||
| REQUIRED_COMPONENT = "childQuickSetup" | ||
| QUERY_GETTER_NAME = "getSupportChildDeviceCategory" | ||
| QUERY_MODULE_NAME = "childControl" | ||
| _categories: list[str] = [] | ||
|
|
||
| def _initialize_features(self) -> None: | ||
| """Initialize features.""" | ||
| self._add_feature( | ||
| Feature( | ||
| self._device, | ||
| id="pair", | ||
| name="Pair", | ||
| container=self, | ||
| attribute_setter="pair", | ||
| category=Feature.Category.Config, | ||
| type=Feature.Type.Action, | ||
| ) | ||
| ) | ||
|
|
||
| async def _post_update_hook(self) -> None: | ||
| if not self._categories: | ||
| self._categories = [ | ||
| cat["category"].replace("ipcamera", "camera") | ||
| for cat in self.data["device_category_list"] | ||
| ] | ||
|
|
||
| @property | ||
| def supported_child_device_categories(self) -> list[str]: | ||
| """Supported child device categories.""" | ||
| return self._categories | ||
|
|
||
| async def pair(self, *, timeout: int = 10) -> list[dict]: | ||
| """Scan for new devices and pair after discovering first new device.""" | ||
| await self.call( | ||
| "startScanChildDevice", {"childControl": {"category": self._categories}} | ||
| ) | ||
|
|
||
| _LOGGER.info("Waiting %s seconds for discovering new devices", timeout) | ||
|
|
||
| await asyncio.sleep(timeout) | ||
| res = await self.call( | ||
| "getScanChildDeviceList", {"childControl": {"category": self._categories}} | ||
| ) | ||
|
|
||
| detected_list = res["getScanChildDeviceList"]["child_device_list"] | ||
| if not detected_list: | ||
| _LOGGER.warning( | ||
| "No devices found, make sure to activate pairing " | ||
| "mode on the devices to be added." | ||
| ) | ||
| return [] | ||
|
|
||
| _LOGGER.info( | ||
| "Discovery done, found %s devices: %s", | ||
| len(detected_list), | ||
| detected_list, | ||
| ) | ||
| return await self._add_devices(detected_list) | ||
|
|
||
| async def _add_devices(self, detected_list: list[dict]) -> list: | ||
| """Add devices based on getScanChildDeviceList response.""" | ||
| await self.call( | ||
| "addScanChildDeviceList", | ||
| {"childControl": {"child_device_list": detected_list}}, | ||
| ) | ||
|
|
||
| await self._device.update() | ||
|
|
||
| successes = [] | ||
| for detected in detected_list: | ||
| device_id = detected["device_id"] | ||
|
|
||
| result = "not added" | ||
| if device_id in self._device._children: | ||
| result = "added" | ||
| successes.append(detected) | ||
|
|
||
| msg = f"{detected['device_model']} - {device_id} - {result}" | ||
| _LOGGER.info("Adding child to %s: %s", self._device.host, msg) | ||
|
|
||
| return successes | ||
|
|
||
| async def unpair(self, device_id: str) -> dict: | ||
| """Remove device from the hub.""" | ||
| _LOGGER.info("Going to unpair %s from %s", device_id, self) | ||
|
|
||
| payload = {"childControl": {"child_device_list": [{"device_id": device_id}]}} | ||
| return await self.call("removeChildDeviceList", payload) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| import pytest | ||
| from pytest_mock import MockerFixture | ||
|
|
||
| from kasa import Feature, Module, SmartDevice | ||
|
|
||
| from ...device_fixtures import parametrize | ||
|
|
||
| childsetup = parametrize( | ||
| "supports pairing", component_filter="childQuickSetup", protocol_filter={"SMARTCAM"} | ||
| ) | ||
|
|
||
|
|
||
| @childsetup | ||
| async def test_childsetup_features(dev: SmartDevice): | ||
| """Test the exposed features.""" | ||
| cs = dev.modules[Module.ChildSetup] | ||
|
|
||
| assert "pair" in cs._module_features | ||
| pair = cs._module_features["pair"] | ||
| assert pair.type == Feature.Type.Action | ||
|
|
||
|
|
||
| @childsetup | ||
| async def test_childsetup_pair( | ||
| dev: SmartDevice, mocker: MockerFixture, caplog: pytest.LogCaptureFixture | ||
| ): | ||
| """Test device pairing.""" | ||
| caplog.set_level(logging.INFO) | ||
| mock_query_helper = mocker.spy(dev, "_query_helper") | ||
| mocker.patch("asyncio.sleep") | ||
|
|
||
| cs = dev.modules[Module.ChildSetup] | ||
|
|
||
| await cs.pair() | ||
|
|
||
| mock_query_helper.assert_has_awaits( | ||
| [ | ||
| mocker.call( | ||
| "startScanChildDevice", | ||
| params={ | ||
| "childControl": { | ||
| "category": [ | ||
| "camera", | ||
| "subg.trv", | ||
| "subg.trigger", | ||
| "subg.plugswitch", | ||
| ] | ||
| } | ||
| }, | ||
| ), | ||
| mocker.call( | ||
| "getScanChildDeviceList", | ||
| { | ||
| "childControl": { | ||
| "category": [ | ||
| "camera", | ||
| "subg.trv", | ||
| "subg.trigger", | ||
| "subg.plugswitch", | ||
| ] | ||
| } | ||
| }, | ||
| ), | ||
| mocker.call( | ||
| "addScanChildDeviceList", | ||
| { | ||
| "childControl": { | ||
| "child_device_list": [ | ||
| { | ||
| "device_id": "0000000000000000000000000000000000000000", | ||
| "category": "subg.trigger.button", | ||
| "device_model": "S200B", | ||
| "name": "I01BU0tFRF9OQU1FIw====", | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| ), | ||
| ] | ||
| ) | ||
| assert "Discovery done" in caplog.text | ||
|
|
||
|
|
||
| @childsetup | ||
| async def test_childsetup_unpair( | ||
| dev: SmartDevice, mocker: MockerFixture, caplog: pytest.LogCaptureFixture | ||
| ): | ||
| """Test unpair.""" | ||
| mock_query_helper = mocker.spy(dev, "_query_helper") | ||
| DUMMY_ID = "dummy_id" | ||
|
|
||
| cs = dev.modules[Module.ChildSetup] | ||
|
|
||
| await cs.unpair(DUMMY_ID) | ||
|
|
||
| mock_query_helper.assert_awaited_with( | ||
| "removeChildDeviceList", | ||
| params={"childControl": {"child_device_list": [{"device_id": DUMMY_ID}]}}, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.