-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESPHome v1.0.2..fqa
1 lines (1 loc) · 9.35 KB
/
ESPHome v1.0.2..fqa
1
{"name":"ESPHome","type":"com.fibaro.deviceController","apiVersion":"1.2","initialProperties":{"viewLayout":{"$jason":{"body":{"header":{"style":{"height":"0"},"title":"quickApp_device_440"},"sections":{"items":[{"components":[{"name":"btnLoad","style":{"weight":"1.2"},"text":"Load Device","type":"button"},{"style":{"weight":"0.5"},"type":"space"}],"style":{"weight":"1.2"},"type":"vertical"},{"components":[{"name":"btnInstall","style":{"weight":"1.2"},"text":"Install Device","type":"button"},{"style":{"weight":"0.5"},"type":"space"}],"style":{"weight":"1.2"},"type":"vertical"},{"components":[{"name":"btnRemove","style":{"weight":"1.2"},"text":"Remove Device","type":"button"},{"style":{"weight":"0.5"},"type":"space"}],"style":{"weight":"1.2"},"type":"vertical"}]}},"head":{"title":"quickApp_device_440"}}},"uiCallbacks":[{"callback":"btnLoadClicked","eventType":"onReleased","name":"btnLoad"},{"callback":"btnInstallClicked","eventType":"onReleased","name":"btnInstall"},{"callback":"btnRemoveClicked","eventType":"onReleased","name":"btnRemove"}],"quickAppVariables":[{"name":"ip","value":"192.168.1."},{"name":"port","value":"80"}],"typeTemplateInitialized":true},"files":[{"name":"main","isMain":true,"isOpen":true,"content":"--- Made By Jayson, Update Date : 2020.12.16---\n\n_app = {name=\"ESPHome\", version=\"1.0.2\"}\n\nlocal url = \"\"\nlocal http = net.HTTPClient({timeout=10000})\nlocal device = {}\nlocal domain = {}\nlocal id = {}\nlocal state = {}\nlocal installedDevice = {}\n\nlocal ESPHomeType = {\nsensor = {ftype=\"com.fibaro.multilevelSensor\",class=\"ESPHome_MultilevelSensor\"},\nbinary_sensor = {ftype=\"com.fibaro.binarySensor\",class=\"ESPHome_BinarySensor\"},\nswitch = {ftype=\"com.fibaro.binarySwitch\",class=\"ESPHome_BinarySwitch\"},\nlight = {ftype=\"com.fibaro.binarySwitch\",class=\"ESPHome_Light\"},\n-- light = {ftype=\"com.fibaro.colorController\",class=\"ESPHome_Light\"},\nfan = {ftype=\"com.fibaro.binarySwitch\",class=\"ESPHome_Fan\"},\n}\n\nfunction searchFType(type)\n for t, i in pairs(ESPHomeType) do if t == type then return i.ftype end end\n return nil\nend\nfunction searchEType(type)\n for t, i in pairs(ESPHomeType) do if t == type then return i.class end end\n return nil\nend\n\nfunction receivedData(response)\n local res = response.data\n for i, v in pairs(splitString(res, \"data:\")) do\n if (string.find(v, \"{\")) ~= nil then\n recievedData = json.decode(v)\n if not contains(device, recievedData.id) then\n table.insert(device, recievedData.id)\n for i, v in pairs(splitString(recievedData.id, \"-\")) do\n if i == 1 then table.insert(domain, v)\n elseif i == 2 then table.insert(id, v)\n end\n end\n table.insert(state, recievedData.state)\n else\n for i, v in pairs(device) do\n if v == recievedData.id then state[i] = recievedData.state end\n end\n end\n end\n end\nend\n\nfunction get(url, cont)\n http:request(url, {\n options={ method = 'GET'},\n success = function(response) receivedData(response) end,\n error = function(err) print(\"error:\",err) end\n })\nend\n\nfunction post(url, state)\n http:request(url, {\n options={ method = 'POST'},\n success = function(res) if state == \"turn_on\" then print(\"Turned On\") else print(\"Turned Off\") end end,\n error = function(err) print(\"error:\",err) end\n })\nend\n\nfunction getStates(url)\n local eventUrl = url .. \"/events\"\n get(eventUrl)\nend\n\nfunction QuickApp:onInit()\n self:debug(string.format(\"%s ( %s ) ESPHome Connector onInit\", _app.name, self.id, _app.version))\n self.ip = self:getVariable(\"ip\")\n self.port = self:getVariable(\"port\")\n url = \"http://\" .. self.ip .. \":\" .. self.port\n self:debug(string.format(\"%s ESPHome device URL is %s \",_app.name, url))\n\n getStates(url)\n self:initChildDevices({\n [\"com.fibaro.binarySwitch\"] = ESPHome_BinarySwitch,\n [\"com.fibaro.multilevelSensor\"] = ESPHome_MultilevelSensor,\n [\"com.fibaro.binarySensor\"] = ESPHome_BinarySensor\n })\n \n self:debug(\"Child devices:\")\n for id,device in pairs(self.childDevices) do\n self:debug(\"[\", id, \"]\", device.name, \", type of: \", device.type)\n table.insert(installedDevice, device.name)\n end\nend\n\nfunction QuickApp:btnLoadClicked()\n self:debug(json.encode(domain))\n self:debug(json.encode(id))\n self:debug(json.encode(state))\n self:debug(\"ESPHome Device is Loaded\")\nend\n\nfunction QuickApp:btnInstallClicked()\n local devNumber = #domain\n for i=1, tonumber(devNumber), 1 do\n self:createESPHome(i, domain[i], id[i], state[i])\n end\n self:debug(\"ESPHome Device has been installed\")\nend\n\nfunction QuickApp:btnRemoveClicked()\n for id,device in pairs(self.childDevices) do\n self:removeChildDevice(id)\n end\n installedDevice = {}\n self:debug(\"ESPHome Device has been removed\")\nend\n\nfunction QuickApp:createESPHome(childNo, d, i, s)\n local c = {}\n c.name = i\n c.type = searchFType(d)\n if d == 'sensor' then\n for i,v in pairs(splitString(s, \" \")) do\n if i == 1 then c.value = tonumber(v)\n elseif i == 2 then c.unit = v end\n end\n else\n if s == \"ON\" then c.value = true elseif s == \"OFF\" then c.value = false else c.value = s end\n end\n local property ={ \n userDescription = childNo,\n unit = c.unit\n }\n c.initialProperties = property\n deviceClass = searchEType(d)\n if not contains(installedDevice, c.name) then local child = self:createChildDevice(c, _G[deviceClass]) table.insert(installedDevice, c.name)\n else self:debug(string.format(\"Device ( %s ) is already installed\", c.name)) end\nend\n\n--------------------- ESPHome ---------------------\nclass 'ESPHome' (QuickAppChild)\nfunction ESPHome:__init(device)\n QuickAppChild.__init(self, device)\n self.childNo = fibaro.getValue(self.id, \"userDescription\")\n self.value = fibaro.getValue(self.id, \"value\")\n self:setValue()\nend\n\nfunction ESPHome:setValue()\n if domain[self.childNo] == 'sensor' then\n for i,v in pairs(splitString(state[self.childNo], \" \")) do\n if i == 1 then self.updatedValue = tonumber(v) end\n end\n else\n if state[self.childNo] == \"ON\" then self.updatedValue = true\n elseif state[self.childNo] == \"OFF\" then self.updatedValue = false end\n end\n if self.value ~= self.updatedValue then self.value = self.updatedValue end\n self:update(self.value)\n fibaro.setTimeout(1000,function() self:setValue() end)\nend\n\nfunction ESPHome:update(value) self:updateProperty(\"value\", value) end\n\n--------------------- ESPHome BinarySwitch ---------------------\nclass 'ESPHome_BinarySwitch' (ESPHome)\nfunction ESPHome_BinarySwitch:__init(device) ESPHome.__init(self, device) end\n\nfunction ESPHome_BinarySwitch:turnOn()\n local deviceDomain = domain[self.childNo]\n local deviceName = id[self.childNo]\n self.turnOnUrl = url .. \"/\" .. deviceDomain .. \"/\" .. deviceName .. \"/turn_on\"\n post(self.turnOnUrl, \"turn_on\")\nend\n\nfunction ESPHome_BinarySwitch:turnOff()\n local deviceDomain = domain[self.childNo]\n local deviceName = id[self.childNo]\n self.turnOffUrl = url .. \"/\" .. deviceDomain .. \"/\" .. deviceName .. \"/turn_off\"\n post(self.turnOffUrl, \"turn_off\")\nend\n--------------------- ESPHome Fan ---------------------\n\nclass 'ESPHome_Fan' (ESPHome_BinarySwitch)\nfunction ESPHome_Fan:__init(device) ESPHome_BinarySwitch.__init(self, device) end\n\n--------------------- ESPHome Light ---------------------\nclass 'ESPHome_Light' (ESPHome_BinarySwitch)\nfunction ESPHome_Light:__init(device) ESPHome_BinarySwitch.__init(self, device) end\n\n--------------------- ESPHome Binary Sensor ---------------------\nclass 'ESPHome_BinarySensor' (ESPHome)\nfunction ESPHome_BinarySensor:__init(device) ESPHome.__init(self, device) end\n\n--------------------- ESPHome Multilevel Sensor ---------------------\nclass 'ESPHome_MultilevelSensor' (ESPHome)\nfunction ESPHome_MultilevelSensor:__init(device) ESPHome.__init(self, device) end\n"},{"name":"functionList","isMain":false,"isOpen":false,"content":"function splitString(str, pat)\r\n local t = {}\r\n local fpat = \"(.-)\" .. pat\r\n local last_end = 1\r\n local s, e, cap = str:find(fpat, 1)\r\n while s do\r\n if s ~= 1 or cap ~= \"\" then\r\n table.insert(t,cap)\r\n end\r\n last_end = e+1\r\n s, e, cap = str:find(fpat, last_end)\r\n end\r\n\r\n if last_end <= #str then\r\n cap = str:sub(last_end)\r\n table.insert(t, cap)\r\n end\r\n return t\r\nend\r\n\r\nfunction dumpTable(o)\r\n if type(o) == 'table' then\r\n local s = '{ '\r\n for k,v in pairs(o) do\r\n -- key 가 숫자가 아닌 경우 \"key\" \r\n if type(k) ~= 'number' then \r\n k = '\"'..k..'\"' \r\n end\r\n s = s .. '['..k..'] = ' .. dumpTable(v) .. ','\r\n end\r\n return s .. '} '\r\n else\r\n return tostring(o)\r\n end\r\nend\r\n\r\nfunction contains(list, x)\r\n\tfor _, v in pairs(list) do\r\n\t\tif v == x then return true end\r\n\tend\r\n\treturn false\r\nend"}]}