mac-zhou / midea-ac-py

Home Assistant Custom Integration for Midea Group(Hualing, Senville, Klimaire, AirCon, Century, Pridiom, Thermocore, Comfee, Toshiba, Carrier, Goodman, Friedrich, Samsung, Kenmore, Trane, Lennox, LG and much more) Air Conditioners via LAN.
MIT License
519 stars 101 forks source link

AC doesn't connect. #130

Closed KhArtNJava closed 2 years ago

KhArtNJava commented 2 years ago

Versions (版本信息)

Describe the bug (描述一下问题) The issue very similar to https://github.com/mac-zhou/midea-ac-py/issues/121 . midea-discover returns different token and key at each run: First run:

c:\Python310\Scripts>midea-discover
INFO:msmart.cli:msmart version: 0.2.0 Currently only supports ac devices.
ERROR:msmart.security:authentication failed
INFO:msmart.lan:Got TCP key for 192.168.0.107:51577 -> 192.168.0.104:6444 retries: 0 time: 0.32 e994220f1b2281c899e02cbcfc61c844a2149ff05fc10b9f0224a88301590029
INFO:msmart.cli:*** Found a device: ←[94m←[1m{'type': 'ac', 'support': True, 'version': 3, 'ip': '192.168.0.104', 'id': 35184372114367, 'port': 6444, 'token': '9D0C185355F5F1D4EFABDF9739D0AA74EF98DBE0906700933F36DC98082F6D61F7BAFB21CE19C54FE9E16C7383A7838BB33EA27BEE0B333B50D94FF24FC4FB90', 'key': '0D6C677656294787A73F6CF29607890971585DBD265149D3A761EBCF7172C365', 'ssid': 'net_ac_093C'} ←[0m

Second run:

c:\Python310\Scripts>midea-discover
INFO:msmart.cli:msmart version: 0.2.0 Currently only supports ac devices.
ERROR:msmart.security:authentication failed
INFO:msmart.lan:Got TCP key for 192.168.0.107:51586 -> 192.168.0.104:6444 retries: 0 time: 0.37 e420e1174300eaa7cce4f002004e2aad198ca2341135e68a43e0da38912a3185
INFO:msmart.cli:*** Found a device: ←[94m←[1m{'type': 'ac', 'support': True, 'version': 3, 'ip': '192.168.0.104', 'id': 35184372114367, 'port': 6444, 'token': '507AA10FD3CD08B5351520B8360C49B5AEC09C9EA10123FF45F771C96892BF3125B20A1FA70C05E208E0952D7D74D9574CE2CC0809B6F7FAE0BC613099A213AB', 'key': '40E2C85D7D8B41CCBBA25A683A2C01C78748095E29234DE0A9A5715228069AEF', 'ssid': 'net_ac_093C'} ←[0m

With configuration

climate:
  - platform: midea_ac
    host: 192.168.1.104
    id: 35184372114367
    # v3 need token and k1(key)
    token: 507AA10FD3CD08B5351520B8360C49B5AEC09C9EA10123FF45F771C96892BF3125B20A1FA70C05E208E0952D7D74D9574CE2CC0809B6F7FAE0BC613099A213AB
    k1: 40E2C85D7D8B41CCBBA25A683A2C01C78748095E29234DE0A9A5715228069AEF

HA can't connect to AC, with errors in logs:

2022-02-21 01:41:32 ERROR (MainThread) [homeassistant.components.automation.update_input_select_fan_mode] Update Input Select Fan Mode: Error executing script. Invalid data for call_service at pos 1: string value is None for dictionary value @ data['option']
2022-02-21 01:41:32 ERROR (MainThread) [homeassistant.components.automation.update_input_select_fan_mode] Error while executing automation automation.update_input_select_fan_mode: string value is None for dictionary value @ data['option']
2022-02-21 01:41:35 ERROR (SyncWorker_3) [msmart.lan] Connect Error: 192.168.1.104:6444 timed out
2022-02-21 01:41:35 ERROR (SyncWorker_3) [msmart.lan] Sokcet is None: 192.168.1.104:6444
2022-02-21 01:41:35 ERROR (SyncWorker_3) [msmart.security] unexpected data length
2022-02-21 01:41:35 ERROR (MainThread) [homeassistant.helpers.entity] Update for climate.midea_ac_35184372114367 fails
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 521, in async_update_ha_state
await self.async_device_update()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 725, in async_device_update
raise exc
File "/config/custom_components/midea_ac/climate.py", line 133, in async_update
await self.hass.async_add_executor_job(self._device.refresh)
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.9/site-packages/msmart/device.py", line 198, in refresh
self._send_cmd(cmd)
File "/usr/local/lib/python3.9/site-packages/msmart/device.py", line 208, in _send_cmd
responses = self._lan_service.appliance_transparent_send_8370(data)
File "/usr/local/lib/python3.9/site-packages/msmart/lan.py", line 136, in appliance_transparent_send_8370
data = self.security.encode_8370(data, msgtype)
File "/usr/local/lib/python3.9/site-packages/msmart/security.py", line 120, in encode_8370
data = self.aes_cbc_encrypt(data, self._tcp_key) + sign
File "/usr/local/lib/python3.9/site-packages/msmart/security.py", line 57, in aes_cbc_encrypt
return AES.new(key, AES.MODE_CBC, iv=self.iv).encrypt(raw)
File "/usr/local/lib/python3.9/site-packages/Cryptodome/Cipher/AES.py", line 232, in new
return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/Cryptodome/Cipher/__init__.py", line 79, in _create_cipher
return modes[mode](factory, **kwargs)
File "/usr/local/lib/python3.9/site-packages/Cryptodome/Cipher/_mode_cbc.py", line 274, in _create_cbc_cipher
cipher_state = factory._create_base_cipher(kwargs)
File "/usr/local/lib/python3.9/site-packages/Cryptodome/Cipher/AES.py", line 92, in _create_base_cipher
if len(key) not in key_size:
TypeError: object of type 'NoneType' has no len()
2022-02-21 01:41:50 ERROR (SyncWorker_0) [msmart.lan] Connect Error: 192.168.1.104:6444 timed out
2022-02-21 01:41:50 ERROR (SyncWorker_0) [msmart.lan] Sokcet is None: 192.168.1.104:6444
2022-02-21 01:41:50 ERROR (SyncWorker_0) [msmart.security] unexpected data length
2022-02-21 01:41:50 ERROR (MainThread) [homeassistant.helpers.entity] Update for climate.midea_ac_35184372114367 fails
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 521, in async_update_ha_state
await self.async_device_update()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 725, in async_device_update
raise exc
File "/config/custom_components/midea_ac/climate.py", line 133, in async_update
await self.hass.async_add_executor_job(self._device.refresh)
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.9/site-packages/msmart/device.py", line 198, in refresh
self._send_cmd(cmd)
File "/usr/local/lib/python3.9/site-packages/msmart/device.py", line 208, in _send_cmd
responses = self._lan_service.appliance_transparent_send_8370(data)
File "/usr/local/lib/python3.9/site-packages/msmart/lan.py", line 136, in appliance_transparent_send_8370
data = self.security.encode_8370(data, msgtype)
File "/usr/local/lib/python3.9/site-packages/msmart/security.py", line 120, in encode_8370
data = self.aes_cbc_encrypt(data, self._tcp_key) + sign
File "/usr/local/lib/python3.9/site-packages/msmart/security.py", line 57, in aes_cbc_encrypt
return AES.new(key, AES.MODE_CBC, iv=self.iv).encrypt(raw)
File "/usr/local/lib/python3.9/site-packages/Cryptodome/Cipher/AES.py", line 232, in new
return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/Cryptodome/Cipher/__init__.py", line 79, in _create_cipher
return modes[mode](factory, **kwargs)
File "/usr/local/lib/python3.9/site-packages/Cryptodome/Cipher/_mode_cbc.py", line 274, in _create_cbc_cipher
cipher_state = factory._create_base_cipher(kwargs)
File "/usr/local/lib/python3.9/site-packages/Cryptodome/Cipher/AES.py", line 92, in _create_base_cipher
if len(key) not in key_size:
TypeError: object of type 'NoneType' has no len()

Here is log file attached home-assistant.zip

When I scan opened ports of 192.168.1.104 with Network Scanner (Android app), I see one opened port 6444/tcp (unknown). When I trying to connect with telnet telnet 192.168.1.104 6444 Connect failed:

C:\Users\art>telnet 192.168.1.104 6444
Connecting To 192.168.1.104...Could not open connection to the host, on port 6444: Connect failed

So, AC has opened port, that can't be accessed with telnet and, probably, HA too (ERROR (SyncWorker_3) [msmart.lan] Connect Error: 192.168.1.104:6444 timed out).

Any ideas, my friends?..

megakid commented 2 years ago

Interestingly I had similar symptoms on my recently installed Midea unit. I ran the discover and it came back with a set of token/key and the controls worked with a HACS install. Nice and easy.

A day later (today), I couldn't control the unit - cloud worked via the Midea Air app - but after rerunning discover, I get a brand new key and token (same local IP).

My worry is this will continue to happen but I hope it's solved now

mac-zhou commented 2 years ago

i saw your midea-discover log, it's ok. got token and key mean device's port(6444) is open and connected. I don't know why can't connect in HA and telenet. it's weird.

mac-zhou commented 2 years ago

Interestingly I had similar symptoms on my recently installed Midea unit. I ran the discover and it came back with a set of token/key and the controls worked with a HACS install. Nice and easy.

A day later (today), I couldn't control the unit - cloud worked via the Midea Air app - but after rerunning discover, I get a brand new key and token (same local IP).

My worry is this will continue to happen but I hope it's solved now

no need to get new token and key. it can reused. show me your home-assistant.log please.

KhArtNJava commented 2 years ago

I didn't understand phrase "befor restart HA", so I uploaded two files. The first one is: I added

logger:
  default: warn
  logs:
    custom_components.midea_smart: debug
    msmart: debug

to config file and waited when error occurred again - link to this file is https://github.com/mac-zhou/midea-ac-py/files/8106235/home-assistant.zip .

After that I restarted HA and got a new one log: https://github.com/mac-zhou/midea-ac-py/files/8106253/home-assistant.zip Looks like logger settings applied after HA restart...

KhArtNJava commented 2 years ago

Update: I don't know, what happened yesterday, but today I can connect to 192.168.0.104 by 6444 port from my Windows machine: telnet 192.168.0.104 6444 So, 6444 port is really opened.

KhArtNJava commented 2 years ago

Also, I can connect to 6444 from HA machine. I've installed Terminal & SSH add-on on my HA, so I've tested port with commands:

apk update
apk add busybox-extras

After that it connected very well:

[core-ssh ~]$ telnet 192.168.0.104 6444
Connected to 192.168.0.104
KhArtNJava commented 2 years ago

But it still doesn't work, uploaded a new one updated log file https://github.com/mac-zhou/midea-ac-py/files/8106364/home-assistant.zip

2022-02-21 08:06:59 DEBUG (SyncWorker_5) [msmart.command] Finalize request data: aa23ac0000000000000240428e6603ff00300000000000000000000000003b000000ee5e
2022-02-21 08:06:59 DEBUG (SyncWorker_5) [msmart.device] pkt_builder: 192.168.1.104:6444 len: 104 data: 5a5a011168002000000000005c3b060815021614bf630000002000000000000000000000000000008a4048d48d872cde0249854df66e61173b800a77b3ae27242d1d2664e5b2e612e69219a9bd0152d46683bd6c079bc7f74be3ea1fe89b5ce40e496b6897273f28
2022-02-21 08:06:59 DEBUG (SyncWorker_5) [msmart.lan] Socket None -> 192.168.1.104:6444 retries: 0 time: 1640.92 Closed, Create New Socket
2022-02-21 08:06:59 DEBUG (SyncWorker_5) [msmart.lan] Attempting new connection to 192.168.1.104:6444
2022-02-21 08:07:01 ERROR (SyncWorker_5) [msmart.lan] Connect Error: 192.168.1.104:6444 timed out
2022-02-21 08:07:01 ERROR (SyncWorker_5) [msmart.lan] Sokcet is None: 192.168.1.104:6444
2022-02-21 08:07:01 ERROR (SyncWorker_5) [msmart.security] unexpected data length
2022-02-21 08:07:01 ERROR (MainThread) [homeassistant.helpers.entity] Update for climate.midea_ac_35184372114367 fails
KhArtNJava commented 2 years ago

So, Windows machine can open 6444 port with telnet. Also telnet connected to 6444 port from HAOS. May be need to make some HaOS configurations to allow HA connect to other IPs by 6444 port? Is there any kind of firewall in HaOS?

mac-zhou commented 2 years ago

Also, I can connect to 6444 from HA machine. I've installed Terminal & SSH add-on on my HA, so I've tested port with commands:

apk update
apk add busybox-extras

After that it connected very well:

[core-ssh ~]$ telnet 192.168.0.104 6444
Connected to 192.168.0.104

Did you run midea-discover on the HA machine?

KhArtNJava commented 2 years ago

Also, I can connect to 6444 from HA machine. I've installed Terminal & SSH add-on on my HA, so I've tested port with commands:

apk update
apk add busybox-extras

After that it connected very well:

[core-ssh ~]$ telnet 192.168.0.104 6444
Connected to 192.168.0.104

Did you run midea-discover on the HA machine?

Thank you for your reply. No, I did run on my Windows machine, because I don't know how to install midea-discover on HaOS. bash console of HaOS (SSH add-on) is not the same as, for example, Ubuntu console, that I usually use.

Can you advise how to install it on HaOS (raspberry pi 4)?

mac-zhou commented 2 years ago

I guess you have solved it? 😄

megakid commented 2 years ago

After my above issue and recreating the tokens, it’s working well. Thanks!

On Wed, 23 Feb 2022 at 09:12, Mac_Zhou @.***> wrote:

I guess you have solved it? 😄

— Reply to this email directly, view it on GitHub https://github.com/mac-zhou/midea-ac-py/issues/130#issuecomment-1048576425, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALI243U2NQFYSWEOTM6KTDU4SQGJANCNFSM5O5AX7EA . You are receiving this because you commented.Message ID: @.***>

megakid commented 2 years ago

It struck again, after a few days of successful control, at 1528 today (no HA restarts, I wasn't home etc) I started getting these messages in the log:

2022-02-23 15:28:09 WARNING (SyncWorker_12) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:28:24 WARNING (SyncWorker_4) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:28:39 WARNING (SyncWorker_13) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:28:54 WARNING (SyncWorker_13) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:29:09 WARNING (SyncWorker_20) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:29:24 WARNING (SyncWorker_18) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:29:39 WARNING (SyncWorker_10) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:29:54 WARNING (SyncWorker_6) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:30:09 WARNING (SyncWorker_5) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:30:24 WARNING (SyncWorker_15) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:30:39 WARNING (SyncWorker_17) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:30:54 WARNING (SyncWorker_6) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:31:09 WARNING (SyncWorker_17) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:31:24 WARNING (SyncWorker_0) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:31:39 WARNING (SyncWorker_16) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:31:54 WARNING (SyncWorker_15) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:32:09 WARNING (SyncWorker_2) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:32:24 WARNING (SyncWorker_16) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:32:39 WARNING (SyncWorker_14) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:32:54 WARNING (SyncWorker_14) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:33:09 WARNING (SyncWorker_14) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:33:24 WARNING (SyncWorker_11) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:33:39 WARNING (SyncWorker_0) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:33:54 WARNING (SyncWorker_9) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:34:09 WARNING (SyncWorker_5) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 15:34:24 WARNING (SyncWorker_10) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
...
2022-02-23 19:19:40 WARNING (SyncWorker_13) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:19:55 WARNING (SyncWorker_11) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:20:10 WARNING (SyncWorker_5) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:20:25 WARNING (SyncWorker_4) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:20:40 WARNING (SyncWorker_2) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:20:55 WARNING (SyncWorker_0) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:21:10 WARNING (SyncWorker_1) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:21:55 WARNING (SyncWorker_3) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:22:10 WARNING (SyncWorker_0) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:22:25 WARNING (SyncWorker_12) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:22:40 WARNING (SyncWorker_2) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:22:55 WARNING (SyncWorker_21) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:23:10 WARNING (SyncWorker_16) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:23:25 WARNING (SyncWorker_16) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 19:23:40 WARNING (SyncWorker_18) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
...
2022-02-23 20:09:56 WARNING (SyncWorker_12) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 20:10:11 WARNING (SyncWorker_3) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 20:10:26 WARNING (SyncWorker_2) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 20:10:41 WARNING (SyncWorker_9) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 20:10:56 WARNING (SyncWorker_0) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0
2022-02-23 20:11:11 WARNING (SyncWorker_1) [msmart.device] Got Null from 192.168.4.107:6444 Version: 3 Count: 0 Spend time: 0.0

Turns out a restart of HA solved it but something goes wrong randomly I think.

I have now tried this https://github.com/mac-zhou/midea-ac-py/issues/132#issuecomment-1047348316 so will keep you updated.

KhArtNJava commented 2 years ago

I guess you have solved it? 😄

Yep, thank you. Dug deep into HA, learnt a lot of new for me and found that it was my configuration mistake in IP. Thank you a lot for your work! You are great!

gsotiriou commented 2 years ago

I guess you have solved it? 😄

Yep, thank you. Dug deep into HA, learnt a lot of new for me and found that it was my configuration mistake in IP. Thank you a lot for your work! You are great!

Could you explain a little bit what you mean and how you solved this?