{"id":3321,"date":"2020-08-21T13:21:10","date_gmt":"2020-08-21T12:21:10","guid":{"rendered":"https:\/\/einstein.amsterdam\/?page_id=3321"},"modified":"2020-08-22T14:19:12","modified_gmt":"2020-08-22T13:19:12","slug":"mi-home-start","status":"publish","type":"page","link":"https:\/\/einstein.amsterdam\/?page_id=3321","title":{"rendered":"Mi Home Start"},"content":{"rendered":"<div id=\"installation\" class=\"section\">\n<h2>Installation<\/h2>\n<p>The easiest way to install the package is to use pip:\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">pip3<\/span>\u00a0<span class=\"pre\">install<\/span>\u00a0<span class=\"pre\">python-miio<\/span><\/code>\u00a0.\u00a0<a class=\"reference external\" href=\"http:\/\/docs.python-guide.org\/en\/latest\/dev\/virtualenvs\/\">Using virtualenv<\/a>\u00a0is recommended.<\/p>\n<p>Please make sure you have\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">libffi<\/span><\/code>\u00a0and\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">openssl<\/span><\/code>\u00a0headers installed, you can do this on Debian-based systems (like Rasperry Pi) with<\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>apt-get install libffi-dev libssl-dev\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"device-discovery\" class=\"section\">\n<h2>Device discovery<\/h2>\n<p>Devices already connected on the same network where the command-line tool is run are automatically detected when\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">mirobo<\/span>\u00a0<span class=\"pre\">discover<\/span><\/code>\u00a0is invoked.<\/p>\n<p>To be able to communicate with devices their IP address and a device-specific encryption token must be known. If the returned a token is with characters other than\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">0<\/span><\/code>s or\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">f<\/span><\/code>s, it is likely a valid token which can be used directly for communication. If not, the token needs to be extracted from the Mi Home Application, see\u00a0<a class=\"reference internal\" href=\"https:\/\/python-miio.readthedocs.io\/en\/latest\/discovery.html#logged-tokens\"><span class=\"std std-ref\">Tokens from Mi Home logs<\/span><\/a>\u00a0for information how to do this.<\/p>\n<div class=\"admonition important\">\n<p class=\"admonition-title\">Important<\/p>\n<p>For some devices (e.g. the vacuum cleaner) the automatic discovery works only before the device has been connected over the app to your local wifi. This does not work starting from firmware version 3.3.9_003077 onwards, in which case the procedure shown in\u00a0<a class=\"reference internal\" href=\"https:\/\/python-miio.readthedocs.io\/en\/latest\/discovery.html#creating-backup\"><span class=\"std std-ref\">Tokens from backups<\/span><\/a>\u00a0has to be used to obtain the token.<\/p>\n<\/div>\n<div class=\"admonition note\">\n<p class=\"admonition-title\">Note<\/p>\n<p>Some devices also do not announce themselves via mDNS (e.g. Philips\u2019 bulbs, and the vacuum when not connected to the Internet), but are nevertheless discoverable by using a miIO discovery. See\u00a0<a class=\"reference internal\" href=\"https:\/\/python-miio.readthedocs.io\/en\/latest\/discovery.html#handshake-discovery\"><span class=\"std std-ref\">Discovery by a handshake<\/span><\/a>\u00a0for more information about the topic.<\/p>\n<\/div>\n<div id=\"discovery-by-a-handshake\" class=\"section\">\n<p>&nbsp;<\/p>\n<h3>Discovery by a handshake<\/h3>\n<p>The devices supporting miIO protocol answer to a broadcasted handshake packet, which also sometime contain the required token.<\/p>\n<p>Executing\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">mirobo<\/span>\u00a0<span class=\"pre\">discover<\/span><\/code>\u00a0with\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">--handshake<\/span>\u00a0<span class=\"pre\">1<\/span><\/code>\u00a0option will send a broadcast handshake. Devices supporting the protocol will response with a message potentially containing a valid token.<\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>$ mirobo discover --handshake <span class=\"m\">1<\/span>\r\nINFO:miio.device:  IP <span class=\"m\">192<\/span>.168.8.1: Xiaomi Mi Robot Vacuum - token: b<span class=\"s1\">'ffffffffffffffffffffffffffffffff'<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"admonition note\">\n<p class=\"admonition-title\">Note<\/p>\n<p>This method can also be useful for devices not yet connected to any network. In those cases the device trying to do the discovery has to connect to the network advertised by the corresponding device (e.g. rockrobo-XXXX for vacuum)<\/p>\n<\/div>\n<p>Tokens full of\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">0<\/span><\/code>s or\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">f<\/span><\/code>s (as above) are either already paired with the mobile app or will not yield a token through this method. In those cases the procedure shown in\u00a0<a class=\"reference internal\" href=\"https:\/\/python-miio.readthedocs.io\/en\/latest\/discovery.html#logged-tokens\"><span class=\"std std-ref\">Tokens from Mi Home logs<\/span><\/a>\u00a0has to be used.<\/p>\n<\/div>\n<\/div>\n<div id=\"tokens-from-mi-home-logs\" class=\"section\">\n<p>&nbsp;<\/p>\n<h2>Tokens from Mi Home logs<\/h2>\n<p>The easiest way to obtain tokens is to browse through log files of the Mi Home app version 5.4.49 for Android. It seems that version was released with debug messages turned on by mistake. An APK file with the old version can be easily found using one of the popular web search engines. After downgrading use a file browser to navigate to directory\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">SmartHome\/logs\/plug_DeviceManager<\/span><\/code>, then open the most recent file and search for the token. When finished, use Google Play to get the most recent version back.<\/p>\n<\/div>\n<div id=\"tokens-from-backups\" class=\"section\">\n<p>&nbsp;<\/p>\n<h2>Tokens from backups<\/h2>\n<p>Extracting tokens from a Mi Home backup is the preferred way to obtain tokens if they cannot be looked up in the Mi Home app version 5.4.49 log files (e.g. no Android device around). For this to work the devices have to be added to the app beforehand before the database (or backup) is extracted.<\/p>\n<div id=\"creating-a-backup\" class=\"section\">\n<h3>Creating a backup<\/h3>\n<p>The first step to do this is to extract a backup or database from the Mi Home app. The procedure is briefly described below, but you may find the following links also useful:<\/p>\n<ul class=\"simple\">\n<li><a class=\"reference external\" href=\"https:\/\/github.com\/jghaanstra\/com.xiaomi-miio\/blob\/master\/docs\/obtain_token.md\">https:\/\/github.com\/jghaanstra\/com.xiaomi-miio\/blob\/master\/docs\/obtain_token.md<\/a><\/li>\n<li><a class=\"reference external\" href=\"https:\/\/github.com\/homeassistantchina\/custom_components\/blob\/master\/doc\/chuang_mi_ir_remote.md\">https:\/\/github.com\/homeassistantchina\/custom_components\/blob\/master\/doc\/chuang_mi_ir_remote.md<\/a><\/li>\n<\/ul>\n<div id=\"android\" class=\"section\">\n<h4>Android<\/h4>\n<p>Start by installing the newest version of the Mi Home app from Google Play and setting up your account. When the app asks you which server you want to use, it\u2019s important to pick one that is also available in older versions of Mi Home (we\u2019ll see why a bit later). U.S or china servers are OK, but the european server is not supported by the old app. Then, set up your Xiaomi device with the Mi Home app.<\/p>\n<p>After the setup is completed, and the device has been connected to the Wi-Fi network of your choice, it is necessary to downgrade the Mi Home app to some version equal or below 5.0.19. As explained\u00a0<a class=\"reference external\" href=\"https:\/\/github.com\/jghaanstra\/com.xiaomi-miio\/blob\/master\/docs\/obtain_token.md#method-3---obtain-mi-home-device-token-for-devices-that-hide-their-tokens-after-setup\">here<\/a>\u00a0and\u00a0<a class=\"reference external\" href=\"https:\/\/github.com\/rytilahti\/python-miio\/issues\/185\">in github issue #185<\/a>, newer versions of the app do not download the token into the local database, which means that we can\u2019t retrieve the token from the backup. You can find older versions of the Mi Home app in\u00a0<a class=\"reference external\" href=\"https:\/\/www.apkmirror.com\/apk\/xiaomi-inc\/mihome\/\">apkmirror<\/a>.<\/p>\n<p>Download, install and start up the older version of the Mi Home app. When the app asks which server should be used, pick the same one you used with the newer version of the app. Then, log into your account.<\/p>\n<p>After this point, you are ready to perform the backup and extract the token. Please note that it\u2019s possible that your device does not show under the old app. As long as you picked the same server, it should be OK, and the token should have been downloaded and stored into the database.<\/p>\n<p>To do a backup of an Android app you need to have the developer mode active, and your device has to be accessible with\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">adb<\/span><\/code>.<\/p>\n<div id=\"id1\" class=\"admonition-todo admonition\">\n<p class=\"admonition-title\">Todo<\/p>\n<p>Add a link how to check and enable the developer mode. This part of documentation needs your help! Please consider submitting a pull request to update this.<\/p>\n<\/div>\n<p>After you have connected your device to your computer, and installed the Android developer tools, you can use\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">adb<\/span><\/code>\u00a0tool to create a backup.<\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>adb backup -noapk com.xiaomi.smarthome -f backup.ab\r\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"admonition note\">\n<p class=\"admonition-title\">Note<\/p>\n<p>Depending on your Android version you may need to insert a password and\/or accept the backup, so check your phone at this point!<\/p>\n<\/div>\n<p>If everything went fine and you got a\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">backup.ab<\/span><\/code>\u00a0file, please continue to\u00a0<a class=\"reference internal\" href=\"https:\/\/python-miio.readthedocs.io\/en\/latest\/discovery.html#token-extraction\"><span class=\"std std-ref\">Extracting tokens<\/span><\/a>.<\/p>\n<\/div>\n<div id=\"apple\" class=\"section\">\n<h4>Apple<\/h4>\n<p>Create a new unencrypted iOS backup to your computer. To do that you\u2019ve to follow these steps:<\/p>\n<ul class=\"simple\">\n<li>Connect your iOS device to the computer<\/li>\n<li>Open iTunes<\/li>\n<li>Click on your iOS device (sidebar left or icon on top navigation bar)<\/li>\n<li>\n<dl class=\"simple\">\n<dt>In the Summary view check the following settings<\/dt>\n<dd>\n<ul>\n<li>Automatically Back Up:\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">This<\/span>\u00a0<span class=\"pre\">Computer<\/span><\/code><\/li>\n<li><strong>Disable<\/strong>\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">Encrypt<\/span>\u00a0<span class=\"pre\">iPhone<\/span>\u00a0<span class=\"pre\">backup<\/span><\/code><\/li>\n<\/ul>\n<\/dd>\n<\/dl>\n<\/li>\n<li>Click\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">Back<\/span>\u00a0<span class=\"pre\">Up<\/span>\u00a0<span class=\"pre\">Now<\/span><\/code><\/li>\n<\/ul>\n<p>When the backup is finished, download\u00a0<a class=\"reference external\" href=\"https:\/\/www.imactools.com\/iphonebackupviewer\/\">iBackup Viewer<\/a>\u00a0and follow these steps:<\/p>\n<ul class=\"simple\">\n<li>Open iBackup Viewer<\/li>\n<li>Click on your newly created backup<\/li>\n<li>Click on the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">Raw<\/span>\u00a0<span class=\"pre\">Files<\/span><\/code>\u00a0icon (looks like a file tree)<\/li>\n<li>On the left column, search for\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">AppDomain-com.xiaomi.mihome<\/span><\/code>\u00a0and select it<\/li>\n<li>Click on the search icon in the header<\/li>\n<li>Enter\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">_mihome<\/span><\/code>\u00a0in the search field<\/li>\n<li>Select the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">Documents\/0123456789_mihome.sqlite<\/span><\/code>\u00a0file (the one with the number prefixed)<\/li>\n<li>Click\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">Export<\/span>\u00a0<span class=\"pre\">-&gt;<\/span>\u00a0<span class=\"pre\">Selected\u2026<\/span><\/code>\u00a0in the header and store the file<\/li>\n<\/ul>\n<p>Now you\u2019ve exported the SQLite database to your Mac and you can extract the tokens.<\/p>\n<div class=\"admonition note\">\n<p class=\"admonition-title\">Note<\/p>\n<p>See also\u00a0<a class=\"reference external\" href=\"https:\/\/github.com\/jghaanstra\/com.xiaomi-miio\/blob\/master\/docs\/obtain_token.md#ios-users\">jghaanstra\u2019s obtain token docs<\/a>\u00a0for alternative ways.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"extracting-tokens\" class=\"section\">\n<p>&nbsp;<\/p>\n<h3>Extracting tokens<\/h3>\n<p>Now having extract either a backup or a database from the application, the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">miio-extract-tokens<\/span><\/code>\u00a0can be used to extract the tokens from it.<\/p>\n<p>At the moment extracting tokens from a backup (Android), or from an extracted database (Android, Apple) are supported.<\/p>\n<p>Encrypted tokens as\u00a0<a class=\"reference external\" href=\"https:\/\/github.com\/rytilahti\/python-miio\/issues\/75\">recently introduced on iOS devices<\/a>\u00a0will be automatically decrypted. For decrypting Android backups the password has to be provided to the tool with\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">--password<\/span>\u00a0<span class=\"pre\">&lt;password&gt;<\/span><\/code>.<\/p>\n<p><em>Please feel free to submit pull requests to simplify this procedure!<\/em><\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>$ miio-extract-tokens backup.ab\r\nOpened backup\/backup.ab\r\nExtracting to \/tmp\/tmpvbregact\r\nReading tokens from Android DB\r\nGateway\r\n        Model: lumi.gateway.v3\r\n        IP address: <span class=\"m\">192<\/span>.168.XXX.XXX\r\n        Token: 91c52a27eff00b954XXX\r\n        MAC: <span class=\"m\">28<\/span>:6C:07:XX:XX:XX\r\nroom1\r\n        Model: yeelink.light.color1\r\n        IP address: <span class=\"m\">192<\/span>.168.XXX.XXX\r\n        Token: 4679442a069f09883XXX\r\n        MAC: F0:B4:29:XX:XX:XX\r\nroom2\r\n        Model: yeelink.light.color1\r\n        IP address: <span class=\"m\">192<\/span>.168.XXX.XXX\r\n        Token: 7433ab14222af5792XXX\r\n        MAC: <span class=\"m\">28<\/span>:6C:07:XX:XX:XX\r\nFlower Care\r\n        Model: hhcc.plantmonitor.v1\r\n        IP address: <span class=\"m\">134<\/span>.XXX.XXX.XXX\r\n        Token: 124f90d87b4b90673XXX\r\n        MAC: C4:7C:8D:XX:XX:XX\r\nMi Robot Vacuum\r\n        Model: rockrobo.vacuum.v1\r\n        IP address: <span class=\"m\">192<\/span>.168.XXX.XXX\r\n        Token: 476e6b70343055483XXX\r\n        MAC: <span class=\"m\">28<\/span>:6C:07:XX:XX:XX\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"extracting-tokens-manually\" class=\"section\">\n<h3>Extracting tokens manually<\/h3>\n<p>Run the following SQLite command:<\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>sqlite3 &lt;path of *_mihome.sqlite database&gt; <span class=\"s2\">\"select ZNAME,ZLOCALIP,ZTOKEN from ZDEVICE\"<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>You should get a list which looks like this:<\/p>\n<div class=\"highlight-text notranslate\">\n<div class=\"highlight\">\n<pre>Device 1|x.x.x.x|0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\r\nDevice 2|x.x.x.x|0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\r\nDevice 3|x.x.x.x|0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\r\n<\/pre>\n<\/div>\n<\/div>\n<p>These are your device names, IP addresses and tokens. However, the tokens are encrypted and you need to decrypt them. The command for decrypting the token manually is:<\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"nb\">echo<\/span> <span class=\"s1\">'0: &lt;YOUR 32 CHARACTER TOKEN&gt;'<\/span> <span class=\"p\">|<\/span> xxd -r -p <span class=\"p\">|<\/span> openssl enc -d -aes-128-ecb -nopad -nosalt -K <span class=\"m\">00000000000000000000000000000000<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"environment-variables-for-command-line-tools\" class=\"section\">\n<h2>Environment variables for command-line tools<\/h2>\n<p>To simplify the use, instead of passing the IP and the token as a parameter for the tool, you can simply set the following environment variables. The following works for\u00a0<cite>mirobo<\/cite>, for other tools you should consult the documentation of corresponding tool.<\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"nb\">export<\/span> <span class=\"nv\">MIROBO_IP<\/span><span class=\"o\">=<\/span><span class=\"m\">192<\/span>.168.1.2\r\n<span class=\"nb\">export<\/span> <span class=\"nv\">MIROBO_TOKEN<\/span><span class=\"o\">=<\/span>476e6b70343055483230644c53707a12<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>If you want to read the device continue <a href=\"https:\/\/einstein.amsterdam\/?page_id=3323\">here<\/a>.<\/p>\n<p>source : <a href=\"https:\/\/python-miio.readthedocs.io\/en\/latest\/discovery.html#environment-variables-for-command-line-tools\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/python-miio.readthedocs.io\/en\/latest\/discovery.html#environment-variables-for-command-line-tools<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Installation The easiest way to install the package is to use pip:\u00a0pip3\u00a0install\u00a0python-miio\u00a0.\u00a0Using virtualenv\u00a0is recommended. Please make sure you have\u00a0libffi\u00a0and\u00a0openssl\u00a0headers installed, you can do this on <a class=\"mh-excerpt-more\" href=\"https:\/\/einstein.amsterdam\/?page_id=3321\" title=\"Mi Home Start\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/einstein.amsterdam\/index.php?rest_route=\/wp\/v2\/pages\/3321"}],"collection":[{"href":"https:\/\/einstein.amsterdam\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/einstein.amsterdam\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/einstein.amsterdam\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/einstein.amsterdam\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3321"}],"version-history":[{"count":8,"href":"https:\/\/einstein.amsterdam\/index.php?rest_route=\/wp\/v2\/pages\/3321\/revisions"}],"predecessor-version":[{"id":3372,"href":"https:\/\/einstein.amsterdam\/index.php?rest_route=\/wp\/v2\/pages\/3321\/revisions\/3372"}],"wp:attachment":[{"href":"https:\/\/einstein.amsterdam\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}