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
Changes from all 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
45 changes: 40 additions & 5 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 All @@ -87,6 +103,8 @@ end
local function device_init(driver, device)
device:subscribe()
device:set_component_to_endpoint_fn(component_to_endpoint)
-- pump should not be allowed to receive a level value of 0.
capabilities.switchLevel.levelRange({ value = {minimum = 1, maximum = 100} })
end

local function info_changed(driver, device, event, args)
Expand Down Expand Up @@ -175,8 +193,9 @@ 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)
if ib.data.value then
local max_level = get_field_for_endpoint(device, LEVEL_MAX, ib.endpoint_id) or DEFAULT_MAX_LEVEL
local level = math.floor((ib.data.value / max_level * MAX_CAP_SWITCH_LEVEL) + 0.5)
device:emit_event_for_endpoint(ib.endpoint_id, capabilities.switchLevel.level(level))
end
end
Expand Down Expand Up @@ -221,6 +240,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 +267,9 @@ 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))
level = math.max(level, MIN_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 +312,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