Skip to content

Commit

Permalink
fixed the issue of getToken with 美的美居 account
Browse files Browse the repository at this point in the history
  • Loading branch information
georgezhao2010 committed Oct 16, 2023
1 parent e82f37d commit 6d7a257
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 29 deletions.
94 changes: 67 additions & 27 deletions custom_components/midea_ac_lan/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from homeassistant.helpers.json import save_json
except ImportError:
from homeassistant.util.json import save_json
from homeassistant.util.json import load_json
import logging
from .const import (
DOMAIN,
Expand Down Expand Up @@ -52,6 +53,12 @@
4: "NetHome Plus",
}

PRESET_ACCOUNT = [
39182118275972017797890111985649342047468653967530949796945843010512,
29406100301096535908214728322278519471982973450672552249652548883645,
39182118275972017797890111985649342050088014265865102175083010656997
]


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
_account = None
Expand All @@ -74,6 +81,21 @@ def _save_device_config(self, data: dict):
record_file = self.hass.config.path(f"{STORAGE_PATH}/{data[CONF_DEVICE_ID]}.json")
save_json(record_file, data)

def _load_device_config(self, device_id):
os.makedirs(self.hass.config.path(STORAGE_PATH), exist_ok=True)
record_file = self.hass.config.path(f"{STORAGE_PATH}/{device_id}.json")
json_data = load_json(record_file, default={})
return json_data

@staticmethod
def _check_storage_device(device: dict, storage_device: dict):
if storage_device.get(CONF_SUBTYPE) is None:
return False
if (device.get(CONF_PROTOCOL) == 3 and
(storage_device.get(CONF_TOKEN) is None or storage_device.get(CONF_KEY) is None)):
return False
return True

