Skip to content

Instantly share code, notes, and snippets.

@InsaneWookie
Last active April 7, 2024 11:54
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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
@niceboygithub
Copy link

Thanks, for your instructions.

I already added the feature to enable telnet in the integration to simply the instructions. Cause install php-miio library or python-miio for Windows User is not convenient. So if user provides token in the settings, it will try to enable telnet.

@rdeeb
Copy link

rdeeb commented Jun 22, 2021

With this integration will I be able to use the speaker/alarm feature of the Hub?

@chrisrock1984
Copy link

does not work for me - always says "aborted"
telnet is enabled and works
tested without and with a password set
tested with token and without

@stockmopar
Copy link

@rdeeb

Did you happen to find an answer to your question?

With this integration will I be able to use the speaker/alarm feature of the Hub?

@niceboygithub

Or asked another way Is there functionality similar to:

xiaomi_aqara.play_ringtone

So that I could use the hub as both a doorbell chime as well as siren?

@rdeeb
Copy link

rdeeb commented Feb 28, 2022 via email

@acato
Copy link

acato commented Jul 16, 2022

I keep getting a timeout when trying to enable telnet:

miiocli device --ip --token raw_command set_ip_info '{"ssid":"""","pswd":"123123 ; passwd -d admin ; echo enable > /sys/class/tty/tty/enable; telnetd"}'
Running command raw_command
ERROR:miio.miioprotocol:Got error when receiving: timed out
Error: No response from the device

The firmware is version 3.1.3_0009

Any ideas?

@AquaCobell
Copy link

I always get a syntax error
miiocli device --ip 192.168.188.43 --token xxxxx... raw_command set_ip_info '{"ssid":"""","pswd":"123123 ; passwd -d admin ; echo enable > /sys/class/tty/tty/enable; telnetd"}'
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\Python310\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "C:\Users\Nico\AppData\Roaming\Python\Python310\Scripts\miiocli.exe_main
.py", line 7, in
File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\miio\cli.py", line 66, in create_cli
return cli(auto_envvar_prefix="MIIO")
File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\miio\click_common.py", line 51, in call
return self.main(*args, **kwargs)
File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 1055, in main
rv = self.invoke(ctx)
File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 1655, in invoke
sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 920, in make_context
self.parse_args(ctx, args)

@Amigos26
Copy link

I always get a syntax error miiocli device --ip 192.168.188.43 --token xxxxx... raw_command set_ip_info '{"ssid":"""","pswd":"123123 ; passwd -d admin ; echo enable > /sys/class/tty/tty/enable; telnetd"}' Traceback (most recent call last): File "C:\Program Files\Python310\lib\runpy.py", line 196, in _run_module_as_main return run_code(code, main_globals, None, File "C:\Program Files\Python310\lib\runpy.py", line 86, in run_code exec(code, run_globals) File "C:\Users\Nico\AppData\Roaming\Python\Python310\Scripts\miiocli.exe__main.py", line 7, in File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\miio\cli.py", line 66, in create_cli return cli(auto_envvar_prefix="MIIO") File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\miio\click_common.py", line 51, in call return self.main(*args, **kwargs) File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 1055, in main rv = self.invoke(ctx) File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 1657, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 1655, in invoke sub_ctx = cmd.make_context(cmd_name, args, parent=ctx) File "C:\Users\Nico\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 920, in make_context self.parse_args(ctx, args)

miiocli device --ip 192.168.188.43 --token xxxxx... raw_command set_ip_info '{"ssid":"""","pswd":"123123 ; passwd -d admin ; echo enable > /sys/class/tty/tty/enable; telnetd"}'

@Jens-Wymeersch
Copy link

@InsaneWookie @niceboygithub After the update, will the aqara hub still work with the Xiaomi app and HomeKit ?

@fmTechnix
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 ?

@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

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