Como apagar tokens antigos / abandonados no HomeAssistant com ajuda do Nodered

token

No Home Assistante somente é necessário criar um scrip para reboot:

reboot:
  alias: reboot
  sequence:
  - service: homeassistant.restart
  mode: single

Flow para importar no NodeRed:

[{"id":"804ab9ebc9da9197","type":"tab","label":"Clean","disabled":false,"info":"","env":[]},{"id":"bc21f197cd23e47c","type":"file in","z":"804ab9ebc9da9197","name":"","filename":"/config/.storage/auth","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":320,"y":60,"wires":[["2ddfc295d2b2a157","668b887adcafa563","0870efa7d35fa525"]]},{"id":"2ddfc295d2b2a157","type":"file","z":"804ab9ebc9da9197","name":"","filename":"/config/.storage/auth_backup","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"none","x":610,"y":40,"wires":[[]]},{"id":"668b887adcafa563","type":"json","z":"804ab9ebc9da9197","name":"","property":"payload","action":"","pretty":false,"x":530,"y":120,"wires":[["408db03b49da12a0"]]},{"id":"408db03b49da12a0","type":"function","z":"804ab9ebc9da9197","name":"","func":"let backup = msg.payload; \nlet oldTokens = msg.payload.data.refresh_tokens \nlet newTokens = []; \nlet ips = [] \nlet oldDate; \nlet currentDate = new Date().getTime(); \nlet thirtyDays = 604800000; //30 24 60 60*1000 \n\nObject.keys(oldTokens).forEach(function(key) { \n // Guardar todas las IP's para revisar \nips.push(oldTokens[key].last_used_ip); \n\noldDate = new Date(oldTokens[key].last_used_at).getTime(); \n\nif (currentDate - oldDate < thirtyDays \n|| oldTokens[key].token_type == \"long_lived_access_token\") { \nnewTokens.push(oldTokens[key]); \n} else { \n \n //node.warn(oldTokens[key].last_used_at); \n}\n\n}); \n\nlet uniqueItems = [ ... new Set(ips)] \n\nnode.warn(uniqueItems); \nmsg.payload.data.refresh_tokens = newTokens; \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":120,"wires":[["dce6618cd3390b04"]]},{"id":"dce6618cd3390b04","type":"file","z":"804ab9ebc9da9197","name":"","filename":"/config/.storage/auth","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"none","x":880,"y":120,"wires":[["8acbefc9f83aae2c"]]},{"id":"8acbefc9f83aae2c","type":"debug","z":"804ab9ebc9da9197","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1090,"y":120,"wires":[]},{"id":"729eb01dddc140a4","type":"cronplus","z":"804ab9ebc9da9197","name":"Clean","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"schedule1","topic":"schedule1","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 22 ? * FRI *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":130,"y":60,"wires":[["bc21f197cd23e47c"]]},{"id":"0870efa7d35fa525","type":"delay","z":"804ab9ebc9da9197","name":"","pauseType":"delay","timeout":"30","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":540,"y":180,"wires":[["ee5330d7c7d76dc8"]]},{"id":"ee5330d7c7d76dc8","type":"api-call-service","z":"804ab9ebc9da9197","name":"reboot","server":"cb31618a1275eda7","version":3,"debugenabled":false,"service_domain":"script","service":"reboot","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":690,"y":180,"wires":[[]]},{"id":"cb31618a1275eda7","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

Qualquer dúvida disponham.

4 Curtiram

Copyright © 2017-2021. Todos os direitos reservados
CPHA.pt - info@cpha.pt


FAQ | Termos de Serviço/Regras | Política de Privacidade