Skip to content

Commit

Permalink
Merge branch 'main' into th1
Browse files Browse the repository at this point in the history
  • Loading branch information
weihuan1111 authored Mar 6, 2025
2 parents 6fd4985 + d7846ad commit 94a5107
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 35 deletions.

This file was deleted.

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,10 +67,11 @@ 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 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 api_max_kelvin = math.floor(utils.mirek_to_kelvin(mirek_schema.mirek_minimum) or Consts.MAX_TEMP_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, Consts.KELVIN_STEP_SIZE) or Consts.MIN_TEMP_KELVIN_COLOR_AMBIANCE)
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, Consts.KELVIN_STEP_SIZE) or Consts.MAX_TEMP_KELVIN)

local update_range = false
if min_kelvin ~= api_min_kelvin then
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
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ function LightLifecycleHandlers.added(driver, device, parent_device_id, resource
if caps.colorTemperature then
local min_ct_kelvin, max_ct_kelvin = nil, nil
if type(light_info.color_temperature.mirek_schema.mirek_maximum) == "number" then
min_ct_kelvin = math.floor(utils.mirek_to_kelvin(light_info.color_temperature.mirek_schema.mirek_maximum))
min_ct_kelvin = math.floor(utils.mirek_to_kelvin(light_info.color_temperature.mirek_schema.mirek_maximum, Consts.KELVIN_STEP_SIZE))
end
if type(light_info.color_temperature.mirek_schema.mirek_minimum) == "number" then
max_ct_kelvin = math.floor(utils.mirek_to_kelvin(light_info.color_temperature.mirek_schema.mirek_minimum))
max_ct_kelvin = math.floor(utils.mirek_to_kelvin(light_info.color_temperature.mirek_schema.mirek_minimum, Consts.KELVIN_STEP_SIZE))
end
if not min_ct_kelvin then
if caps.colorControl then
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, Consts.KELVIN_STEP_SIZE)
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, Consts.KELVIN_STEP_SIZE)
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
11 changes: 8 additions & 3 deletions drivers/SmartThings/philips-hue/src/utils/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local log = require "log"
local st_utils = require "st.utils"

local Consts = require "consts"
local Fields = require "fields"
local HueDeviceTypes = require "hue_device_types"

Expand Down Expand Up @@ -72,9 +72,14 @@ end

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

function utils.mirek_to_kelvin(mirek)
function utils.mirek_to_kelvin(mirek, with_step_size)
local raw_kelvin = 1000000 / mirek
return Consts.KELVIN_STEP_SIZE * math.floor(raw_kelvin / Consts.KELVIN_STEP_SIZE)

if type(with_step_size) == "number" then
return with_step_size * math.floor(raw_kelvin / with_step_size)
end

return st_utils.round(raw_kelvin)
end

function utils.str_starts_with(str, start)
Expand Down

0 comments on commit 94a5107

Please sign in to comment.