Python for pin code management?

Posted on
Thu Dec 10, 2020 11:48 am
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Python for pin code management?

I created variables for all my frontdoor keypad users (24 of them) with their PIN numbers. (seemed a good a place as any to have it written down, plus it makes changing a PIN number easy)

Option 1: Variable Based List
I have this sorta working. The only thing I'm missing is a way to populate a variable with the USER_ID (Variable Name) associated with the correct PIN entered or with the python variable used. Right now, it just updates a variable with either "Fail" or "Success" vs. "Fail" or "Bill"
Code: Select all
# Users
Bill = indigo.variables[699977664].value # "User01_Bill"
User2 = indigo.variables[669277461].value # "User02"
Vikki = indigo.variables[380866193].value # "User03_Vikki"
Anna = indigo.variables[190982521].value # "User04_Anna"
Ashley = indigo.variables[717167343].value # "User05_Ashley"
OneTime1 = indigo.variables[141646497].value # "User20_OneTime1"
# Add 20 more lines

# Build PW List
PWLIST = (Bill, User2, Vikki, Anna, Ashley, OneTime1)

# Receive and check the input variable
PWInput = indigo.variables[777599449].value # "PWInput"

if PWInput in PWLIST:
   indigo.variable.updateValue(748482965, value="Success")

else:
   indigo.variable.updateValue(748482965, value="Fail")

Side Note: I added a couple "OneTime" users to my keypad with a code. Good for a one time use. Once the code is used, a trigger fires that deletes the code from the keypad. So I can give it to a repair guy or something that needs access when I'm away, but I don't want them coming back later that night or whatever.

Option 2: CSV Based:
Ideally, I'd like to move the list to an external CSV file for overall user management. Where I'm stuck is how to use the data in one column on one row as input to return the data from a different column on the same row. (i.e., if I use a PIN as a variable, the script can return the User_Name. If I use the User_Name as the input variable, I can return the CellPhone#, etc.

Code: Select all
User, PIN, Email, CellPhone
Bill, 1234, bill@mail.com, 832-555-1212
Vikki, 5678, vikki@mail.com, 713-555-1212
OneTime1, 9012,  n/a, n/a


Every python script I've run gets me to the "Fail" message, so I'm messing it up big time somewhere.
Example of my failures:
Code: Select all
import os.path
from os import system

import csv
login = False
with open('/Users/williammoore/Desktop/PinList.csv', 'r') as csvfile:
   csv_reader = csv.reader(csvfile)
   password = indigo.variables[777599449].value # "PWInput"
   for column in csv_reader:
       print(column[0],column[1])
       print(password)      
   if column[1] == password:
         login == True
   else:
         login == False
if login == True:
   indigo.variable.updateValue(748482965, value=column[0])

else:
   indigo.variable.updateValue(748482965, value="fail")   

Code: Select all
import csv
import sys

#input number you want to search
password = indigo.variables[777599449].value # "PWInput"

#read csv, and split on "," the line
csv_file = csv.reader(open("/Users/williammoore/Desktop/PinList.csv", "r"), delimiter=",")


#loop through the csv list
for column in csv_file:
   if password == column[1]:
      indigo.variable.updateValue(748482965, value=column[0])
   else:
      indigo.variable.updateValue(748482965, value="Fail")

Bill
My Plugin: My People

Posted on
Fri Dec 11, 2020 10:39 am
jay (support) offline
Site Admin
User avatar
Posts: 18216
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python for pin code management?

There appeared to be some indentation issues with your script, and I simplified it a bit. Untested, but should be close:

Code: Select all
import csv

# Default failure indicator to be written to the variable
login_name = "fail"
with open('/Users/williammoore/Desktop/PinList.csv', 'r') as csvfile:
   csv_reader = csv.reader(csvfile)
   password = indigo.variables[777599449].value # "PWInput"
   for column in csv_reader:
      # You had prints which would be eaten - this will write to the Indigo log
      indigo.server.log(column[0],column[1])
      indigo.server.log(password)
      if column[1] == password:
         # When you find the first match, set the login_name to the correct Value
         # and break out of the for loop since you don't need to go further
         login_name = column[0]
         break
# Update the variable with the login name or "fail" if it hasn't changed
indigo.variable.updateValue(748482965, value=login_name)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Dec 11, 2020 11:42 am
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Python for pin code management?

jay (support) wrote:
Untested, but should be close:


Tested.... spot on! Thank you.

Bill
My Plugin: My People

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests