Skip to content

Commit d7846ad

Browse files
authored
Merge pull request #1979 from SmartThingsCommunity/fix/hue-incorrect-kelvin-rounding
fix(hue): Make rounding logic platform consistent
2 parents 8f7b753 + b263b9f commit d7846ad

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

drivers/SmartThings/philips-hue/src/handlers/attribute_emitters.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ local function _emit_light_events_inner(light_device, light_repr)
6969
-- if the below is not intuitive.
7070
local color_temp_range = light_device:get_latest_state("main", capabilities.colorTemperature.ID, capabilities.colorTemperature.colorTemperatureRange.NAME);
7171
local min_kelvin = (color_temp_range and color_temp_range.minimum)
72-
local api_min_kelvin = math.floor(utils.mirek_to_kelvin(mirek_schema.mirek_maximum) or Consts.MIN_TEMP_KELVIN_COLOR_AMBIANCE)
72+
local api_min_kelvin = math.floor(utils.mirek_to_kelvin(mirek_schema.mirek_maximum, Consts.KELVIN_STEP_SIZE) or Consts.MIN_TEMP_KELVIN_COLOR_AMBIANCE)
7373
local max_kelvin = (color_temp_range and color_temp_range.maximum)
74-
local api_max_kelvin = math.floor(utils.mirek_to_kelvin(mirek_schema.mirek_minimum) or Consts.MAX_TEMP_KELVIN)
74+
local api_max_kelvin = math.floor(utils.mirek_to_kelvin(mirek_schema.mirek_minimum, Consts.KELVIN_STEP_SIZE) or Consts.MAX_TEMP_KELVIN)
7575

7676
local update_range = false
7777
if min_kelvin ~= api_min_kelvin then

drivers/SmartThings/philips-hue/src/handlers/lifecycle_handlers/light.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ function LightLifecycleHandlers.added(driver, device, parent_device_id, resource
158158
if caps.colorTemperature then
159159
local min_ct_kelvin, max_ct_kelvin = nil, nil
160160
if type(light_info.color_temperature.mirek_schema.mirek_maximum) == "number" then
161-
min_ct_kelvin = math.floor(utils.mirek_to_kelvin(light_info.color_temperature.mirek_schema.mirek_maximum))
161+
min_ct_kelvin = math.floor(utils.mirek_to_kelvin(light_info.color_temperature.mirek_schema.mirek_maximum, Consts.KELVIN_STEP_SIZE))
162162
end
163163
if type(light_info.color_temperature.mirek_schema.mirek_minimum) == "number" then
164-
max_ct_kelvin = math.floor(utils.mirek_to_kelvin(light_info.color_temperature.mirek_schema.mirek_minimum))
164+
max_ct_kelvin = math.floor(utils.mirek_to_kelvin(light_info.color_temperature.mirek_schema.mirek_minimum, Consts.KELVIN_STEP_SIZE))
165165
end
166166
if not min_ct_kelvin then
167167
if caps.colorControl then

drivers/SmartThings/philips-hue/src/test/spec/unit/utils_spec.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ describe("utility functions", function()
1616
describe("that perform mirek to kelvin conversions behave properly:", function()
1717
it("Common minimum mirek of 153 results in 6500 Kelvin", function()
1818
local expected_kelvin = 6500
19-
local computed_kelvin = utils.mirek_to_kelvin(Consts.DEFAULT_MIN_MIREK)
19+
local computed_kelvin = utils.mirek_to_kelvin(Consts.DEFAULT_MIN_MIREK, Consts.KELVIN_STEP_SIZE)
2020
assert.are.equal(expected_kelvin, computed_kelvin, string.format("Expected value of %s, got %s", expected_kelvin, computed_kelvin))
2121
end)
2222

2323
it("Common maximum mirek of 500 results in 2000 Kelvin", function()
2424
local expected_kelvin = 2000
25-
local computed_kelvin = utils.mirek_to_kelvin(Consts.DEFAULT_MAX_MIREK)
25+
local computed_kelvin = utils.mirek_to_kelvin(Consts.DEFAULT_MAX_MIREK, Consts.KELVIN_STEP_SIZE)
2626
assert.are.equal(expected_kelvin, computed_kelvin, string.format("Expected value of %s, got %s", expected_kelvin, computed_kelvin))
2727
end)
2828
end)

drivers/SmartThings/philips-hue/src/utils/init.lua

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local log = require "log"
2+
local st_utils = require "st.utils"
23

3-
local Consts = require "consts"
44
local Fields = require "fields"
55
local HueDeviceTypes = require "hue_device_types"
66

@@ -72,9 +72,14 @@ end
7272

7373
function utils.kelvin_to_mirek(kelvin) return 1000000 / kelvin end
7474

75-
function utils.mirek_to_kelvin(mirek)
75+
function utils.mirek_to_kelvin(mirek, with_step_size)
7676
local raw_kelvin = 1000000 / mirek
77-
return Consts.KELVIN_STEP_SIZE * math.floor(raw_kelvin / Consts.KELVIN_STEP_SIZE)
77+
78+
if type(with_step_size) == "number" then
79+
return with_step_size * math.floor(raw_kelvin / with_step_size)
80+
end
81+
82+
return st_utils.round(raw_kelvin)
7883
end
7984

8085
function utils.str_starts_with(str, start)

0 commit comments

Comments
 (0)