问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Home Assistant配置入门:从基础配置到前端资源管理

创作时间:
作者:
@小白创作中心

Home Assistant配置入门:从基础配置到前端资源管理

引用
CSDN
1.
https://blog.csdn.net/Dbh321/article/details/142452460

本章节将详细介绍Home Assistant(HA)的配置入门,包括基本配置文件的设置、前端资源的加载方式、配置文件的分隔方法以及模板脚本的使用等技术细节。

一、路径解释

在HA中,内部资源的访问路径有以下两种方式:

  • /local/community/xxxx/yyyyy.js:表示HA配置目录下的www文件夹。
  • /hacsfiles/xxoxyyy-js:表示HA的/config/community路径。

二、下载前端的三种方式

1. HACS商店搜索下载

这是最简单的方式,直接在HACS商店中搜索并下载所需的插件。

2. 通过GitHub仓库地址添加

当HACS商店中找不到所需插件时,可以通过添加GitHub仓库地址来手动添加。具体步骤如下:

  1. 下载插件的JS文件。
  2. /config/www/community目录下创建与该组件名相同的名字,并将JS文件保存进去。
  3. 在设置->仪表盘->资源中写入组件路径,添加前端资源。

三、configuration文件基本配置

在HA的早期版本,需要将HA的基本配置放在配置文件中,这里一块记录,当然这些配置都可以通过页面进行配置。

homeassistant:
  # 你HA的名字
  name: "My Home Assistant Instance"
  # 经纬度
  latitude: 37
  longitude: -121
  # 时区
  time_zone: "America/Los_Angeles"

四、分隔配置文件

(一)基本用法

使用!include语法可以将configuration.yml以外的配置拉入主配置。例如:

automation: !include automation.yaml
zone: !include zone.yaml
sensor: !include sensor.yaml
switch: !include switch.yaml
device_tracker: !include device_tracker.yaml

(二)高级用法

1. !include_dir_list

将目录的内容作为一个列表返回,其中每个文件的内容都是列表中的一个条目。需要注意的是,在使用!include_dir_list时,每个文件必须只包含一个条目。

2. !include_dir_named

将返回目录的内容作为一个字典,该字典将文件名映射到文件的内容。

3. !include_dir_merge_list

会将一个目录的内容作为一个列表返回,方法是将所有文件(这些文件应该包含列表)合并成一个大的列表。重要的是要注意,当使用!include_dir_merge_list时,你必须在每个文件中包含一个列表(每个列表项都用连字符[-]表示)。每个文件可以包含一个或多个条目。

4. !include_dir_merge_named

将加载目录中的每个文件,并将它们合并成一个大的字典,然后返回这个字典的内容。

五、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

根据配置建立相应文件夹,放入文件即可。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号