-
Notifications
You must be signed in to change notification settings - Fork 484
Philips-hue batched command handling support #2075
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aee9851
philips-hue: Add support for tracking rooms/zones hue groups
NoahCornell 262f5a3
philips-hue: Add grouped light command handling
NoahCornell 9a937cb
philips-hue: Add util for tracking number of items in KV table
NoahCornell d0cfef9
philips-hue: Add quiet param to `get_hue_bridge_for_device`
NoahCornell 27aa1ef
philips-hue: Add batched command handling support
NoahCornell 90da94a
philips-hue: Scan for groups on light added lifecycle events
NoahCornell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
266 changes: 266 additions & 0 deletions
266
drivers/SmartThings/philips-hue/src/handlers/grouped_light_commands.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
local log = require "log" | ||
local st_utils = require "st.utils" | ||
local Consts = require "consts" | ||
local Fields = require "fields" | ||
local HueColorUtils = require "utils.cie_utils" | ||
local grouped_utils = require "utils.grouped_utils" | ||
local utils = require "utils" | ||
|
||
|
||
---@class GroupedLightCommandHandlers | ||
local GroupedLightCommandHandlers = {} | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
local function do_switch_action(driver, bridge_device, group, args) | ||
local on = args.command == "on" | ||
|
||
local grouped_light_id = group.grouped_light_rid | ||
if not grouped_light_id then | ||
log.error(string.format("Couldn't find grouped light id for group %s", | ||
group.id or "unknown group id")) | ||
return | ||
end | ||
|
||
local hue_api = bridge_device:get_field(Fields.BRIDGE_API) --[[@as PhilipsHueApi]] | ||
if not hue_api then | ||
log.error(string.format("Couldn't find api instance for bridge %s", | ||
bridge_device.label or bridge_device.id or "unknown bridge")) | ||
return | ||
end | ||
|
||
local resp, err = hue_api:set_grouped_light_on_state(grouped_light_id, on) | ||
if not resp or (resp.errors and #resp.errors == 0) then | ||
if err ~= nil then | ||
log.error_with({ hub_logs = true }, "Error performing on/off action: " .. err) | ||
elseif resp and #resp.errors > 0 then | ||
for _, error in ipairs(resp.errors) do | ||
log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) | ||
end | ||
end | ||
end | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
local function do_switch_level_action(driver, bridge_device, group, args) | ||
local level = st_utils.clamp_value(args.args.level, 1, 100) | ||
|
||
local grouped_light_id = group.grouped_light_rid | ||
if not grouped_light_id then | ||
log.error(string.format("Couldn't find grouped light id for group %s", | ||
group.id or "unknown group id")) | ||
return | ||
end | ||
|
||
local hue_api = bridge_device:get_field(Fields.BRIDGE_API) --[[@as PhilipsHueApi]] | ||
if not hue_api then | ||
log.error(string.format("Couldn't find api instance for bridge %s", | ||
bridge_device.label or bridge_device.id or "unknown bridge")) | ||
return | ||
end | ||
|
||
-- An individual command checks the state of the device before doing this. | ||
-- It is probably not worth iterating through all the devices to check their state. | ||
local resp, err = hue_api:set_grouped_light_on_state(grouped_light_id, true) | ||
if not resp or (resp.errors and #resp.errors == 0) then | ||
if err ~= nil then | ||
log.error_with({ hub_logs = true }, "Error performing on/off action: " .. err) | ||
elseif resp and #resp.errors > 0 then | ||
for _, error in ipairs(resp.errors) do | ||
log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) | ||
end | ||
end | ||
end | ||
|
||
local resp, err = hue_api:set_grouped_light_level(grouped_light_id, level) | ||
if not resp or (resp.errors and #resp.errors == 0) then | ||
if err ~= nil then | ||
log.error_with({ hub_logs = true }, "Error performing switch level action: " .. err) | ||
elseif resp and #resp.errors > 0 then | ||
for _, error in ipairs(resp.errors) do | ||
log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
local function do_color_action(driver, bridge_device, group, args, aux) | ||
local hue, sat = (args.args.color.hue / 100), (args.args.color.saturation / 100) | ||
if hue == 1 then -- 0 and 360 degrees are equivalent in HSV, but not in our conversion function | ||
hue = 0 | ||
grouped_utils.set_field_on_group_devices(group, Fields.WRAPPED_HUE, true) | ||
end | ||
|
||
local grouped_light_id = group.grouped_light_rid | ||
if not grouped_light_id then | ||
log.error(string.format("Couldn't find grouped light id for group %s", | ||
group.id or "unknown group id")) | ||
return | ||
end | ||
|
||
local hue_api = bridge_device:get_field(Fields.BRIDGE_API) --[[@as PhilipsHueApi]] | ||
if not hue_api then | ||
log.error(string.format("Couldn't find api instance for bridge %s", | ||
bridge_device.label or bridge_device.id or "unknown bridge")) | ||
return | ||
end | ||
|
||
local red, green, blue = st_utils.hsv_to_rgb(hue, sat) | ||
local xy = HueColorUtils.safe_rgb_to_xy(red, green, blue, aux[Fields.GAMUT]) | ||
|
||
local resp, err = hue_api:set_grouped_light_color_xy(grouped_light_id, xy) | ||
if not resp or (resp.errors and #resp.errors == 0) then | ||
if err ~= nil then | ||
log.error_with({ hub_logs = true }, "Error performing color action: " .. err) | ||
elseif resp and #resp.errors > 0 then | ||
for _, error in ipairs(resp.errors) do | ||
log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) | ||
end | ||
end | ||
end | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
local function do_setHue_action(driver, bridge_device, group, args, aux) | ||
local currentSaturation = aux[Fields.COLOR_SATURATION] or 0 | ||
args.args.color = { | ||
hue = args.args.hue, | ||
saturation = currentSaturation | ||
} | ||
do_color_action(driver, bridge_device, group, args, aux) | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
local function do_setSaturation_action(driver, bridge_device, group, args, aux) | ||
local currentHue = aux[Fields.COLOR_HUE] or 0 | ||
args.args.color = { | ||
hue = currentHue, | ||
saturation = args.args.saturation | ||
} | ||
do_color_action(driver, bridge_device, group, args, aux) | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
local function do_color_temp_action(driver, bridge_device, group, args, aux) | ||
local kelvin = args.args.temperature | ||
|
||
local grouped_light_id = group.grouped_light_rid | ||
if not grouped_light_id then | ||
log.error(string.format("Couldn't find grouped light id for group %s", | ||
group.id or "unknown group id")) | ||
return | ||
end | ||
|
||
local hue_api = bridge_device:get_field(Fields.BRIDGE_API) --[[@as PhilipsHueApi]] | ||
if not hue_api then | ||
log.error(string.format("Couldn't find api instance for bridge %s", | ||
bridge_device.label or bridge_device.id or "unknown bridge")) | ||
return | ||
end | ||
|
||
local min = aux[Fields.MIN_KELVIN] or Consts.MIN_TEMP_KELVIN_WHITE_AMBIANCE | ||
local clamped_kelvin = st_utils.clamp_value(kelvin, min, Consts.MAX_TEMP_KELVIN) | ||
local mirek = math.floor(utils.kelvin_to_mirek(clamped_kelvin)) | ||
|
||
local resp, err = hue_api:set_grouped_light_color_temp(grouped_light_id, mirek) | ||
|
||
if not resp or (resp.errors and #resp.errors == 0) then | ||
if err ~= nil then | ||
log.error_with({ hub_logs = true }, "Error performing color temp action: " .. err) | ||
elseif resp and #resp.errors > 0 then | ||
for _, error in ipairs(resp.errors) do | ||
log.error_with({ hub_logs = true }, "Error returned in Hue response: " .. error.description) | ||
end | ||
end | ||
end | ||
grouped_utils.set_field_on_group_devices(group, Fields.COLOR_TEMP_SETPOINT, clamped_kelvin); | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
function GroupedLightCommandHandlers.switch_on_handler(driver, bridge_device, group, args, aux) | ||
do_switch_action(driver, bridge_device, group, args) | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
function GroupedLightCommandHandlers.switch_off_handler(driver, bridge_device, group, args, aux) | ||
do_switch_action(driver, bridge_device, group, args) | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
function GroupedLightCommandHandlers.switch_level_handler(driver, bridge_device, group, args, aux) | ||
do_switch_level_action(driver, bridge_device, group, args) | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
function GroupedLightCommandHandlers.set_color_handler(driver, bridge_device, group, args, aux) | ||
do_color_action(driver, bridge_device, group, args, aux) | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
function GroupedLightCommandHandlers.set_hue_handler(driver, bridge_device, group, args, aux) | ||
do_setHue_action(driver, bridge_device, group, args, aux) | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
function GroupedLightCommandHandlers.set_saturation_handler(driver, bridge_device, group, args, aux) | ||
do_setSaturation_action(driver, bridge_device, group, args, aux) | ||
end | ||
|
||
---@param driver HueDriver | ||
---@param bridge_device HueBridgeDevice | ||
---@param group table | ||
---@param args table | ||
---@param aux table auxilary data needed for the command that the devices all had in common | ||
function GroupedLightCommandHandlers.set_color_temp_handler(driver, bridge_device, group, args, aux) | ||
do_color_temp_action(driver, bridge_device, group, args, aux) | ||
end | ||
|
||
|
||
return GroupedLightCommandHandlers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we fail this call does it fall back to unicast commands?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not right now, but it could be added. In the case of
grouped_light_id
beingnil
, this shouldn't be possible because we clear out the device table if it isn't on the group response. This is a remnant of when I was iterating through the services in the commands and wasn't saving the service off on group intake.