Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Dec 1, 2024
1 parent 61a69bb commit 5646f10
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 31 deletions.
5 changes: 3 additions & 2 deletions extensions/conbee-2/HueBridgeV1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ return require('jls.lang.class').create(function(hueBridge)
self.url = Url:new(apiUrl)
self.user = user or ''
self.mapping = utils.replaceRefs(mapping or {}, {
Thing = Thing,
color = utils,
Thing = Thing, -- TODO remove
color = utils, -- TODO remove
utils = utils,
math = math,
BUTTON_EVENT = {'pressed', 'hold', 'released', 'long-released'}
})
Expand Down
6 changes: 3 additions & 3 deletions extensions/conbee-2/mapping-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
{
"path": "state/xy",
"name": "color",
"adapter": "$lua:return Thing.formatRgbHex(color.xyBriToRgb(value[1], value[2], 1.0))",
"setAdapter": "$lua:return {color.rgbToXy(Thing.parseRgbHex(value))}"
"adapter": "$lua:return utils.formatRgbHex(utils.xyBriToRgb(value[1], value[2], 1.0))",
"setAdapter": "$lua:return {utils.rgbToXy(utils.parseRgbHex(value))}"
}
],
"TemperatureSensor": [
Expand Down Expand Up @@ -181,7 +181,7 @@
]
},
"adapters": {
"mirek": "$lua:return color.mirekToColorTemperature(value)"
"mirek": "$lua:return utils.mirekToColorTemperature(value)"
},
"metadata": {
"button": {
Expand Down
5 changes: 3 additions & 2 deletions extensions/hue-v2/HueBridgeV2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ return require('jls.lang.class').create(function(hueBridge)
['hue-application-key'] = self.key
}
self.mapping = utils.replaceRefs(mapping or {}, {
Thing = Thing,
color = utils,
Thing = Thing, -- TODO remove
color = utils, -- TODO remove
utils = utils,
math = math,
-- one of initial_press, repeat, short_release, long_release, double_short_release, long_press
BUTTON_EVENT = {
Expand Down
6 changes: 3 additions & 3 deletions extensions/hue-v2/mapping-v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
{
"path": "color/xy",
"name": "color",
"adapter": "$lua:return Thing.formatRgbHex(color.xyBriToRgb(value.x, value.y, 1.0))",
"setAdapter": "$lua:local x, y = color.rgbToXy(Thing.parseRgbHex(value)); return {x = x, y = y}"
"adapter": "$lua:return utils.formatRgbHex(utils.xyBriToRgb(value.x, value.y, 1.0))",
"setAdapter": "$lua:local x, y = utils.rgbToXy(utils.parseRgbHex(value)); return {x = x, y = y}"
},
{
"path": "dimming/brightness",
Expand Down Expand Up @@ -126,7 +126,7 @@
}
},
"adapters": {
"mirek": "$lua:return color.mirekToColorTemperature(value)"
"mirek": "$lua:return utils.mirekToColorTemperature(value)"
},
"metadata": {
"button": {
Expand Down
5 changes: 3 additions & 2 deletions extensions/zwave-js-ws/ZWaveJs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ return require('jls.lang.class').create(function(zWaveJs)
function zWaveJs:initialize(url, mapping)
self.url = url
self.mapping = utils.replaceRefs(mapping or {}, {
Thing = Thing,
color = utils,
Thing = Thing, -- TODO remove
color = utils, -- TODO remove
utils = utils,
math = math,
})
end
Expand Down
2 changes: 1 addition & 1 deletion extensions/zwave-js-ws/mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
}
],
"adapters": {
"mirek": "$lua:return color.mirekToColorTemperature(value)"
"mirek": "$lua:return utils.mirekToColorTemperature(value)"
},
"property": {
"battery": {
Expand Down
18 changes: 0 additions & 18 deletions lha/Thing.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
local tables = require('jls.util.tables')
local List = require('jls.util.List')
local Codec = require('jls.util.Codec')
local hex = Codec.getInstance('hex')

local ThingProperty = require('lha.ThingProperty')
local utils = require('lha.utils')
Expand Down Expand Up @@ -329,22 +327,6 @@ return require('jls.lang.class').create(function(thing)

end, function(Thing)

function Thing.formatRgbHex(r, g, b)
return string.format('#%02X%02X%02X', math.floor(r * 255), math.floor(g * 255), math.floor(b * 255))
end

function Thing.parseRgbHex(rgbHex)
if string.sub(rgbHex, 1, 1) == '#' then
rgbHex = string.sub(rgbHex, 2)
end
if #rgbHex < 6 then
return 0, 0, 0
end
local rgb = hex:decode(rgbHex)
local r, g, b = string.byte(rgb, 1, 3)
return r / 255, g / 255, b / 255
end

function Thing.getDefaultValueForType(valueType, value)
if valueType == 'boolean' then
return (type(value) == 'boolean') and value or false
Expand Down
19 changes: 19 additions & 0 deletions lha/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local File = require('jls.io.File')
local Date = require('jls.util.Date')
local json = require('jls.util.json')
local tables = require('jls.util.tables')
local Codec = require('jls.util.Codec')
local hex = Codec.getInstance('hex')

local utils = {}

Expand Down Expand Up @@ -366,4 +368,21 @@ function utils.rgbToHsv(r, g, b)
return h, s, v
end

function utils.formatRgbHex(r, g, b)
return string.format('#%02X%02X%02X', math.floor(r * 255), math.floor(g * 255), math.floor(b * 255))
end

function utils.parseRgbHex(rgbHex)
if string.sub(rgbHex, 1, 1) == '#' then
rgbHex = string.sub(rgbHex, 2)
end
if #rgbHex < 6 then
return 0, 0, 0
end
local rgb = hex:decode(rgbHex)
local r, g, b = string.byte(rgb, 1, 3)
return r / 255, g / 255, b / 255
end


return utils

0 comments on commit 5646f10

Please sign in to comment.