Skip to content

Commit

Permalink
re-write the cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
georgezhao2010 committed Sep 16, 2023
1 parent 2f9d946 commit 3c42296
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 316 deletions.
93 changes: 32 additions & 61 deletions custom_components/midea_ac_lan/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
except ImportError:
from homeassistant.util.json import save_json
from .midea.core.discover import discover
from .midea.core.cloud import MeijuCloud, MSmartHomeCloud, SmartLifeCloud
from .midea.core.cloud import MeijuCloud, MSmartHomeCloud
from .midea.core.device import MiedaDevice
from .midea_devices import MIDEA_DEVICES
import voluptuous as vol
Expand All @@ -43,9 +43,6 @@

ADD_WAY = {"auto": "Auto", "by_ip": "By IP", "manual": "Manual", "list": "Just list appliances"}
PROTOCOLS = {1: "V1", 2: "V2", 3: "V3"}
DEFAULT_TOKEN = "EE755A84A115703768BCC7C6C13D3D629AA416F1E2FD798BEB9F78CBB1381D09" \
"1CC245D7B063AAD2A900E5B498FBD936C811F5D504B2E656D4F33B3BBC6D1DA3"
DEFAULT_KEY = "ED37BD31558A4B039AAF4E7A7A59AA7A75FD9101682045F69BAF45D28380AE5C"
STORAGE_PATH = f".storage/{DOMAIN}"


Expand Down Expand Up @@ -198,63 +195,37 @@ async def async_step_auto(self, user_input=None, error=None):
elif MIDEA_DEFAULT_SERVER == "MSmartHome":
cloud = MSmartHomeCloud(self._session, MIDEA_DEFAULT_ACCOUNT, MIDEA_DEFAULT_PASSWORD)
else:
cloud = SmartLifeCloud(self._session, MIDEA_DEFAULT_ACCOUNT, MIDEA_DEFAULT_PASSWORD)
dm = MiedaDevice(
name="",
device_id=device_id,
device_type=device.get(CONF_TYPE),
ip_address=device.get(CONF_IP_ADDRESS),
port=device.get(CONF_PORT),
token=DEFAULT_TOKEN,
key=DEFAULT_KEY,
protocol=3,
model=device.get(CONF_MODEL),
attributes={}
)
if dm.connect(refresh_status=False):
self.found_device = {
CONF_DEVICE_ID: device_id,
CONF_TYPE: device.get(CONF_TYPE),
CONF_PROTOCOL: 3,
CONF_IP_ADDRESS: device.get(CONF_IP_ADDRESS),
CONF_PORT: device.get(CONF_PORT),
CONF_MODEL: device.get(CONF_MODEL),
CONF_TOKEN: DEFAULT_TOKEN,
CONF_KEY: DEFAULT_KEY,
}
dm.close_socket()
return await self.async_step_manual()
elif await cloud.login():
for byte_order_big in [False, True]:
token, key = await cloud.get_token(user_input[CONF_DEVICE], byte_order_big=byte_order_big)
if token and key:
dm = MiedaDevice(
name="",
device_id=device_id,
device_type=device.get(CONF_TYPE),
ip_address=device.get(CONF_IP_ADDRESS),
port=device.get(CONF_PORT),
token=token,
key=key,
protocol=3,
model=device.get(CONF_MODEL),
attributes={}
)
_LOGGER.debug(f"Successful to take token and key, token: {token}, key: {key}, "
f"byte_order_big: {byte_order_big}")
if dm.connect(refresh_status=False):
self.found_device = {
CONF_DEVICE_ID: device_id,
CONF_TYPE: device.get(CONF_TYPE),
CONF_PROTOCOL: 3,
CONF_IP_ADDRESS: device.get(CONF_IP_ADDRESS),
CONF_PORT: device.get(CONF_PORT),
CONF_MODEL: device.get(CONF_MODEL),
CONF_TOKEN: token,
CONF_KEY: key,
}
dm.close_socket()
return await self.async_step_manual()
return await self.async_step_auto(error="cant_get_token")
if await cloud.login():
keys = await cloud.get_keys(user_input[CONF_DEVICE])
for method, key in keys.items():
dm = MiedaDevice(
name="",
device_id=device_id,
device_type=device.get(CONF_TYPE),
ip_address=device.get(CONF_IP_ADDRESS),
port=device.get(CONF_PORT),
token=key["token"],
key=key["key"],
protocol=3,
model=device.get(CONF_MODEL),
attributes={}
)
_LOGGER.debug(f"Successful to take token and key, token: {key['token']},"
f" key: { key['key']}, method: {method}")
if dm.connect(refresh_status=False):
self.found_device = {
CONF_DEVICE_ID: device_id,
CONF_TYPE: device.get(CONF_TYPE),
CONF_PROTOCOL: 3,
CONF_IP_ADDRESS: device.get(CONF_IP_ADDRESS),
CONF_PORT: device.get(CONF_PORT),
CONF_MODEL: device.get(CONF_MODEL),
CONF_TOKEN: key["token"],
CONF_KEY: key["key"],
}
dm.close_socket()
return await self.async_step_manual()
return await self.async_step_auto(error="connect_error")
return await self.async_step_auto(error="cant_get_token")
else:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/midea_ac_lan/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
52060369457599433878037326532120919 ^
556568442326836838504752433645123377
), 'X')).decode('UTF-8')
MIDEA_DEFAULT_SERVER = "MSmartHome" # MSmartHome/美居/SmartLife
MIDEA_DEFAULT_SERVER = "MSmartHome" # MSmartHome/美居
Loading

0 comments on commit 3c42296

Please sign in to comment.