ホーム » コンピュータ » Linux » HomeAssistantのボタン・スイッチ

HomeAssistantのボタン・スイッチ

HomeAssistantの設定にようやく慣れてきた。

Raspberry-Pi の別ホスト(host)で CEC を使ってテレビの ON/OFF のスイッチや、入力切替操作ができるボタンを設置してみた。

別ホストでの処理を ssh で実行

HomeAssistant のローカルから 別ホスト に ssh で login できるように設定。

$ sudo docker exec -it homeassistant bash
host:/config# ssh-keygen -i .ssh/id_ed25519
host:/config# ssh-copy-id -i .ssh/id_ed25519.pub user@host

ボタンとスイッチを yaml で登録

スイッチ操作するための設定を configuration.yaml に登録する。

shell_command , template は、この後、色々と登録することになるので、別ファイルにまとめる。

((( configuration.yaml )))
# 設定を別ファイルにまとめる
shell_command: !include shell_command.yaml
template: !include template.yaml

((( shell_command.yaml )))
# スイッチ/on/off (cec-client.sh でCECで制御できるように設定済み)
sh_tv_on:  "/usr/bin/ssh -i .ssh/id_ed25519 user@host /usr/local/bin/cec-client.sh tv on"
sh_tv_off: "/usr/bin/ssh -i .ssh/id_ed25519 user@host /usr/local/bin/cec-client.sh tv off"
# 入力切替のコマンド
sh_tv_src: "/usr/bin/ssh -i .ssh/id_ed25519 user@host /usr/local/bin/cec-client.sh tv src"

((( template.yaml )))
# ボタン-------------------
- button:
    - name: "TV入力"
      unique_id: "btn_tv_src"
      press:
        action: "shell_command.sh_tv_src"

# スイッチ
# サービス Ping(ICMP) で tv の ping を登録済み
- switch:
    - name: "TV"
      unique_id: "sw_tv_power"
      state: "{{ is_state('binary_sensor.tv', 'on') }}"
      turn_on:
        - action: "shell_command.sh_tv_on"
      turn_off:
        - action: "shell_command.sh_tv_off"