【IoT】Home Assistant 折腾

Posted by 西维蜀黍 on 2020-12-06, Last Modified on 2024-02-26

Config File

# 修改port
http:
  server_port: 8123
  ssl_certificate: '/etc/letsencrypt/live/your server host/fullchain.pem'
  ssl_key: '/etc/letsencrypt/live/your server host/privkey.pem
  
  
# debug
logger:
  default: debug

Ref

Restart HA

Config file

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# Use HTTPS
# duckdns:
  # domain: swhome666
  # access_token: ...
# http:
#   ssl_certificate: /etc/letsencrypt/live/swhome666.duckdns.org/fullchain.pem
#   ssl_key: /etc/letsencrypt/live/swhome666.duckdns.org/privkey.pem

Veify Config File

$ hass --script check_config -c .

HACS 安装

https://hacs.xyz/docs/installation/prerequisites

Ingetrage With Homekit

Add homekit: into your configuration.yaml, like

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

switch:
  - platform: xiaomi_miio_plug
    name: plug1
    host: 192.168.2.177
    token: 75816a8a5532015865c314758e5b9a8d
    model: chuangmi.plug.m3

homekit:

Restart home assistant and then you would be able to see this in Nofitications:

Open Home in your iPhone (if haven’t downloaded it, download it from AppStore).

Client + on the right top and scan the QRcode.

Ref

Troubleshoot

如果iPhone 无法连接 HA,可能是防火墙把连接block了,因此:

# 允许 iPhone与 HA的通讯
$ sudo iptables -A INPUT -s 192.168.18.0/24 -p tcp -m state --state NEW -m tcp --dport 8123 -j ACCEPT

# 找到 HA 中 HASS Bridge 工作的 port
netstat -an | grep LISTEN
tcp        0      0 0.0.0.0:9090            0.0.0.0:*               LISTEN
tcp        0      0 192.168.18.129:21064    0.0.0.0:*               LISTEN

当然,这样也可以找到这个port:

$ sudo iptables -A INPUT -s 192.168.18.0/24 -p tcp -m state --state NEW -m tcp --dport 21064 -j ACCEPT

永久保存到iptables中,以避免重启后 accept rule被reset:

$ sudo iptables-save

还有一种可能是,homekit对应的端口,被绑定到了127.0.0.1,

创建TLS

first up, run this to install the certbot client

$ brew install certbot

You can restart Home Assistant after the next step using the same command and replacing stop with start. Now we will run the certbot program to get our SSL certificate. You will need to include your email address and your DuckDNS URL in the appropriate places:

$ certbot certonly --standalone --preferred-challenges http-01 --email swsmile1028@gmail.com -d swhome666.duckdns.org

Once the program has run it will generate a certificate and other files and place them in a folder /etc/letsencrypt/ .

HACS

Home Assistant Community Store

HACS gives you a powerful UI to handle downloads of all your custom needs.

Before you start with the configuration, you will need to go to GitHub to get a Personal Access Token.

And follow https://hacs.xyz/docs/configuration/start

You will find the custom component in the integration menu afterwards, look for ‘Xiaomi Mi Electric Rice Cooker Integration’.

Troubleshout

The headers or library files could not be found for zlib

    The headers or library files could not be found for zlib,
    a required dependency when compiling Pillow from source.

    Please see the install instructions at:
       https://pillow.readthedocs.io/en/latest/installation.html

    Traceback (most recent call last):
      File "/private/var/folders/42/27cgq8k50hsc48kdnpwpt4080000gn/T/pip-install-jvwfft97/pillow_ba9296869fd84f44808c26de4b3ae4b2/setup.py", line 864, in <module>
        setup(
      File "/Users/weishi/SW/homeassistant/homeassistant/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
        return distutils.core.setup(**attrs)
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/core.py", line 148, in setup
        dist.run_commands()
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 966, in run_commands
        self.run_command(cmd)
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 985, in run_command
        cmd_obj.run()
      File "/Users/weishi/SW/homeassistant/homeassistant/lib/python3.9/site-packages/setuptools/command/install.py", line 61, in run
        return orig.install.run(self)
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/command/install.py", line 546, in run
        self.run_command('build')
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 985, in run_command
        cmd_obj.run()
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/command/build.py", line 135, in run
        self.run_command(cmd_name)
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 985, in run_command
        cmd_obj.run()
      File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/command/build_ext.py", line 340, in run
        self.build_extensions()
      File "/private/var/folders/42/27cgq8k50hsc48kdnpwpt4080000gn/T/pip-install-jvwfft97/pillow_ba9296869fd84f44808c26de4b3ae4b2/setup.py", line 694, in build_extensions
        raise RequiredDependencyException(f)
    __main__.RequiredDependencyException: zlib

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/42/27cgq8k50hsc48kdnpwpt4080000gn/T/pip-install-jvwfft97/pillow_ba9296869fd84f44808c26de4b3ae4b2/setup.py", line 918, in <module>
        raise RequiredDependencyException(msg)
    __main__.RequiredDependencyException:

    The headers or library files could not be found for zlib,
    a required dependency when compiling Pillow from source.

    Please see the install instructions at:
       https://pillow.readthedocs.io/en/latest/installation.html


    ----------------------------------------
ERROR: Command errored out with exit status 1: /Users/weishi/SW/homeassistant/homeassistant/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/42/27cgq8k50hsc48kdnpwpt4080000gn/T/pip-install-jvwfft97/pillow_ba9296869fd84f44808c26de4b3ae4b2/setup.py'"'"'; __file__='"'"'/private/var/folders/42/27cgq8k50hsc48kdnpwpt4080000gn/T/pip-install-jvwfft97/pillow_ba9296869fd84f44808c26de4b3ae4b2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/42/27cgq8k50hsc48kdnpwpt4080000gn/T/pip-record-ami1omqy/install-record.txt --single-version-externally-managed --compile --install-headers /Users/weishi/SW/homeassistant/homeassistant/include/site/python3.9/pillow Check the logs for full command output.
$ pip3 install --ignore-installed pillow

I checked the source code of setup.py, it searches for zlib.h within specified folders, like /usr/local/include, while on Mac, built-in zlib.h is in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include, so I just created a link as shown below, and it worked.

$ ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/zlib.h /usr/local/include/zlib.h

Issue 2

2020-12-02 23:02:05 ERROR (SyncWorker_0) [homeassistant.util.package] Unable to install package python-miio==0.5.3: ERROR: Could not find a version that satisfies the requirement pytz<2020.0,>=2019.3 (from python-miio)

Miscellaneous

和天猫精灵hub集成(使用天猫精灵控制Home Assitant中的设备)

https://post.smzdm.com/p/av7k64nn/

重置 HA 账号密码

login to the homeassistant docker or console

hass --script auth -c /config change_password <user> <password>

Restart HA

# List the users
$ hass --script auth -c /config list

Reference

Minor

home assistant Siri