2020/02/11

Configurator Add-On

安裝 configurator,這個方便可以直接線上修改相關的設定。
安裝方式很簡單,依照下面步驟:
1. Supervisor -> ADD-ON Store
2. Search 輸入 Configurator
3. 點擊 Configurator,再點擊 INSTALL
4. 安裝完畢,勾選 Show in sidebar,這樣左側選單就會出現 Configurator 快捷按鈕
5. 點擊 START

這樣就安裝完畢了。
建議安裝完 Hass.io 後可以先安裝這個插件。

2020/02/10

Let's encrypt 申請 ssl 憑證

Let’s Encrypt 可對 domain 做憑證以便後續的功能使用以及遠端登入可以提供更加的安全性。
如果沒有自己的 domain name,可以至 duckdns 免費申請。

Let’s Encrypt 有一個限制就是至少每 30-40 天必須更新憑證。
因此如果所使用的 domain 是從 duckdns 申請的,可以考慮安裝另外一個 addon - Duck Dns

這邊我只安裝 Let’s Encrypt 來做 domain 的憑證。domain 的部份則交由我另外一個地方的主機來做更新。

安裝 Let’s Encrypt
1. Supervisor -> ADD-ON STORE
2. Search Let’s 找到後點進去並點擊 INSTALL
3. 安裝完畢,不要勾選 Start on boot
4. config 依照實際需求修改,設定好後點擊右下 Save

email: abc@example.com
domains:
  - test.duckdns.org
certfile: fullchanin.pem # 不要修改
keyfile: privkey.pem # 不要修改
challenge: http # 依照需求可改成 dns,這邊我是使用 http
dns: {} # 依照需求修改
  1. 回到畫面最上面點擊 Start
  2. 拉到最下面點擊 Refresh ,看一下偵錯訊息,正常的話就表示申請憑證成功

申請成功後,開啟 configuration.yaml,加上以下的部份。

http:
  base_url: test.duckdns.org:8123
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem

重啟 hass,這樣就可以使用 https 連線了,且也不能再使用 http 連線了。
遠端連線方式變成: https://test.duckdns.org:8123
本地端連線就要改成: https://本地端IP:8123

另外建議備份 config/addons/data/ 裡面的 core_letsencrypt 這個目錄,裡面有相關的憑證以及認證訊息。
這個可以在重新安裝時直接把這些資料覆蓋回去,這樣就不用重新申請憑證。
太頻繁申請憑證會暫時無法申請。

由於 Let’s encrypt 只用於申請憑證並沒有定時自己更新的功能。
因此需要加上自動更新的功能。
開啟 automations.yaml,加上以下的部份。
這會定期在每天晚上 11 點重新做憑證更新的動作。
憑證更新的部份會先檢查是否需要更新,如果有需要才會更新,
沒有需要則直接結束。

- id: 'system_lets_encrypt_renewal'
  alias: "[ System ] Let's Encrypt Renewal"
  trigger:
    - platform: time
      at: '23:00:00'
  action:
    - service: hassio.addon_restart
      data:
        addon: core_letsencrypt

Hass 重啟後,部份 automation 被觸發

Hass 重啟後,發現部份 automation 會被觸發。

比如當某人到某個區域時會自動送訊息給我。

automation:
  - id: 'notify_me_when_someone_is_arrived_at'
    alias: 'notify_me_someone_is_arrived'
    description: ''
    trigger:
      - platform: state
        entity_id: device_tracker.someone
        to: 'Work'
    acton:
      - service: notify.mobile_app_me_mobile
        data_template:
          message: 'Someone is arrived'

看起來很正常。
但是在以下狀況卻會發生問題。
如果 someone 已經在 Work 這個地方,當我重啟 Hass 則會再觸發一次這個 automation。
原因是當重啟時,Hass 會重新設定這個 device_track 的狀態,導致就觸發了這個 automation。
解決方式如下

automation:
  - id: 'notify_me_when_someone_is_arrived_at'
    alias: 'notify_me_someone_is_arrived'
    description: ''
    trigger:
      - platform: state
        entity_id: device_tracker.someone
        to: 'Work'
    condition: 
      - condition: template
        value_template: '{{ true if trigger.from_state is not None else false }}'
    acton:
      - service: notify.mobile_app_me_mobile
        data_template:
          message: 'Someone is arrived'

–
參考連結

2020/01/17

如何在 raspbian 安裝 hass.io?

Based on these posts:

目前我安裝 raspbian os 在 Raspberry Pi 3 B+,並且在此之上再安裝 hass.io。

不直接使用 hass.io 的原因是 raspbian 本身是 debian base,平常我電腦也是使用 debian,

這樣對我來說操作彈性更大。

