简介:

Home Assistant 是一款基于 Python 的智能家居开源系统,支持众多品牌的智能家居设备,可以轻松实现设备的语音控制、自动化等。

我的智能家庭包含了众多牌子的智能产品 ,为了将所有的产品进行整合,我选择了HomeAssistant来控制所有家电。
此项目于17年末开始,18年初完成 (宿舍只有少量智能设备) ,19年完善(搬近学校旁的house后完整建设)

作为一个现代化懒人,能张嘴控制绝不动手,什么开关灯,设定空调之类,的喊一声多好。
大冬天的,谁愿意从温暖的被窝里面出来关灯,谁愿意从黑漆漆的地方摸进去开灯。

此篇文章代码较多,欢迎使用右侧导航前往需要查看的部分

设计:

我的智能家庭设计目标是:
– 桌上,门口,床头各一个开关来控制所以灯具以及安防的开关
– 在我离开家之后自动关闭所有灯具,回到家后自动开启灯光(仅限日落后到12:00睡觉前)
– 在我不在家的时候开启安防并通知手机当有人进入我的房间时
– 监控我的房间并记录所有时间戳并生成随时可查看的报告页面
– 可远程访问控制与查看房间状态
– 房间外借时可方便共享权限
– 拥有开放 API 来完成其他有意思的项目
– 接入 Homekit 来使用 IOS 的家庭应用控制
– 启用 GoogleAssistant, Siri, Alexa 来实现语言控制

我的智能家庭所使用的产品:
– TP-Link
– Yeelight
– Google Home (Hub*1, home*1, mini*3)
– Philips Hue (Bulb*3, Go*1, Switch*1)
– Nest (谷歌已经取消了Nest Api所以现在无法使用了)
– Augest Lock
– Unifi Network, Netgear Router, Asus Router
– Unifi 全套
– 小米网关以及门窗传感器,人体传感器,烟雾报警器,来电传感器

效果:

控制界面(PS: 之前写过超好看的lovelace UI,可是一次更新后就消失了,到现在我还没开始重写,因为太累了)

主界面
登录界面(支持信任网段方便共享)
日记

灯具与开关(全都能通过网页,语音,手机互动并控制)



实现:

HomeAssistant的安装这里就不多赘述了,留下的几个链接都写得非常详细易懂,我的环境也十分简单,基于群晖的docker来实现的,所有配置文件都会在 //群晖/docker/config 下。

[隐藏内容] 写博客可辛苦了,快滑到最下面给我个评论就给你看~ [隐藏内容]


连接设备

下面是我的 configuration.yaml 配置详情 (这是我的配置不可照搬,但可以作为你配置的模板)

HA的基本配置:

homeassistant:
  # 站点名称
  name: KRUNK.CN SMARTHOME
  # 你的位置坐标
  latitude: !secret locationa
  longitude: !secret locationb
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 0
  # metric for Metric, imperial for Imperial
  unit_system: metric
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: America/Toronto
  # Customization file
  customize: !include customize.yaml
  #信任的网段以及api密码 - 如不使用API不建议开启api密码
  auth_providers:
   - type: legacy_api_password
     api_password: !secret http_password
   - type: trusted_networks
     trusted_networks:
        - 10.0.0.0/24
     allow_bypass_login: true
   - type: homeassistant

一些需要使用的内置服务:

# Enables configuration UI
config:
system_health:
auth:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
http:
    base_url: krunk.cn #如需外网访问需要设定这个地址
    #api_password: !secret http_password
    ssl_certificate: /config/smarthome.krunk.cn.crt #外网需要ssl证书
    ssl_key: /config/smarthome.krunk.cn.key #证书密钥
    cors_allowed_origins:
    - https://ca.krunk.cn
    - https://ws.krunk.cn

api:

# Checks for available updates
# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
updater:
  # Optional, allows Home Assistant developers to focus on popular components.
  # include_used_components: true

# Discover some devices automatically
#开启自动搜索新智能设备 我禁用是因为我想纯手工配置
#discovery:

# Allows you to issue voice commands from the frontend in enabled browsers
#conversation:
# Enables support for tracking state changes over time
history:
# View all events in a logbook
logbook:
# Enables a map showing the location of tracked devices
map:
# Track the sun
sun:

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

天气服务:

weather:
  - platform: darksky
    api_key: cac4a0dc10fe69a15a3d6367e51e342c

检测我的手机还需付款多少钱:

# Sensors
sensor:
    #Fido
  - platform: fido
    name: Fido Balance
    username: !secret fidoaccount
    password: !secret fidopw
    monitored_variables:
     - balance

检测服务器Uptime:

binary_sensor:
  - platform: uptimerobot
    api_key: !secret bloguptimerobot #Blog
  #- platform: uptimerobot
  #  api_key: !secret wsuptimerobot #WS

文字转语言服务(曾经想用来实现“欢迎到家”):

# Text to speech
tts:
  - platform: google_translate
    service_name: google_say

TP-Link 墙上开关:

tplink:
  discovery: false
  switch:
    - host: !secret IP

HomeKit配置:

homekit:
  auto_start: true  
  port: 51827  
  filter:  
    include_domains:  
      - switch
      - media_player
    include_entities:
      - light.yeelight_strip_34ce008b1407
      - light.gateway_light_7811dcfd6574
      - light.yeelight_strip
      - sensor.illumination_7811dcfd6574
      - binary_sensor.motion_sensor_158d00024818ad
      - binary_sensor.smoke_sensor_158d0001e6426c
      - binary_sensor.door_window_sensor_158d000237cf91
  entity_config:
    media_player.krunk_bedroom_home:
      feature_list:
        - feature: play_pause

连接我的路由器实现追踪手机是否在家:

asuswrt:
  host: !secret IP
  username: !secret asusun
  password: !secret asuspw

device_tracker:
  - platform: asuswrt
    interval_seconds: 12
    consider_home: 60
    new_device_defaults:
      track_new_devices: false
      hide_if_away: False
  - platform: unifi
    host: !secret IP
    username: krunk
    password: !secret unifipassword
    verify_ssl: false
    interval_seconds: 12
    consider_home: 60
    new_device_defaults:
      track_new_devices: false
      hide_if_away: False
  - platform: netgear
    host: !secret IP
    password: !secret netgearpassword
    port: 80
    interval_seconds: 12
    consider_home: 60
    new_device_defaults:
      track_new_devices: false
      hide_if_away: False

Plex媒体控制:

media_player:
  - platform: plex
    entity_namespace: 'plex'
    include_non_clients: true
    scan_interval: 60
    show_all_controls: false
    use_custom_entity_ids: true
    use_episode_art: true
    remove_unavailable_clients: true
    client_remove_interval: 120

小米网关:

xiaomi_aqara:
  discovery_retry: 5
  gateways:
    - key: !secret xiaomigatewayapi
      host: !secret IP

飞利浦Hue:

hue:
  bridges:
    - host: !secret IP
      allow_unreachable: true

Yeelight 灯带:

yeelight:
  devices:
    !secret IP:
      name: Yeelight Strip

IFTTT Webhook:

ifttt:
  key: !secret iftttkey

Google Home:

cast:
  media_player:
  - host: !secret IP
  - host: !secret IP
  - host: !secret IP
  - host: !secret IP
  - host: !secret IP

监控:

camera:
  - platform: ffmpeg
    name: Bedroom Camera
    input: !secret RTSP
  - platform: uvc
    nvr: !secret IP
    port: 7443
    key: !secret rtspkey
    password: !secret rtsppw
    ssl: true


主题配置

套用好看的主题:

# Enables the frontend
frontend: 
  themes: !include theme.yaml
  extra_html_url:
    - /local/icons/icons.html

新建一个文件 theme.yaml :

darkred:
    # Main colors that can be changed
  dark-primary-color: "#c66900"
  disabled-text-color: "#545454"
  divider-color: "rgba(255, 255, 255, 0.12)"
  light-primary-color: "#e06c6c"
  paper-card-background-color: "#1d1d1d"
  paper-grey-200: "#191919"
  paper-item-icon-color: "#d3d3d3"
  paper-listbox-background-color: "#202020"
  primary-background-color: "#303030"
  primary-color: "#d32f2f"
  primary-text-color: "#cfcfcf"
  secondary-background-color: "#131313"
  sidebar-text_-_background: "#62717b"
    # Colors based on variables, see above
  paper-card-header-color: "var(--paper-item-icon-color)"
  paper-item-icon-active-color: "var(--primary-color)"
  paper-item-icon_-_color: "var(--primary-text-color)"
  paper-listbox-color: "var(--primary-text-color)"
  paper-grey-50: "var(--primary-text-color)"
  paper-slider-active-color: "var(--primary-color)"
  paper-slider-knob-color: "var(--primary-color)"
  paper-slider-knob-start-color: "var(--primary-color)"
  paper-slider-pin-color: "var(--primary-color)"
  paper-slider-secondary-color: "var(--light-primary-color)"
  paper-toggle-button-checked-ink-color: "var(--dark-primary-color)"
  paper-toggle-button-checked-button-color: "var(--primary-color)"
  paper-toggle-button-checked-bar-color: "var(--light-primary-color)"
  paper-toggle-button-unchecked-bar-color: "var(--primary-text-color)"
  secondary-text-color: "var(--primary-color)"
  table-row-background-color: "var(--paper-card-background-color)"
  table-row-alternative-background-color: "var(--sidebar-text_-_background)"
