forked from HomeAssistant/midea-meiju-codec
rename repo
This commit is contained in:
465
custom_components/midea_auto_codec/core/cloud.py
Normal file
465
custom_components/midea_auto_codec/core/cloud.py
Normal file
@@ -0,0 +1,465 @@
|
||||
import logging
|
||||
import time
|
||||
import datetime
|
||||
import json
|
||||
import base64
|
||||
from threading import Lock
|
||||
from aiohttp import ClientSession
|
||||
from secrets import token_hex
|
||||
from .security import CloudSecurity, MeijuCloudSecurity, MSmartCloudSecurity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
clouds = {
|
||||
"美的美居": {
|
||||
"class_name": "MeijuCloud",
|
||||
"app_key": "46579c15",
|
||||
"login_key": "ad0ee21d48a64bf49f4fb583ab76e799",
|
||||
"iot_key": bytes.fromhex(format(9795516279659324117647275084689641883661667, 'x')).decode(),
|
||||
"hmac_key": bytes.fromhex(format(117390035944627627450677220413733956185864939010425, 'x')).decode(),
|
||||
"api_url": "https://mp-prod.smartmidea.net/mas/v5/app/proxy?alias=",
|
||||
},
|
||||
"MSmartHome": {
|
||||
"class_name": "MSmartHomeCloud",
|
||||
"app_key": "ac21b9f9cbfe4ca5a88562ef25e2b768",
|
||||
"iot_key": bytes.fromhex(format(7882822598523843940, 'x')).decode(),
|
||||
"hmac_key": bytes.fromhex(format(117390035944627627450677220413733956185864939010425, 'x')).decode(),
|
||||
"api_url": "https://mp-prod.appsmb.com/mas/v5/app/proxy?alias=",
|
||||
},
|
||||
}
|
||||
|
||||
default_keys = {
|
||||
99: {
|
||||
"token": "ee755a84a115703768bcc7c6c13d3d629aa416f1e2fd798beb9f78cbb1381d09"
|
||||
"1cc245d7b063aad2a900e5b498fbd936c811f5d504b2e656d4f33b3bbc6d1da3",
|
||||
"key": "ed37bd31558a4b039aaf4e7a7a59aa7a75fd9101682045f69baf45d28380ae5c"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class MideaCloud:
|
||||
def __init__(
|
||||
self,
|
||||
session: ClientSession,
|
||||
security: CloudSecurity,
|
||||
app_key: str,
|
||||
account: str,
|
||||
password: str,
|
||||
api_url: str
|
||||
):
|
||||
self._device_id = CloudSecurity.get_deviceid(account)
|
||||
self._session = session
|
||||
self._security = security
|
||||
self._api_lock = Lock()
|
||||
self._app_key = app_key
|
||||
self._account = account
|
||||
self._password = password
|
||||
self._api_url = api_url
|
||||
self._access_token = None
|
||||
self._login_id = None
|
||||
|
||||
def _make_general_data(self):
|
||||
return {
|
||||
}
|
||||
|
||||
async def _api_request(self, endpoint: str, data: dict, header=None) -> dict | None:
|
||||
header = header or {}
|
||||
if not data.get("reqId"):
|
||||
data.update({
|
||||
"reqId": token_hex(16)
|
||||
})
|
||||
if not data.get("stamp"):
|
||||
data.update({
|
||||
"stamp": datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
})
|
||||
random = str(int(time.time()))
|
||||
url = self._api_url + endpoint
|
||||
dump_data = json.dumps(data)
|
||||
sign = self._security.sign(dump_data, random)
|
||||
header.update({
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
"secretVersion": "1",
|
||||
"sign": sign,
|
||||
"random": random,
|
||||
})
|
||||
if self._access_token is not None:
|
||||
header.update({
|
||||
"accesstoken": self._access_token
|
||||
})
|
||||
response:dict = {"code": -1}
|
||||
for i in range(0, 3):
|
||||
try:
|
||||
with self._api_lock:
|
||||
r = await self._session.request("POST", url, headers=header, data=dump_data, timeout=10)
|
||||
raw = await r.read()
|
||||
_LOGGER.debug(f"Midea cloud API url: {url}, data: {data}, response: {raw}")
|
||||
response = json.loads(raw)
|
||||
break
|
||||
except Exception as e:
|
||||
pass
|
||||
if int(response["code"]) == 0 and "data" in response:
|
||||
return response["data"]
|
||||
print(response)
|
||||
return None
|
||||
|
||||
async def _get_login_id(self) -> str | None:
|
||||
data = self._make_general_data()
|
||||
data.update({
|
||||
"loginAccount": f"{self._account}"
|
||||
})
|
||||
if response := await self._api_request(
|
||||
endpoint="/v1/user/login/id/get",
|
||||
data=data
|
||||
):
|
||||
return response.get("loginId")
|
||||
return None
|
||||
|
||||
async def login(self) -> bool:
|
||||
raise NotImplementedError()
|
||||
|
||||
async def get_keys(self, appliance_id: int):
|
||||
result = {}
|
||||
for method in [1, 2]:
|
||||
udp_id = self._security.get_udp_id(appliance_id, method)
|
||||
data = self._make_general_data()
|
||||
data.update({
|
||||
"udpid": udp_id
|
||||
})
|
||||
response = await self._api_request(
|
||||
endpoint="/v1/iot/secure/getToken",
|
||||
data=data
|
||||
)
|
||||
if response and "tokenlist" in response:
|
||||
for token in response["tokenlist"]:
|
||||
if token["udpId"] == udp_id:
|
||||
result[method] = {
|
||||
"token": token["token"].lower(),
|
||||
"key": token["key"].lower()
|
||||
}
|
||||
result.update(default_keys)
|
||||
return result
|
||||
|
||||
async def list_home(self) -> dict | None:
|
||||
return {1: "My home"}
|
||||
|
||||
async def list_appliances(self, home_id) -> dict | None:
|
||||
raise NotImplementedError()
|
||||
|
||||
async def download_lua(
|
||||
self, path: str,
|
||||
device_type: str,
|
||||
sn: str,
|
||||
model_number: str | None,
|
||||
manufacturer_code: str = "0000",
|
||||
):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class MeijuCloud(MideaCloud):
|
||||
APP_ID = "900"
|
||||
APP_VERSION = "8.20.0.2"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
cloud_name: str,
|
||||
session: ClientSession,
|
||||
account: str,
|
||||
password: str,
|
||||
):
|
||||
super().__init__(
|
||||
session=session,
|
||||
security=MeijuCloudSecurity(
|
||||
login_key=clouds[cloud_name]["login_key"],
|
||||
iot_key=clouds[cloud_name]["iot_key"],
|
||||
hmac_key=clouds[cloud_name]["hmac_key"],
|
||||
),
|
||||
app_key=clouds[cloud_name]["app_key"],
|
||||
account=account,
|
||||
password=password,
|
||||
api_url=clouds[cloud_name]["api_url"]
|
||||
)
|
||||
|
||||
async def login(self) -> bool:
|
||||
if login_id := await self._get_login_id():
|
||||
self._login_id = login_id
|
||||
stamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
data = {
|
||||
"iotData": {
|
||||
"clientType": 1,
|
||||
"deviceId": self._device_id,
|
||||
"iampwd": self._security.encrypt_iam_password(self._login_id, self._password),
|
||||
"iotAppId": self.APP_ID,
|
||||
"loginAccount": self._account,
|
||||
"password": self._security.encrypt_password(self._login_id, self._password),
|
||||
"reqId": token_hex(16),
|
||||
"stamp": stamp
|
||||
},
|
||||
"data": {
|
||||
"appKey": self._app_key,
|
||||
"deviceId": self._device_id,
|
||||
"platform": 2
|
||||
},
|
||||
"timestamp": stamp,
|
||||
"stamp": stamp
|
||||
}
|
||||
if response := await self._api_request(
|
||||
endpoint="/mj/user/login",
|
||||
data=data
|
||||
):
|
||||
self._access_token = response["mdata"]["accessToken"]
|
||||
self._security.set_aes_keys(
|
||||
self._security.aes_decrypt_with_fixed_key(
|
||||
response["key"]
|
||||
), None
|
||||
)
|
||||
|
||||
return True
|
||||
return False
|
||||
|
||||
async def list_home(self):
|
||||
if response := await self._api_request(
|
||||
endpoint="/v1/homegroup/list/get",
|
||||
data={}
|
||||
):
|
||||
homes = {}
|
||||
for home in response["homeList"]:
|
||||
homes.update({
|
||||
int(home["homegroupId"]): home["name"]
|
||||
})
|
||||
return homes
|
||||
return None
|
||||
|
||||
async def list_appliances(self, home_id) -> dict | None:
|
||||
data = {
|
||||
"homegroupId": home_id
|
||||
}
|
||||
if response := await self._api_request(
|
||||
endpoint="/v1/appliance/home/list/get",
|
||||
data=data
|
||||
):
|
||||
appliances = {}
|
||||
for home in response.get("homeList") or []:
|
||||
for room in home.get("roomList") or []:
|
||||
for appliance in room.get("applianceList"):
|
||||
device_info = {
|
||||
"name": appliance.get("name"),
|
||||
"type": int(appliance.get("type"), 16),
|
||||
"sn": self._security.aes_decrypt(appliance.get("sn")) if appliance.get("sn") else "",
|
||||
"sn8": appliance.get("sn8", "00000000"),
|
||||
"model_number": appliance.get("modelNumber", "0"),
|
||||
"manufacturer_code":appliance.get("enterpriseCode", "0000"),
|
||||
"model": appliance.get("productModel"),
|
||||
"online": appliance.get("onlineStatus") == "1",
|
||||
}
|
||||
if device_info.get("sn8") is None or len(device_info.get("sn8")) == 0:
|
||||
device_info["sn8"] = "00000000"
|
||||
if device_info.get("model") is None or len(device_info.get("model")) == 0:
|
||||
device_info["model"] = device_info["sn8"]
|
||||
appliances[int(appliance["applianceCode"])] = device_info
|
||||
return appliances
|
||||
return None
|
||||
|
||||
async def download_lua(
|
||||
self, path: str,
|
||||
device_type: int,
|
||||
sn: str,
|
||||
model_number: str | None,
|
||||
manufacturer_code: str = "0000",
|
||||
):
|
||||
data = {
|
||||
"applianceSn": sn,
|
||||
"applianceType": "0x%02X" % device_type,
|
||||
"applianceMFCode": manufacturer_code,
|
||||
'version': "0",
|
||||
"iotAppId": self.APP_ID,
|
||||
}
|
||||
fnm = None
|
||||
if response := await self._api_request(
|
||||
endpoint="/v1/appliance/protocol/lua/luaGet",
|
||||
data=data
|
||||
):
|
||||
res = await self._session.get(response["url"])
|
||||
if res.status == 200:
|
||||
lua = await res.text()
|
||||
if lua:
|
||||
stream = ('local bit = require "bit"\n' +
|
||||
self._security.aes_decrypt_with_fixed_key(lua))
|
||||
stream = stream.replace("\r\n", "\n")
|
||||
fnm = f"{path}/{response['fileName']}"
|
||||
with open(fnm, "w") as fp:
|
||||
fp.write(stream)
|
||||
return fnm
|
||||
|
||||
|
||||
class MSmartHomeCloud(MideaCloud):
|
||||
APP_ID = "1010"
|
||||
SRC = "10"
|
||||
APP_VERSION = "3.0.2"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
cloud_name: str,
|
||||
session: ClientSession,
|
||||
account: str,
|
||||
password: str,
|
||||
):
|
||||
super().__init__(
|
||||
session=session,
|
||||
security=MSmartCloudSecurity(
|
||||
login_key=clouds[cloud_name]["app_key"],
|
||||
iot_key=clouds[cloud_name]["iot_key"],
|
||||
hmac_key=clouds[cloud_name]["hmac_key"],
|
||||
),
|
||||
app_key=clouds[cloud_name]["app_key"],
|
||||
account=account,
|
||||
password=password,
|
||||
api_url=clouds[cloud_name]["api_url"]
|
||||
)
|
||||
self._auth_base = base64.b64encode(
|
||||
f"{self._app_key}:{clouds['MSmartHome']['iot_key']}".encode("ascii")
|
||||
).decode("ascii")
|
||||
self._uid = ""
|
||||
|
||||
def _make_general_data(self):
|
||||
return {
|
||||
"appVersion": self.APP_VERSION,
|
||||
"src": self.SRC,
|
||||
"format": "2",
|
||||
"stamp": datetime.datetime.now().strftime("%Y%m%d%H%M%S"),
|
||||
"platformId": "1",
|
||||
"deviceId": self._device_id,
|
||||
"reqId": token_hex(16),
|
||||
"uid": self._uid,
|
||||
"clientType": "1",
|
||||
"appId": self.APP_ID,
|
||||
}
|
||||
|
||||
async def _api_request(self, endpoint: str, data: dict, header=None) -> dict | None:
|
||||
header = header or {}
|
||||
header.update({
|
||||
"x-recipe-app": self.APP_ID,
|
||||
"authorization": f"Basic {self._auth_base}"
|
||||
})
|
||||
if len(self._uid) > 0:
|
||||
header.update({
|
||||
"uid": self._uid
|
||||
})
|
||||
return await super()._api_request(endpoint, data, header)
|
||||
|
||||
async def _re_route(self):
|
||||
data = self._make_general_data()
|
||||
data.update({
|
||||
"userType": "0",
|
||||
"userName": f"{self._account}"
|
||||
})
|
||||
if response := await self._api_request(
|
||||
endpoint="/v1/multicloud/platform/user/route",
|
||||
data=data
|
||||
):
|
||||
if api_url := response.get("masUrl"):
|
||||
self._api_url = api_url
|
||||
|
||||
async def login(self) -> bool:
|
||||
await self._re_route()
|
||||
if login_id := await self._get_login_id():
|
||||
self._login_id = login_id
|
||||
iot_data = self._make_general_data()
|
||||
iot_data.pop("uid")
|
||||
stamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
iot_data.update({
|
||||
"iampwd": self._security.encrypt_iam_password(self._login_id, self._password),
|
||||
"loginAccount": self._account,
|
||||
"password": self._security.encrypt_password(self._login_id, self._password),
|
||||
"stamp": stamp
|
||||
})
|
||||
data = {
|
||||
"iotData": iot_data,
|
||||
"data": {
|
||||
"appKey": self._app_key,
|
||||
"deviceId": self._device_id,
|
||||
"platform": "2"
|
||||
},
|
||||
"stamp": stamp
|
||||
}
|
||||
if response := await self._api_request(
|
||||
endpoint="/mj/user/login",
|
||||
data=data
|
||||
):
|
||||
self._uid = response["uid"]
|
||||
self._access_token = response["mdata"]["accessToken"]
|
||||
self._security.set_aes_keys(response["accessToken"], response["randomData"])
|
||||
return True
|
||||
return False
|
||||
|
||||
async def list_appliances(self, home_id=None) -> dict | None:
|
||||
data = self._make_general_data()
|
||||
if response := await self._api_request(
|
||||
endpoint="/v1/appliance/user/list/get",
|
||||
data=data
|
||||
):
|
||||
appliances = {}
|
||||
for appliance in response["list"]:
|
||||
device_info = {
|
||||
"name": appliance.get("name"),
|
||||
"type": int(appliance.get("type"), 16),
|
||||
"sn": self._security.aes_decrypt(appliance.get("sn")) if appliance.get("sn") else "",
|
||||
"sn8": "",
|
||||
"model_number": appliance.get("modelNumber", "0"),
|
||||
"manufacturer_code":appliance.get("enterpriseCode", "0000"),
|
||||
"model": "",
|
||||
"online": appliance.get("onlineStatus") == "1",
|
||||
}
|
||||
device_info["sn8"] = device_info.get("sn")[9:17] if len(device_info["sn"]) > 17 else ""
|
||||
device_info["model"] = device_info.get("sn8")
|
||||
appliances[int(appliance["id"])] = device_info
|
||||
return appliances
|
||||
return None
|
||||
|
||||
async def download_lua(
|
||||
self, path: str,
|
||||
device_type: int,
|
||||
sn: str,
|
||||
model_number: str | None,
|
||||
manufacturer_code: str = "0000",
|
||||
):
|
||||
data = {
|
||||
"clientType": "1",
|
||||
"appId": self.APP_ID,
|
||||
"format": "2",
|
||||
"deviceId": self._device_id,
|
||||
"iotAppId": self.APP_ID,
|
||||
"applianceMFCode": manufacturer_code,
|
||||
"applianceType": "0x%02X" % device_type,
|
||||
"modelNumber": model_number,
|
||||
"applianceSn": self._security.aes_encrypt_with_fixed_key(sn.encode("ascii")).hex(),
|
||||
"version": "0",
|
||||
"encryptedType ": "2"
|
||||
}
|
||||
fnm = None
|
||||
if response := await self._api_request(
|
||||
endpoint="/v2/luaEncryption/luaGet",
|
||||
data=data
|
||||
):
|
||||
res = await self._session.get(response["url"])
|
||||
if res.status == 200:
|
||||
lua = await res.text()
|
||||
if lua:
|
||||
stream = ('local bit = require "bit"\n' +
|
||||
self._security.aes_decrypt_with_fixed_key(lua))
|
||||
stream = stream.replace("\r\n", "\n")
|
||||
fnm = f"{path}/{response['fileName']}"
|
||||
with open(fnm, "w") as fp:
|
||||
fp.write(stream)
|
||||
return fnm
|
||||
|
||||
|
||||
def get_midea_cloud(cloud_name: str, session: ClientSession, account: str, password: str) -> MideaCloud | None:
|
||||
cloud = None
|
||||
if cloud_name in clouds.keys():
|
||||
cloud = globals()[clouds[cloud_name]["class_name"]](
|
||||
cloud_name=cloud_name,
|
||||
session=session,
|
||||
account=account,
|
||||
password=password
|
||||
)
|
||||
return cloud
|
46
custom_components/midea_auto_codec/core/crc8.py
Normal file
46
custom_components/midea_auto_codec/core/crc8.py
Normal file
@@ -0,0 +1,46 @@
|
||||
crc8_854_table = [
|
||||
0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83,
|
||||
0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
|
||||
0x9D, 0xC3, 0x21, 0x7F, 0xFC, 0xA2, 0x40, 0x1E,
|
||||
0x5F, 0x01, 0xE3, 0xBD, 0x3E, 0x60, 0x82, 0xDC,
|
||||
0x23, 0x7D, 0x9F, 0xC1, 0x42, 0x1C, 0xFE, 0xA0,
|
||||
0xE1, 0xBF, 0x5D, 0x03, 0x80, 0xDE, 0x3C, 0x62,
|
||||
0xBE, 0xE0, 0x02, 0x5C, 0xDF, 0x81, 0x63, 0x3D,
|
||||
0x7C, 0x22, 0xC0, 0x9E, 0x1D, 0x43, 0xA1, 0xFF,
|
||||
0x46, 0x18, 0xFA, 0xA4, 0x27, 0x79, 0x9B, 0xC5,
|
||||
0x84, 0xDA, 0x38, 0x66, 0xE5, 0xBB, 0x59, 0x07,
|
||||
0xDB, 0x85, 0x67, 0x39, 0xBA, 0xE4, 0x06, 0x58,
|
||||
0x19, 0x47, 0xA5, 0xFB, 0x78, 0x26, 0xC4, 0x9A,
|
||||
0x65, 0x3B, 0xD9, 0x87, 0x04, 0x5A, 0xB8, 0xE6,
|
||||
0xA7, 0xF9, 0x1B, 0x45, 0xC6, 0x98, 0x7A, 0x24,
|
||||
0xF8, 0xA6, 0x44, 0x1A, 0x99, 0xC7, 0x25, 0x7B,
|
||||
0x3A, 0x64, 0x86, 0xD8, 0x5B, 0x05, 0xE7, 0xB9,
|
||||
0x8C, 0xD2, 0x30, 0x6E, 0xED, 0xB3, 0x51, 0x0F,
|
||||
0x4E, 0x10, 0xF2, 0xAC, 0x2F, 0x71, 0x93, 0xCD,
|
||||
0x11, 0x4F, 0xAD, 0xF3, 0x70, 0x2E, 0xCC, 0x92,
|
||||
0xD3, 0x8D, 0x6F, 0x31, 0xB2, 0xEC, 0x0E, 0x50,
|
||||
0xAF, 0xF1, 0x13, 0x4D, 0xCE, 0x90, 0x72, 0x2C,
|
||||
0x6D, 0x33, 0xD1, 0x8F, 0x0C, 0x52, 0xB0, 0xEE,
|
||||
0x32, 0x6C, 0x8E, 0xD0, 0x53, 0x0D, 0xEF, 0xB1,
|
||||
0xF0, 0xAE, 0x4C, 0x12, 0x91, 0xCF, 0x2D, 0x73,
|
||||
0xCA, 0x94, 0x76, 0x28, 0xAB, 0xF5, 0x17, 0x49,
|
||||
0x08, 0x56, 0xB4, 0xEA, 0x69, 0x37, 0xD5, 0x8B,
|
||||
0x57, 0x09, 0xEB, 0xB5, 0x36, 0x68, 0x8A, 0xD4,
|
||||
0x95, 0xCB, 0x29, 0x77, 0xF4, 0xAA, 0x48, 0x16,
|
||||
0xE9, 0xB7, 0x55, 0x0B, 0x88, 0xD6, 0x34, 0x6A,
|
||||
0x2B, 0x75, 0x97, 0xC9, 0x4A, 0x14, 0xF6, 0xA8,
|
||||
0x74, 0x2A, 0xC8, 0x96, 0x15, 0x4B, 0xA9, 0xF7,
|
||||
0xB6, 0xE8, 0x0A, 0x54, 0xD7, 0x89, 0x6B, 0x35
|
||||
]
|
||||
|
||||
|
||||
def calculate(data):
|
||||
crc_value = 0
|
||||
for m in data:
|
||||
k = crc_value ^ m
|
||||
if k > 256:
|
||||
k -= 256
|
||||
if k < 0:
|
||||
k += 256
|
||||
crc_value = crc8_854_table[k]
|
||||
return crc_value
|
396
custom_components/midea_auto_codec/core/device.py
Normal file
396
custom_components/midea_auto_codec/core/device.py
Normal file
@@ -0,0 +1,396 @@
|
||||
import threading
|
||||
import socket
|
||||
import time
|
||||
from enum import IntEnum
|
||||
from .security import LocalSecurity, MSGTYPE_HANDSHAKE_REQUEST, MSGTYPE_ENCRYPTED_REQUEST
|
||||
from .packet_builder import PacketBuilder
|
||||
from .lua_runtime import MideaCodec
|
||||
from .message import MessageQuestCustom
|
||||
from .logger import MideaLogger
|
||||
|
||||
|
||||
class AuthException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ResponseException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class RefreshFailed(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ParseMessageResult(IntEnum):
|
||||
SUCCESS = 0
|
||||
PADDING = 1
|
||||
ERROR = 99
|
||||
|
||||
|
||||
class MiedaDevice(threading.Thread):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
device_id: int,
|
||||
device_type: int,
|
||||
ip_address: str,
|
||||
port: int,
|
||||
token: str | None,
|
||||
key: str | None,
|
||||
protocol: int,
|
||||
model: str | None,
|
||||
subtype: int | None,
|
||||
sn: str | None,
|
||||
sn8: str | None,
|
||||
lua_file: str | None):
|
||||
threading.Thread.__init__(self)
|
||||
self._socket = None
|
||||
self._ip_address = ip_address
|
||||
self._port = port
|
||||
self._security = LocalSecurity()
|
||||
self._token = bytes.fromhex(token) if token else None
|
||||
self._key = bytes.fromhex(key) if key else None
|
||||
self._buffer = b""
|
||||
self._device_name = name
|
||||
self._device_id = device_id
|
||||
self._device_type = device_type
|
||||
self._protocol = protocol
|
||||
self._model = model
|
||||
self._updates = []
|
||||
self._is_run = False
|
||||
self._subtype = subtype
|
||||
self._sn = sn
|
||||
self._sn8 = sn8
|
||||
self._attributes = {
|
||||
"device_type": "T0x%02X" % device_type,
|
||||
"sn": sn,
|
||||
"sn8": sn8,
|
||||
"subtype": subtype
|
||||
}
|
||||
self._refresh_interval = 30
|
||||
self._heartbeat_interval = 10
|
||||
self._connected = False
|
||||
self._queries = [{}]
|
||||
self._centralized = []
|
||||
self._calculate_get = []
|
||||
self._calculate_set = []
|
||||
self._lua_runtime = MideaCodec(lua_file, sn=sn, subtype=subtype) if lua_file is not None else None
|
||||
|
||||
@property
|
||||
def device_name(self):
|
||||
return self._device_name
|
||||
|
||||
@property
|
||||
def device_id(self):
|
||||
return self._device_id
|
||||
|
||||
@property
|
||||
def device_type(self):
|
||||
return self._device_type
|
||||
|
||||
@property
|
||||
def model(self):
|
||||
return self._model
|
||||
|
||||
@property
|
||||
def sn(self):
|
||||
return self._sn
|
||||
|
||||
@property
|
||||
def sn8(self):
|
||||
return self._sn8
|
||||
|
||||
@property
|
||||
def subtype(self):
|
||||
return self._subtype
|
||||
|
||||
@property
|
||||
def attributes(self):
|
||||
return self._attributes
|
||||
|
||||
@property
|
||||
def connected(self):
|
||||
return self._connected
|
||||
|
||||
def set_refresh_interval(self, refresh_interval):
|
||||
self._refresh_interval = refresh_interval
|
||||
|
||||
def set_queries(self, queries: list):
|
||||
self._queries = queries
|
||||
|
||||
def set_centralized(self, centralized: list):
|
||||
self._centralized = centralized
|
||||
|
||||
def set_calculate(self, calculate: dict):
|
||||
values_get = calculate.get("get")
|
||||
values_set = calculate.get("set")
|
||||
self._calculate_get = values_get if values_get else []
|
||||
self._calculate_set = values_set if values_set else []
|
||||
|
||||
def get_attribute(self, attribute):
|
||||
return self._attributes.get(attribute)
|
||||
|
||||
def set_attribute(self, attribute, value):
|
||||
if attribute in self._attributes.keys():
|
||||
new_status = {}
|
||||
for attr in self._centralized:
|
||||
new_status[attr] = self._attributes.get(attr)
|
||||
new_status[attribute] = value
|
||||
if set_cmd := self._lua_runtime.build_control(new_status):
|
||||
self._build_send(set_cmd)
|
||||
|
||||
def set_attributes(self, attributes):
|
||||
new_status = {}
|
||||
for attr in self._centralized:
|
||||
new_status[attr] = self._attributes.get(attr)
|
||||
has_new = False
|
||||
for attribute, value in attributes.items():
|
||||
if attribute in self._attributes.keys():
|
||||
has_new = True
|
||||
new_status[attribute] = value
|
||||
if has_new:
|
||||
if set_cmd := self._lua_runtime.build_control(new_status):
|
||||
self._build_send(set_cmd)
|
||||
|
||||
def set_ip_address(self, ip_address):
|
||||
MideaLogger.debug(f"Update IP address to {ip_address}")
|
||||
self._ip_address = ip_address
|
||||
self.close_socket()
|
||||
|
||||
def send_command(self, cmd_type, cmd_body: bytearray):
|
||||
cmd = MessageQuestCustom(self._device_type, cmd_type, cmd_body)
|
||||
try:
|
||||
self._build_send(cmd.serialize().hex())
|
||||
except socket.error as e:
|
||||
MideaLogger.debug(
|
||||
f"Interface send_command failure, {repr(e)}, "
|
||||
f"cmd_type: {cmd_type}, cmd_body: {cmd_body.hex()}",
|
||||
self._device_id
|
||||
)
|
||||
|
||||
def register_update(self, update):
|
||||
self._updates.append(update)
|
||||
|
||||
def connect(self, refresh=False):
|
||||
try:
|
||||
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self._socket.settimeout(10)
|
||||
MideaLogger.debug(f"Connecting to {self._ip_address}:{self._port}", self._device_id)
|
||||
self._socket.connect((self._ip_address, self._port))
|
||||
MideaLogger.debug(f"Connected", self._device_id)
|
||||
if self._protocol == 3:
|
||||
self._authenticate()
|
||||
MideaLogger.debug(f"Authentication success", self._device_id)
|
||||
self._device_connected(True)
|
||||
if refresh:
|
||||
self._refresh_status()
|
||||
return True
|
||||
except socket.timeout:
|
||||
MideaLogger.debug(f"Connection timed out", self._device_id)
|
||||
except socket.error:
|
||||
MideaLogger.debug(f"Connection error", self._device_id)
|
||||
except AuthException:
|
||||
MideaLogger.debug(f"Authentication failed", self._device_id)
|
||||
except ResponseException:
|
||||
MideaLogger.debug(f"Unexpected response received", self._device_id)
|
||||
except RefreshFailed:
|
||||
MideaLogger.debug(f"Refresh status is timed out", self._device_id)
|
||||
except Exception as e:
|
||||
MideaLogger.error(f"Unknown error: {e.__traceback__.tb_frame.f_globals['__file__']}, "
|
||||
f"{e.__traceback__.tb_lineno}, {repr(e)}")
|
||||
if refresh:
|
||||
self._device_connected(False)
|
||||
self._socket = None
|
||||
return False
|
||||
|
||||
|
||||
def disconnect(self):
|
||||
self._buffer = b""
|
||||
if self._socket:
|
||||
self._socket.close()
|
||||
self._socket = None
|
||||
|
||||
@staticmethod
|
||||
def _fetch_v2_message(msg):
|
||||
result = []
|
||||
while len(msg) > 0:
|
||||
factual_msg_len = len(msg)
|
||||
if factual_msg_len < 6:
|
||||
break
|
||||
alleged_msg_len = msg[4] + (msg[5] << 8)
|
||||
if factual_msg_len >= alleged_msg_len:
|
||||
result.append(msg[:alleged_msg_len])
|
||||
msg = msg[alleged_msg_len:]
|
||||
else:
|
||||
break
|
||||
return result, msg
|
||||
|
||||
def _authenticate(self):
|
||||
request = self._security.encode_8370(
|
||||
self._token, MSGTYPE_HANDSHAKE_REQUEST)
|
||||
MideaLogger.debug(f"Handshaking")
|
||||
self._socket.send(request)
|
||||
response = self._socket.recv(512)
|
||||
if len(response) < 20:
|
||||
raise AuthException()
|
||||
response = response[8: 72]
|
||||
self._security.tcp_key(response, self._key)
|
||||
|
||||
def _send_message(self, data):
|
||||
if self._protocol == 3:
|
||||
self._send_message_v3(data, msg_type=MSGTYPE_ENCRYPTED_REQUEST)
|
||||
else:
|
||||
self._send_message_v2(data)
|
||||
|
||||
def _send_message_v2(self, data):
|
||||
if self._socket is not None:
|
||||
self._socket.send(data)
|
||||
else:
|
||||
MideaLogger.debug(f"Command send failure, device disconnected, data: {data.hex()}")
|
||||
|
||||
def _send_message_v3(self, data, msg_type=MSGTYPE_ENCRYPTED_REQUEST):
|
||||
data = self._security.encode_8370(data, msg_type)
|
||||
self._send_message_v2(data)
|
||||
|
||||
def _build_send(self, cmd: str):
|
||||
MideaLogger.debug(f"Sending: {cmd.lower()}")
|
||||
bytes_cmd = bytes.fromhex(cmd)
|
||||
msg = PacketBuilder(self._device_id, bytes_cmd).finalize()
|
||||
self._send_message(msg)
|
||||
|
||||
def _refresh_status(self):
|
||||
for query in self._queries:
|
||||
if query_cmd := self._lua_runtime.build_query(query):
|
||||
self._build_send(query_cmd)
|
||||
|
||||
def _parse_message(self, msg):
|
||||
if self._protocol == 3:
|
||||
messages, self._buffer = self._security.decode_8370(self._buffer + msg)
|
||||
else:
|
||||
messages, self._buffer = self.fetch_v2_message(self._buffer + msg)
|
||||
if len(messages) == 0:
|
||||
return ParseMessageResult.PADDING
|
||||
for message in messages:
|
||||
if message == b"ERROR":
|
||||
return ParseMessageResult.ERROR
|
||||
payload_len = message[4] + (message[5] << 8) - 56
|
||||
payload_type = message[2] + (message[3] << 8)
|
||||
if payload_type in [0x1001, 0x0001]:
|
||||
# Heartbeat detected
|
||||
pass
|
||||
elif len(message) > 56:
|
||||
cryptographic = message[40:-16]
|
||||
if payload_len % 16 == 0:
|
||||
decrypted = self._security.aes_decrypt(cryptographic)
|
||||
MideaLogger.debug(f"Received: {decrypted.hex().lower()}")
|
||||
if status := self._lua_runtime.decode_status(decrypted.hex()):
|
||||
MideaLogger.debug(f"Decoded: {status}")
|
||||
new_status = {}
|
||||
for single in status.keys():
|
||||
value = status.get(single)
|
||||
if single not in self._attributes or self._attributes[single] != value:
|
||||
self._attributes[single] = value
|
||||
new_status[single] = value
|
||||
if len(new_status) > 0:
|
||||
for c in self._calculate_get:
|
||||
lvalue = c.get("lvalue")
|
||||
rvalue = c.get("rvalue")
|
||||
if lvalue and rvalue:
|
||||
calculate = False
|
||||
for s, v in new_status.items():
|
||||
if rvalue.find(f"[{s}]") >= 0:
|
||||
calculate = True
|
||||
break
|
||||
if calculate:
|
||||
calculate_str1 = \
|
||||
(f"{lvalue.replace('[', 'self._attributes[')} = "
|
||||
f"{rvalue.replace('[', 'self._attributes[')}") \
|
||||
.replace("[","[\"").replace("]","\"]")
|
||||
calculate_str2 = \
|
||||
(f"{lvalue.replace('[', 'new_status[')} = "
|
||||
f"{rvalue.replace('[', 'self._attributes[')}") \
|
||||
.replace("[","[\"").replace("]","\"]")
|
||||
try:
|
||||
exec(calculate_str1)
|
||||
exec(calculate_str2)
|
||||
except Exception:
|
||||
MideaLogger.warning(
|
||||
f"Calculation Error: {lvalue} = {rvalue}", self._device_id
|
||||
)
|
||||
self._update_all(new_status)
|
||||
return ParseMessageResult.SUCCESS
|
||||
|
||||
def _send_heartbeat(self):
|
||||
msg = PacketBuilder(self._device_id, bytearray([0x00])).finalize(msg_type=0)
|
||||
self._send_message(msg)
|
||||
|
||||
def _device_connected(self, connected=True):
|
||||
self._connected = connected
|
||||
status = {"connected": connected}
|
||||
self._update_all(status)
|
||||
|
||||
def _update_all(self, status):
|
||||
MideaLogger.debug(f"Status update: {status}")
|
||||
for update in self._updates:
|
||||
update(status)
|
||||
|
||||
def open(self):
|
||||
if not self._is_run:
|
||||
self._is_run = True
|
||||
threading.Thread.start(self)
|
||||
|
||||
def close(self):
|
||||
if self._is_run:
|
||||
self._is_run = False
|
||||
self._lua_runtime = None
|
||||
self.disconnect()
|
||||
|
||||
def run(self):
|
||||
while self._is_run:
|
||||
while self._socket is None:
|
||||
if self.connect(refresh=True) is False:
|
||||
if not self._is_run:
|
||||
return
|
||||
self.disconnect()
|
||||
time.sleep(5)
|
||||
timeout_counter = 0
|
||||
start = time.time()
|
||||
previous_refresh = start
|
||||
previous_heartbeat = start
|
||||
self._socket.settimeout(1)
|
||||
while True:
|
||||
try:
|
||||
now = time.time()
|
||||
if 0 < self._refresh_interval <= now - previous_refresh:
|
||||
self._refresh_status()
|
||||
previous_refresh = now
|
||||
if now - previous_heartbeat >= self._heartbeat_interval:
|
||||
self._send_heartbeat()
|
||||
previous_heartbeat = now
|
||||
msg = self._socket.recv(512)
|
||||
msg_len = len(msg)
|
||||
if msg_len == 0:
|
||||
raise socket.error("Connection closed by peer")
|
||||
result = self._parse_message(msg)
|
||||
if result == ParseMessageResult.ERROR:
|
||||
MideaLogger.debug(f"Message 'ERROR' received")
|
||||
self.disconnect()
|
||||
break
|
||||
elif result == ParseMessageResult.SUCCESS:
|
||||
timeout_counter = 0
|
||||
except socket.timeout:
|
||||
timeout_counter = timeout_counter + 1
|
||||
if timeout_counter >= 120:
|
||||
MideaLogger.debug(f"Heartbeat timed out")
|
||||
self.disconnect()
|
||||
break
|
||||
except socket.error as e:
|
||||
MideaLogger.debug(f"Socket error {repr(e)}")
|
||||
self.disconnect()
|
||||
break
|
||||
except Exception as e:
|
||||
MideaLogger.error(f"Unknown error :{e.__traceback__.tb_frame.f_globals['__file__']}, "
|
||||
f"{e.__traceback__.tb_lineno}, {repr(e)}")
|
||||
self.disconnect()
|
||||
break
|
||||
|
||||
|
174
custom_components/midea_auto_codec/core/discover.py
Normal file
174
custom_components/midea_auto_codec/core/discover.py
Normal file
@@ -0,0 +1,174 @@
|
||||
import socket
|
||||
import ifaddr
|
||||
from ipaddress import IPv4Network
|
||||
from .security import LocalSecurity
|
||||
from .logger import MideaLogger
|
||||
try:
|
||||
import xml.etree.cElementTree as ET
|
||||
except ImportError:
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
|
||||
BROADCAST_MSG = bytearray([
|
||||
0x5a, 0x5a, 0x01, 0x11, 0x48, 0x00, 0x92, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x7f, 0x75, 0xbd, 0x6b, 0x3e, 0x4f, 0x8b, 0x76,
|
||||
0x2e, 0x84, 0x9c, 0x6e, 0x57, 0x8d, 0x65, 0x90,
|
||||
0x03, 0x6e, 0x9d, 0x43, 0x42, 0xa5, 0x0f, 0x1f,
|
||||
0x56, 0x9e, 0xb8, 0xec, 0x91, 0x8e, 0x92, 0xe5
|
||||
])
|
||||
|
||||
DEVICE_INFO_MSG = bytearray([
|
||||
0x5a, 0x5a, 0x15, 0x00, 0x00, 0x38, 0x00, 0x04,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x33, 0x05,
|
||||
0x13, 0x06, 0x14, 0x14, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x03, 0xe8, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0xca, 0x8d, 0x9b, 0xf9, 0xa0, 0x30, 0x1a, 0xe3,
|
||||
0xb7, 0xe4, 0x2d, 0x53, 0x49, 0x47, 0x62, 0xbe
|
||||
])
|
||||
|
||||
|
||||
def discover(discover_type=None, ip_address=None):
|
||||
MideaLogger.debug(f"Begin discover, type: {discover_type}, ip_address: {ip_address}")
|
||||
if discover_type is None:
|
||||
discover_type = []
|
||||
security = LocalSecurity()
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
sock.settimeout(5)
|
||||
found_devices = {}
|
||||
if ip_address is None:
|
||||
addrs = enum_all_broadcast()
|
||||
else:
|
||||
addrs = [ip_address]
|
||||
|
||||
for v in range(0, 3):
|
||||
for addr in addrs:
|
||||
sock.sendto(BROADCAST_MSG, (addr, 6445))
|
||||
sock.sendto(BROADCAST_MSG, (addr, 20086))
|
||||
while True:
|
||||
try:
|
||||
data, addr = sock.recvfrom(512)
|
||||
ip = addr[0]
|
||||
MideaLogger.debug(f"Received broadcast from {addr}: {data.hex()}")
|
||||
if len(data) >= 104 and (data[:2].hex() == "5a5a" or data[8:10].hex() == "5a5a"):
|
||||
if data[:2].hex() == "5a5a":
|
||||
protocol = 2
|
||||
elif data[:2].hex() == "8370":
|
||||
protocol = 3
|
||||
if data[8:10].hex() == "5a5a":
|
||||
data = data[8:-16]
|
||||
else:
|
||||
continue
|
||||
device_id = int.from_bytes(bytearray.fromhex(data[20:26].hex()), "little")
|
||||
if device_id in found_devices:
|
||||
continue
|
||||
encrypt_data = data[40:-16]
|
||||
reply = security.aes_decrypt(encrypt_data)
|
||||
MideaLogger.debug(f"Declassified reply: {reply.hex()}")
|
||||
ssid = reply[41:41 + reply[40]].decode("utf-8")
|
||||
device_type = ssid.split("_")[1]
|
||||
port = bytes2port(reply[4:8])
|
||||
model = reply[17:25].decode("utf-8")
|
||||
sn = reply[8:40].decode("utf-8")
|
||||
elif data[:6].hex() == "3c3f786d6c20":
|
||||
protocol = 1
|
||||
root = ET.fromstring(data.decode(
|
||||
encoding="utf-8", errors="replace"))
|
||||
child = root.find("body/device")
|
||||
m = child.attrib
|
||||
port, sn, device_type = int(m["port"]), m["apc_sn"], str(
|
||||
hex(int(m["apc_type"])))[2:]
|
||||
response = get_device_info(ip, int(port))
|
||||
device_id = get_id_from_response(response)
|
||||
if len(sn) == 32:
|
||||
model = sn[9:17]
|
||||
elif len(sn) == 22:
|
||||
model = sn[3:11]
|
||||
else:
|
||||
model = ""
|
||||
else:
|
||||
continue
|
||||
device = {
|
||||
"device_id": device_id,
|
||||
"type": int(device_type, 16),
|
||||
"ip_address": ip,
|
||||
"port": port,
|
||||
"model": model,
|
||||
"sn": sn,
|
||||
"protocol": protocol
|
||||
}
|
||||
if len(discover_type) == 0 or device.get("type") in discover_type:
|
||||
found_devices[device_id] = device
|
||||
MideaLogger.debug(f"Found a supported device: {device}")
|
||||
else:
|
||||
MideaLogger.debug(f"Found a unsupported device: {device}")
|
||||
if ip_address is not None:
|
||||
break
|
||||
except socket.timeout:
|
||||
break
|
||||
except socket.error as e:
|
||||
MideaLogger.debug(f"Socket error: {repr(e)}")
|
||||
return found_devices
|
||||
|
||||
|
||||
def get_id_from_response(response):
|
||||
if response[64:-16][:6].hex() == "3c3f786d6c20":
|
||||
xml = response[64:-16]
|
||||
root = ET.fromstring(xml.decode(encoding="utf-8", errors="replace"))
|
||||
child = root.find("smartDevice")
|
||||
m = child.attrib
|
||||
return int.from_bytes(bytearray.fromhex(m["devId"]), "little")
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def bytes2port(paramArrayOfbyte):
|
||||
if paramArrayOfbyte is None:
|
||||
return 0
|
||||
b, i = 0, 0
|
||||
while b < 4:
|
||||
if b < len(paramArrayOfbyte):
|
||||
b1 = paramArrayOfbyte[b] & 0xFF
|
||||
else:
|
||||
b1 = 0
|
||||
i |= b1 << b * 8
|
||||
b += 1
|
||||
return i
|
||||
|
||||
|
||||
def get_device_info(device_ip, device_port: int):
|
||||
response = bytearray(0)
|
||||
try:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||
sock.settimeout(8)
|
||||
device_address = (device_ip, device_port)
|
||||
sock.connect(device_address)
|
||||
MideaLogger.debug(f"Sending to {device_ip}:{device_port} {DEVICE_INFO_MSG.hex()}")
|
||||
sock.sendall(DEVICE_INFO_MSG)
|
||||
response = sock.recv(512)
|
||||
except socket.timeout:
|
||||
MideaLogger.warning(f"Connect the device {device_ip}:{device_port} timed out for 8s. "
|
||||
f"Don't care about a small amount of this. if many maybe not support."
|
||||
)
|
||||
except socket.error:
|
||||
MideaLogger.warning(f"Can't connect to Device {device_ip}:{device_port}")
|
||||
return response
|
||||
|
||||
|
||||
def enum_all_broadcast():
|
||||
nets = []
|
||||
adapters = ifaddr.get_adapters()
|
||||
for adapter in adapters:
|
||||
for ip in adapter.ips:
|
||||
if ip.is_IPv4 and ip.network_prefix < 32:
|
||||
localNet = IPv4Network(f"{ip.ip}/{ip.network_prefix}", strict=False)
|
||||
if localNet.is_private and not localNet.is_loopback and not localNet.is_link_local:
|
||||
addr = str(localNet.broadcast_address)
|
||||
if addr not in nets:
|
||||
nets.append(addr)
|
||||
return nets
|
36
custom_components/midea_auto_codec/core/logger.py
Normal file
36
custom_components/midea_auto_codec/core/logger.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import inspect
|
||||
import logging
|
||||
from enum import IntEnum
|
||||
|
||||
|
||||
class MideaLogType(IntEnum):
|
||||
DEBUG = 1
|
||||
WARN = 2
|
||||
ERROR = 3
|
||||
|
||||
|
||||
class MideaLogger:
|
||||
@staticmethod
|
||||
def _log(log_type, log, device_id):
|
||||
frm = inspect.stack()[2]
|
||||
mod = inspect.getmodule(frm[0])
|
||||
if device_id is not None:
|
||||
log = f"[{device_id}] {log}"
|
||||
if log_type == MideaLogType.DEBUG:
|
||||
logging.getLogger(mod.__name__).debug(log)
|
||||
elif log_type == MideaLogType.WARN:
|
||||
logging.getLogger(mod.__name__).warning(log)
|
||||
elif log_type == MideaLogType.ERROR:
|
||||
logging.getLogger(mod.__name__).error(log)
|
||||
|
||||
@staticmethod
|
||||
def debug(log, device_id=None):
|
||||
MideaLogger._log(MideaLogType.DEBUG, log, device_id)
|
||||
|
||||
@staticmethod
|
||||
def warning(log, device_id=None):
|
||||
MideaLogger._log(MideaLogType.WARN, log, device_id)
|
||||
|
||||
@staticmethod
|
||||
def error(log, device_id=None):
|
||||
MideaLogger._log(MideaLogType.ERROR, log, device_id)
|
91
custom_components/midea_auto_codec/core/lua_runtime.py
Normal file
91
custom_components/midea_auto_codec/core/lua_runtime.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import lupa
|
||||
import threading
|
||||
import json
|
||||
from .logger import MideaLogger
|
||||
|
||||
|
||||
class LuaRuntime:
|
||||
def __init__(self, file):
|
||||
self._runtimes = lupa.LuaRuntime()
|
||||
string = f'dofile("{file}")'
|
||||
self._runtimes.execute(string)
|
||||
self._lock = threading.Lock()
|
||||
self._json_to_data = self._runtimes.eval("function(param) return jsonToData(param) end")
|
||||
self._data_to_json = self._runtimes.eval("function(param) return dataToJson(param) end")
|
||||
|
||||
def json_to_data(self, json_value):
|
||||
with self._lock:
|
||||
result = self._json_to_data(json_value)
|
||||
|
||||
return result
|
||||
|
||||
def data_to_json(self, data_value):
|
||||
with self._lock:
|
||||
result = self._data_to_json(data_value)
|
||||
return result
|
||||
|
||||
|
||||
class MideaCodec(LuaRuntime):
|
||||
def __init__(self, file, sn=None, subtype=None):
|
||||
super().__init__(file)
|
||||
self._sn = sn
|
||||
self._subtype = subtype
|
||||
|
||||
def _build_base_dict(self):
|
||||
device_info ={}
|
||||
if self._sn is not None:
|
||||
device_info["deviceSN"] = self._sn
|
||||
if self._subtype is not None:
|
||||
device_info["deviceSubType"] = self._subtype
|
||||
base_dict = {
|
||||
"deviceinfo": device_info
|
||||
}
|
||||
return base_dict
|
||||
|
||||
def build_query(self, append=None):
|
||||
query_dict = self._build_base_dict()
|
||||
query_dict["query"] = {} if append is None else append
|
||||
json_str = json.dumps(query_dict)
|
||||
try:
|
||||
result = self.json_to_data(json_str)
|
||||
return result
|
||||
except lupa.LuaError as e:
|
||||
MideaLogger.error(f"LuaRuntimeError in build_query {json_str}: {repr(e)}")
|
||||
return None
|
||||
|
||||
def build_control(self, append=None):
|
||||
query_dict = self._build_base_dict()
|
||||
query_dict["control"] = {} if append is None else append
|
||||
json_str = json.dumps(query_dict)
|
||||
try:
|
||||
result = self.json_to_data(json_str)
|
||||
return result
|
||||
except lupa.LuaError as e:
|
||||
MideaLogger.error(f"LuaRuntimeError in build_control {json_str}: {repr(e)}")
|
||||
return None
|
||||
|
||||
def build_status(self, append=None):
|
||||
query_dict = self._build_base_dict()
|
||||
query_dict["status"] = {} if append is None else append
|
||||
json_str = json.dumps(query_dict)
|
||||
try:
|
||||
result = self.json_to_data(json_str)
|
||||
return result
|
||||
except lupa.LuaError as e:
|
||||
MideaLogger.error(f"LuaRuntimeError in build_status {json_str}: {repr(e)}")
|
||||
return None
|
||||
|
||||
def decode_status(self, data: str):
|
||||
data_dict = self._build_base_dict()
|
||||
data_dict["msg"] = {
|
||||
"data": data
|
||||
}
|
||||
json_str = json.dumps(data_dict)
|
||||
try:
|
||||
result = self.data_to_json(json_str)
|
||||
status = json.loads(result)
|
||||
return status.get("status")
|
||||
except lupa.LuaError as e:
|
||||
MideaLogger.error(f"LuaRuntimeError in decode_status {data}: {repr(e)}")
|
||||
return None
|
||||
|
158
custom_components/midea_auto_codec/core/message.py
Normal file
158
custom_components/midea_auto_codec/core/message.py
Normal file
@@ -0,0 +1,158 @@
|
||||
from abc import ABC
|
||||
from enum import IntEnum
|
||||
|
||||
|
||||
class MessageLenError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class MessageBodyError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class MessageCheckSumError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class MessageType(IntEnum):
|
||||
set = 0x02,
|
||||
query = 0x03,
|
||||
notify1 = 0x04,
|
||||
notify2 = 0x05,
|
||||
exception = 0x06,
|
||||
querySN = 0x07,
|
||||
exception2 = 0x0A,
|
||||
querySubtype = 0xA0
|
||||
|
||||
|
||||
class MessageBase(ABC):
|
||||
HEADER_LENGTH = 10
|
||||
|
||||
def __init__(self):
|
||||
self._device_type = 0x00
|
||||
self._message_type = 0x00
|
||||
self._body_type = 0x00
|
||||
self._device_protocol_version = 0
|
||||
|
||||
@staticmethod
|
||||
def checksum(data):
|
||||
return (~ sum(data) + 1) & 0xff
|
||||
|
||||
@property
|
||||
def header(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def message_type(self):
|
||||
return self._message_type
|
||||
|
||||
@message_type.setter
|
||||
def message_type(self, value):
|
||||
self._message_type = value
|
||||
|
||||
@property
|
||||
def device_type(self):
|
||||
return self._device_type
|
||||
|
||||
@device_type.setter
|
||||
def device_type(self, value):
|
||||
self._device_type = value
|
||||
|
||||
@property
|
||||
def body_type(self):
|
||||
return self._body_type
|
||||
|
||||
@body_type.setter
|
||||
def body_type(self, value):
|
||||
self._body_type = value
|
||||
|
||||
@property
|
||||
def device_protocol_version(self):
|
||||
return self._device_protocol_version
|
||||
|
||||
@device_protocol_version.setter
|
||||
def device_protocol_version(self, value):
|
||||
self._device_protocol_version = value
|
||||
|
||||
def __str__(self) -> str:
|
||||
output = {
|
||||
"header": self.header.hex(),
|
||||
"body": self.body.hex(),
|
||||
"message type": "%02x" % self._message_type,
|
||||
"body type": ("%02x" % self._body_type) if self._body_type is not None else "None"
|
||||
}
|
||||
return str(output)
|
||||
|
||||
|
||||
class MessageRequest(MessageBase):
|
||||
def __init__(self, device_protocol_version, device_type, message_type, body_type):
|
||||
super().__init__()
|
||||
self.device_protocol_version = device_protocol_version
|
||||
self.device_type = device_type
|
||||
self.message_type = message_type
|
||||
self.body_type = body_type
|
||||
|
||||
@property
|
||||
def header(self):
|
||||
length = self.HEADER_LENGTH + len(self.body)
|
||||
return bytearray([
|
||||
# flag
|
||||
0xAA,
|
||||
# length
|
||||
length,
|
||||
# device type
|
||||
self._device_type,
|
||||
# frame checksum
|
||||
0x00, # self._device_type ^ length,
|
||||
# unused
|
||||
0x00, 0x00,
|
||||
# frame ID
|
||||
0x00,
|
||||
# frame protocol version
|
||||
0x00,
|
||||
# device protocol version
|
||||
self._device_protocol_version,
|
||||
# frame type
|
||||
self._message_type
|
||||
])
|
||||
|
||||
@property
|
||||
def _body(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
body = bytearray([])
|
||||
if self.body_type is not None:
|
||||
body.append(self.body_type)
|
||||
if self._body is not None:
|
||||
body.extend(self._body)
|
||||
return body
|
||||
|
||||
def serialize(self):
|
||||
stream = self.header + self.body
|
||||
stream.append(MessageBase.checksum(stream[1:]))
|
||||
return stream
|
||||
|
||||
|
||||
class MessageQuestCustom(MessageRequest):
|
||||
def __init__(self, device_type, cmd_type, cmd_body):
|
||||
super().__init__(
|
||||
device_protocol_version=0,
|
||||
device_type=device_type,
|
||||
message_type=cmd_type,
|
||||
body_type=None)
|
||||
self._cmd_body = cmd_body
|
||||
|
||||
@property
|
||||
def _body(self):
|
||||
return bytearray([])
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
return self._cmd_body
|
||||
|
59
custom_components/midea_auto_codec/core/packet_builder.py
Normal file
59
custom_components/midea_auto_codec/core/packet_builder.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from .security import LocalSecurity
|
||||
import datetime
|
||||
|
||||
|
||||
class PacketBuilder:
|
||||
def __init__(self, device_id: int, command):
|
||||
self.command = None
|
||||
self.security = LocalSecurity()
|
||||
# Init the packet with the header data.
|
||||
self.packet = bytearray([
|
||||
# 2 bytes - StaicHeader
|
||||
0x5a, 0x5a,
|
||||
# 2 bytes - mMessageType
|
||||
0x01, 0x11,
|
||||
# 2 bytes - PacketLenght
|
||||
0x00, 0x00,
|
||||
# 2 bytes
|
||||
0x20, 0x00,
|
||||
# 4 bytes - MessageId
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
# 8 bytes - Date&Time
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
# 6 bytes - mDeviceID
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
# 12 bytes
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
])
|
||||
self.packet[12:20] = self.packet_time()
|
||||
self.packet[20:28] = device_id.to_bytes(8, "little")
|
||||
self.command = command
|
||||
|
||||
def finalize(self, msg_type=1):
|
||||
if msg_type != 1:
|
||||
self.packet[3] = 0x10
|
||||
self.packet[6] = 0x7b
|
||||
else:
|
||||
self.packet.extend(self.security.aes_encrypt(self.command))
|
||||
# PacketLenght
|
||||
self.packet[4:6] = (len(self.packet) + 16).to_bytes(2, "little")
|
||||
# Append a basic checksum data(16 bytes) to the packet
|
||||
self.packet.extend(self.encode32(self.packet))
|
||||
return self.packet
|
||||
|
||||
def encode32(self, data: bytearray):
|
||||
return self.security.encode32_data(data)
|
||||
|
||||
@staticmethod
|
||||
def checksum(data):
|
||||
return (~ sum(data) + 1) & 0xff
|
||||
|
||||
@staticmethod
|
||||
def packet_time():
|
||||
t = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")[
|
||||
:16]
|
||||
b = bytearray()
|
||||
for i in range(0, len(t), 2):
|
||||
d = int(t[i:i+2])
|
||||
b.insert(0, d)
|
||||
return b
|
243
custom_components/midea_auto_codec/core/security.py
Normal file
243
custom_components/midea_auto_codec/core/security.py
Normal file
@@ -0,0 +1,243 @@
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
from Crypto.Util.strxor import strxor
|
||||
from Crypto.Random import get_random_bytes
|
||||
from hashlib import md5, sha256
|
||||
import hmac
|
||||
|
||||
|
||||
MSGTYPE_HANDSHAKE_REQUEST = 0x0
|
||||
MSGTYPE_HANDSHAKE_RESPONSE = 0x1
|
||||
MSGTYPE_ENCRYPTED_RESPONSE = 0x3
|
||||
MSGTYPE_ENCRYPTED_REQUEST = 0x6
|
||||
|
||||
|
||||
class CloudSecurity:
|
||||
def __init__(self, login_key, iot_key, hmac_key, fixed_key=None, fixed_iv=None):
|
||||
self._login_key = login_key
|
||||
self._iot_key = iot_key
|
||||
self._hmac_key = hmac_key
|
||||
self._aes_key = None
|
||||
self._aes_iv = None
|
||||
self._fixed_key = format(fixed_key, 'x').encode("ascii") if fixed_key else None
|
||||
self._fixed_iv = format(fixed_iv, 'x').encode("ascii") if fixed_iv else None
|
||||
|
||||
def sign(self, data: str, random: str) -> str:
|
||||
msg = self._iot_key
|
||||
msg += data
|
||||
msg += random
|
||||
sign = hmac.new(self._hmac_key.encode("ascii"), msg.encode("ascii"), sha256)
|
||||
return sign.hexdigest()
|
||||
|
||||
def encrypt_password(self, login_id, data):
|
||||
m = sha256()
|
||||
m.update(data.encode("ascii"))
|
||||
login_hash = login_id + m.hexdigest() + self._login_key
|
||||
m = sha256()
|
||||
m.update(login_hash.encode("ascii"))
|
||||
return m.hexdigest()
|
||||
|
||||
def encrypt_iam_password(self, login_id, data) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
@staticmethod
|
||||
def get_deviceid(username):
|
||||
return md5(f"Hello, {username}!".encode("ascii")).digest().hex()[:16]
|
||||
|
||||
@staticmethod
|
||||
def get_udp_id(appliance_id, method=0):
|
||||
if method == 0:
|
||||
bytes_id = bytes(reversed(appliance_id.to_bytes(8, "big")))
|
||||
elif method == 1:
|
||||
bytes_id = appliance_id.to_bytes(6, "big")
|
||||
elif method == 2:
|
||||
bytes_id = appliance_id.to_bytes(6, "little")
|
||||
else:
|
||||
return None
|
||||
data = bytearray(sha256(bytes_id).digest())
|
||||
for i in range(0, 16):
|
||||
data[i] ^= data[i + 16]
|
||||
return data[0: 16].hex()
|
||||
|
||||
def set_aes_keys(self, key, iv):
|
||||
if isinstance(key, str):
|
||||
key = key.encode("ascii")
|
||||
if isinstance(iv, str):
|
||||
iv = iv.encode("ascii")
|
||||
self._aes_key = key
|
||||
self._aes_iv = iv
|
||||
|
||||
def aes_encrypt_with_fixed_key(self, data):
|
||||
return self.aes_encrypt(data, self._fixed_key, self._fixed_iv)
|
||||
|
||||
def aes_decrypt_with_fixed_key(self, data):
|
||||
return self.aes_decrypt(data, self._fixed_key, self._fixed_iv)
|
||||
|
||||
def aes_encrypt(self, data, key=None, iv=None):
|
||||
if key is not None:
|
||||
aes_key = key
|
||||
aes_iv = iv
|
||||
else:
|
||||
aes_key = self._aes_key
|
||||
aes_iv = self._aes_iv
|
||||
if aes_key is None:
|
||||
raise ValueError("Encrypt need a key")
|
||||
if isinstance(data, str):
|
||||
data = bytes.fromhex(data)
|
||||
if aes_iv is None: # ECB
|
||||
return AES.new(aes_key, AES.MODE_ECB).encrypt(pad(data, 16))
|
||||
else: # CBC
|
||||
return AES.new(aes_key, AES.MODE_CBC, iv=aes_iv).encrypt(pad(data, 16))
|
||||
|
||||
def aes_decrypt(self, data, key=None, iv=None):
|
||||
if key is not None:
|
||||
aes_key = key
|
||||
aes_iv = iv
|
||||
else:
|
||||
aes_key = self._aes_key
|
||||
aes_iv = self._aes_iv
|
||||
if aes_key is None:
|
||||
raise ValueError("Encrypt need a key")
|
||||
if isinstance(data, str):
|
||||
data = bytes.fromhex(data)
|
||||
if aes_iv is None: # ECB
|
||||
return unpad(AES.new(aes_key, AES.MODE_ECB).decrypt(data), len(aes_key)).decode()
|
||||
else: # CBC
|
||||
return unpad(AES.new(aes_key, AES.MODE_CBC, iv=aes_iv).decrypt(data), len(aes_key)).decode()
|
||||
|
||||
|
||||
class MeijuCloudSecurity(CloudSecurity):
|
||||
def __init__(self, login_key, iot_key, hmac_key):
|
||||
super().__init__(login_key, iot_key, hmac_key,
|
||||
10864842703515613082)
|
||||
|
||||
def encrypt_iam_password(self, login_id, data) -> str:
|
||||
md = md5()
|
||||
md.update(data.encode("ascii"))
|
||||
md_second = md5()
|
||||
md_second.update(md.hexdigest().encode("ascii"))
|
||||
return md_second.hexdigest()
|
||||
|
||||
|
||||
class MSmartCloudSecurity(CloudSecurity):
|
||||
def __init__(self, login_key, iot_key, hmac_key):
|
||||
super().__init__(login_key, iot_key, hmac_key,
|
||||
13101328926877700970,
|
||||
16429062708050928556)
|
||||
|
||||
def encrypt_iam_password(self, login_id, data) -> str:
|
||||
md = md5()
|
||||
md.update(data.encode("ascii"))
|
||||
md_second = md5()
|
||||
md_second.update(md.hexdigest().encode("ascii"))
|
||||
login_hash = login_id + md_second.hexdigest() + self._login_key
|
||||
sha = sha256()
|
||||
sha.update(login_hash.encode("ascii"))
|
||||
return sha.hexdigest()
|
||||
|
||||
def set_aes_keys(self, encrypted_key, encrypted_iv):
|
||||
key_digest = sha256(self._login_key.encode("ascii")).hexdigest()
|
||||
tmp_key = key_digest[:16].encode("ascii")
|
||||
tmp_iv = key_digest[16:32].encode("ascii")
|
||||
self._aes_key = self.aes_decrypt(encrypted_key, tmp_key, tmp_iv).encode('ascii')
|
||||
self._aes_iv = self.aes_decrypt(encrypted_iv, tmp_key, tmp_iv).encode('ascii')
|
||||
|
||||
|
||||
class LocalSecurity:
|
||||
def __init__(self):
|
||||
self.blockSize = 16
|
||||
self.iv = b"\0" * 16
|
||||
self.aes_key = bytes.fromhex(
|
||||
format(141661095494369103254425781617665632877, 'x')
|
||||
)
|
||||
self.salt = bytes.fromhex(
|
||||
format(233912452794221312800602098970898185176935770387238278451789080441632479840061417076563, 'x')
|
||||
)
|
||||
self._tcp_key = None
|
||||
self._request_count = 0
|
||||
self._response_count = 0
|
||||
|
||||
def aes_decrypt(self, raw):
|
||||
try:
|
||||
return unpad(AES.new(self.aes_key, AES.MODE_ECB).decrypt(bytearray(raw)), 16)
|
||||
except ValueError as e:
|
||||
return bytearray(0)
|
||||
|
||||
def aes_encrypt(self, raw):
|
||||
return AES.new(self.aes_key, AES.MODE_ECB).encrypt(bytearray(pad(raw, 16)))
|
||||
|
||||
def aes_cbc_decrypt(self, raw, key):
|
||||
return AES.new(key=key, mode=AES.MODE_CBC, iv=self.iv).decrypt(raw)
|
||||
|
||||
def aes_cbc_encrypt(self, raw, key):
|
||||
return AES.new(key=key, mode=AES.MODE_CBC, iv=self.iv).encrypt(raw)
|
||||
|
||||
def encode32_data(self, raw):
|
||||
return md5(raw + self.salt).digest()
|
||||
|
||||
def tcp_key(self, response, key):
|
||||
if response == b"ERROR":
|
||||
raise Exception("authentication failed")
|
||||
if len(response) != 64:
|
||||
raise Exception("unexpected data length")
|
||||
payload = response[:32]
|
||||
sign = response[32:]
|
||||
plain = self.aes_cbc_decrypt(payload, key)
|
||||
if sha256(plain).digest() != sign:
|
||||
raise Exception("sign does not match")
|
||||
self._tcp_key = strxor(plain, key)
|
||||
self._request_count = 0
|
||||
self._response_count = 0
|
||||
return self._tcp_key
|
||||
|
||||
def encode_8370(self, data, msgtype):
|
||||
header = bytearray([0x83, 0x70])
|
||||
size, padding = len(data), 0
|
||||
if msgtype in (MSGTYPE_ENCRYPTED_RESPONSE, MSGTYPE_ENCRYPTED_REQUEST):
|
||||
if (size + 2) % 16 != 0:
|
||||
padding = 16 - (size + 2 & 0xf)
|
||||
size += padding + 32
|
||||
data += get_random_bytes(padding)
|
||||
header += size.to_bytes(2, "big")
|
||||
header += bytearray([0x20, padding << 4 | msgtype])
|
||||
data = self._request_count.to_bytes(2, "big") + data
|
||||
self._request_count += 1
|
||||
if self._request_count >= 0xFFFF:
|
||||
self._request_count = 0
|
||||
if msgtype in (MSGTYPE_ENCRYPTED_RESPONSE, MSGTYPE_ENCRYPTED_REQUEST):
|
||||
sign = sha256(header + data).digest()
|
||||
data = self.aes_cbc_encrypt(raw=data, key=self._tcp_key) + sign
|
||||
return header + data
|
||||
|
||||
def decode_8370(self, data):
|
||||
if len(data) < 6:
|
||||
return [], data
|
||||
header = data[:6]
|
||||
if header[0] != 0x83 or header[1] != 0x70:
|
||||
raise Exception("not an 8370 message")
|
||||
size = int.from_bytes(header[2:4], "big") + 8
|
||||
leftover = None
|
||||
if len(data) < size:
|
||||
return [], data
|
||||
elif len(data) > size:
|
||||
leftover = data[size:]
|
||||
data = data[:size]
|
||||
if header[4] != 0x20:
|
||||
raise Exception("missing byte 4")
|
||||
padding = header[5] >> 4
|
||||
msgtype = header[5] & 0xf
|
||||
data = data[6:]
|
||||
if msgtype in (MSGTYPE_ENCRYPTED_RESPONSE, MSGTYPE_ENCRYPTED_REQUEST):
|
||||
sign = data[-32:]
|
||||
data = data[:-32]
|
||||
data = self.aes_cbc_decrypt(raw=data, key=self._tcp_key)
|
||||
if sha256(header + data).digest() != sign:
|
||||
raise Exception("sign does not match")
|
||||
if padding:
|
||||
data = data[:-padding]
|
||||
self._response_count = int.from_bytes(data[:2], "big")
|
||||
data = data[2:]
|
||||
if leftover:
|
||||
packets, incomplete = self.decode_8370(leftover)
|
||||
return [data] + packets, incomplete
|
||||
return [data], b""
|
Reference in New Issue
Block a user