以下是安裝方式:

  1. 安裝基本環境,這邊要特別注意的是 network-manager 預設會使用 wifi 動態來連線,所以如果有使用 router 鎖 mac 或者把 mac 綁在某個固定 IP 的記得參考此篇。

    sudo -i
    apt-get install software-properties-common
    apt-get update
    apt-get install -y apparmor-utils apt-transport-https \
     avahi-daemon ca-certificates curl dbus \
     jq network-manager socat
    systemctl disable ModemManager # ModemManager 會對 Z-wave 和 Zigbee 影響,因此關閉也可以移除。
    curl -fsSL get.docker.com | sh
    
  2. 以下 script 會需要使用到 root 權限,建議先切換至 root 後再執行。-d 後面是 hass.io 儲存資料的位置,建議選擇自己容易備份的地方。

    curl -o hassio_install.sh -sL "https://raw.githubusercontent.com/home-assistant/hassio-installer/master/hassio_install.sh"
    chmod +x hassio_install.sh
    ./hassio_install.sh -m raspberrypi3 -d /home/me/hassio_data
    
  3. 安裝完畢後,輸入網址 http://[hostip]:8123,然後等待建置完畢後就可以建立帳號登入了。hostip 是安裝設備的 IP。

將 google assistant 加入 hass.io

Based on these posts:

Hass.io 支援 google assistant 有兩種方式

  1. 使用 Home Assistant Cloud (需要付費,每月 5 美金)。
  2. 手動加入 (免費但是複雜)。

這邊我採用手動加入的方式。以下為手動加入步驟。(使用手動加入前提是必須可使用 hostname 和有效的 ssl 憑證,這部分可以使用 duckdns addon 達成)

  1. 至 Actions on Google console 建立專案。

    1. 點擊 New Project。Project Name 則輸入你想要的英文名稱,再設定語言跟所在國家或地區。然後點擊 Create Project。

    2. 點擊 Smart Home,並且再點擊 Smart Home Recommended。

    3. 點擊 Build your Action 再點擊 Add Actions(s)

    4. 填入以下資訊

      1. Fulfillment URL: https://[host:port]/api/google_assistant,host:port 輸入連結到 home assistant 的網址。舉例 example.org:8123
      2. 點擊右上角 Save 後在點擊左上角 Overview 回到之前頁面。
  2. 點擊 Setup account linking 連結帳號以便 google home 可以連結到你設定的專案。

    1. 勾選 No, I only want to allow account creation on my website,點擊 Next。

    2. Select linking type 選擇 OAuth,Select grant type 選擇 Authorization code。點擊 Next。

    3. 填入以下資訊

      • Client ID issued by your Actions to Google: https://oauth-redirect.googleusercontent.com/,最後的 / 不能少。
      • Client secret: 可隨便輸入
      • Authorization URL: https://[host:port]/auth/authorize,host:port 為連結到 home assistant 的網址。舉例 example.org:8123
      • Token URL: https://[host:port]/auth/token,host:port 為連結到 home assistant 的網址。舉例 example.org:8123
      • 點擊 Next。
    4. Scopes 輸入 name 再點擊 Add scope,然後再輸入 email 再點擊 Add scope。記得確認 Google to transmit clientID and secret via HTTP basic auth header 不可勾選,然後點擊 Next。

    5. Testing instructions 可隨便輸入。 然後點擊右下角 Save。

  3. 點擊上方 Develop,Display name 輸入你想要看到在 Google Home 做第三方連結時的名稱。然後點擊右上方 Save。

  4. 點擊上方 Test,這會產生一個測試的 app 版本,這可以讓我們從 Google Home 連結。

  5. 至 Google Could Console 建立服務帳號,這在 Home assistant 設定時會使用到。

    1. 上方選取專案。選擇前面所建立的專案。

    2. 點擊左方憑證。點擊建立憑證後再選取服務帳戶金鑰。

    3. 服務帳戶設定如下:

      1. 新增服務帳戶。
      2. 服務帳戶名稱輸入隨便任一名稱
      3. 角色選擇服務帳戶裡面的服務帳戶憑證建立者。
      4. 通常輸入服務帳戶時會一併自動產生服務帳戶 ID,如果沒有自己產生,可以點擊右方圖示或者自己輸入即可。這 ID 必須是不能重複的。
      5. 勾選 JSON,點擊建立。
      6. 把此檔案透過 Hass.io 的 Configurator 上傳至 Hass.io 的 config,可先修改檔案名稱再上傳。此檔案後續設定 google assistant 時會使用到。
  6. 啟用 HomeGraph API。

    1. 至 Google Could Console
    2. 上方選取專案。選擇前面所建立的專案。
    3. 點擊啟用 API 和服務。
    4. 輸入 HomeGraph,選擇 HomeGraph API,然後點擊啟用。
  7. 開啟 configuration.yaml。設定範例如下,仔細設定可參考 configuration。

    google_assistant:
      project_id: YOUR_PROJECT_ID
      service_account: !include SERVICE_ACCOUNT.JSON
      report_state: true
      expose_by_default: false
      entity_config:
        light.living_room:
          expose: true
          aliases: 
            - LIGHT
    
    • YOUR_PROJECT_ID 改成實際的 project id,可至 Google Could Console 點擊上方選取專案後,查看專案後面的 ID。
    • SERVICE_ACCOUNT.JSON 則為前面所下載的檔案名稱。
    • expose_by_default: false,這設定預設為 true,會讓所有符合 exposed_domains 的件都被 google home 所看到。建議使用 false,後面再手動自己把想要顯示的加上去。
    • 如果 expose_by_default 設定為 false,一定要記得下面的 entity_config 裡面的設定必須加上 expose 為 true。
    • 另外 entity_config 裡面,aliases 的部分會顯示在 Google Home 裡面,測試發現即使設定多個也只會顯示第一個。
    • 設定完畢後,重啟 Hass。
  8. 開啟 Google Home,點擊左上角 +,

    1. 點擊設定裝置。
    2. Google 的合作夥伴。
    3. 點擊右上角搜尋圖示,輸入 test,應該可以看到一個 [test] XXX 的服務,XXX 是前面 Develop 所設定的 Display name。點擊這個服務輸入帳號密碼後,成功登入後會跳為前面選擇夥伴的畫面,此時再點進去 Google 的合作夥伴看看有沒有成功連接,正常就會出現在已連結的服務了。
  9. 開啟 google assistant 輸入或說出 sync my devices。

  10. 開啟 Google Home 就可以看到所增加的物件了。