def _get_configured_account(self):
for entry in self._async_current_entries():
if entry.data.get(CONF_TYPE) == CONF_ACCOUNT:
Expand Down Expand Up @@ -189,10 +211,49 @@ async def async_step_auto(self, user_input=None, error=None):
if user_input is not None:
device_id = user_input[CONF_DEVICE]
device = self.devices.get(device_id)
if device.get(CONF_PROTOCOL) == 3:
storage_device = self._load_device_config(device_id)
if self._check_storage_device(device, storage_device):
self.found_device = {
CONF_DEVICE_ID: device_id,
CONF_TYPE: device.get(CONF_TYPE),
CONF_PROTOCOL: device.get(CONF_PROTOCOL),
CONF_IP_ADDRESS: device.get(CONF_IP_ADDRESS),
CONF_PORT: device.get(CONF_PORT),
CONF_MODEL: device.get(CONF_MODEL),
CONF_NAME: storage_device.get(CONF_NAME),
CONF_SUBTYPE: storage_device.get(CONF_SUBTYPE),
CONF_TOKEN: storage_device.get(CONF_TOKEN),
CONF_KEY: storage_device.get(CONF_KEY)
}
_LOGGER.debug(f"Loaded configuration for device {device_id} from storage")
return await self.async_step_manually()
else:
session = async_create_clientsession(self.hass)
cloud = get_midea_cloud(self._server, session, self._account, self._password)
self.found_device = {
CONF_DEVICE_ID: device_id,
CONF_TYPE: device.get(CONF_TYPE),
CONF_PROTOCOL: device.get(CONF_PROTOCOL),
CONF_IP_ADDRESS: device.get(CONF_IP_ADDRESS),
CONF_PORT: device.get(CONF_PORT),
CONF_MODEL: device.get(CONF_MODEL),
}
if await cloud.login():
if device_info := await cloud.get_device_info(device_id):
self.found_device[CONF_NAME] = device_info.get("name")
self.found_device[CONF_SUBTYPE] = device_info.get("model_number")
else:
return await self.async_step_auto(error="login_failed")
if device.get(CONF_PROTOCOL) == 3:
if self._server == "美的美居":
_LOGGER.debug(f"Try to get the Token and the Key use the preset MSmartHome account")
cloud = get_midea_cloud(
"MSmartHome",
session,
bytes.fromhex(format((PRESET_ACCOUNT[0] ^ PRESET_ACCOUNT[1]), 'X')).decode('ASCII'),
bytes.fromhex(format((PRESET_ACCOUNT[0] ^ PRESET_ACCOUNT[2]), 'X')).decode('ASCII'))
if not await cloud.login():
return await self.async_step_auto(error="preset_account")
keys = await cloud.get_keys(user_input[CONF_DEVICE])
for method, key in keys.items():
dm = MiedaDevice(
Expand All @@ -208,36 +269,15 @@ async def async_step_auto(self, user_input=None, error=None):
subtype=0,
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()
if device_info := await cloud.get_device_info(device_id):
self.found_device[CONF_NAME] = device_info.get("name")
self.found_device[CONF_SUBTYPE] = device_info.get("model_number")
self.found_device[CONF_TOKEN] = key["token"]
self.found_device[CONF_KEY] = key["key"]
return await self.async_step_manually()
return await self.async_step_auto(error="connect_error")
return await self.async_step_auto(error="login_failed")
else:
self.found_device = {
CONF_DEVICE_ID: device_id,
CONF_TYPE: device.get(CONF_TYPE),
CONF_PROTOCOL: 2,
CONF_IP_ADDRESS: device.get(CONF_IP_ADDRESS),
CONF_PORT: device.get(CONF_PORT),
CONF_MODEL: device.get(CONF_MODEL),
}
return await self.async_step_manually()
else:
return await self.async_step_manually()

return self.async_show_form(
step_id="auto",
data_schema=vol.Schema({
Expand Down
5 changes: 3 additions & 2 deletions custom_components/midea_ac_lan/midea/core/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,9 @@ def run(self):
self.close_socket()
break
except socket.error as e:
_LOGGER.debug(f"[{self._device_id}] Socket error {repr(e)}")
self.close_socket()
if self._is_run:
_LOGGER.debug(f"[{self._device_id}] Socket error {repr(e)}")
self.close_socket()
break
except Exception as e:
_LOGGER.error(f"[{self._device_id}] Unknown error :{e.__traceback__.tb_frame.f_globals['__file__']}, "
Expand Down
1 change: 1 addition & 0 deletions custom_components/midea_ac_lan/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"config": {
"error": {
"preset_account": "Failed to login with preset account, please report this issue",
"login_failed": "Failed to login, account or password was wrong",
"no_devices": "No new available appliances found on the network",
"device_exist": "Appliance is already configured",
Expand Down
1 change: 1 addition & 0 deletions custom_components/midea_ac_lan/translations/fr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"config": {
"error": {
"preset_account": "Failed to login with preset account, please report this issue",
"login_failed": "Failed to login, account or password was wrong",
"no_devices": "Aucun nouvel appareil trouvé sur le réseau",
"device_exist": "L'appareil est déjà configurée",
Expand Down
1 change: 1 addition & 0 deletions custom_components/midea_ac_lan/translations/hu.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"config": {
"error": {
"preset_account": "Failed to login with preset account, please report this issue",
"login_failed": "Bejelentkezés sikertelen, a fiók vagy a jelszó helytelen",
"no_devices": "Nem találhatóak új eszközök a hálózaton",
"device_exist": "Az eszköz már be van állítva",
Expand Down
1 change: 1 addition & 0 deletions custom_components/midea_ac_lan/translations/ru.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"config": {
"error": {
"preset_account": "Failed to login with preset account, please report this issue",
"login_failed": "Failed to login, account or password was wrong",
"no_devices": "Новые доступные устройства в локальной сети не найдены",
"device_exist": "Устройство уже настроено",
Expand Down
1 change: 1 addition & 0 deletions custom_components/midea_ac_lan/translations/sk.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"config": {
"error": {
"preset_account": "Failed to login with preset account, please report this issue",
"login_failed": "Failed to login, account or password was wrong",
"no_devices": "V sieti sa nenašli žiadne nové dostupné zariadenia",
"device_exist": "Spotrebič je už nakonfigurovaný",
Expand Down
1 change: 1 addition & 0 deletions custom_components/midea_ac_lan/translations/zh-Hans.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"config": {
"error": {
"preset_account": "预置账户登录失败,请报告此问题",
"login_failed": "登录失败, 用户名或密码错",
"no_devices": "未在网络上发现可用新设备",
"device_exist": "设备已经存在, 请添加其它设备",
Expand Down

0 comments on commit 6d7a257

Please sign in to comment.