Skip to content
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

Matter Pump: improve support for OperatingMode handling #1439

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions drivers/SmartThings/matter-pump/profiles/pump-level.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ components:
version: 1
- id: switchLevel
version: 1
config:
values:
- key: "level.value"
range: [1, 100]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Presentation team, I heard and checked when embedded config is applied, metadata cannot be applied.

- id: pumpOperationMode
version: 1
- id: pumpControlMode
Expand Down
45 changes: 39 additions & 6 deletions drivers/SmartThings/matter-pump/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ local clusters = require "st.matter.clusters"
local embedded_cluster_utils = require "embedded-cluster-utils"
local MatterDriver = require "st.matter.driver"


local IS_LOCAL_OVERRIDE = "__is_local_override"
local LEVEL_MIN = "__level_min"
local LEVEL_MAX = "__level_max"
local DEFAULT_MAX_LEVEL = 254
local MIN_CAP_SWITCH_LEVEL = 1 -- we don't want 0% to be a reasonable request.
local MAX_CAP_SWITCH_LEVEL = 100

local pumpOperationMode = capabilities.pumpOperationMode
local pumpControlMode = capabilities.pumpControlMode
Expand Down Expand Up @@ -53,7 +59,9 @@ local subscribed_attributes = {
clusters.OnOff.attributes.OnOff,
},
[capabilities.switchLevel.ID] = {
clusters.LevelControl.attributes.CurrentLevel
clusters.LevelControl.attributes.CurrentLevel,
clusters.LevelControl.attributes.MaxLevel,
clusters.LevelControl.attributes.MinLevel,
},
[capabilities.pumpOperationMode.ID]={
clusters.PumpConfigurationAndControl.attributes.OperationMode,
Expand All @@ -65,6 +73,14 @@ local subscribed_attributes = {
},
}

local function get_field_for_endpoint(device, field, endpoint)
return device:get_field(string.format("%s_%d", field, endpoint))
end

local function set_field_for_endpoint(device, field, endpoint, value, additional_params)
device:set_field(string.format("%s_%d", field, endpoint), value, additional_params)
end

local function find_default_endpoint(device, cluster)
local res = device.MATTER_DEFAULT_ENDPOINT
local eps = embedded_cluster_utils.get_endpoints(device, cluster)
Expand Down Expand Up @@ -175,9 +191,11 @@ local function on_off_attr_handler(driver, device, ib, response)
end

local function level_attr_handler(driver, device, ib, response)
if ib.data.value ~= nil then
local level = math.floor((ib.data.value / 254.0 * 100) + 0.5)
device:emit_event_for_endpoint(ib.endpoint_id, capabilities.switchLevel.level(level))
if ib.data.value then
local max_level = get_field_for_endpoint(device, LEVEL_MAX, ib.endpoint_id) or DEFAULT_MAX_LEVEL
Copy link
Contributor

@ctowns ctowns Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unindent this section by one tab (2 spaces)

local level = math.floor((ib.data.value / max_level * MAX_CAP_SWITCH_LEVEL) + 0.5)
level = math.max(level, MIN_CAP_SWITCH_LEVEL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the value we actually receive from the device, so I don't think we should change this value. If the device gives us 0, then that should be considered valid if that is how the device works

device:emit_event_for_endpoint(ib.endpoint_id, capabilities.switchLevel.level(level))
end
end

Expand Down Expand Up @@ -221,6 +239,18 @@ local function pump_status_handler(driver, device, ib, response)
end
end

local level_bounds_handler = function(a_min_or_max_val)
return function(driver, device, ib, response)
if ib.data.value then
set_field_for_endpoint(device, a_min_or_max_val, ib.endpoint_id, ib.data.value, {persist = true})
local level_min = get_field_for_endpoint(device, LEVEL_MIN, ib.endpoint_id)
if level_min then
device:emit_event_for_endpoint(ib.endpoint_id, capabilities.switchLevel.levelRange({ value = {minimum = level_min, maximum = MAX_CAP_SWITCH_LEVEL} }))
end
end
end
end

-- Capability Handlers --
local function handle_switch_on(driver, device, cmd)
local endpoint_id = device:component_to_endpoint(cmd.component)
Expand All @@ -236,7 +266,8 @@ end

local function handle_set_level(driver, device, cmd)
local endpoint_id = device:component_to_endpoint(cmd.component)
local level = math.floor(cmd.args.level / 100.0 * 254)
local max_level = get_field_for_endpoint(device, LEVEL_MAX, endpoint_id) or DEFAULT_MAX_LEVEL
local level = math.floor((max_level * cmd.args.level) / (MAX_CAP_SWITCH_LEVEL))
local req = clusters.LevelControl.server.commands.MoveToLevelWithOnOff(device, endpoint_id, level, cmd.args.rate or 0, 0 ,0)
device:send(req)
end
Expand Down Expand Up @@ -279,7 +310,9 @@ local matter_driver_template = {
[clusters.OnOff.attributes.OnOff.ID] = on_off_attr_handler,
},
[clusters.LevelControl.ID] = {
[clusters.LevelControl.attributes.CurrentLevel.ID] = level_attr_handler
[clusters.LevelControl.attributes.CurrentLevel.ID] = level_attr_handler,
[clusters.LevelControl.attributes.MaxLevel.ID] = level_bounds_handler(LEVEL_MAX),
[clusters.LevelControl.attributes.MinLevel.ID] = level_bounds_handler(LEVEL_MIN),
},
[clusters.PumpConfigurationAndControl.ID] = {
[clusters.PumpConfigurationAndControl.attributes.EffectiveOperationMode.ID] = effective_operation_mode_handler,
Expand Down
Loading