-
Notifications
You must be signed in to change notification settings - Fork 557
[WWSTCERT-13462, WWSTCERT-13465] Sonoff SWV2C valve support #3171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yezi289
wants to merge
2
commits into
SmartThingsCommunity:main
Choose a base branch
from
yezi289:swv2c
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
drivers/SmartThings/zigbee-valve/profiles/valve-battery-2.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| name: valve-battery-2 | ||
| components: | ||
| - id: main | ||
| capabilities: | ||
| - id: valve | ||
| version: 1 | ||
| - id: battery | ||
| version: 1 | ||
| - id: powerSource | ||
| version: 1 | ||
| config: | ||
| values: | ||
| - key: "powerSource.value" | ||
| enabledValues: | ||
| - battery | ||
| - id: firmwareUpdate | ||
| version: 1 | ||
| - id: refresh | ||
| version: 1 | ||
| categories: | ||
| - name: WaterValve |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| name: valve | ||
| components: | ||
| - id: main | ||
| capabilities: | ||
| - id: valve | ||
| version: 1 | ||
| - id: refresh | ||
| version: 1 | ||
| categories: | ||
| - name: WaterValve |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
drivers/SmartThings/zigbee-valve/src/sonoff/can_handle.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| -- Copyright 2026 SmartThings, Inc. | ||
| -- Licensed under the Apache License, Version 2.0 | ||
|
|
||
| local FINGERPRINTS = { | ||
| { mfr = "SONOFF", model = "SWV-ZF2U" }, | ||
| { mfr = "SONOFF", model = "SWV-ZF2" } | ||
| } | ||
|
|
||
| local function sonoff_can_handle(opts, driver, device, ...) | ||
| for _, fingerprint in ipairs(FINGERPRINTS) do | ||
| if device:get_manufacturer() == fingerprint.mfr and device:get_model() == fingerprint.model then | ||
| return true, require "sonoff" | ||
| end | ||
| end | ||
| if device.parent_device_id ~= nil then | ||
| return true, require "sonoff" | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| return sonoff_can_handle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| -- Copyright 2026 SmartThings, Inc. | ||
| -- Licensed under the Apache License, Version 2.0 | ||
|
|
||
| local capabilities = require "st.capabilities" | ||
| local log = require "log" | ||
| local zcl_clusters = require "st.zigbee.zcl.clusters" | ||
| local Basic = zcl_clusters.Basic | ||
| local OnOff = zcl_clusters.OnOff | ||
| local PowerConfiguration = zcl_clusters.PowerConfiguration | ||
| local st_device = require "st.device" | ||
| local utils = require "st.utils" | ||
|
|
||
| local BATTERY_POLL_INTERVAL = 7200 | ||
|
|
||
| local function find_child(parent, ep_id) | ||
| return parent:get_child_by_parent_assigned_key(string.format("%02X", ep_id)) | ||
| end | ||
|
|
||
| --- OnOff Property Reporting → Valve Capability Point Event | ||
| local function onoff_attr_handler(driver, device, value, zb_rx) | ||
| local ep = zb_rx.address_header.src_endpoint.value | ||
| local target = device | ||
| if ep == 0x02 then | ||
| local child = find_child(device, 2) | ||
| if child then target = child end | ||
| end | ||
| log.info(string.format("[SWV] OnOff report ep=%s value=%s", tostring(ep), tostring(value.value))) | ||
| if value.value == true or value.value == 1 then | ||
| target:emit_event(capabilities.valve.valve.open()) | ||
| else | ||
| target:emit_event(capabilities.valve.valve.closed()) | ||
| end | ||
| end | ||
|
|
||
| --- Battery Percentage Attribute Handler | ||
| local function battery_percentage_handler(driver, device, value) | ||
| device:emit_event(capabilities.battery.battery(utils.round(value.value / 2))) | ||
| end | ||
|
|
||
| --- Lifecycle initialization handler | ||
| local function device_init(driver, device) | ||
| if device.network_type == st_device.NETWORK_TYPE_ZIGBEE then | ||
| device:set_find_child(find_child) | ||
| device.thread:call_on_schedule( | ||
| BATTERY_POLL_INTERVAL, | ||
| function() | ||
| device:send(PowerConfiguration.attributes.BatteryPercentageRemaining:read(device)) | ||
| end | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| --- doConfigure | ||
| local function do_configure(driver, device) | ||
| device:try_update_metadata({ provisioning_state = "PROVISIONED" }) | ||
| if device.network_type == st_device.NETWORK_TYPE_ZIGBEE then | ||
| device:emit_event(capabilities.valve.valve.closed()) | ||
| device:emit_event(capabilities.battery.battery(100)) | ||
| else | ||
| device:emit_event(capabilities.valve.valve.closed()) | ||
| end | ||
| end | ||
|
|
||
| --- added | ||
| local function device_added(driver, device) | ||
| if device.network_type == st_device.NETWORK_TYPE_ZIGBEE then | ||
| if find_child(device, 2) == nil then | ||
| driver:try_create_device({ | ||
| type = "EDGE_CHILD", | ||
| label = string.format("%s 2", device.label), | ||
| profile = "valve", | ||
| parent_device_id = device.id, | ||
| parent_assigned_child_key = string.format("%02X", 2), | ||
| vendor_provided_label = string.format("%s 2", device.label), | ||
| }) | ||
| end | ||
| else | ||
| device:emit_event(capabilities.valve.valve.closed()) | ||
| end | ||
| end | ||
|
|
||
| --- driverSwitched | ||
| local function driver_switched(driver, device) | ||
| device_added(driver, device) | ||
| end | ||
|
|
||
| --- valve.open | ||
| local function valve_open_handler(driver, device, command) | ||
| device:send(OnOff.server.commands.On(device)) | ||
| device:send(OnOff.attributes.OnOff:read(device)) | ||
| end | ||
|
|
||
| --- valve.close | ||
| local function valve_close_handler(driver, device, command) | ||
| device:send(OnOff.server.commands.Off(device)) | ||
| device:send(OnOff.attributes.OnOff:read(device)) | ||
| end | ||
|
|
||
| --- Identity cluster handler:button pressed on the device, sync valve states | ||
| local function identify_handler(driver, device, zb_rx) | ||
| log.info("[SWV] Identify received, syncing valve states") | ||
| device.thread:call_with_delay(2, function() | ||
| device:send(OnOff.attributes.OnOff:read(device)) | ||
| device:send(OnOff.attributes.OnOff:read(device):to_endpoint(0x02)) | ||
| end) | ||
| end | ||
|
|
||
| local sonoff_valve_handler = { | ||
| NAME = "SONOFF Water Valve Handler", | ||
| lifecycle_handlers = { | ||
| init = device_init, | ||
| added = device_added, | ||
| doConfigure = do_configure, | ||
| driverSwitched = driver_switched, | ||
| }, | ||
| capability_handlers = { | ||
| [capabilities.valve.ID] = { | ||
| [capabilities.valve.commands.open.NAME] = valve_open_handler, | ||
| [capabilities.valve.commands.close.NAME] = valve_close_handler, | ||
| } | ||
| }, | ||
| zigbee_handlers = { | ||
| attr = { | ||
| [OnOff.ID] = { | ||
| [OnOff.attributes.OnOff.ID] = onoff_attr_handler | ||
| }, | ||
| [PowerConfiguration.ID] = { | ||
| [PowerConfiguration.attributes.BatteryPercentageRemaining.ID] = battery_percentage_handler | ||
| } | ||
| }, | ||
| cluster = { | ||
| [Basic.ID] = { | ||
| [Basic.server.commands.ResetToFactoryDefaults.ID] = identify_handler | ||
| } | ||
| } | ||
| }, | ||
| can_handle = require "sonoff.can_handle", | ||
| } | ||
|
|
||
| return sonoff_valve_handler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,5 +5,6 @@ local lazy_load_if_possible = require "lazy_load_subdriver" | |
| local sub_drivers = { | ||
| lazy_load_if_possible("sinope"), | ||
| lazy_load_if_possible("ezex"), | ||
| lazy_load_if_possible("sonoff"), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd remove that comma |
||
| } | ||
| return sub_drivers | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to change that.