第五章 Home assistant 配置入门
这里需要暂停整理一下,安装HA之后,自己到底想干什么。做为一个程序员,肯定想将自己所学知识用在改进家装体验上。因此我先确定了以下几个需求:
-
常用智能设备接入HA平台
-
在HA上实现对家庭智能的概览
-
在家能语音控制
-
远端能微信控制
-
有一些基本的智能联动
-
HA接入模型,模型通过收集你的语音判断你的心情,从而改变环境
因此根据上述需求,我确定了学习方向(按顺序编写):
- HA UI设计
- Node-RED Companion学习,实现自动化和微信接入
- MQTT协议,以及简单的手改电板的知识,实现一些已有非联网设备的接入
在HA中,最最最最重要的文件就是在系统/config目录下的configuration.yaml,HA的一切配置都可以在configuration.yaml中找到,以后我自己的板块也主要围绕configuration.yaml文件来展开。为了后面更便于读取该文件,需要讲解一些HA特有的配置约定和HA的基本操作,这里不涉及yaml语法,如果不懂的可以自行去百度一下。
一、路径解释
在HA文件或者资源加载中,难免会遇到需要访问内部资源,HA关于界面图片的资源默认在:/config/www/community/下面,如果手动加歇资源(resources)的话,也可以放在这个下面,然后:HA对于路径的访问有两个方式。
-
/local/communit/xxxx/yyyyy.js
: /local就代表HA/config目录下的www文件夹。 -
/hacsfiles/xxoxyyy-js
: /hacsfile就代表HA的 /config/community路径
二、下载前端的三种方式
- HACS商店搜索下载
-
在HACS商店搜索不到的时候,可以通过添加GITHUB仓库地址添加。
-
部分插件会因为HACS版本问题,下载不了,这时候就需要我们手工添加了。
下载插件的JS文件。
在/config/www/community目录下创建与该组件名相同的名字,并将js文件保存进去
在设置–>仪表盘–>资源中写入组件路径,添加前端资源
三、configuration文件基本配置
在HA的早期版本,需要将HA的基本配置放在配置文件中,这里一块记录,当然这些配置都可以通过页面进行配置。
homeassistant:
# 你HA的名字
name: "My Home Assistant Instance"
# 经纬度
latitude: 37
longitude: -121
# 时区
time_zone: "America/Los_Angeles"
四、分隔配置文件
configuraton可以使用!include
语法将,configuration.yml以外的配置拉入主配置。
(一)基本用法
!include + 文件 :前提需要文件在configuration同一目录下,下面是几个常用的分解。
automation: !include automation.yaml
zone: !include zone.yaml
sensor: !include sensor.yaml
switch: !include switch.yaml
device_tracker: !include device_tracker.yaml
具体案例,主文件
light:
- platform: group
name: "Bedside Lights"
entities:
- light.left_bedside_light
- light.right_bedside_light
# define more light groups in a separate file
light groups: !include light-groups.yaml
light-groups.yaml
- platform: group
name: "Outside Lights"
entities:
- light.porch_lights
- light.patio_lights
(二)高级用法
!!!!!在用之前,这里需要注意:所有的配置文件需要.yaml后缀,不能以.yml为后缀。
-
!include_dir_list
将会把目录的内容作为一个列表返回,其中每个文件的内容都是列表中的一个条目。列表中的条目是根据文件名的字母数字顺序进行排序的。需要注意的是,在使用!include_dir_list时,每个文件必须只包含一个条目。
原文件:configuration.yaml
automation:
- alias: "Automation 1"
trigger:
platform: state
entity_id: device_tracker.iphone
to: "home"
action:
action: light.turn_on
target:
entity_id: light.entryway
- alias: "Automation 2"
trigger:
platform: state
entity_id: device_tracker.iphone
from: "home"
action:
action: light.turn_off
target:
entity_id: light.entryway
分隔后
automation: !include_dir_list automation/presence/
automation/presence/automation1.yaml
alias: "Automation 1"
trigger:
platform: state
entity_id: device_tracker.iphone
to: "home"
action:
action: light.turn_on
target:
entity_id: light.entryway
automation/presence/automation2.yaml
alias: "Automation 2"
trigger:
platform: state
entity_id: device_tracker.iphone
from: "home"
action:
action: light.turn_off
target:
entity_id: light.entryway
!include_dir_named
将返回目录的内容作为一个字典,该字典将文件名映射到文件的内容。
原文件:configuration.yaml
alexa:
intents:
LocateIntent:
action:
action: notify.pushover
data:
message: "Your location has been queried via Alexa."
speech:
type: plaintext
text: >
{%- for state in states.device_tracker -%}
{%- if state.name.lower() == User.lower() -%}
{{ state.name }} is at {{ state.state }}
{%- endif -%}
{%- else -%}
I am sorry. Pootie! I do not know where {{User}} is.
{%- endfor -%}
WhereAreWeIntent:
speech:
type: plaintext
text: >
{%- if is_state('device_tracker.iphone', 'home') -%}
iPhone is home.
{%- else -%}
iPhone is not home.
{% endif %}
分隔后
configuration.yaml
alexa:
intents: !include_dir_named alexa/
alexa/LocateIntent.yaml
action:
action: notify.pushover
data:
message: "Your location has been queried via Alexa."
speech:
type: plaintext
text: >
{%- for state in states.device_tracker -%}
{%- if state.name.lower() == User.lower() -%}
{{ state.name }} is at {{ state.state }}
{%- endif -%}
{%- else -%}
I am sorry. Pootie! I do not know where {{User}} is.
{%- endfor -%}
alexa/WhereAreWeIntent.yaml
speech:
type: plaintext
text: >
{%- if is_state('device_tracker.iphone', 'home') -%}
iPhone is home.
{%- else -%}
iPhone is not home.
{% endif %}
-
!include_dir_merge_list
会将一个目录的内容作为一个列表返回,方法是将所有文件(这些文件应该包含列表)合并成一个大的列表。重要的是要注意,当使用!include_dir_merge_list时,你必须在每个文件中包含一个列表(每个列表项都用连字符[-]表示)。每个文件可以包含一个或多个条目。
原文件:configuration.yaml
automation:
- alias: "Automation 1"
trigger:
- platform: state
entity_id: device_tracker.iphone
to: "home"
action:
- action: light.turn_on
target:
entity_id: light.entryway
- alias: "Automation 2"
trigger:
- platform: state
entity_id: device_tracker.iphone
from: "home"
action:
- action: light.turn_off
target:
entity_id: light.entryway
分隔后:
configuration.yaml
automation: !include_dir_merge_list automation/
automation/presence.yaml
- alias: "Automation 1"
trigger:
- platform: state
entity_id: device_tracker.iphone
to: "home"
action:
- action: light.turn_on
target:
entity_id: light.entryway
- alias: "Automation 2"
trigger:
- platform: state
entity_id: device_tracker.iphone
from: "home"
action:
- action: light.turn_off
target:
entity_id: light.entryway
-
!include_dir_merge_named
将加载目录中的每个文件,并将它们合并成一个大的字典,然后返回这个字典的内容。
原文件:configuration.yaml
group:
bedroom:
name: "Bedroom"
entities:
- light.bedroom_lamp
- light.bedroom_overhead
hallway:
name: "Hallway"
entities:
- light.hallway
- thermostat.home
front_yard:
name: "Front Yard"
entities:
- light.front_porch
- light.security
- light.pathway
- sensor.mailbox
- camera.front_porch
分隔后:
configuration.yaml
group: !include_dir_merge_named group/
group/interior.yaml
bedroom:
name: "Bedroom"
entities:
- light.bedroom_lamp
- light.bedroom_overhead
hallway:
name: Hallway
entities:
- light.hallway
- thermostat.home
group/exterior.yaml
front_yard:
name: "Front Yard"
entities:
- light.front_porch
- light.security
- light.pathway
- sensor.mailbox
- camera.front_porch
五、Templating(脚本)
简单来说就是在配置文件中使用脚本来访问一些集成和环境信息,举个例子。
script:
msg_who_is_home:
sequence:
- action: notify.notify
data:
message: >
{% if is_state('device_tracker.paulus', 'home') %}
Ha, Paulus is home!
{% else %}
Paulus is at {{ states('device_tracker.paulus') }}.
{% endif %}
其中if-else判断就是Templating。这里不做详细介绍了,用到哪里,再去看看吧,官方地址。
六、引入前端文件
众所周知,前端就是为了抄作业,下面给出引入前端页面步骤。
根本就是在lovelace中更改mode为yaml。
- 配置说明
- 全部改为yaml模式
lovelace:
mode: yaml
# Include external resources only add when mode is yaml, otherwise manage in the resources in the dashboard configuration panel.
resources:
- url: /local/my-custom-card.js
type: module
- url: /local/my-webfont.css
type: css
# Add more dashboards
dashboards:
lovelace-generated: # Needs to contain a hyphen (-)
mode: yaml
filename: notexist.yaml
title: Generated
icon: mdi:tools
show_in_sidebar: true
require_admin: true
lovelace-hidden:
mode: yaml
title: hidden
show_in_sidebar: false
filename: hidden.yaml
- 部分改为yaml
lovelace:
mode: storage
# Add yaml dashboards
dashboards:
lovelace-yaml:
mode: yaml
title: YAML
icon: mdi:script
show_in_sidebar: true
filename: dashboards.yaml
- 最后抄一个大佬的配置。
homeassistant:
name: Home
latitude: !secret homeassistant_latitude
longitude: !secret homeassistant_longitude
elevation: !secret homeassistant_elevation
unit_system: metric
time_zone: Asia/Shanghai
packages: !include_dir_named packages/
allowlist_external_dirs:
['/config/']
whitelist_external_dirs:
- /config
media_dirs:
local: /media
rest: !include rest.yaml
template: !include template.yaml
frontend:
themes: !include_dir_merge_named themes
default_config:
energy:
history:
ios:
mobile_app:
media_source:
sun:
system_health:
http:
use_x_forwarded_for: true
trusted_proxies:
- 172.0.0.0/8
- 192.168.2.0/24
lovelace:
mode: yaml
resources:
# mushroom hacs
- url: /hacsfiles/chineseholiday_card/ch_calendar-card.js
type: module
- url: /hacsfiles/lovelace-colorfulclouds-weather-card/colorfulclouds-weather-card.js
type: module
- url: /hacsfiles/lovelace-layout-card/layout-card.js
type: module
- url: /hacsfiles/lovelace-mushroom/mushroom.js
type: module
- url: /hacsfiles/lovelace-rpi-monitor-card/rpi-monitor-card.js
type: module
- url: /hacsfiles/stack-in-card/stack-in-card.js
type: module
- url: /hacsfiles/vertical-stack-in-card/vertical-stack-in-card.js
type: module
# tablet hacs
- url: /hacsfiles/apexcharts-card/apexcharts-card.js
type: module
- url: /hacsfiles/bar-card/bar-card.js
type: module
- url: /hacsfiles/button-card/button-card.js
type: module
- url: /hacsfiles/custom-icons/custom-icons.js
type: module
- url: /hacsfiles/flipdown-timer-card/flipdown-timer-card.js
type: module
- url: /hacsfiles/lovelace-card-mod/card-mod.js
type: module
- url: /hacsfiles/lovelace-card-tools/card-tools.js
type: module
- url: /hacsfiles/lovelace-hui-element/hui-element.js
type: module
- url: /hacsfiles/mini-graph-card/mini-graph-card-bundle.js
type: module
- url: /hacsfiles/mini-media-player/mini-media-player-bundle.js
type: module
- url: /hacsfiles/PlexMeetsHomeAssistant/plex-meets-homeassistant.js
type: module
- url: /hacsfiles/secondaryinfo-entity-row/secondaryinfo-entity-row.js
type: module
- url: /hacsfiles/simple-weather-card/simple-weather-card-bundle.js
type: module
- url: /hacsfiles/swipe-card/swipe-card.js
type: module
- url: /hacsfiles/vacuum-card/vacuum-card.js
type: module
- url: /hacsfiles/weather-card/weather-card.js
type: module
- url: '/local/ui/js/curtain-card.js'
type: module
dashboards:
lovelace-phone:
mode: yaml
title: 手机
icon: hass:cellphone
show_in_sidebar: true
filename: dashboards/phone/mushroom.yaml
lovelace-tablet:
mode: yaml
title: 平板
icon: hass:tablet
show_in_sidebar: true
filename: dashboards/tablet/ui-tablet.yaml
logger:
default: warning
filters:
pyhap.characteristic:
- "SecuritySystemCurrentState: value=0 is an invalid value."
- "SecuritySystemTargetState: value=0 is an invalid value."
- "TargetHeatingCoolingState: value=0 is an invalid value."
logs:
homeassistant.components.command_line: fatal
homeassistant.components.command_line.sensor: error
homeassistant.components.homekit.type_media_players: error
homeassistant.components.media_player: error
homeassistant.components.rest.sensor: error
homeassistant.components.websocket_api: fatal
homeassistant.helpers.entity: error
homeassistant.helpers.event: fatal
homeassistant.helpers.service: error
alarm_control_panel:
- platform: manual
name: Home Alarm
code: !secret alarm_code
arming_time: 30
delay_time: 20
trigger_time: 4
disarmed:
trigger_time: 0
armed_home:
arming_time: 0
delay_time: 0
recorder:
#需要保存的天数
purge_keep_days: 30
#db_url: mysql://账户:密码@ip地址:端口/数据库名字
# db_url: !secret mariadb_url
#排除日志
exclude:
entities:
- media_player.yun_yin_le_homepod_mini
- media_player.yun_yin_le_mpd
timer:
laundry:
duration: "00:01:00"
tts:
- platform: edge_tts
language: zh-CN # 默认语言或声音 zh_CN(zh-CN-XiaoxiaoNeural)
- platform: baidu
app_id: !secret baidutts_id
api_key: !secret baidutts_api_key
secret_key: !secret baidutts_secret_key
- 根据配置建立相应文件夹,放入文件即可。