『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 59052|回复: 63

[进阶教程] homeassistant中添加小米净化器与小米插座(js代码方法)

  [复制链接]

26

主题

94

帖子

1943

积分

金牌会员

Rank: 6Rank: 6

积分
1943
金钱
1814
HASS币
60

教程狂人论坛风云人物突出贡献

发表于 2017-6-3 23:30:58 | 显示全部楼层 |阅读模式
本帖最后由 FrankLv 于 2017-6-3 23:30 编辑

homeassistant添加小米家族的产品已经有很多现成的插件了,有的已经比较完善了,如小米网关、yeelight等,但是对与净化器和插座的插件虽然有但是还存在一些问题,比如无法显示传感器状态、无法加载第二个设备等。本次给大家带来的就是如何解决这些问题。
所使用的方法是通过JS文件配合miio插件来完成的,示范系统为树莓派的debian系统,其他系统可以参照。
1. miio及其插件安装
首先是需要安装miio,代码如下:
sudo npm install --save miio
sudo npm install -g miio

安装完毕后则需要创建js文件了,可以直接使用我的: js.rar (1.59 KB, 下载次数: 300)

2. homeassistant的配置
(1)净化器配置
由于本次使用的是cmd平台,有一定的延迟,所以小米净化器开关还是用py的插件来控制,小米净化器开关配置大家自行搜索,其余功能使用command-line平台实现,这里要介绍的是如何使用cmd平台设置运行模式、LED灯以及实时显示传感器数据。
首先需要查出你的净化器IP地址,这里有很多方法,直接进路由、fing 等,查到IP后,需要设置shell_commands来建立shell_commands服务。
在homeassistant配置目录中建立shell_commands.yaml文件,里面配置如下(192.168.1.197为净化器ip地址,以下一样):
a_auto: 'node /home/pi/airpurifier.js 192.168.1.197 fanmode auto'
a_silent: 'node /home/pi/airpurifier.js 192.168.1.197 fanmode silent'
a_favorite: 'node /home/pi/airpurifier.js 192.168.1.197 fanmode favorite'
a_bright: 'node /home/pi/airpurifier.js 192.168.1.197 led bright'
a_dim: 'node /home/pi/airpurifier.js 192.168.1.197 led dim'
a_ledoff: 'node /home/pi/airpurifier.js 192.168.1.197 led off'

configuration文件文件中加入如下代码:
shell_command: !include shell_commands.yaml

过后需要在传感器配置(sensors)文件或者configuration文件中配置传感器,配置可以参照我的格式,大家注意速进,不然运行会提示错误,(备注:name可以直接中文,这样就不需要后续汉化)
- platform: command_line
name: 'airpurifier-mode'
command: node /home/pi/airpurifier.js 192.168.1.197 status-mode
- platform: command_line
name: 'airpurifier-aqi'
command: node /home/pi/airpurifier.js 192.168.1.197 status-aqi
unit_of_measurement: "AQI"
- platform: command_line
name: 'airpurifier-temp'
command: node /home/pi/airpurifier.js 192.168.1.197 status-temp
unit_of_measurement: "°C"
- platform: command_line
name: 'airpurifier-hum'
command: node /home/pi/airpurifier.js 192.168.1.197 status-hum
unit_of_measurement: "%"
- platform: command_line
name: 'airpurifier-led'
command: node /home/pi/airpurifier.js 192.168.1.197 status-led

过后需要建立input_select的选择开关和binary_sensor传感器,你可以直接在configuration文件中配置,也可以将其在子文件中编辑。
我采用的是在子文件编辑,需要在homeassistant配置目录中建立input_select.yaml文件,里面配置如下:
airpurifier_mode:
name: '净化器模式'
options:
- '自动模式'
- '安静模式'
- '最爱模式'
- '关'
initial: '自动模式'
icon: mdi:fan
airpurifier_led:
name: '净化器LED'
options:
- '高亮'
- '微亮'
- '关闭'
initial: '高亮'
icon: mdi:led-on

在homeassistant配置目录中建立binary_sensor.yaml文件,里面配置如下:
- platform: command_line
command: node /home/pi/airpurifier.js 192.168.1.197 status-power
name: 'airpurifier-power'
device_class: connectivity
payload_on: "true"
payload_off: "false"

过后在configuration文件中插入如下代码:
input_select: !include input_select.yaml
binary_sensor: !include binary_sensor.yaml

