diff --git a/midealocal/cli.py b/midealocal/cli.py index b834584b..76f9c093 100644 --- a/midealocal/cli.py +++ b/midealocal/cli.py @@ -55,8 +55,14 @@ async def _get_cloud(self) -> MideaCloud: async def _get_keys(self, device_id: int) -> dict[int, dict[str, Any]]: cloud = await self._get_cloud() - cloud_keys = await cloud.get_cloud_keys(device_id) default_keys = await cloud.get_default_keys() + if not await cloud.login(): + _LOGGER.warning( + "Failed to authenticate to the cloud. Using only default keys.", + ) + return default_keys + cloud_keys = await cloud.get_cloud_keys(device_id) + return {**cloud_keys, **default_keys} async def discover(self) -> MideaDevice | None: diff --git a/tests/cli_test.py b/tests/cli_test.py index 0f87045c..63268b09 100644 --- a/tests/cli_test.py +++ b/tests/cli_test.py @@ -60,6 +60,46 @@ async def test_get_cloud(self) -> None: with pytest.raises(ElementMissing): await self.cli._get_cloud() + async def test_get_keys(self) -> None: + """Test get keys.""" + mock_cloud = AsyncMock() + with ( + patch("midealocal.cli.get_midea_cloud", return_value=mock_cloud), + patch.object( + mock_cloud, + "get_default_keys", + return_value={99: {"key": "key99", "token": "token99"}}, + ) as mock_default_keys, + patch.object( + mock_cloud, + "get_cloud_keys", + return_value={ + 0: {"key": "key0", "token": "token0"}, + 1: {"key": "key1", "token": "token1"}, + }, + ) as mock_cloud_keys, + patch.object(mock_cloud, "login", side_effect=[True, False]), + ): + keys = await self.cli._get_keys(0) + assert len(keys) == 3 + assert keys[0]["key"] == "key0" + assert keys[1]["key"] == "key1" + assert keys[99]["key"] == "key99" + assert keys[0]["token"] == "token0" + assert keys[1]["token"] == "token1" + assert keys[99]["token"] == "token99" + mock_default_keys.assert_called_once() + mock_default_keys.reset_mock() + mock_cloud_keys.assert_called_once_with(0) + mock_cloud_keys.reset_mock() + + keys = await self.cli._get_keys(0) + assert len(keys) == 1 + assert keys[99]["key"] == "key99" + assert keys[99]["token"] == "token99" + mock_default_keys.assert_called_once() + mock_cloud_keys.assert_not_called() + async def test_discover(self) -> None: """Test discover.""" mock_device = {