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

fix(hue): Fix Mirek -> Kelvin conversion constant. #1934

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion drivers/SmartThings/philips-hue/src/consts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ Consts.DEFAULT_MAX_MIREK = 500
Consts.MIN_TEMP_KELVIN_COLOR_AMBIANCE = 2000
Consts.MIN_TEMP_KELVIN_WHITE_AMBIANCE = 2200
Consts.MAX_TEMP_KELVIN = 6500
Consts.KELVIN_STEP_SIZE = 11
Consts.KELVIN_STEP_SIZE = 100

return Consts
1 change: 1 addition & 0 deletions drivers/SmartThings/philips-hue/src/fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ local Fields = {
COLOR_SATURATION = "color_saturation",
COLOR_HUE = "color_hue",
SWITCH_STATE = "switch_state_cache",
COLOR_TEMP_SETPOINT = "ct_setpoint"
}

return Fields
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ local function _emit_light_events_inner(light_device, light_repr)

-- See note in `src/handlers/lifecycle_handlers/light.lua` about min/max relationship
-- if the below is not intuitive.
local min_kelvin = light_device:get_field(Fields.MIN_KELVIN)
local color_temp_range = light_device:get_latest_state("main", capabilities.colorTemperature.ID, capabilities.colorTemperature.colorTemperatureRange.NAME);
local min_kelvin = (color_temp_range and color_temp_range.minimum)
local api_min_kelvin = math.floor(utils.mirek_to_kelvin(mirek_schema.mirek_maximum) or Consts.MIN_TEMP_KELVIN_COLOR_AMBIANCE)
local max_kelvin = light_device:get_field(Fields.MAX_KELVIN)
local max_kelvin = (color_temp_range and color_temp_range.maximum)
local api_max_kelvin = math.floor(utils.mirek_to_kelvin(mirek_schema.mirek_minimum) or Consts.MAX_TEMP_KELVIN)

local update_range = false
Expand All @@ -86,7 +87,10 @@ local function _emit_light_events_inner(light_device, light_repr)
end

if update_range then
light_device.log.debug(st_utils.stringify_table({ minimum = min_kelvin, maximum = max_kelvin }, "updating color temp range"));
light_device:emit_event(capabilities.colorTemperature.colorTemperatureRange({ minimum = min_kelvin, maximum = max_kelvin }))
else
light_device.log.debug(st_utils.stringify_table(color_temp_range, "color temp range unchanged"));
end

-- local min = or Consts.MIN_TEMP_KELVIN_WHITE_AMBIANCE
Expand All @@ -101,8 +105,17 @@ local function _emit_light_events_inner(light_device, light_repr)
)
)
else
local last_kelvin_setpoint = light_device:get_field(Fields.COLOR_TEMP_SETPOINT)
if
last_kelvin_setpoint ~= nil and
last_kelvin_setpoint >= utils.mirek_to_kelvin(mirek + 1) and
last_kelvin_setpoint <= utils.mirek_to_kelvin(mirek - 1)
then
kelvin = last_kelvin_setpoint;
end
light_device:emit_event(capabilities.colorTemperature.colorTemperature(kelvin))
end
light_device:set_field(Fields.COLOR_TEMP_SETPOINT, nil);
end

if light_repr.color then
Expand Down
1 change: 1 addition & 0 deletions drivers/SmartThings/philips-hue/src/handlers/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ local function do_color_temp_action(driver, device, args)
end
end
end
device:set_field(Fields.COLOR_TEMP_SETPOINT, clamped_kelvin);
end

---@param driver HueDriver
Expand Down
16 changes: 16 additions & 0 deletions drivers/SmartThings/philips-hue/src/test/spec/unit/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
describe("utility functions", function()
local Consts
local Fields
local HueDeviceTypes
local st_utils
local utils

setup(function()
Consts = require "consts"
Fields = require "fields"
HueDeviceTypes = require "hue_device_types"
utils = require "utils"
st_utils = require "st.utils"
end)

describe("that perform mirek to kelvin conversions behave properly:", function()
it("Common minimum mirek of 153 results in 6500 Kelvin", function()
local expected_kelvin = 6500
local computed_kelvin = utils.mirek_to_kelvin(Consts.DEFAULT_MIN_MIREK)
assert.are.equal(expected_kelvin, computed_kelvin, string.format("Expected value of %s, got %s", expected_kelvin, computed_kelvin))
end)

it("Common maximum mirek of 500 results in 2000 Kelvin", function()
local expected_kelvin = 2000
local computed_kelvin = utils.mirek_to_kelvin(Consts.DEFAULT_MAX_MIREK)
assert.are.equal(expected_kelvin, computed_kelvin, string.format("Expected value of %s, got %s", expected_kelvin, computed_kelvin))
end)
end)

describe("that handle raw data will handle their input correctly:", function()
local test_mac_addr
local test_cisco_mac_addr
Expand Down
Loading