过后就是自动化配置了,在automations配置文件中,输入如下代码:
#########################################
############ 小米净化器配置 #############
#########################################
- id: '1495687391563'
alias: airpurifier-mode1
initial_state: true
hide_entity: true
trigger:
platform: state
entity_id: input_select.airpurifier_mode
action:
service_template: >
{% if is_state("input_select.airpurifier_mode", "关") %}
switch.turn_off
{% elif is_state("input_select.airpurifier_mode", "自动模式") %}
shell_command.a_auto
{% elif is_state("input_select.airpurifier_mode", "安静模式") %}
shell_command.a_silent
{% elif is_state("input_select.airpurifier_mode", "最爱模式") %}
shell_command.a_favorite
{% endif %}
entity_id: switch.air_purifier
- id: '1495687391564'
alias: airpurifier-mode2
initial_state: true
hide_entity: true
trigger:
platform: state
entity_id: sensor.airpurifiermode
action:
service: input_select.select_option
entity_id: input_select.airpurifier_mode
data_template:
option: >
{% if is_state("switch.air_purifier", "off") %}

{% elif is_state("sensor.airpurifiermode", "auto") %}
自动模式
{% elif is_state("sensor.airpurifiermode", "silent") %}
安静模式
{% elif is_state("sensor.airpurifiermode", "favorite") %}
最爱模式
{% endif %}
- id: '1495687391565'
alias: airpurifier-led1
initial_state: true
hide_entity: true
trigger:
platform: state
entity_id: input_select.airpurifier_led
action:
service_template: >
{% if is_state("input_select.airpurifier_led", "高亮") %}
shell_command.a_bright
{% elif is_state("input_select.airpurifier_led", "微亮") %}
shell_command.a_dim
{% elif is_state("input_select.airpurifier_led", "关闭") %}
shell_command.a_ledoff
{% endif %}
entity_id: switch.air_purifier
- id: '1495687391566'
alias: airpurifier-led2
initial_state: true
hide_entity: true
trigger:
platform: state
entity_id: sensor.airpurifierled
action:
service: input_select.select_option
entity_id: input_select.airpurifier_led
data_template:
option: >
{% if is_state("sensor.airpurifierled", "bright") %}
高亮
{% elif is_state("sensor.airpurifierled", "dim") %}
微亮
{% elif is_state("sensor.airpurifierled", "off") %}
关闭
{% endif %}

过后是分组,分组在groups配置文件中输入如下代码
airpurifier:
name: 小米净化器
view: no
entities:
- sensor.airpurifieraqi
- sensor.airpurifiertemp
- sensor.airpurifierhum
####这是py净化器开关####
- switch.air_purifier
###根据自己的配置修改###
- input_select.airpurifier_mode
- input_select.airpurifier_led

最后是汉化,在customize配置文件中设置,代码如下:
sensor.airpurifieraqi:
friendly_name: 小米净化器AQI
homebridge_name: 小米净化器AQI
icon: mdi:apple-mobileme
sensor.airpurifiertemp:
friendly_name: 小米净化器温度
homebridge_name: 小米净化器温度
sensor.airpurifierhum:
friendly_name: 小米净化器湿度
homebridge_name: 小米净化器湿度
icon: mdi:thermometer-lines
input_select.airpurifier_mode:
friendly_name: 小米净化器模式
homebridge_name: 小米净化器模式
input_select.airpurifier_led:
friendly_name: 小米净化器指示灯
homebridge_name: 小米净化器指示灯
binary_sensor.airpurifierpower:
friendly_name: 小米净化器状态
homebridge_name: 小米净化器状态
hidden: true
sensor.airpurifiermode:
friendly_name: 小米净化器模式状态
homebridge_name: 小米净化器模式状态
hidden: true
sensor.airpurifierled:
friendly_name: 小米净化器指示状态
homebridge_name: 小米净化器指示状态
hidden: true

(2)小米插座开关配置
类似与净化器,在刚才创建的shell_commands.yaml文件中加入如下代码:
h_on: 'node /home/pi/powerstrip.js 192.168.1.111 power true'
h_off: 'node /home/pi/powerstrip.js 192.168.1.111 power false'

input_boolean.yaml文件里面配置如下:
powerstrip:
name: '插座开关'
icon: mdi:lightbulb-on
initial: off

binary_sensor.yaml文件里面配置如下:
- platform: command_line
command: node /home/pi/powerstrip.js 192.168.1.111 status
name: 'powerstrip'
device_class: connectivity
payload_on: "{ '0': true }"
payload_off: "{ '0': false }"

configuration文件文件中加入如下代码:
input_boolean: !include input_boolean.yaml

