Skip to content

Commit

Permalink
fix: Address bug in color temp range emit logic
Browse files Browse the repository at this point in the history
The existing logic utilized the device persistent fields to determine
if a range update was needed, as to avoid un-necessary attribute emits.

This would lead to existing devices not ever emitting the min/max color
temp range values if the saved min/max matched what was reported by the
API.

This is adjusted to use the cached latest state for the device instead,
so that we can detect the `nil` if the device has never emitted a value
for its range.
  • Loading branch information
dljsjr committed Mar 4, 2025
1 parent 59b1da3 commit 153c9d6
Showing 1 changed file with 6 additions and 2 deletions.
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 Down

0 comments on commit 153c9d6

Please sign in to comment.