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

feat: rework auth flow - part 1 #219

Merged
merged 7 commits into from
Jul 12, 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
2 changes: 1 addition & 1 deletion library_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
5 changes: 4 additions & 1 deletion midealocal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions midealocal/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
}

default_keys = {
DEFAULT_KEYS = {
99: {
"token": "ee755a84a115703768bcc7c6c13d3d629aa416f1e2fd798beb9f78cbb1381d09"
"1cc245d7b063aad2a900e5b498fbd936c811f5d504b2e656d4f33b3bbc6d1da3",
Expand Down Expand Up @@ -182,7 +182,11 @@ async def login(self) -> bool:
"""Authenticate."""
raise NotImplementedError

async def get_keys(self, appliance_id: int) -> dict[int, dict[str, Any]]:
async def get_default_keys(self) -> dict[int, dict[str, Any]]:
"""Get default cloud keys."""
return DEFAULT_KEYS

async def get_cloud_keys(self, appliance_id: int) -> dict[int, dict[str, Any]]:
"""Get keys for device."""
result = {}
for method in [1, 2]:
Expand All @@ -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
Expand Down
26 changes: 13 additions & 13 deletions tests/cloud_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -140,36 +140,36 @@ 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)
# test method1 + method2
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"
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
keys1: dict = await cloud.get_keys(100)
# 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
keys2: dict = await cloud.get_keys(100)
# 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_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."""
Expand Down
6 changes: 3 additions & 3 deletions tests/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from midealocal.cloud import default_keys
from midealocal.cloud import DEFAULT_KEYS
from midealocal.device import (
AuthException,
MideaDevice,
Expand Down Expand Up @@ -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,
Expand Down