###### tags: `raspberrypi` `switchbot` # Home Assistant で Switchbot Bot を動作させる Switchbot Bot を買ったけど、Switchbot hub は買わなくてもいいかなと思っている人向けの方法です。 ## やりたいこと - Raspberry Pi 上に構築した Home Assitant の GUI 画面から Switchbot プラグインを導入して、Switchbot Bot のボタン押下処理を BLE で実行する(Wi-Fi は使わない)。 - 「スイッチ」の「お風呂自動ボタン」でボタン押下する。 <img src="https://i.imgur.com/xc0XbKo.png" width="500px"> - Switchbot Bot が動作して、自動ボタンが押下される。 <img src="https://i.imgur.com/ys4Myzs.jpg" width="300px"> ## 準備するもの - Switchbot Bot - Raspberry Pi 4 - 8 GB RAM - Ubuntu 20.04 (64bit) ``` $ uname -a Linux *****-pi4-8gb-1 5.4.0-1042-raspi #46-Ubuntu SMP PREEMPT Fri Jul 30 00:35:40 UTC 2021 aarch64 aarch64 aarch64 GNU/Linux ``` ## Home Assistant 環境の構築 ### Home Assistant - 公式サイト https://www.home-assistant.io/ #### インストール - https://www.home-assistant.io/installation/linux#install-home-assistant-container インストール方法は色々ありますが、今回は docker を使ってインストールしました。 TZ は JST-9、/PATH_TO_YOUR_CONFIG は `/opt/home-assistant/config` にしています。 ``` # docker run -d \ --name homeassistant \ --privileged \ --restart=unless-stopped \ -e TZ=JST-9 \ -v /opt/home-assistant/config:/config \ --network=host \ ghcr.io/home-assistant/home-assistant:stable ``` #### 初期設定 - docker コマンドを実行後に、`http://(IP アドレス):8123` にアクセスすると初期設定画面になります。 - `/opt/home-assistant/config/configuration.yaml` に Switchbot Bot デバイスの設定を追加します。 - https://www.home-assistant.io/integrations/switchbot/ ``` switch: - platform: switchbot name: 'お風呂自動ボタン' mac: "C8:6B:EA:XX:XX:XX" ``` - MAC アドレスは Android アプリケーションの「BLE スキャナー」を使用して検索しました。 - https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner&hl=ja&gl=US #### 再起動 - Switchbot Bot の設定を行った後 docker コンテナを再起動します。 ``` # docker restart homeassistant ``` ## 動作確認 - `http://(IP アドレス):8123` にアクセスすると、「お風呂自動ボタン」の項目が追加されているので、右側の稲妻マークを押して、Switchbot Bot が動作することを確認します。