Skip to content

Commit

Permalink
fixup: use clamp function to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolas-deboom committed Oct 18, 2024
1 parent 96fe98d commit 182d860
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,7 @@ local function setpoint_limit_handler(limit_field)
local field = string.format("%s-%d", limit_field, ib.endpoint_id)
local val = ib.data.value / 100.0

-- We clamp the max and min values as per the assumed oven temperature ranges.
if val < DISHWASHER_MIN_TEMP_IN_C or val > DISHWASHER_MAX_TEMP_IN_C then
if limit_field == setpoint_limit_device_field.MIN_TEMP then
val = DISHWASHER_MIN_TEMP_IN_C
else
val = DISHWASHER_MAX_TEMP_IN_C
end
end
val = utils.clamp_value(val, DISHWASHER_MIN_TEMP_IN_C, DISHWASHER_MAX_TEMP_IN_C)

log.info("Setting " .. field .. " to " .. string.format("%s", val))
device:set_field(field, val, { persist = true })
Expand Down
10 changes: 2 additions & 8 deletions drivers/SmartThings/matter-appliance/src/matter-laundry/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,8 @@ local function setpoint_limit_handler(limit_field)
device.log.warn(string.format("Not a supported device type"))
return
end
-- We clamp the max and min values as per the assumed oven temperature ranges.
if val < min_temp_in_c or val > max_temp_in_c then
if limit_field == setpoint_limit_device_field.MIN_TEMP then
val = min_temp_in_c
else
val = max_temp_in_c
end
end

val = utils.clamp_value(val, min_temp_in_c, max_temp_in_c)

device.log.info("Setting " .. field .. " to " .. string.format("%s", val))
device:set_field(field, val, { persist = true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,7 @@ local function setpoint_limit_handler(limit_field)
local field = string.format("%s-%d", limit_field, ib.endpoint_id)
local val = ib.data.value / 100.0

-- We clamp the max and min values as per the assumed oven temperature ranges.
if val < OVEN_MIN_TEMP_IN_C or val > OVEN_MAX_TEMP_IN_C then
if limit_field == setpoint_limit_device_field.MIN_TEMP then
val = OVEN_MIN_TEMP_IN_C
else
val = OVEN_MAX_TEMP_IN_C
end
end
val = utils.clamp_value(val, OVEN_MIN_TEMP_IN_C, OVEN_MAX_TEMP_IN_C)

device.log.info("Setting " .. field .. " to " .. string.format("%s", val))
device:set_field(field, val, { persist = true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,8 @@ local function setpoint_limit_handler(limit_field)
device.log.warn(string.format("Not a supported device type"))
return
end
-- We clamp the max and min values as per the assumed refrigerator temperature ranges.
if val < min_temp_in_c or val > max_temp_in_c then
if limit_field == setpoint_limit_device_field.MIN_TEMP then
val = min_temp_in_c
else
val = max_temp_in_c
end
end

val = utils.clamp_value(val, min_temp_in_c, max_temp_in_c)

device.log.info("Setting " .. field .. " to " .. string.format("%s", val))
device:set_field(field, val, { persist = true })
Expand Down
24 changes: 3 additions & 21 deletions drivers/SmartThings/matter-thermostat/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -733,13 +733,7 @@ local temp_attr_handler_factory = function(minOrMax)
end
local temp = ib.data.value / 100.0
local unit = "C"
if temp < THERMOSTAT_MIN_TEMP_IN_C or temp > THERMOSTAT_MAX_TEMP_IN_C then
if minOrMax == setpoint_limit_device_field.MIN_TEMP then
temp = THERMOSTAT_MIN_TEMP_IN_C
else
temp = THERMOSTAT_MAX_TEMP_IN_C
end
end
temp = utils.clamp_value(temp, THERMOSTAT_MIN_TEMP_IN_C, THERMOSTAT_MAX_TEMP_IN_C)
set_field_for_endpoint(device, minOrMax, ib.endpoint_id, temp)
local min = get_field_for_endpoint(device, setpoint_limit_device_field.MIN_TEMP, ib.endpoint_id)
local max = get_field_for_endpoint(device, setpoint_limit_device_field.MAX_TEMP, ib.endpoint_id)
Expand Down Expand Up @@ -1169,13 +1163,7 @@ local heating_setpoint_limit_handler_factory = function(minOrMax)
return
end
local val = ib.data.value / 100.0
if val < THERMOSTAT_MIN_TEMP_IN_C or val > THERMOSTAT_MAX_TEMP_IN_C then
if minOrMax == setpoint_limit_device_field.MIN_HEAT then
val = THERMOSTAT_MIN_TEMP_IN_C
else
val = THERMOSTAT_MAX_TEMP_IN_C
end
end
val = utils.clamp_value(val, THERMOSTAT_MIN_TEMP_IN_C, THERMOSTAT_MAX_TEMP_IN_C)
device:set_field(minOrMax, val)
local min = device:get_field(setpoint_limit_device_field.MIN_HEAT)
local max = device:get_field(setpoint_limit_device_field.MAX_HEAT)
Expand All @@ -1199,13 +1187,7 @@ local cooling_setpoint_limit_handler_factory = function(minOrMax)
return
end
local val = ib.data.value / 100.0
if val < THERMOSTAT_MIN_TEMP_IN_C or val > THERMOSTAT_MAX_TEMP_IN_C then
if minOrMax == setpoint_limit_device_field.MIN_COOL then
val = THERMOSTAT_MIN_TEMP_IN_C
else
val = THERMOSTAT_MAX_TEMP_IN_C
end
end
val = utils.clamp_value(val, THERMOSTAT_MIN_TEMP_IN_C, THERMOSTAT_MAX_TEMP_IN_C)
device:set_field(minOrMax, val)
local min = device:get_field(setpoint_limit_device_field.MIN_COOL)
local max = device:get_field(setpoint_limit_device_field.MAX_COOL)
Expand Down

0 comments on commit 182d860

Please sign in to comment.