Skip to content

Instantly share code, notes, and snippets.

@InsaneWookie
Last active August 26, 2024 10:37
Show Gist options
  • Save InsaneWookie/1221cd6267745ea3c16f6a2a83ba3a44 to your computer and use it in GitHub Desktop.
Save InsaneWookie/1221cd6267745ea3c16f6a2a83ba3a44 to your computer and use it in GitHub Desktop.
Configure Aqara M1S Gateway for Home Assistant

Connect to Gateway

  1. Download the Xiaomi app https://play.google.com/store/apps/details?id=com.xiaomi.smarthome
  2. Create an account
  3. Add the M1S Gateway in the app (hold button on M1S for 10 seconds to enter discovery mode)

Extract key

  1. Folow the instuctions here: https://github.com/PiotrMachowski/Xiaomi-cloud-tokens-extractor
  2. Copy the output

You should get an output something like the following

Devices found for server "cn":
---------
NAME: Aqara Hub M1S
ID: 12345678
IP: <gateway_ip>
TOKEN: <gateway_token>
MODEL: lumi.gateway.acn01
---------

Install the python-miio library https://github.com/rytilahti/python-miio

  1. Install library pip3 install python-miio
  2. Run the following to verify install miiocli device --ip <gateway_ip> --token <gateway_token> info

Should ouput something like

Model: lumi.gateway.acn01
Hardware version: Linux
Firmware version: 3.1.3_0011