automations配置文件中,输入如下代码:
#########################################
############# 小米插座配置 ##############
#########################################
- id: '1495687391558'
alias: powerstrip_on
initial_state: true
hide_entity: true
trigger:
- platform: state
entity_id: input_boolean.powerstrip
from: 'off'
to: 'on'
action:
- service: shell_command.h_on
- id: '1495687391559'
alias: powerstrip_off
initial_state: true
hide_entity: true
trigger:
- platform: state
entity_id: input_boolean.powerstrip
from: 'on'
to: 'off'
action:
- service: shell_command.h_off
- id: '1495687391560'
alias: powerstrip_status_on
initial_state: true
hide_entity: true
trigger:
- platform: state
entity_id: binary_sensor.powerstrip
from: 'off'
to: 'on'
condition:
- condition: state
entity_id: input_boolean.powerstrip
state: 'off'
action:
- service: input_boolean.turn_on
entity_id: input_boolean.powerstrip
- id: '1495687391561'
alias: powerstrip_status_off
initial_state: true
hide_entity: true
trigger:
- platform: state
entity_id: binary_sensor.powerstrip
from: 'on'
to: 'off'
condition:
- condition: state
entity_id: input_boolean.powerstrip
state: 'on'
action:
- service: input_boolean.turn_off
entity_id: input_boolean.powerstrip
- id: '1495687391562'
alias: powerstrip_status
initial_state: true
hide_entity: true
trigger:
- platform: homeassistant
event: start
condition:
- condition: template
value_template: "{% if states.input_boolean.powerstrip.state != states.binary_sensor.powerstrip.state %}true{% endif %}"
action:
- service: input_boolean.turn_on
entity_id: input_boolean.powerstrip

至此,所以配置完毕,效果大致如下:

N38~[GV_OA76X8~3C}_KQBA.png
1YY[HMB59Z5W39_VQP_5`S3.png

评分

参与人数 1金钱 +5 收起 理由
lidicn + 5 神马都是浮云

查看全部评分

回复

使用道具 举报

24

主题

604

帖子

3402

积分

元老级技术达人

积分
3402
金钱
2793
HASS币
0

卓越贡献

发表于 2017-6-3 23:36:39 | 显示全部楼层
多谢分享啊!!太好了。马上购买净化器!!
回复

使用道具 举报

18

主题

142

帖子

2576

积分

金牌会员

Rank: 6Rank: 6

积分
2576
金钱
2434
HASS币
0

论坛风云人物

发表于 2017-6-3 23:41:35 来自手机 | 显示全部楼层
支持支持
回复

使用道具 举报

5

主题

252

帖子

1931

积分

金牌会员

Rank: 6Rank: 6

积分
1931
金钱
1679
HASS币
0
发表于 2017-6-4 00:04:01 来自手机 | 显示全部楼层
支持支持
回复

使用道具 举报

3

主题

219

帖子

930

积分

高级会员

Rank: 4

积分
930
金钱
711
HASS币
0
发表于 2017-6-4 00:56:19 | 显示全部楼层
不错不错。
回复

使用道具 举报

1

主题

77

帖子

248

积分

中级会员

Rank: 3Rank: 3

积分
248
金钱
171
HASS币
0
发表于 2017-6-4 01:36:33 来自手机 | 显示全部楼层
支持支持
回复

使用道具 举报

1

主题

37

帖子

595

积分

管理员

米罗月色

Rank: 9Rank: 9Rank: 9

积分
595
金钱
558
HASS币
0
发表于 2017-6-4 02:14:12 | 显示全部楼层
必须大力支持啊!
回复

使用道具 举报

30

主题

999

帖子

4119

积分

论坛元老

Rank: 8Rank: 8

积分
4119
金钱
3115
HASS币
0

活跃会员

发表于 2017-6-4 08:12:14 来自手机 | 显示全部楼层
frank大神的教程写的真详细!
回复

使用道具 举报

3

主题

219

帖子

930

积分

高级会员

Rank: 4

积分
930
金钱
711
HASS币
0
发表于 2017-6-4 11:45:02 | 显示全部楼层
拜托把小米净化器开关也加上吧,谢谢
回复

使用道具 举报

26

主题

94

帖子

1943

积分

金牌会员

Rank: 6Rank: 6

积分
1943
金钱
1814
HASS币
60

教程狂人论坛风云人物突出贡献

 楼主| 发表于 2017-6-4 14:22:30 来自手机 | 显示全部楼层
本帖最后由 FrankLv 于 2017-6-4 21:56 编辑
xinqinew 发表于 2017-6-4 11:45
拜托把小米净化器开关也加上吧,谢谢

净化器开关cmd为:
node /home/pi/airpurifier.js 192.168.1.197 power true
node /home/pi/airpurifier.js 192.168.1.197 power false
自己按照教程方法添加即可
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-14 06:27 , Processed in 0.502235 second(s), 36 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表