Skip to content

Instantly share code, notes, and snippets.

@willwhui
Created December 3, 2017 03:19
Show Gist options
  • Save willwhui/425dcb10ddc361a2230dd56c3ea821f3 to your computer and use it in GitHub Desktop.
Save willwhui/425dcb10ddc361a2230dd56c3ea821f3 to your computer and use it in GitHub Desktop.
在hass中接入小米网关
@willwhui
Copy link
Author

willwhui commented Dec 3, 2017

按照hass官方文档:https://home-assistant.io/components/xiaomi_aqara/
设置并重启。
后台输出错误:

Setup failed for xiaomi_aqara: Component failed to initialize.

根据这里的一些讨论,联想到可能是我的hass处于子网,而小米gateway在上一级网络内。
试试增加配置ip:

xiaomi_aqara:
  gateways:
    - host: 192.168.31.183
      key: !secret xiaomi_gateway_password

不行,再增加mac

gateways:
  - host: 192.168.31.183
    mac: !secret xiaomi_gateway_mac
    key: !secret xiaomi_gateway_password

好了。
但是不能操纵网关的灯,提示错误

 [PyXiaomiGateway] Got error element in data {"error":"Invalid key"}

经过各种尝试,最终发现是不能把key用 !secret模式包装起来

写成这样就可以了:

gateways:
  - host: 192.168.31.183
    mac: !secret xiaomi_gateway_mac
    key: yj2k6jl6lj86n5vg

并且,应该让小米网关和hass处于同一个子网段中

@willwhui
Copy link
Author

willwhui commented Dec 4, 2017

定制名字

小米网关绑定的感应器,在后台的名字是带了一串数字的,可以在通过customize定制显示名

################################################################
# Xiaomi Gateway
light.gateway_light_7811dc64e702:
   friendly_name: 'Xiaomi Gateway Light'
sensor.Illumination_7811dc64e702:
   friendly_name: 'Xiaomi Gateway Light Sensor'


binary_sensor.motion_sensor_158d0001d984c6:
  icon: mdi:numeric-0-box
  friendly_name: 'Body Sensor 0'

binary_sensor.motion_sensor_158d0001db25e2:
  icon: mdi:numeric-1-box
  friendly_name: 'Body Sensor 1'

binary_sensor.motion_sensor_158d0001db5021:
  icon: mdi:numeric-2-box
  friendly_name: 'Body Sensor 2'

binary_sensor.cube_158d000103a704:
  icon: mdi:cube
  friendly_name: 'Magic Cube'
################################################################

但是id不能改变

@willwhui
Copy link
Author

使用模板控制网关自带的灯的颜色:

如果想使用颜色名字(比如"red","orange")来控制,其结果是灯的某些实际颜色和预期的颜色有差别。
所以尝试用data_template

data_template:
  rgb_color: > 
    {% if states.sensor.waqi_zhuhai.state | int >=  51 %}
      [255,0,0]   
    {% else %}
      [128,0,128]
    {% endif %}

结果不能成功:
ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got '[128, 0, 128]'
可见其原因是不能正确的将模板中的字符串转换为rgb值
可行的解决方案是:
将模板改为普通的规则,有多少个if就设置多少个规则。
这些规则具有相同的trigger,不同的condition。
如:

- alias: "Light on Air Quality Index: brown"
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_boolean.aqi_checker 
      to: 'on'
  condition:
    condition: numeric_state
    entity_id: sensor.waqi_zhuhai
    #below: 
    above: 300
  action:
    - service: light.turn_on
      entity_id: !secret xiaomi_gateway_light_id
      data:
        rgb_color: [165,42,42]

- alias: "Light on Air Quality Index: purple"
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_boolean.aqi_checker 
      to: 'on'
  condition:
    condition: numeric_state
    entity_id: sensor.waqi_zhuhai
    below: 301
    above: 200
  action:
    - service: light.turn_on
      entity_id: !secret xiaomi_gateway_light_id
      data:
        rgb_color: [128,0,128]

@willwhui
Copy link
Author

不能正常接受感应器信息

在hass的设备(树莓派)上不恰当的配置iptables,更改端口转发规则,会导致无法发现感应器,并出现invalide key错误。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment