|
3 | 3 |
|
4 | 4 | local capabilities = require "st.capabilities" |
5 | 5 | local zcl_clusters = require "st.zigbee.zcl.clusters" |
| 6 | +local Identify = zcl_clusters.Identify |
6 | 7 | local OnOff = zcl_clusters.OnOff |
7 | 8 | local PowerConfiguration = zcl_clusters.PowerConfiguration |
8 | | -local utils = require "st.utils" |
9 | | - |
10 | | ---- OnOff Property Reporting Handler → Valve Capability Point Event |
11 | | ---- SONOFF reports valve state through the OnOff cluster, so keep an explicit mapping |
12 | | ---- from OnOff reports to valve events while using the parent driver's default commands. |
13 | | ---- @param driver table Driver instance |
14 | | ---- @param device table Device instance |
15 | | ---- @param value table Zigbee attribute value |
16 | | -local function onoff_attr_handler(driver, device, value) |
17 | | - local is_on = value.value ~= false and value.value ~= 0 |
18 | | - if is_on then |
19 | | - device:emit_event(capabilities.valve.valve.open()) |
| 9 | +local st_device = require "st.device" |
| 10 | + |
| 11 | +local CONFIGURATION = { |
| 12 | + { |
| 13 | + cluster = PowerConfiguration.ID, |
| 14 | + attribute = PowerConfiguration.attributes.BatteryPercentageRemaining.ID, |
| 15 | + minimum_interval = 30, |
| 16 | + maximum_interval = 21600, |
| 17 | + data_type = PowerConfiguration.attributes.BatteryPercentageRemaining.base_type, |
| 18 | + reportable_change = 1 |
| 19 | + }, |
| 20 | + { |
| 21 | + cluster = OnOff.ID, |
| 22 | + attribute = OnOff.attributes.OnOff.ID, |
| 23 | + minimum_interval = 0, |
| 24 | + maximum_interval = 600, |
| 25 | + data_type = OnOff.attributes.OnOff.base_type |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +local function find_child(parent, ep_id) |
| 30 | + return parent:get_child_by_parent_assigned_key(string.format("%02X", ep_id)) |
| 31 | +end |
| 32 | + |
| 33 | +local function is_dual_channel(device) |
| 34 | + local model = device:get_model() |
| 35 | + return model == "SWV-ZF2U" or model == "SWV-ZF2" |
| 36 | +end |
| 37 | + |
| 38 | +--- Map OnOff reports to the corresponding valve. |
| 39 | +local function onoff_attr_handler(driver, device, value, zb_rx) |
| 40 | + local ep = zb_rx.address_header.src_endpoint.value |
| 41 | + local target = device |
| 42 | + if device.network_type == st_device.NETWORK_TYPE_ZIGBEE and is_dual_channel(device) and ep == 0x02 then |
| 43 | + target = find_child(device, 2) |
| 44 | + if not target then return end |
| 45 | + end |
| 46 | + if value.value ~= false and value.value ~= 0 then |
| 47 | + target:emit_event(capabilities.valve.valve.open()) |
20 | 48 | else |
21 | | - device:emit_event(capabilities.valve.valve.closed()) |
| 49 | + target:emit_event(capabilities.valve.valve.closed()) |
| 50 | + end |
| 51 | +end |
| 52 | + |
| 53 | +local function device_init(driver, device) |
| 54 | + if device.network_type == st_device.NETWORK_TYPE_ZIGBEE then |
| 55 | + if is_dual_channel(device) then |
| 56 | + device:set_find_child(find_child) |
| 57 | + end |
| 58 | + for _, attribute in ipairs(CONFIGURATION) do |
| 59 | + device:add_configured_attribute(attribute) |
| 60 | + end |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +local function do_configure(driver, device) |
| 65 | + if device.network_type == st_device.NETWORK_TYPE_ZIGBEE then |
| 66 | + device:refresh() |
| 67 | + device:configure() |
22 | 68 | end |
23 | 69 | end |
24 | 70 |
|
25 | | ---- Battery Percentage Attribute Handler |
26 | | ---- Zigbee BatteryPercentageRemaining range 0-200 (0%-100%), needs to be divided by 2 |
27 | | ---- @param driver table Driver instance |
28 | | ---- @param device table Device instance |
29 | | ---- @param value table Zigbee attribute value |
30 | | -local function battery_percentage_handler(driver, device, value) |
31 | | - local raw = value.value |
32 | | - local percent = utils.round(raw / 2) |
33 | | - device:emit_event(capabilities.battery.battery(percent)) |
| 71 | +local function device_added(driver, device) |
| 72 | + if device.network_type == st_device.NETWORK_TYPE_ZIGBEE and is_dual_channel(device) then |
| 73 | + if find_child(device, 2) == nil then |
| 74 | + driver:try_create_device({ |
| 75 | + type = "EDGE_CHILD", |
| 76 | + label = string.format("%s 2", device.label), |
| 77 | + profile = "valve", |
| 78 | + parent_device_id = device.id, |
| 79 | + parent_assigned_child_key = string.format("%02X", 2), |
| 80 | + vendor_provided_label = string.format("%s 2", device.label), |
| 81 | + }) |
| 82 | + end |
| 83 | + end |
| 84 | + device:refresh() |
| 85 | +end |
| 86 | + |
| 87 | +local function driver_switched(driver, device) |
| 88 | + device_added(driver, device) |
| 89 | +end |
| 90 | + |
| 91 | +--- A device button press prompts a refresh of both SWV2C channels. |
| 92 | +local function identify_handler(driver, device, zb_rx) |
| 93 | + if not is_dual_channel(device) then return end |
| 94 | + device.thread:call_with_delay(2, function() |
| 95 | + device:send(OnOff.attributes.OnOff:read(device)) |
| 96 | + device:send(OnOff.attributes.OnOff:read(device):to_endpoint(0x02)) |
| 97 | + end) |
34 | 98 | end |
35 | 99 |
|
36 | 100 | local sonoff_valve_handler = { |
37 | 101 | NAME = "SONOFF Water Valve Handler", |
| 102 | + lifecycle_handlers = { |
| 103 | + init = device_init, |
| 104 | + added = device_added, |
| 105 | + doConfigure = do_configure, |
| 106 | + driverSwitched = driver_switched, |
| 107 | + }, |
38 | 108 | zigbee_handlers = { |
39 | 109 | attr = { |
40 | | - -- OnOff property reporting → valve event (to compensate for the defect of the parent driver's default mapping being skipped) |
41 | 110 | [OnOff.ID] = { |
42 | 111 | [OnOff.attributes.OnOff.ID] = onoff_attr_handler |
43 | | - }, |
44 | | - -- Battery percentage attribute reporting → battery event |
45 | | - [PowerConfiguration.ID] = { |
46 | | - [PowerConfiguration.attributes.BatteryPercentageRemaining.ID] = battery_percentage_handler |
| 112 | + } |
| 113 | + }, |
| 114 | + cluster = { |
| 115 | + [Identify.ID] = { |
| 116 | + [Identify.server.commands.Identify.ID] = identify_handler |
47 | 117 | } |
48 | 118 | } |
49 | 119 | }, |
|
0 commit comments