- "content": "--[[\nNetatmo Unified Sensor\n@author ikubicki\n]]\nfunction QuickApp:onInit()\n self.config = Config:new(self)\n self.i18n = i18n:new(api.get(\"/settings/info\").defaultLanguage)\n self.netatmo = Netatmo:new(self.config)\n self:trace('')\n self:trace('Netatmo Unified Sensor - ' .. self:getTypeLabel())\n self:updateProperty('manufacturer', 'Netatmo')\n self:updateProperty('model', 'Weather Station')\n self:updateView(\"button2_1\", \"text\", self.i18n:get('search-devices'))\n self:updateView(\"button2_2\", \"text\", self.i18n:get('refresh'))\n self.interfaces = api.get(\"/devices/\" .. self.id).interfaces\n self:run()\nend\n\nfunction QuickApp:run()\n self:pullNetatmoData()\n local interval = self.config:getTimeoutInterval()\n if (interval > 0) then\n fibaro.setTimeout(interval, function() self:run() end)\n end\nend\n\nfunction QuickApp:pullNetatmoData()\n self:updateView(\"button2_2\", \"text\", self.i18n:get('refreshing'))\n local getSensorDataCallback = function(sensorData)\n local property = self:getProperty()\n self:updateProperty(\"value\", sensorData.data[property])\n if sensorData.battery ~= nil then\n self:updateProperty(\"batteryLevel\", sensorData.battery)\n if not utils:contains(self.interfaces, \"battery\") then\n api.put(\"/devices/\" .. self.id, { interfaces = {\"quickApp\", \"battery\"} })\n end\n else\n self:updateProperty(\"batteryLevel\", sensorData.battery)\n if not utils:contains(self.interfaces, \"power\") then\n api.put(\"/devices/\" .. self.id, { interfaces = {\"quickApp\", \"power\"} })\n end\n end\n self:updateView(\"label\", \"text\", string.format(self.i18n:get('last-update'), os.date('%Y-%m-%d %H:%M:%S')))\n self:updateView(\"button2_2\", \"text\", self.i18n:get('refresh'))\n end\n local types = self:getType()\n local id = self.config:getModuleID()\n self.netatmo:getSensorData(types, id, getSensorDataCallback)\nend\n\nfunction QuickApp:refreshEvent()\n self:pullNetatmoData()\nend\n\nfunction QuickApp:searchEvent()\n self:debug(self.i18n:get('searching-devices'))\n self:updateView(\"button2_1\", \"text\", self.i18n:get('searching-devices'))\n local searchDevicesCallback = function(stations)\n QuickApp:debug(json.encode(stations))\n -- printing results\n for _, station in pairs(stations) do\n QuickApp:trace(string.format(self.i18n:get('search-row-station'), station.name, station.id))\n QuickApp:trace(string.format(self.i18n:get('search-row-station-modules'), #station.modules))\n for __, module in ipairs(station.modules) do\n QuickApp:trace(string.format(self.i18n:get('search-row-module'), module.name, module.id, module.type))\n QuickApp:trace(string.format(self.i18n:get('search-row-module_types'), table.concat(module.data_type, ', ')))\n end\n end\n self:updateView(\"button2_1\", \"text\", self.i18n:get('search-devices'))\n self:updateView(\"label\", \"text\", string.format(self.i18n:get('check-logs'), 'QUICKAPP' .. self.id))\n end\n local types = self:getType()\n self.netatmo:searchDevices(types, searchDevicesCallback)\nend\n\nfunction QuickApp:getType()\n if self.type == \"com.fibaro.temperatureSensor\" or self.config:getDeviceType() == \"Temperature\" then \n return { \"NAMain\", \"NAModule1\", \"NAModule4\" } \n end\n if self.type == \"com.fibaro.windSensor\" or self.config:getDeviceType() == \"Wind\" or self.config:getDeviceType() == \"Gusts\" or self.config:getDeviceType() == \"Gust\" then \n self:updateProperty(\"unit\", \"km/h\")\n return { \"NAModule2\" } \n end\n if self.type == \"com.fibaro.rainSensor\" or self.config:getDeviceType() == \"Rain\" then \n self:updateProperty(\"unit\", \"km/h\")\n return { \"NAModule3\" } \n end\n if self.type == \"com.fibaro.humiditySensor\" or self.config:getDeviceType() == \"Humidity\" then \n return { \"NAMain\", \"NAModule1\", \"NAModule4\" } \n end\n if self.config:getDeviceType() == \"Pressure\" then\n return { \"NAMain\" }\n end\n if self.config:getDeviceType() == \"Noise\" then\n return { \"NAMain\" }\n end\n if self.config:getDeviceType() == \"CO2\" then\n return { \"NAMain\", \"NAModule4\" } \n end\nend\n\nfunction QuickApp:getTypeLabel()\n if self.type == \"com.fibaro.temperatureSensor\" or self.config:getDeviceType() == \"Temperature\" then \n return \"Temperature\"\n end\n if self.type == \"com.fibaro.windSensor\" or self.config:getDeviceType() == \"Wind\" then\n return \"Wind\"\n end\n if self.config:getDeviceType() == \"Gusts\" or self.config:getDeviceType() == \"Gust\" then \n return \"Gusts\"\n end\n if self.type == \"com.fibaro.rainSensor\" or self.config:getDeviceType() == \"Rain\" then \n return \"Rain\"\n end\n if self.type == \"com.fibaro.humiditySensor\" or self.config:getDeviceType() == \"Humidity\" then \n return \"Humidity\"\n end\n if self.config:getDeviceType() == \"Pressure\" then\n return \"Pressure\"\n end\n if self.config:getDeviceType() == \"Noise\" then\n return \"Noise\"\n end\n if self.config:getDeviceType() == \"CO2\" then\n return \"CO2\"\n end\nend\n\nfunction QuickApp:getProperty()\n if self.type == \"com.fibaro.temperatureSensor\" or self.config:getDeviceType() == \"Temperature\" then \n return \"Temperature\"\n end\n if self.config:getDeviceType() == \"Gusts\" or self.config:getDeviceType() == \"Gust\" then \n return \"GustStrength\"\n end\n if self.type == \"com.fibaro.windSensor\" or self.config:getDeviceType() == \"Wind\" then\n return \"WindStrength\"\n end\n if self.config:getDeviceType() == \"Rain1h\" or self.config:getDeviceType() == \"Rain1\" then \n return \"sum_rain_1\"\n end\n if self.config:getDeviceType() == \"Rain24h\" or self.config:getDeviceType() == \"Rain24\" then \n return \"sum_rain_24\"\n end\n if self.type == \"com.fibaro.rainSensor\" or self.config:getDeviceType() == \"Rain\" then \n return \"Rain\"\n end\n if self.type == \"com.fibaro.humiditySensor\" or self.config:getDeviceType() == \"Humidity\" then \n return \"Humidity\"\n end\n if self.config:getDeviceType() == \"Pressure\" then\n return \"Pressure\"\n end\n if self.config:getDeviceType() == \"Noise\" then\n return \"Noise\"\n end\n if self.config:getDeviceType() == \"CO2\" then\n return \"CO2\"\n end\nend\n"
0 commit comments