Enable telnet

  1. Run the following command to enable telent (more details here: https://gist.github.com/zvldz/1bd6b21539f84339c218f9427e022709)
  2. miiocli device --ip <gateway_id> --token <gateway_token> raw_command set_ip_info '{"ssid":"\"\"","pswd":"123123 ; passwd -d admin ; echo enable > /sys/class/tty/tty/enable; telnetd"}'

Update M1S to work with Home Assistant

  1. Login to Gateway with telnet <gateway_ip>
  2. Run the following commands from https://github.com/niceboygithub/AqaraGateway
mkdir /data/bin
cd /data/bin
wget -O /data/bin/curl "http://master.dl.sourceforge.net/project/mgl03/bin/curl?viasf=1"
chmod +x /data/bin/curl
/data/bin/curl -s -k -L -o /data/bin/mosquitto https://raw.githubusercontent.com/niceboygithub/AqaraM1SM2fw/main/binutils/mosquitto
chmod a+x /data/bin/mosquitto

mkdir /data/scripts
cd /data/scripts
/data/bin/curl -s -k -L -o /data/scripts/post_init.sh https://raw.githubusercontent.com/niceboygithub/AqaraM1SM2fw/main/binutils/post_init.sh
chmod +x /data/scripts/post_init.sh
  1. Reboot M1S with reboot

Install HACS

  1. Follow install instructions from https://hacs.xyz/docs/installation/prerequisites

Install niceboygithub/AqaraGateway Integration

  1. Navigate to HACS > Integrations
  2. Three dots top right > Custom repositories
  3. Add

This may take a while, and there is a posibility you will run into GitHub rate limits

Add AqaraGateway Integration

  1. Configuration > Integrations
  2. Add Integration button
  3. Seach for Aqara Gateway
  4. Add
    • Host: <gateway_ip>
    • Password:
    • Access Token: <gateway_token>
    • Model: Aqara Gateway M1S
  5. If everything went well you should see home assistant add some new devices
@SlippinDylan
Copy link

Hi, after I bound my m1s to mija, and after switching to aqara mode and binding to the aqara platform after turning on telnet, the ha successfully discovered my device, but in the ha, the entity for all devices in aqara is showing unavailable

@nikitakvitov
Copy link

nikitakvitov commented Nov 16, 2023

please help Running command info
ERROR:miio.miioprotocol:Got error when receiving: timed out
ERROR:miio.click_common:Exception: No response from the device
Traceback (most recent call last):
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\miioprotocol.py", line 194, in send
data, addr = s.recvfrom(4096)
^^^^^^^^^^^^^^^^
TimeoutError: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\miioprotocol.py", line 194, in send
data, addr = s.recvfrom(4096)
^^^^^^^^^^^^^^^^
TimeoutError: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\miioprotocol.py", line 194, in send
data, addr = s.recvfrom(4096)
^^^^^^^^^^^^^^^^
TimeoutError: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\miioprotocol.py", line 194, in send
data, addr = s.recvfrom(4096)
^^^^^^^^^^^^^^^^
TimeoutError: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\click_common.py", line 54, in call
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\click_common.py", line 308, in wrap
and getattr(result, "cli_output", None) is not None
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\deviceinfo.py", line 106, in cli_output
s += f"Supported by genericmiot: {dev.supports_miot()}"
^^^^^^^^^^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\device.py", line 313, in supports_miot
self.send("get_properties", [{"did": "dummy", "siid": 1, "piid": 1}])
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\device.py", line 98, in send
return self._protocol.send(
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\miioprotocol.py", line 234, in send
return self.send(
^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\miioprotocol.py", line 234, in send
return self.send(
^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\miioprotocol.py", line 234, in send
return self.send(
^^^^^^^^^^
File "C:\Users\nik\AppData\Local\Programs\Python\Python311\Lib\site-packages\miio\miioprotocol.py", line 242, in send
raise DeviceException("No response from the device") from ex
miio.exceptions.DeviceException: No response from the device
the network is the same both on the gateway and on the PC I work in windows 11
Python ver:3.11.5

@kai-zer-ru
Copy link

https://dzen.ru/a/Zg5nNBHnsk6exK_f - read the article, I've written everything down point by point. Thanks

@mcnguyen33r
Copy link

https://dzen.ru/a/Zg5nNBHnsk6exK_f - read the article, I've written everything down point by point. Thanks

Hi,
Can you tell me if I have to enable telnet first?

@kai-zer-ru
Copy link

https://dzen.ru/a/Zg5nNBHnsk6exK_f - read the article, I've written everything down point by point. Thanks

Hi, Can you tell me if I have to enable telnet first?

Yes, open telnet - first step

@mcnguyen33r
Copy link

Thank you for quick reply.
I's my problem, I try enabling telnet but failed always

@mcnguyen33r
Copy link

mcnguyen33r commented Apr 28, 2024

I re-tried for times but after enabling telnet successfully (with OK confimation), the hub became disconnected from the network.
Can you tell me what can I do next?

@kai-zer-ru
Copy link

I re-tried for time but after the enabling telnet successfully (with OK confimation), the hub became disconnected from the network. Can you tell me what can I do next?

hi. I have never encountered such a thing. everything went great for me, I repeated it several times before writing the article. try connecting it to another Wi-Fi network. if this does not help, then the problem is in the gateway itself. and another question - did you switch it back to aqara home mode?

@mcnguyen33r
Copy link

Many thanks for your quick reply.
I've not yet backed to Aqara home, as I haven't flash customed firmware yet, just telneted, then it disconected from the network, nothing more steps can be done

@kai-zer-ru
Copy link

Many thanks for your quick reply. I've not yet backed to Aqara home, as I haven't flash customed firmware yet, just telneted, then it disconected from the network, nothing more steps can be done

how do you determine that the gateway is disconnected from the network? in MiHome, ping, or otherwise? do you enter the correct ip address when connecting via telnet?

@mcnguyen33r
Copy link

I determined by:

  • Asus Router App (I fixed IP address for the hub)
  • Mihome shows it offline

@kai-zer-ru
Copy link

I determined by:

  • Asus Router App (I fixed IP address for the hub)
  • Mihome shows it offline

does the gateway disappear from the network immediately after opening telnet? restarting the gateway doesn't help? disconnect it from the 220 volt mains and turn it on after a few seconds. telnet should remain on at the same time

@mcnguyen33r
Copy link

After I enter this command ,,miiocli device --ip <gateway_id> --token <gateway_token> raw_command set_ip_info '{"ssid":"""", "pswd": "123123 ; passwd -d admin ; echo enable > /sys/class/tty/tty/enable; telnetd"}' '' comes up

Running command raw_command ['ok']

but after that the hub M1S loses the network connection and I can't run telnet ? Where is my problem ?

Same to mine

@kai-zer-ru
Copy link

kai-zer-ru commented Apr 28, 2024

After I enter this command ,,miiocli device --ip <gateway_id> --token <gateway_token> raw_command set_ip_info '{"ssid":"""", "pswd": "123123 ; passwd -d admin ; echo enable > /sys/class/tty/tty/enable; telnetd"}' '' comes up
Running command raw_command ['ok']
but after that the hub M1S loses the network connection and I can't run telnet ? Where is my problem ?

Same to mine

read the article carefully. Now this method does not work. you need to switch the gateway to the mode of working with MiHome, add it to the application. After this, press the button 5 times in a row. wait half a second, press 2 more times, wait again, 2 more times. and so on until the gateway pronounces a voice message (possibly in Chinese). this will mean that telnet is enabled. Now you can connect to it from the terminal via telnet. you don't need to enter your command

https://dzen.ru/a/Zg5nNBHnsk6exK_f

@mcnguyen33r
Copy link

Hi,
I tried to soft method by pressing 5 times, pause, 2 times, pause….
But it only activates the arm/ disarm the hub (trying pressing both quick and slow…)
So sad

@kai-zer-ru
Copy link

Hi, I tried to soft method by pressing 5 times, pause, 2 times, pause…. But it only activates the arm/ disarm the hub (trying pressing both quick and slow…) So sad

I tried activating telnet this way about 20 times before I wrote the article. you need to press it with short taps. and it is in the MiHome mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment