Logic Machine Forum
Rituals Genie - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Rituals Genie (/showthread.php?tid=4887)



Rituals Genie - gjniewenhuijse - 24.07.2023

Here my code to control a Rituals Genie.

add a library
Code:
--[[
based on
    - https://community.home-assistant.io/t/restful-or-not-rituals-perfume-genie/167112
    - fibaro rituals genie quickapp
--]]

json = require('json')
https = require('ssl.https')
ltn12 = require('ltn12')
escape = require('socket.url').escape

-- rituals account
ritualsServer = 'https://rituals.sense-company.com'
ritualsUserName = 'xxx@xxxxxxxx.nl'
ritualsPassword = 'xxxxxxxx'

-- hubs
hubWoonkamer = 1  -- get from getHubs()


-- general debug logging
debug = false
function debugLog(iLog)
    if debug then
    log(iLog)
  end
end
 

-- general web request function
function request(endpoint, data, method)
  local url = ritualsServer .. endpoint
  local resp = {}
  local headers = {
    ['Content-Type'] = 'application/json'
  }
  local source

  if data then
    method = method or 'POST'
    source = ltn12.source.string(data)
    headers['Content-Length'] = #data
  else
    method = 'GET'
  end

  local res, code = https.request({
    url = url,
    method = method,
    headers = headers,
    source = source,
    sink = ltn12.sink.table(resp)
  })
  debugLog(res,code,resp)
 
  if res and code == 200 then
    return json.pdecode(table.concat(resp))
  else
    return res, code
  end
end


-- get account hash
function getAccount()
  data = '{\"email\": \"' .. ritualsUserName .. '\", \"password\": \"' .. ritualsPassword .. '\"}'
  resp = request('/ocapi/login', data, 'POST')
  accountHash = resp.account_hash
  return accountHash
end
--getAccount()


-- get hub hash
function getHubs(iHub)
    data = nil
    resp = request('/api/account/hubs/'..getAccount(), data, 'GET')
  if iHub then
      hubHash = resp[iHub].hub.hash
      return hubHash
  else
    debugLog(resp) -- get all hubs
  end
end
--getHubs()
--getHubs(hubWoonkamer)


-- get hub hash
function getHubInfo(iHub)
    data = nil
    resp = request('/api/account/hub/'..getHubs(iHub), data, 'GET')
    debugLog({'power:'..resp.hub.attributes.fanc, 'speed:'..resp.hub.attributes.speedc, 'cardridge:'..resp.hub.sensors.rfidc.title, 'volume:'..resp.hub.sensors.fillc.title})
end
--getHubInfo(hubWoonkamer)


-- set hub state
function setHubState(iHub, iState)
    if iState then oState = 1 else oState = 0 end
  data = '{\"hub\": \"' .. getHubs(iHub) .. '\",\"json\": {\"attr\": {\"fanc\": \"'..oState..'\"}}}'
  resp = request('/api/hub/update/attr', data, 'POST')
end
--setHubState(hubWoonkamer,true)
--setHubState(hubWoonkamer,false)


-- set hub speed (1=low, 2=med, 3=high)
function setHubSpeed(iHub, iSpeed)
  data = '{\"hub\": \"' .. getHubs(iHub) .. '\",\"json\": {\"attr\": {\"speedc\": \"'..iSpeed..'\"}}}'
  resp = request('/api/hub/update/attr', data, 'POST')
end
--setHubSpeed(hubWoonkamer,1)
--setHubSpeed(hubWoonkamer,2)
--setHubSpeed(hubWoonkamer,3)

use this code for example
Code:
require('user.nit_rituals')
setHubState(hubWoonkamer,true)