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
73local capabilities = require " st.capabilities"
84local zcl_clusters = require " st.zigbee.zcl.clusters"
95local OnOff = zcl_clusters .OnOff
106local PowerConfiguration = zcl_clusters .PowerConfiguration
117local utils = require " st.utils"
128
13- -- 电池轮询间隔(秒): SWV1C 是电池休眠设备,每 2 小时轮询一次
9+ -- Battery Polling Interval (seconds): SWV1C is a battery sleep device, polling every 2 hours
1410local BATTERY_POLL_INTERVAL = 7200
1511
16- -- SWV1C 设备指纹匹配表
12+ -- SWV1C Fingerprint list
1713local FINGERPRINTS = {
1814 { mfr = " SONOFF" , model = " SWV-ZFU" },
1915 { mfr = " SONOFF" , model = " SWV-ZFE" },
2016 { mfr = " SONOFF" , model = " SWV-ZNU" },
2117 { mfr = " SONOFF" , model = " SWV-ZNE" }
2218}
2319
24- --- OnOff 属性上报处理器 → valve 能力点事件
25- --- 必须显式处理,因为子驱动定义了 capability_handlers 后,
26- --- 父驱动的默认 OnOff→valve 映射会被跳过(只剩 OnOff→switch)
27- --- @param driver table 驱动实例
28- --- @param device table 设备实例
29- --- @param value table Zigbee 属性值
20+ --- OnOff Property Reporting Handler → Valve Capability Point Event
21+ --- It must be handled explicitly because the child driver defines capability_handlers.
22+ --- The default OnOff→valve mapping from the parent driver will be skipped (only OnOff→switch remains)
23+ --- @param driver table Driver instance
24+ --- @param device table Device instance
25+ --- @param value table Zigbee attribute value
3026local function onoff_attr_handler (driver , device , value )
3127 local is_on = value .value ~= false and value .value ~= 0
3228 if is_on then
@@ -36,21 +32,21 @@ local function onoff_attr_handler(driver, device, value)
3632 end
3733end
3834
39- --- 电池百分比属性处理器
40- --- Zigbee BatteryPercentageRemaining 范围 0-200( 0%-100%),需除以 2
41- --- @param driver table 驱动实例
42- --- @param device table 设备实例
43- --- @param value table Zigbee 属性值
35+ --- Battery Percentage Attribute Handler
36+ --- Zigbee BatteryPercentageRemaining range 0-200 ( 0%-100%), needs to be divided by 2
37+ --- @param driver table Driver instance
38+ --- @param device table Device instance
39+ --- @param value table Zigbee attribute value
4440local function battery_percentage_handler (driver , device , value )
4541 local raw = value .value
4642 local percent = utils .round (raw / 2 )
4743 device :emit_event (capabilities .battery .battery (percent ))
4844end
4945
50- --- 生命周期初始化处理函数
51- --- 通过周期性主动读取 BatteryPercentageRemaining 来保证电池数据可用
52- --- @param driver table 驱动实例
53- --- @param device table 设备实例
46+ --- Lifecycle initialization handler
47+ --- Ensures battery data is available by periodically actively reading BatteryPercentageRemaining
48+ --- @param driver table Driver instance
49+ --- @param device table Device instance
5450local function device_init (driver , device )
5551 device .thread :call_on_schedule (
5652 BATTERY_POLL_INTERVAL ,
@@ -60,29 +56,29 @@ local function device_init(driver, device)
6056 )
6157end
6258
63- --- valve.open 能力点处理器
64- --- @param driver table 驱动实例
65- --- @param device table 设备实例
66- --- @param command table 能力点命令
59+ --- valve.open ability handler
60+ --- @param driver table driver instance
61+ --- @param device table device instance
62+ --- @param command table ability command
6763local function valve_open_handler (driver , device , command )
6864 device :send (OnOff .server .commands .On (device ))
6965 device :send (OnOff .attributes .OnOff :read (device ))
7066end
7167
72- --- valve.close 能力点处理器
73- --- @param driver table 驱动实例
74- --- @param device table 设备实例
75- --- @param command table 能力点命令
68+ --- valve.close capability handler
69+ --- @param driver table driver instance
70+ --- @param device table device instance
71+ --- @param command table capability command
7672local function valve_close_handler (driver , device , command )
7773 device :send (OnOff .server .commands .Off (device ))
7874 device :send (OnOff .attributes .OnOff :read (device ))
7975end
8076
81- --- 设备匹配检查
82- --- @param opts table 选项
83- --- @param driver table 驱动实例
84- --- @param device table 设备实例
85- --- @return boolean 是否由此子驱动接管
77+ --- Device Matching Check
78+ --- @param opts table Options
79+ --- @param driver table Driver instance
80+ --- @param device table Device instance
81+ --- @return boolean Whether this sub-driver takes over
8682local function is_sonoff_valve (opts , driver , device )
8783 for _ , fingerprint in ipairs (FINGERPRINTS ) do
8884 if device :get_manufacturer () == fingerprint .mfr and device :get_model () == fingerprint .model then
@@ -105,11 +101,11 @@ local sonoff_valve_handler = {
105101 },
106102 zigbee_handlers = {
107103 attr = {
108- -- OnOff 属性上报 → valve 事件(弥补父驱动默认映射被跳过的缺陷)
104+ -- OnOff property reporting → valve event (to compensate for the defect of the parent driver's default mapping being skipped)
109105 [OnOff .ID ] = {
110106 [OnOff .attributes .OnOff .ID ] = onoff_attr_handler
111107 },
112- -- 电池百分比属性上报 → battery 事件
108+ -- Battery percentage attribute reporting → battery event
113109 [PowerConfiguration .ID ] = {
114110 [PowerConfiguration .attributes .BatteryPercentageRemaining .ID ] = battery_percentage_handler
115111 }
0 commit comments