更新 Let's Encrypt 失敗

將 hass.io 做 snapshot 還原時,針對 Let's Encrypt addon 啟動確認是否可以正常更新憑證。
發現卻無法更新,出現以下錯誤。
File “/root/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt/storage.py”, line 273, in _check_symlinks
“expected {0} to be a symlink”.format(link))
CertStorageError: expected /etc/letsencrypt/live/example.org/cert.pem to be a symlink
這原因是當 Let's Encrypt 更新憑證時,會將 live/example.org/cert.pem 做 symbolic link 到 archive/example.org 裡面 certX.pem (x 表示數字,越大越新)。
如果檢查到不是 symbolic link 就會判斷有問題而不執行更新。
而做 snapshot 還原時會修改到這部分,此時只要移除 live/example.org/ 裡面的憑證重新做 symbolic link 即可。
以下 X 表示最大的數字。
rm -f live/example.org/*.pem
ln -sf archive/example.org/certX.pem live/example.org/cert.pem
ln -sf archive/example.org/chainX.pem live/example.org/chain.pem
ln -sf archive/example.org/fullchainX.pem live/example.org/fullchain.pem
ln -sf archive/example.org/privkeyX.pem live/example.org/privkey.pem

2020/01/16

Using nmap to do presence-detection

Based on these two posts.
- https://www.home-assistant.io/integrations/nmap_tracker/
- https://www.home-assistant.io/integrations/person/

1. add these lines to configuration.yaml
device_tracker:
  - platform: nmap_tracker
    # global config
    interval_seconds: 15
    consider_home: 180
    new_device_defaults:
      track_new_devices: false
      hide_if_away: false
    # config for nmap_tracker
    hosts: # network address to scan
      - 192.168.1.1/24
    home_interval: 10 # if it is home, not scan this device at least 10 mins
    exclude: # not to scan this device
      - 192.168.1.1
2. restart hass.io
3. check known_devices.yaml in Configurator, this file will be auto generated.
original:
11_22_33_44_55_66:
  hide_if_away: false
  icon:
  mac: 11:22:33:44:55:66
  name: 11 22 33 44 55 66
  picture:
  track: false

22_22_33_44_55_66:
  hide_if_away: false
  icon:
  mac: 22:22:33:44:55:66
  name: 22 22 33 44 55 66
  picture:
  track: false

updated:
me:
  hide_if_away: false
  icon:
  mac: 11:22:33:44:55:66
  name: me
  picture:
  track: true

her:
  hide_if_away: false
  icon:
  mac: 22:22:33:44:55:66
  name: her
  picture:
  track: true
4. go Configuration -> Persons, Click on the account, and select Track Device, in this case, choose device_tracker.me
5. If someone doesn't have the account in hass.io, go to Configurator and edit configuration.yaml.
Add these lines and then restart hass.io
person:
  - name: Her
    id: her1224 # this must be unique.
    device_trackers:
      - device_tracker.her
6. go Configuration -> Persons to check if add successfully.