Skip to content
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

fix(cli): discover must return a list #266

Merged
merged 7 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions midealocal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ async def _get_keys(self, device_id: int) -> dict[int, dict[str, Any]]:

return {**cloud_keys, **default_keys}

async def discover(self) -> MideaDevice | None:
async def discover(self) -> list[MideaDevice]:
"""Discover device information."""
devices = discover(ip_address=self.namespace.host)

device_list: list[MideaDevice] = []
if len(devices) == 0:
_LOGGER.error("No devices found.")
return None
return device_list

# Dump only basic device info from the base class
_LOGGER.info("Found %d devices.", len(devices))
Expand Down Expand Up @@ -112,8 +113,8 @@ async def discover(self) -> MideaDevice | None:
_LOGGER.exception("Unable to retrieve device attributes.")
else:
_LOGGER.info("Found device:\n%s", dev.attributes)
return dev
return None
device_list.append(dev)
return device_list

def message(self) -> None:
"""Load message into device."""
Expand Down Expand Up @@ -179,23 +180,23 @@ async def download(self) -> None:

async def set_attribute(self) -> None:
"""Set attribute for device."""
device = await self.discover()
if device is None:
device_list = await self.discover()
if len(device_list) != 1:
return

_LOGGER.info(
"Setting attribute %s for %s [%s]",
self.namespace.attribute,
device.device_id,
device.device_type,
device_list[0].device_id,
device_list[0].device_type,
)
device.set_attribute(
device_list[0].set_attribute(
self.namespace.attribute,
self._cast_attr_value(),
)
await asyncio.sleep(2)
device.refresh_status(True)
_LOGGER.info("New device status:\n%s", device.attributes)
device_list[0].refresh_status(True)
_LOGGER.info("New device status:\n%s", device_list[0].attributes)

def _cast_attr_value(self) -> int | bool | str:
if self.namespace.attr_type == "bool":
Expand Down
46 changes: 14 additions & 32 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ async def test_discover(self) -> None:
patch.object(
mock_device_instance,
"authenticate",
side_effect=[None, AuthException, SocketException],
side_effect=[None, None, AuthException, SocketException],
) as authenticate_mock,
patch.object(
mock_device_instance,
"refresh_status",
side_effect=[None, RefreshFailed, None],
side_effect=[None, None, RefreshFailed, None],
) as refresh_status_mock,
):
mock_discover.return_value = {1: mock_device}
Expand All @@ -148,8 +148,8 @@ async def test_discover(self) -> None:
}

await self.cli.discover() # V3 device
authenticate_mock.assert_called_once()
refresh_status_mock.assert_called_once_with(True)
authenticate_mock.assert_called()
refresh_status_mock.assert_called_with(True)
authenticate_mock.reset_mock()
refresh_status_mock.reset_mock()

Expand Down Expand Up @@ -265,41 +265,23 @@ async def test_download(self) -> None:

async def test_set_attribute(self) -> None:
"""Test set attribute."""
mock_device = {
"device_id": 1,
"protocol": ProtocolVersion.V3,
"type": 0xAC,
"ip_address": "192.168.0.2",
"port": 6444,
"model": "AC123",
"sn": "AC123",
}
mock_cloud_instance = AsyncMock()
socket_instance = AsyncMock()
mock_device_instance = MagicMock()
mock_device_instance.connect.return_value = True
mock_cloud_instance.get_cloud_keys.return_value = {
0: {"token": "token", "key": "key"},
}
mock_cloud_instance.get_default_keys.return_value = {
99: {"token": "token", "key": "key"},
}
with (
patch(
"midealocal.cli.discover",
return_value={1: mock_device},
),
patch.object(
self.cli,
"_get_cloud",
return_value=mock_cloud_instance,
),
patch("socket.socket", return_value=socket_instance),
patch(
"midealocal.cli.device_selector",
return_value=mock_device_instance,
"discover",
side_effect=[
[],
[mock_device_instance],
[mock_device_instance],
[mock_device_instance],
],
),
):
await self.cli.set_attribute()
mock_device_instance.set_attribute.assert_not_called()

await self.cli.set_attribute()
mock_device_instance.set_attribute.assert_called_once_with("power", False)
mock_device_instance.reset_mock()
Expand Down
Loading