1- --[[
2- Description: SONOFF Water Valve sub-driver for zigbee-valve
3- Version: 1.2
4- Author: guoxin.yang
5- Date: 2026-07-06
6- --]]
1+ -- Copyright 2026 SmartThings, Inc.
2+ -- Licensed under the Apache License, Version 2.0
3+
74local capabilities = require " st.capabilities"
85local zcl_clusters = require " st.zigbee.zcl.clusters"
96local OnOff = zcl_clusters .OnOff
107local PowerConfiguration = zcl_clusters .PowerConfiguration
118local utils = require " st.utils"
129
13- -- 电池轮询间隔(秒): SWV1C 是电池休眠设备,每 2 小时轮询一次
10+ -- Battery Polling Interval (seconds): SWV1C is a battery sleep device, polling every 2 hours
1411local BATTERY_POLL_INTERVAL = 7200
1512
16- -- SWV1C 设备指纹匹配表
13+ -- SWV1C Fingerprint list
1714local FINGERPRINTS = {
1815 { mfr = " SONOFF" , model = " SWV-ZFU" },
1916 { mfr = " SONOFF" , model = " SWV-ZFE" },
2017 { mfr = " SONOFF" , model = " SWV-ZNU" },
2118 { mfr = " SONOFF" , model = " SWV-ZNE" }
2219}
2320
24- --- OnOff 属性上报处理器 → valve 能力点事件
25- --- 必须显式处理,因为子驱动定义了 capability_handlers 后,
26- --- 父驱动的默认 OnOff→valve 映射会被跳过(只剩 OnOff→switch)
27- --- @param driver table 驱动实例
28- --- @param device table 设备实例
29- --- @param value table Zigbee 属性值
21+ --- OnOff Property Reporting Handler → Valve Capability Point Event
22+ --- It must be handled explicitly because the child driver defines capability_handlers.
23+ --- The default OnOff→valve mapping from the parent driver will be skipped (only OnOff→switch remains)
24+ --- @param driver table Driver instance
25+ --- @param device table Device instance
26+ --- @param value table Zigbee attribute value
3027local function onoff_attr_handler (driver , device , value )
3128 local is_on = value .value ~= false and value .value ~= 0
3229 if is_on then
@@ -36,21 +33,21 @@ local function onoff_attr_handler(driver, device, value)
3633 end
3734end
3835
39- --- 电池百分比属性处理器
40- --- Zigbee BatteryPercentageRemaining 范围 0-200( 0%-100%),需除以 2
41- --- @param driver table 驱动实例
42- --- @param device table 设备实例
43- --- @param value table Zigbee 属性值
36+ --- Battery Percentage Attribute Handler
37+ --- Zigbee BatteryPercentageRemaining range 0-200 ( 0%-100%), needs to be divided by 2
38+ --- @param driver table Driver instance
39+ --- @param device table Device instance
40+ --- @param value table Zigbee attribute value
4441local function battery_percentage_handler (driver , device , value )
4542 local raw = value .value
4643 local percent = utils .round (raw / 2 )
4744 device :emit_event (capabilities .battery .battery (percent ))
4845end
4946
50- --- 生命周期初始化处理函数
51- --- 通过周期性主动读取 BatteryPercentageRemaining 来保证电池数据可用
52- --- @param driver table 驱动实例
53- --- @param device table 设备实例
47+ --- Lifecycle initialization handler
48+ --- Ensures battery data is available by periodically actively reading BatteryPercentageRemaining
49+ --- @param driver table Driver instance
50+ --- @param device table Device instance
5451local function device_init (driver , device )
5552 device .thread :call_on_schedule (
5653 BATTERY_POLL_INTERVAL ,
@@ -60,29 +57,29 @@ local function device_init(driver, device)
6057 )
6158end
6259
63- --- valve.open 能力点处理器
64- --- @param driver table 驱动实例
65- --- @param device table 设备实例
66- --- @param command table 能力点命令
60+ --- valve.open ability handler
61+ --- @param driver table driver instance
62+ --- @param device table device instance
63+ --- @param command table ability command
6764local function valve_open_handler (driver , device , command )
6865 device :send (OnOff .server .commands .On (device ))
6966 device :send (OnOff .attributes .OnOff :read (device ))
7067end
7168
72- --- valve.close 能力点处理器
73- --- @param driver table 驱动实例
74- --- @param device table 设备实例
75- --- @param command table 能力点命令
69+ --- valve.close capability handler
70+ --- @param driver table driver instance
71+ --- @param device table device instance
72+ --- @param command table capability command
7673local function valve_close_handler (driver , device , command )
7774 device :send (OnOff .server .commands .Off (device ))
7875 device :send (OnOff .attributes .OnOff :read (device ))
7976end
8077
81- --- 设备匹配检查
82- --- @param opts table 选项
83- --- @param driver table 驱动实例
84- --- @param device table 设备实例
85- --- @return boolean 是否由此子驱动接管
78+ --- Device Matching Check
79+ --- @param opts table Options
80+ --- @param driver table Driver instance
81+ --- @param device table Device instance
82+ --- @return boolean Whether this sub-driver takes over
8683local function is_sonoff_valve (opts , driver , device )
8784 for _ , fingerprint in ipairs (FINGERPRINTS ) do
8885 if device :get_manufacturer () == fingerprint .mfr and device :get_model () == fingerprint .model then
@@ -105,11 +102,11 @@ local sonoff_valve_handler = {
105102 },
106103 zigbee_handlers = {
107104 attr = {
108- -- OnOff 属性上报 → valve 事件(弥补父驱动默认映射被跳过的缺陷)
105+ -- OnOff property reporting → valve event (to compensate for the defect of the parent driver's default mapping being skipped)
109106 [OnOff .ID ] = {
110107 [OnOff .attributes .OnOff .ID ] = onoff_attr_handler
111108 },
112- -- 电池百分比属性上报 → battery 事件
109+ -- Battery percentage attribute reporting → battery event
113110 [PowerConfiguration .ID ] = {
114111 [PowerConfiguration .attributes .BatteryPercentageRemaining .ID ] = battery_percentage_handler
115112 }
0 commit comments