|
| 1 | +-- Copyright 2022 SmartThings |
| 2 | +-- |
| 3 | +-- Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +-- you may not use this file except in compliance with the License. |
| 5 | +-- You may obtain a copy of the License at |
| 6 | +-- |
| 7 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +-- |
| 9 | +-- Unless required by applicable law or agreed to in writing, software |
| 10 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +-- See the License for the specific language governing permissions and |
| 13 | +-- limitations under the License. |
| 14 | + |
| 15 | +local capabilities = require "st.capabilities" |
| 16 | +local clusters = require "st.zigbee.zcl.clusters" |
| 17 | +local switch_defaults = require "st.zigbee.defaults.switch_defaults" |
| 18 | +local configurationMap = require "configurations" |
| 19 | +local utils = require "st.utils" |
| 20 | + |
| 21 | +local ColorControl = clusters.ColorControl |
| 22 | + |
| 23 | +local CURRENT_X = "current_x_value" -- y value from xyY color space |
| 24 | +local CURRENT_Y = "current_y_value" -- x value from xyY color space |
| 25 | +local Y_TRISTIMULUS_VALUE = "y_tristimulus_value" -- Y tristimulus value which is used to convert color xyY -> RGB -> HSV |
| 26 | +local HUESAT_TIMER = "huesat_timer" |
| 27 | +local TARGET_HUE = "target_hue" |
| 28 | +local TARGET_SAT = "target_sat" |
| 29 | + |
| 30 | +local IKEA_XY_COLOR_BULB_FINGERPRINTS = { |
| 31 | + ["IKEA of Sweden"] = { |
| 32 | + ["TRADFRI bulb E27 CWS opal 600lm"] = true, |
| 33 | + ["TRADFRI bulb E26 CWS opal 600lm"] = true |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +local function can_handle_ikea_xy_color_bulb(opts, driver, device) |
| 38 | + local can_handle = (IKEA_XY_COLOR_BULB_FINGERPRINTS[device:get_manufacturer()] or {})[device:get_model()] |
| 39 | + if can_handle then |
| 40 | + local subdriver = require("ikea-xy-color-bulb") |
| 41 | + return true, subdriver |
| 42 | + else |
| 43 | + return false |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +local function do_configure(driver, device) |
| 48 | + device:configure() |
| 49 | +end |
| 50 | + |
| 51 | +local function device_added(driver, device) |
| 52 | + device:refresh() |
| 53 | +end |
| 54 | + |
| 55 | +local device_init = function(self, device) |
| 56 | + device:remove_configured_attribute(ColorControl.ID, ColorControl.attributes.CurrentHue.ID) |
| 57 | + device:remove_configured_attribute(ColorControl.ID, ColorControl.attributes.CurrentSaturation.ID) |
| 58 | + device:remove_monitored_attribute(ColorControl.ID, ColorControl.attributes.CurrentHue.ID) |
| 59 | + device:remove_monitored_attribute(ColorControl.ID, ColorControl.attributes.CurrentSaturation.ID) |
| 60 | + |
| 61 | + local configuration = configurationMap.get_device_configuration(device) |
| 62 | + if configuration ~= nil then |
| 63 | + for _, attribute in ipairs(configuration) do |
| 64 | + device:add_configured_attribute(attribute) |
| 65 | + end |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +local function store_xyY_values(device, x, y, Y) |
| 70 | + device:set_field(Y_TRISTIMULUS_VALUE, Y) |
| 71 | + device:set_field(CURRENT_X, x) |
| 72 | + device:set_field(CURRENT_Y, y) |
| 73 | +end |
| 74 | + |
| 75 | +local query_device = function(device) |
| 76 | + return function() |
| 77 | + device:send(ColorControl.attributes.CurrentX:read(device)) |
| 78 | + device:send(ColorControl.attributes.CurrentY:read(device)) |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +local function set_color_handler(driver, device, cmd) |
| 83 | + -- Cancel the hue/sat timer if it's running, since setColor includes both hue and saturation |
| 84 | + local huesat_timer = device:get_field(HUESAT_TIMER) |
| 85 | + if huesat_timer ~= nil then |
| 86 | + device.thread:cancel_timer(huesat_timer) |
| 87 | + device:set_field(HUESAT_TIMER, nil) |
| 88 | + end |
| 89 | + |
| 90 | + local hue = (cmd.args.color.hue ~= nil and cmd.args.color.hue > 99) and 99 or cmd.args.color.hue |
| 91 | + local sat = cmd.args.color.saturation |
| 92 | + |
| 93 | + local x, y, Y = utils.safe_hsv_to_xy(hue, sat) |
| 94 | + store_xyY_values(device, x, y, Y) |
| 95 | + switch_defaults.on(driver, device, cmd) |
| 96 | + |
| 97 | + device:send(ColorControl.commands.MoveToColor(device, x, y, 0x0000)) |
| 98 | + |
| 99 | + device:set_field(TARGET_HUE, nil) |
| 100 | + device:set_field(TARGET_SAT, nil) |
| 101 | + device.thread:call_with_delay(2, query_device(device)) |
| 102 | +end |
| 103 | + |
| 104 | +local huesat_timer_callback = function(driver, device, cmd) |
| 105 | + return function() |
| 106 | + device:set_field(HUESAT_TIMER, nil) |
| 107 | + local hue = device:get_field(TARGET_HUE) |
| 108 | + local sat = device:get_field(TARGET_SAT) |
| 109 | + hue = hue ~= nil and hue or device:get_latest_state("main", capabilities.colorControl.ID, capabilities.colorControl.hue.NAME) |
| 110 | + sat = sat ~= nil and sat or device:get_latest_state("main", capabilities.colorControl.ID, capabilities.colorControl.saturation.NAME) |
| 111 | + cmd.args = { |
| 112 | + color = { |
| 113 | + hue = hue, |
| 114 | + saturation = sat |
| 115 | + } |
| 116 | + } |
| 117 | + set_color_handler(driver, device, cmd) |
| 118 | + end |
| 119 | +end |
| 120 | + |
| 121 | +local function set_hue_sat_helper(driver, device, cmd, hue, sat) |
| 122 | + local huesat_timer = device:get_field(HUESAT_TIMER) |
| 123 | + if huesat_timer ~= nil then |
| 124 | + device.thread:cancel_timer(huesat_timer) |
| 125 | + device:set_field(HUESAT_TIMER, nil) |
| 126 | + end |
| 127 | + if hue ~= nil and sat ~= nil then |
| 128 | + cmd.args = { |
| 129 | + color = { |
| 130 | + hue = hue, |
| 131 | + saturation = sat |
| 132 | + } |
| 133 | + } |
| 134 | + set_color_handler(driver, device, cmd) |
| 135 | + else |
| 136 | + if hue ~= nil then |
| 137 | + device:set_field(TARGET_HUE, hue) |
| 138 | + elseif sat ~= nil then |
| 139 | + device:set_field(TARGET_SAT, sat) |
| 140 | + end |
| 141 | + device:set_field(HUESAT_TIMER, device.thread:call_with_delay(0.2, huesat_timer_callback(driver, device, cmd))) |
| 142 | + end |
| 143 | +end |
| 144 | + |
| 145 | +local function set_hue_handler(driver, device, cmd) |
| 146 | + set_hue_sat_helper(driver, device, cmd, cmd.args.hue, device:get_field(TARGET_SAT)) |
| 147 | +end |
| 148 | + |
| 149 | +local function set_saturation_handler(driver, device, cmd) |
| 150 | + set_hue_sat_helper(driver, device, cmd, device:get_field(TARGET_HUE), cmd.args.saturation) |
| 151 | +end |
| 152 | + |
| 153 | +local function current_x_attr_handler(driver, device, value, zb_rx) |
| 154 | + local Y_tristimulus = device:get_field(Y_TRISTIMULUS_VALUE) |
| 155 | + local y = device:get_field(CURRENT_Y) |
| 156 | + local x = value.value |
| 157 | + |
| 158 | + if y then |
| 159 | + local hue, saturation = utils.safe_xy_to_hsv(x, y, Y_tristimulus) |
| 160 | + |
| 161 | + device:emit_event(capabilities.colorControl.hue(hue)) |
| 162 | + device:emit_event(capabilities.colorControl.saturation(saturation)) |
| 163 | + end |
| 164 | + |
| 165 | + device:set_field(CURRENT_X, x) |
| 166 | +end |
| 167 | + |
| 168 | +local function current_y_attr_handler(driver, device, value, zb_rx) |
| 169 | + local Y_tristimulus = device:get_field(Y_TRISTIMULUS_VALUE) |
| 170 | + local x = device:get_field(CURRENT_X) |
| 171 | + local y = value.value |
| 172 | + |
| 173 | + if x then |
| 174 | + local hue, saturation = utils.safe_xy_to_hsv(x, y, Y_tristimulus) |
| 175 | + |
| 176 | + device:emit_event(capabilities.colorControl.hue(hue)) |
| 177 | + device:emit_event(capabilities.colorControl.saturation(saturation)) |
| 178 | + end |
| 179 | + |
| 180 | + device:set_field(CURRENT_Y, y) |
| 181 | +end |
| 182 | + |
| 183 | +local ikea_xy_color_bulb = { |
| 184 | + NAME = "IKEA XY Color Bulb", |
| 185 | + lifecycle_handlers = { |
| 186 | + doConfigure = do_configure, |
| 187 | + added = device_added, |
| 188 | + init = configurationMap.power_reconfig_wrapper(device_init) |
| 189 | + }, |
| 190 | + capability_handlers = { |
| 191 | + [capabilities.colorControl.ID] = { |
| 192 | + [capabilities.colorControl.commands.setColor.NAME] = set_color_handler, |
| 193 | + [capabilities.colorControl.commands.setHue.NAME] = set_hue_handler, |
| 194 | + [capabilities.colorControl.commands.setSaturation.NAME] = set_saturation_handler |
| 195 | + } |
| 196 | + }, |
| 197 | + zigbee_handlers = { |
| 198 | + attr = { |
| 199 | + [ColorControl.ID] = { |
| 200 | + [ColorControl.attributes.CurrentX.ID] = current_x_attr_handler, |
| 201 | + [ColorControl.attributes.CurrentY.ID] = current_y_attr_handler |
| 202 | + } |
| 203 | + } |
| 204 | + }, |
| 205 | + can_handle = can_handle_ikea_xy_color_bulb |
| 206 | +} |
| 207 | + |
| 208 | +return ikea_xy_color_bulb |
0 commit comments