- 积分
- 123
- 金钱
- 101
- 威望
- 0
- 贡献
- 0
- HASS币
- 0
注册会员
- 积分
- 123
- 金钱
- 101
- HASS币
- 0
|
发表于 2022-8-14 11:16:16
|
显示全部楼层
本帖最后由 coderwj 于 2022-8-14 11:20 编辑
1. 下面是代码片段
void init() {
println("--------------- add sensor to configure.yaml ----------------------")
// 固定的前缀
println(
"""mqtt:
sensor:
# ${serverConf.user}.${serverConf.name}""")
// 反射各字段, 转为yaml
ServerInfo.getDeclaredFields().each { toSensorYaml(it) }
println("\n------------------------------------------------------------------")
}
void toSensorYaml(Field field) {
// 获取传感器的各属性的值
def name = field.getName()
def haSensor = field.getAnnotation(HaSensor)
if (haSensor?.ignore()) {
// 配置了ignore就忽略该字段
log.debug("skip sensor ${name}")
return
}
def serverName = serverConf.getName()
def user = serverConf.getUser()
def friendlyPattern = haSensor?.friendlyName() ?: haSensor?.value()
def friendlyName = friendlyPattern ? friendlyPattern.formatted(serverName) : "${serverName} ${name}"
def uniqueId = "${serverName}_${StringUtil.camelToUnderline(name, false)}"
// 统一加到variables集合,key为ha中sensor的字段名,value为字段值
// def variables = [:] 下面一行应该是这个, 论坛好像会把行尾的 [ : ] 转义
def variables = [:
// 实体名称
variables."name" = "${friendlyName}"
variables."state_topic" = "home/${user}/${serverName}/sensor/info"
// 实体id?
variables."unique_id" = uniqueId
variables."object_id" = uniqueId
variables."value_template" = "{{ value_json.${name} }}"
def icon = haSensor?.icon() ?: (homeConf.icon ?: "")
if (icon) {
variables."icon" = icon
}
def expireAfter = haSensor?.expireAfter()
if (expireAfter) {
variables."expire_after" = expireAfter
}
def unit = haSensor?.unitOfMeasurement()
if (unit) {
variables."unit_of_measurement" = unit
}
// 遍历variables拼接sensor字段
def sensor = ""
variables.each { it ->
def pre = it.key == "name" ? " - " : " "
sensor += "${pre}${it.key}: \"${it.value}\"\n"
}
print(sensor)
}
2. 输出效果如下
--------------- add sensor to configure.yaml ----------------------
mqtt:
sensor:
# me.pc
- name: "pc 设备名"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_name"
object_id: "pc_name"
value_template: "{{ value_json.name }}"
icon: "mdi:desktop-tower-monitor"
- name: "pc 设备别名"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_alias"
object_id: "pc_alias"
value_template: "{{ value_json.alias }}"
icon: "mdi:desktop-tower-monitor"
- name: "pc 操作系统"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_os"
object_id: "pc_os"
value_template: "{{ value_json.os }}"
icon: "mdi:microsoft-windows"
- name: "pc 启动时间"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_boot_time"
object_id: "pc_boot_time"
value_template: "{{ value_json.bootTime }}"
icon: "mdi:clock-time-two"
- name: "pc ip"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_ip"
object_id: "pc_ip"
value_template: "{{ value_json.ip }}"
icon: "mdi:ip-network"
- name: "pc CPU型号"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_cpu_type"
object_id: "pc_cpu_type"
value_template: "{{ value_json.cpuType }}"
icon: "mdi:cpu-64-bit"
- name: "pc CPU占用率"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_cpu_used"
object_id: "pc_cpu_used"
value_template: "{{ value_json.cpuUsed }}"
icon: "mdi:cpu-64-bit"
expire_after: "60"
unit_of_measurement: "%"
- name: "pc 内存占用信息"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_mem_used_info"
object_id: "pc_mem_used_info"
value_template: "{{ value_json.memUsedInfo }}"
icon: "mdi:memory"
expire_after: "60"
- name: "pc 内存占用率"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_mem_used"
object_id: "pc_mem_used"
value_template: "{{ value_json.memUsed }}"
icon: "mdi:memory"
expire_after: "60"
unit_of_measurement: "%"
- name: "pc 风扇速度"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_fan_speed"
object_id: "pc_fan_speed"
value_template: "{{ value_json.fanSpeed }}"
icon: "mdi:fan"
expire_after: "60"
- name: "pc 温度"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_temperature"
object_id: "pc_temperature"
value_template: "{{ value_json.temperature }}"
icon: "mdi:car-brake-temperature"
expire_after: "60"
unit_of_measurement: "℃"
- name: "pc 状态刷新时间"
state_topic: "home/me/pc/sensor/info"
unique_id: "pc_last_modify"
object_id: "pc_last_modify"
value_template: "{{ value_json.lastModify }}"
icon: "mdi:clock-time-two"
------------------------------------------------------------------
|
评分
-
查看全部评分
|