example.lua
local ansicolors = require("ansicolors") local ll = require("logging") require "logging.console"
ll.defaultLogger(ll.console {
logLevel = ll.INFO, destination = "stderr",
timestampPattern = "%y-%m-%d %H:%M:%S.%q",
logPatterns = {
[ll.DEBUG] = ansicolors("%date%{cyan} %level %message %{reset}(%source)\n"),
[ll.INFO] = ansicolors("%date %level %message\n"),
[ll.WARN] = ansicolors("%date%{yellow} %level %message\n"),
[ll.ERROR] = ansicolors("%date%{red bright} %level %message %{reset}(%source)\n"),
[ll.FATAL] = ansicolors("%date%{magenta bright} %level %message %{reset}(%source)\n"),
}
})
local copas = require "copas"
local millheat = require("millheat")
local mh = millheat.new {
username = "someone@here.com",
password = "sooper secret",
}
local task = function()
local _, houses = assert(mh:srequest("GET:/houses"))
local houses = houses.ownHouses or {}
for _, house in ipairs(houses) do
local _, devices = assert(mh:srequest("GET:/houses/{houseId}/devices/independent", {
houseId = house.id
}))
devices = devices.items or {}
mh.log:info("House: '%s' (id: %s), has %d independent devices", house.name, house.id, #devices)
for _, device in ipairs(devices) do
local deviceName = device.customName or "unnamed device"
local ambient = (device.lastMetrics or {}).temperatureAmbient
local setpoint = (device.lastMetrics or {}).temperature
local unit = ((device.deviceSettings or {}).reported or {}).display_unit or "(unknown unit)"
mh.log:info("Device: '%s', current temperature: %d, setpoint: %d (%s)", deviceName, ambient, setpoint, unit)
assert(mh:srequest("PATCH:/devices/{deviceId}/settings", {
deviceId = device.deviceId
}, {
deviceType = device.deviceType.parentType.name,
enabled = device.isEnabled,
settings = {
operation_mode = "independent_device", temperature_normal = setpoint + 1, }
}))
mh.log:info("Setpoint for '%s' increased to %d", deviceName, setpoint + 1)
end
end
mh:logout()
end
copas(task)
print("Copas exited")