darkcyan:
    # Main colors that can be changed
  primary-color: "#00bcd4"
  disabled-text-color: "#545454"
  divider-color: "rgba(255, 255, 255, 0.12)"
  paper-card-background-color: "#4e4e4e"
  paper-grey-200: "#191919"
  paper-item-icon-color: "#d3d3d3"
  paper-listbox-background-color: "#202020"
  paper-listbox-color: "#FFFFFF"
  primary-background-color: "#303030"
  primary-text-color: "#cfcfcf"
  secondary-background-color: "#2b2b2b"
  secondary-text-color: "#04a7bc"
    # Colors based on variables, see above
  label-badge-background-color: "var(--secondary-background-color)"
  label-badge-text-color: "var(--text-primary-color)"
  paper-card-header-color: "#var(--paper-item-icon-color)"
  paper-grey-50: "var(--primary-text-color)"
  paper-item-icon-active-color: "var(--primary-color)"
  paper-item-icon_-_color: "var(--primary-text-color)"
  paper-slider-active-color: "var(--primary-color)"
  paper-slider-knob-color: "var(--primary-color)"
  paper-slider-knob-start-color: "var(--primary-color)"
  paper-slider-pin-color: "var(--primary-color)"
  paper-slider-secondary-color: "var(--light-primary-color)"
  paper-toggle-button-checked-ink-color: "var(--dark-primary-color)"
  paper-toggle-button-checked-button-color: "var(--primary-color)"
  paper-toggle-button-checked-bar-color: "var(--light-primary-color)"
  paper-toggle-button-unchecked-bar-color: "var(--primary-text-color)"

到此为止就完成了所有设备的连接以及界面也应该全部展现

自动化配置

开机设置主题:

- alias: 'Set theme at startup'
  initial_state: 'on'
  trigger: 
  - platform: homeassistant
    event: start
  action:
    service: frontend.set_theme
    data:
      name: darkred

开机通知:

- alias: 'Notified Krunk at startup'
  initial_state: 'on'
  trigger: 
  - platform: homeassistant
    event: start
  action:
    service: ifttt.trigger
    data: {"event":"start_up"}

不在家时的安全警报:

- alias: 'Security System start when Krunk leaves home'
  initial_state: 'on'
  trigger: 
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00024818ad
    to: 'on'
  condition:
  - condition: state
    entity_id: device_tracker.krunkgalaxys9
    state: 'not_home'
  action:
    service: ifttt.trigger
    data: {"event":"Alarm_HA"}

离家关灯,到家开灯:

- alias: Turn on the lights when Krunk gets home
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: device_tracker.krunkgalaxys9
    from: 'not_home'
    to: 'home'
  condition:
  - after: sunset
    condition: sun
  action:
    - service: light.turn_on
      entity_id: group.all_lights
    - service: switch.turn_on
      entity_id: switch.main_light
      
- alias: Turn off the lights when Krunk leaves home
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: device_tracker.krunkgalaxys9
    from: 'home'
    to: 'not_home'
  action:
    - service: light.turn_off
      entity_id: group.all_lights
    - service: switch.turn_off
      entity_id: switch.main_light

通过小米按钮控制开关灯

- alias: Turn on all the lights by Mi button
  initial_state: 'on'
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d0002412a5d
      click_type: single
  condition:
    - condition: state
      entity_id: switch.main_light
      state: 'off'
  action:
    - service: light.turn_on
      entity_id: group.all_lights
    - service: switch.turn_on
      entity_id: switch.main_light

- alias: Turn off all the lights by Mi button
  initial_state: 'on'
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d0002412a5d
      click_type: single
  condition:
    - condition: state
      entity_id: switch.main_light
      state: 'on'
  action:
    - service: light.turn_off
      entity_id: group.all_lights
    - service: switch.turn_off
      entity_id: switch.main_light

自动关闭小米网关灯(这样就不用一个个排除关闭这个灯光了):

- alias: Turn off the useless light
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: light.gateway_light_7811dcfd6574
    to: 'on'
  action:
    - service: light.turn_off
      entity_id: light.gateway_light_7811dcfd6574


API的使用

很久以前就使用Python控制灯光玩过,并写了一个拍手开关灯的项目,还有更多的项目等待你的挖掘

详情可以看我的github项目:

[隐藏内容] 写博客可辛苦了,快滑到最下面给我个评论就给你看~ [隐藏内容]

视频演示:
https://www.instagram.com/p/Bfd9XiGl3wQ/




桌面触控控制面板的制作

还在制作中…