From 85b5ac3a30bd4b9cfee093b60f23154d8fc82adf Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 12 Jul 2024 08:51:40 +0000 Subject: [PATCH 1/6] feat: rework auth flow - part 1 --- midealocal/cloud.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/midealocal/cloud.py b/midealocal/cloud.py index f18dac4d..56485f51 100644 --- a/midealocal/cloud.py +++ b/midealocal/cloud.py @@ -70,7 +70,7 @@ }, } -default_keys = { +DEFAULT_KEYS = { 99: { "token": "ee755a84a115703768bcc7c6c13d3d629aa416f1e2fd798beb9f78cbb1381d09" "1cc245d7b063aad2a900e5b498fbd936c811f5d504b2e656d4f33b3bbc6d1da3", @@ -182,6 +182,10 @@ async def login(self) -> bool: """Authenticate.""" raise NotImplementedError + async def get_default_keys(self) -> dict[int, dict[str, Any]]: + """Get default cloud keys.""" + return DEFAULT_KEYS + async def get_keys(self, appliance_id: int) -> dict[int, dict[str, Any]]: """Get keys for device.""" result = {} @@ -206,8 +210,6 @@ async def get_keys(self, appliance_id: int) -> dict[int, dict[str, Any]]: "token": token["token"].lower(), "key": token["key"].lower(), } - # add default key with method 1 and method 2 key - result.update(default_keys) return result @staticmethod From ffed61de278c4a9eea2fc5a4262d05b8df7d3815 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 12 Jul 2024 08:56:50 +0000 Subject: [PATCH 2/6] chore: rename method --- midealocal/cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/midealocal/cloud.py b/midealocal/cloud.py index 56485f51..1e9c1c3b 100644 --- a/midealocal/cloud.py +++ b/midealocal/cloud.py @@ -186,7 +186,7 @@ async def get_default_keys(self) -> dict[int, dict[str, Any]]: """Get default cloud keys.""" return DEFAULT_KEYS - async def get_keys(self, appliance_id: int) -> dict[int, dict[str, Any]]: + async def get_cloud_keys(self, appliance_id: int) -> dict[int, dict[str, Any]]: """Get keys for device.""" result = {} for method in [1, 2]: From 8988df5f89bbab2b2923f6d14c6c2fd6571eac13 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 12 Jul 2024 09:05:22 +0000 Subject: [PATCH 3/6] chore: fix tests --- tests/cloud_test.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/cloud_test.py b/tests/cloud_test.py index 7e446ac0..e95b1285 100644 --- a/tests/cloud_test.py +++ b/tests/cloud_test.py @@ -10,11 +10,11 @@ from aiohttp import ClientConnectionError from midealocal.cloud import ( + DEFAULT_KEYS, MeijuCloud, MideaAirCloud, MideaCloud, MSmartHomeCloud, - default_keys, get_midea_cloud, ) from midealocal.exceptions import ElementMissing @@ -116,7 +116,7 @@ async def test_meijucloud_login_invalid_user(self) -> None: assert not await cloud.login() async def test_meijucloud_get_keys(self) -> None: - """Test MeijuCloud get_keys.""" + """Test MeijuCloud get_cloud_keys.""" session = Mock() response = Mock() response.read = AsyncMock( @@ -141,7 +141,7 @@ async def test_meijucloud_get_keys(self) -> None: assert cloud is not None # test method1 + method2 + default key - keys3: dict = await cloud.get_keys(100) + keys3: dict = await cloud.get_cloud_keys(100) # test response token/key assert keys3[1]["token"] == "method1_return_token1" assert keys3[1]["key"] == "method1_return_key1" @@ -151,7 +151,7 @@ async def test_meijucloud_get_keys(self) -> None: assert len(keys3) == 3 # test method1 + default key - keys1: dict = await cloud.get_keys(100) + keys1: dict = await cloud.get_cloud_keys(100) # test response token/key assert keys1[1]["token"] == "method1_return_token1" assert keys1[1]["key"] == "method1_return_key1" @@ -159,7 +159,7 @@ async def test_meijucloud_get_keys(self) -> None: assert len(keys1) == 2 # test method2 + default key - keys2: dict = await cloud.get_keys(100) + keys2: dict = await cloud.get_cloud_keys(100) # test response token/key assert keys2[2]["token"] == "method2_return_token2" assert keys2[2]["key"] == "method2_return_key2" @@ -167,9 +167,9 @@ async def test_meijucloud_get_keys(self) -> None: assert len(keys2) == 2 # test only default key - keys = await cloud.get_keys(100) + keys = await cloud.get_default_keys() assert len(keys) == 1 - assert keys == default_keys + assert keys == DEFAULT_KEYS async def test_meijucloud_list_home(self) -> None: """Test MeijuCloud list_home.""" From 830bd5762b6cdb6eea3e41750bb41678f7b8d38b Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 12 Jul 2024 09:06:21 +0000 Subject: [PATCH 4/6] chore: fix device test --- tests/device_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/device_test.py b/tests/device_test.py index ef64c797..491813be 100644 --- a/tests/device_test.py +++ b/tests/device_test.py @@ -5,7 +5,7 @@ import pytest -from midealocal.cloud import default_keys +from midealocal.cloud import DEFAULT_KEYS from midealocal.device import ( AuthException, MideaDevice, @@ -42,8 +42,8 @@ def _setup_device(self) -> None: device_type=0xAC, ip_address="192.168.1.100", port=6444, - token=default_keys[99]["token"], - key=default_keys[99]["key"], + token=DEFAULT_KEYS[99]["token"], + key=DEFAULT_KEYS[99]["key"], protocol=3, model="test_model", subtype=1, From 7c3fa81adfdfa060539c29313b253b6c7ec632fb Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 12 Jul 2024 09:13:12 +0000 Subject: [PATCH 5/6] chore: fix cmdline tools --- library_test.py | 2 +- midealocal/cli.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/library_test.py b/library_test.py index b79ce03e..5c4b6f39 100644 --- a/library_test.py +++ b/library_test.py @@ -85,7 +85,7 @@ async def main() -> None: _LOGGER.error(msg) await session.close() sys.exit(2) - cloud_keys = await cloud.get_keys(first_device["device_id"]) + cloud_keys = await cloud.get_cloud_keys(first_device["device_id"]) _LOGGER.info("Fist device Cloud info: %s", cloud_keys) token = "" diff --git a/midealocal/cli.py b/midealocal/cli.py index 3176441b..41bad5fd 100644 --- a/midealocal/cli.py +++ b/midealocal/cli.py @@ -37,6 +37,7 @@ def get_config_file_path(relative: bool = False) -> Path: async def _get_keys(args: Namespace, device_id: int) -> dict[int, dict[str, Any]]: if not args.cloud_name or not args.username or not args.password: raise ElementMissing("Missing required parameters for cloud request.") + cloud_keys = {} async with aiohttp.ClientSession() as session: cloud = get_midea_cloud( cloud_name=args.cloud_name, @@ -45,7 +46,9 @@ async def _get_keys(args: Namespace, device_id: int) -> dict[int, dict[str, Any] password=args.password, ) - return await cloud.get_keys(device_id) + cloud_keys = await cloud.get_cloud_keys(device_id) + default_keys = await cloud.get_default_keys() + return {**cloud_keys, **default_keys} async def _discover(args: Namespace) -> None: From dedd402bcea7f0186618230d99fd356d270daf29 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 12 Jul 2024 09:17:31 +0000 Subject: [PATCH 6/6] chore: update cloud test --- tests/cloud_test.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/cloud_test.py b/tests/cloud_test.py index e95b1285..995ac565 100644 --- a/tests/cloud_test.py +++ b/tests/cloud_test.py @@ -140,7 +140,7 @@ async def test_meijucloud_get_keys(self) -> None: ) assert cloud is not None - # test method1 + method2 + default key + # test method1 + method2 keys3: dict = await cloud.get_cloud_keys(100) # test response token/key assert keys3[1]["token"] == "method1_return_token1" @@ -148,23 +148,23 @@ async def test_meijucloud_get_keys(self) -> None: assert keys3[2]["token"] == "method2_return_token2" assert keys3[2]["key"] == "method2_return_key2" # simple test default key with length - assert len(keys3) == 3 + assert len(keys3) == 2 - # test method1 + default key + # test method1 keys1: dict = await cloud.get_cloud_keys(100) # test response token/key assert keys1[1]["token"] == "method1_return_token1" assert keys1[1]["key"] == "method1_return_key1" # simple test default key with length - assert len(keys1) == 2 + assert len(keys1) == 1 - # test method2 + default key + # test method2 keys2: dict = await cloud.get_cloud_keys(100) # test response token/key assert keys2[2]["token"] == "method2_return_token2" assert keys2[2]["key"] == "method2_return_key2" # simple test default key with length - assert len(keys2) == 2 + assert len(keys2) == 1 # test only default key keys = await cloud.get_default_keys()