|
| 1 | +local log = require "log" |
| 2 | +local st_utils = require "st.utils" |
| 3 | +local Consts = require "consts" |
| 4 | +local Fields = require "fields" |
| 5 | +local HueColorUtils = require "utils.cie_utils" |
| 6 | +local grouped_utils = require "utils.grouped_utils" |
| 7 | +local utils = require "utils" |
| 8 | + |
| 9 | + |
| 10 | +---@class GroupedLightCommandHandlers |
| 11 | +local GroupedLightCommandHandlers = {} |
| 12 | + |
| 13 | +---@param driver HueDriver |
| 14 | +---@param bridge_device HueBridgeDevice |
| 15 | +---@param group table |
| 16 | +---@param args table |
| 17 | +local function do_switch_action(driver, bridge_device, group, args) |
| 18 | + local on = args.command == "on" |
| 19 | + |
| 20 | + local grouped_light_id = group.grouped_light_rid |
| 21 | + if not grouped_light_id then |
| 22 | + log.error(string.format("Couldn't find grouped light id for group %s", |
| 23 | + group.id or "unknown group id")) |
| 24 | + return |
| 25 | + end |
| 26 | + |
| 27 | + local hue_api = bridge_device:get_field(Fields.BRIDGE_API) --[[@as PhilipsHueApi]] |
| 28 | + if not hue_api then |
| 29 | + log.error(string.format("Couldn't find api instance for bridge %s", |
| 30 | + bridge_device.label or bridge_device.id or "unknown bridge")) |
| 31 | + return |
| 32 | + end |
| 33 | + |
| 34 | + local resp, err = hue_api:set_grouped_light_on_state(grouped_light_id, on) |
| 35 | + if not resp or (resp.errors and #resp.errors == 0) then |
| 36 | + if err ~= nil then |
| 37 | + log.error_with({ hub_logs = true }, "Error performing on/off action: " .. err) |
| 38 | + elseif resp and #resp.errors > 0 then |
| 39 | + for _, error in ipairs(resp.errors) do |
| 40 | + log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +---@param driver HueDriver |
| 47 | +---@param bridge_device HueBridgeDevice |
| 48 | +---@param group table |
| 49 | +---@param args table |
| 50 | +local function do_switch_level_action(driver, bridge_device, group, args) |
| 51 | + local level = st_utils.clamp_value(args.args.level, 1, 100) |
| 52 | + |
| 53 | + local grouped_light_id = group.grouped_light_rid |
| 54 | + if not grouped_light_id then |
| 55 | + log.error(string.format("Couldn't find grouped light id for group %s", |
| 56 | + group.id or "unknown group id")) |
| 57 | + return |
| 58 | + end |
| 59 | + |
| 60 | + local hue_api = bridge_device:get_field(Fields.BRIDGE_API) --[[@as PhilipsHueApi]] |
| 61 | + if not hue_api then |
| 62 | + log.error(string.format("Couldn't find api instance for bridge %s", |
| 63 | + bridge_device.label or bridge_device.id or "unknown bridge")) |
| 64 | + return |
| 65 | + end |
| 66 | + |
| 67 | + -- An individual command checks the state of the device before doing this. |
| 68 | + -- It is probably not worth iterating through all the devices to check their state. |
| 69 | + local resp, err = hue_api:set_grouped_light_on_state(grouped_light_id, true) |
| 70 | + if not resp or (resp.errors and #resp.errors == 0) then |
| 71 | + if err ~= nil then |
| 72 | + log.error_with({ hub_logs = true }, "Error performing on/off action: " .. err) |
| 73 | + elseif resp and #resp.errors > 0 then |
| 74 | + for _, error in ipairs(resp.errors) do |
| 75 | + log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + local resp, err = hue_api:set_grouped_light_level(grouped_light_id, level) |
| 81 | + if not resp or (resp.errors and #resp.errors == 0) then |
| 82 | + if err ~= nil then |
| 83 | + log.error_with({ hub_logs = true }, "Error performing switch level action: " .. err) |
| 84 | + elseif resp and #resp.errors > 0 then |
| 85 | + for _, error in ipairs(resp.errors) do |
| 86 | + log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | + |
| 93 | +---@param driver HueDriver |
| 94 | +---@param bridge_device HueBridgeDevice |
| 95 | +---@param group table |
| 96 | +---@param args table |
| 97 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 98 | +local function do_color_action(driver, bridge_device, group, args, aux) |
| 99 | + local hue, sat = (args.args.color.hue / 100), (args.args.color.saturation / 100) |
| 100 | + if hue == 1 then -- 0 and 360 degrees are equivalent in HSV, but not in our conversion function |
| 101 | + hue = 0 |
| 102 | + grouped_utils.set_field_on_group_devices(group, Fields.WRAPPED_HUE, true) |
| 103 | + end |
| 104 | + |
| 105 | + local grouped_light_id = group.grouped_light_rid |
| 106 | + if not grouped_light_id then |
| 107 | + log.error(string.format("Couldn't find grouped light id for group %s", |
| 108 | + group.id or "unknown group id")) |
| 109 | + return |
| 110 | + end |
| 111 | + |
| 112 | + local hue_api = bridge_device:get_field(Fields.BRIDGE_API) --[[@as PhilipsHueApi]] |
| 113 | + if not hue_api then |
| 114 | + log.error(string.format("Couldn't find api instance for bridge %s", |
| 115 | + bridge_device.label or bridge_device.id or "unknown bridge")) |
| 116 | + return |
| 117 | + end |
| 118 | + |
| 119 | + local red, green, blue = st_utils.hsv_to_rgb(hue, sat) |
| 120 | + local xy = HueColorUtils.safe_rgb_to_xy(red, green, blue, aux[Fields.GAMUT]) |
| 121 | + |
| 122 | + local resp, err = hue_api:set_grouped_light_color_xy(grouped_light_id, xy) |
| 123 | + if not resp or (resp.errors and #resp.errors == 0) then |
| 124 | + if err ~= nil then |
| 125 | + log.error_with({ hub_logs = true }, "Error performing color action: " .. err) |
| 126 | + elseif resp and #resp.errors > 0 then |
| 127 | + for _, error in ipairs(resp.errors) do |
| 128 | + log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +---@param driver HueDriver |
| 135 | +---@param bridge_device HueBridgeDevice |
| 136 | +---@param group table |
| 137 | +---@param args table |
| 138 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 139 | +local function do_setHue_action(driver, bridge_device, group, args, aux) |
| 140 | + local currentSaturation = aux[Fields.COLOR_SATURATION] or 0 |
| 141 | + args.args.color = { |
| 142 | + hue = args.args.hue, |
| 143 | + saturation = currentSaturation |
| 144 | + } |
| 145 | + do_color_action(driver, bridge_device, group, args, aux) |
| 146 | +end |
| 147 | + |
| 148 | +---@param driver HueDriver |
| 149 | +---@param bridge_device HueBridgeDevice |
| 150 | +---@param group table |
| 151 | +---@param args table |
| 152 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 153 | +local function do_setSaturation_action(driver, bridge_device, group, args, aux) |
| 154 | + local currentHue = aux[Fields.COLOR_HUE] or 0 |
| 155 | + args.args.color = { |
| 156 | + hue = currentHue, |
| 157 | + saturation = args.args.saturation |
| 158 | + } |
| 159 | + do_color_action(driver, bridge_device, group, args, aux) |
| 160 | +end |
| 161 | + |
| 162 | +---@param driver HueDriver |
| 163 | +---@param bridge_device HueBridgeDevice |
| 164 | +---@param group table |
| 165 | +---@param args table |
| 166 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 167 | +local function do_color_temp_action(driver, bridge_device, group, args, aux) |
| 168 | + local kelvin = args.args.temperature |
| 169 | + |
| 170 | + local grouped_light_id = group.grouped_light_rid |
| 171 | + if not grouped_light_id then |
| 172 | + log.error(string.format("Couldn't find grouped light id for group %s", |
| 173 | + group.id or "unknown group id")) |
| 174 | + return |
| 175 | + end |
| 176 | + |
| 177 | + local hue_api = bridge_device:get_field(Fields.BRIDGE_API) --[[@as PhilipsHueApi]] |
| 178 | + if not hue_api then |
| 179 | + log.error(string.format("Couldn't find api instance for bridge %s", |
| 180 | + bridge_device.label or bridge_device.id or "unknown bridge")) |
| 181 | + return |
| 182 | + end |
| 183 | + |
| 184 | + local min = aux[Fields.MIN_KELVIN] or Consts.MIN_TEMP_KELVIN_WHITE_AMBIANCE |
| 185 | + local clamped_kelvin = st_utils.clamp_value(kelvin, min, Consts.MAX_TEMP_KELVIN) |
| 186 | + local mirek = math.floor(utils.kelvin_to_mirek(clamped_kelvin)) |
| 187 | + |
| 188 | + local resp, err = hue_api:set_grouped_light_color_temp(grouped_light_id, mirek) |
| 189 | + |
| 190 | + if not resp or (resp.errors and #resp.errors == 0) then |
| 191 | + if err ~= nil then |
| 192 | + log.error_with({ hub_logs = true }, "Error performing color temp action: " .. err) |
| 193 | + elseif resp and #resp.errors > 0 then |
| 194 | + for _, error in ipairs(resp.errors) do |
| 195 | + log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) |
| 196 | + end |
| 197 | + end |
| 198 | + end |
| 199 | + grouped_utils.set_field_on_group_devices(group, Fields.COLOR_TEMP_SETPOINT, clamped_kelvin); |
| 200 | +end |
| 201 | + |
| 202 | +---@param driver HueDriver |
| 203 | +---@param bridge_device HueBridgeDevice |
| 204 | +---@param group table |
| 205 | +---@param args table |
| 206 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 207 | +function GroupedLightCommandHandlers.switch_on_handler(driver, bridge_device, group, args, aux) |
| 208 | + do_switch_action(driver, bridge_device, group, args) |
| 209 | +end |
| 210 | + |
| 211 | +---@param driver HueDriver |
| 212 | +---@param bridge_device HueBridgeDevice |
| 213 | +---@param group table |
| 214 | +---@param args table |
| 215 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 216 | +function GroupedLightCommandHandlers.switch_off_handler(driver, bridge_device, group, args, aux) |
| 217 | + do_switch_action(driver, bridge_device, group, args) |
| 218 | +end |
| 219 | + |
| 220 | +---@param driver HueDriver |
| 221 | +---@param bridge_device HueBridgeDevice |
| 222 | +---@param group table |
| 223 | +---@param args table |
| 224 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 225 | +function GroupedLightCommandHandlers.switch_level_handler(driver, bridge_device, group, args, aux) |
| 226 | + do_switch_level_action(driver, bridge_device, group, args) |
| 227 | +end |
| 228 | + |
| 229 | +---@param driver HueDriver |
| 230 | +---@param bridge_device HueBridgeDevice |
| 231 | +---@param group table |
| 232 | +---@param args table |
| 233 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 234 | +function GroupedLightCommandHandlers.set_color_handler(driver, bridge_device, group, args, aux) |
| 235 | + do_color_action(driver, bridge_device, group, args, aux) |
| 236 | +end |
| 237 | + |
| 238 | +---@param driver HueDriver |
| 239 | +---@param bridge_device HueBridgeDevice |
| 240 | +---@param group table |
| 241 | +---@param args table |
| 242 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 243 | +function GroupedLightCommandHandlers.set_hue_handler(driver, bridge_device, group, args, aux) |
| 244 | + do_setHue_action(driver, bridge_device, group, args, aux) |
| 245 | +end |
| 246 | + |
| 247 | +---@param driver HueDriver |
| 248 | +---@param bridge_device HueBridgeDevice |
| 249 | +---@param group table |
| 250 | +---@param args table |
| 251 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 252 | +function GroupedLightCommandHandlers.set_saturation_handler(driver, bridge_device, group, args, aux) |
| 253 | + do_setSaturation_action(driver, bridge_device, group, args, aux) |
| 254 | +end |
| 255 | + |
| 256 | +---@param driver HueDriver |
| 257 | +---@param bridge_device HueBridgeDevice |
| 258 | +---@param group table |
| 259 | +---@param args table |
| 260 | +---@param aux table auxilary data needed for the command that the devices all had in common |
| 261 | +function GroupedLightCommandHandlers.set_color_temp_handler(driver, bridge_device, group, args, aux) |
| 262 | + do_color_temp_action(driver, bridge_device, group, args, aux) |
| 263 | +end |
| 264 | + |
| 265 | + |
| 266 | +return GroupedLightCommandHandlers |
0 commit comments