{"id":39,"date":"2021-02-27T15:30:42","date_gmt":"2021-02-27T07:30:42","guid":{"rendered":"https:\/\/www.sy-blog.moe\/?p=39"},"modified":"2025-05-26T10:20:44","modified_gmt":"2025-05-26T02:20:44","slug":"%e5%b0%8f%e7%b1%b3%e6%99%ba%e8%83%bd%e6%8f%92%e5%ba%a72%e8%93%9d%e7%89%99%e7%bd%91%e5%85%b3%e7%89%88-%e6%8e%a5%e5%85%a5homeassistant","status":"publish","type":"post","link":"https:\/\/sy-blog.moe\/en\/39.html","title":{"rendered":"Xiaomi Smart Plug 2 Bluetooth Gateway Edition Integration with Home Assistant"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>[infobox title=&quot;2022.3 Update&quot;] <strong>Xiaomi Miot Auto is now available for quick access<\/strong>, no complex configuration is required, see<a href=\"https:\/\/github.com\/al-one\/hass-xiaomi-miot\/blob\/master\/README_zh.md\" target=\"_blank\" rel=\"noopener\" >https:\/\/github.com\/al-one\/hass-xiaomi-miot\/blob\/master\/README_zh.md<\/a> [\/infobox]<\/p>\n<p>Before you start, use the Mijia APP to connect the socket to the Internet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Get a device token<\/h2>\n\n\n\n<p>The traditional root method is not available, but I found a script on GitHub that can quickly get the device token.<\/p>\n<p>Website:<a href=\"https:\/\/github.com\/PiotrMachowski\/Xiaomi-cloud-tokens-extractor\" target=\"_blank\" rel=\"noopener\" >https:\/\/github.com\/PiotrMachowski\/Xiaomi-cloud-tokens-extractor<\/a><\/p>\n<p>Directions:<\/p>\n<p>download<strong>token_extractor.exe<\/strong> [bdbtn]https:\/\/github.com\/PiotrMachowski\/Xiaomi-cloud-tokens-extractor\/raw\/master\/token_extractor.exe[\/bdbtn], run it, enter your account and password as prompted to get the device token, and copy it for later use<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-47\" src=\"http:\/\/23.97.70.13\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20212927112923.png\" alt=\"\" width=\"683\" height=\"309\" srcset=\"https:\/\/sy-blog.moe\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20212927112923.png 683w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20212927112923-300x136.png 300w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/p>\n\n<h2>2. Access homeassistant<\/h2>\n<p>[success]<\/p>\n<h4>[Updated on August 19, 2021] Method 1: Use xiaomi-miot (recommended)<\/h4>\n<p>1. Download <a href=\"https:\/\/github.com\/al-one\/hass-xiaomi-miot\/archive\/refs\/heads\/master.zip\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/github.com\/al-one\/hass-xiaomi-miot\/archive\/refs\/heads\/master.zip<\/a>, copy the .\/custom_components\/xiaomi_miot folder to the \/config\/custom_components directory, and restart homeassistant.<\/p>\n<p>2. Add in the homeassistant integration interface <strong>Xiaomi Miot Auto<\/strong>, select local mode, enter the socket token and IP, and confirm.<\/p>\n<p>[\/success]<\/p>\n<h3>Method 2: Use xiaomi_miio_raw (may not work)<\/h3>\n<p>Warehouse Address:<a href=\"https:\/\/github.com\/syssi\/xiaomi_raw\" target=\"_blank\" rel=\"noopener\" >https:\/\/github.com\/syssi\/xiaomi_raw<\/a><\/p>\n<p>Installation method:<\/p>\n<p>Download the xiaomi_miio_raw source code [bdbtn]https:\/\/wwa.lanzous.com\/ij1F9m8nbba[\/bdbtn], copy it to .\/custom_components\/, and restart homeassistant<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-49\" src=\"http:\/\/23.97.70.13\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20211227011203.png\" alt=\"\" width=\"798\" height=\"361\" srcset=\"https:\/\/sy-blog.moe\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20211227011203.png 798w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20211227011203-300x136.png 300w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20211227011203-768x347.png 768w\" sizes=\"auto, (max-width: 798px) 100vw, 798px\" \/><\/p>\n<p>Edit configuration.yaml and configure as follows<\/p>\n\n<pre class=\"wp-block-code\" data-no-translation=\"\"><code>sensor:\n  - platform: xiaomi_miio_raw\n    name: server_plug # \u81ea\u5b9a\u4e49\u540d\u79f0\n    host: 192.168.0.43 # \u63d2\u5ea7IP\n    token: \"xxx\" # \u524d\u9762\u83b7\u53d6\u5230\u7684token\n    max_properties: 10\n    default_properties_getter: get_properties\n    default_properties: \n      - \"{'did': 'power', 'piid': 1, 'siid': 2}\"       \n      - \"{'did': 'temperature', 'piid': 6, 'siid': 2}\"       \n      - \"{'did': 'indicator_light', 'piid': 1, 'siid': 3}\"       \n      - \"{'did': 'on_duration', 'piid': 1, 'siid': 4}\"       \n      - \"{'did': 'off_duration', 'piid': 2, 'siid': 4}\"       \n      - \"{'did': 'countdown', 'piid': 3, 'siid': 4}\"       \n      - \"{'did': 'task_switch', 'piid': 4, 'siid': 4}\"       \n      - \"{'did': 'countdown_info', 'piid': 5, 'siid': 4}\"       \n      - \"{'did': 'power_consumption', 'piid': 1, 'siid': 5}\"       \n      - \"{'did': 'electric_current', 'piid': 2, 'siid': 5}\"       \n      - \"{'did': 'voltage', 'piid': 3, 'siid': 5}\"       \n      - \"{'did': 'electric_power', 'piid': 6, 'siid': 5}\"<\/code><\/pre>\n\n<p>After restarting, you can find the socket in the developer tools<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-147\" src=\"http:\/\/23.97.70.13\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20213527013502.png\" alt=\"\" width=\"575\" height=\"388\" srcset=\"https:\/\/sy-blog.moe\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20213527013502.png 575w, https:\/\/sy-blog.moe\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20213527013502-300x202.png 300w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/p>\n<p>Parameters such as power need to be further calculated before they can be inserted into the panel<strong>(Only the configuration of temperature and power is demonstrated here, the same applies to other parameters)<\/strong><\/p>\n<pre class=\"wp-block-code\" data-no-translation=\"\"><code>sensor:\n  - platform: template\n    sensors:\n        server_plug_power: # \u81ea\u5b9a\u4e49\n            unique_id: server_plug_power # \u81ea\u5b9a\u4e49\n            unit_of_measurement: W # \u5355\u4f4d\n            value_template: \"{{ state_attr('sensor.server_plug', 'electric_power') | int \/ 100 }}\" # \u8ba1\u7b97\n        server_plug_temp: # \u81ea\u5b9a\u4e49\n            unique_id: server_plug_temp # \u81ea\u5b9a\u4e49\n            unit_of_measurement: \u2103 # \u5355\u4f4d\n            value_template: \"{{ state_attr('sensor.server_plug', 'temperature')}}\"<\/code><\/pre>\n<p>After restarting, you can find the parameters you just added in the panel<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-148\" src=\"http:\/\/23.97.70.13\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20212127032156.png\" alt=\"\" width=\"522\" height=\"473\" \/><\/p>\n<p>Finally, encapsulate a switch to control the socket switch<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-150\" src=\"http:\/\/23.97.70.13\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20212127032126.png\" alt=\"\" width=\"634\" height=\"314\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-151\" src=\"http:\/\/23.97.70.13\/wp-content\/uploads\/2021\/02\/\u622a\u56fe_20212427032444.png\" alt=\"\" width=\"524\" height=\"184\" \/><\/p>\n<p>&nbsp;<\/p>\n<blockquote>Reference Links: <a href=\"https:\/\/post.smzdm.com\/p\/az5g23x0\/\" target=\"_blank\" rel=\"noopener\" >https:\/\/post.smzdm.com\/p\/az5g23x0\/<\/a><\/blockquote>","protected":false},"excerpt":{"rendered":"Discover the secret of connecting Xiaomi Smart Socket 2 Bluetooth Gateway to Home Assistant! Quickly obtain device token, easily configure, and achieve smart control. Start from the Mijia APP and connect step by step to take your smart home to the next level.","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3],"tags":[5,9],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-server","tag-homeassistant","tag-9"],"_links":{"self":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":1,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":752,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts\/39\/revisions\/752"}],"wp:attachment":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}