Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use pyISY to access node valye #391

Open
Kajtek13 opened this issue Sep 8, 2023 · 4 comments
Open

How to use pyISY to access node valye #391

Kajtek13 opened this issue Sep 8, 2023 · 4 comments

Comments

@Kajtek13
Copy link

Kajtek13 commented Sep 8, 2023

I hope this is appropriate to ask this question.
My goal is simple. I want find a value of the node from a nodeserver already installed.
I know pyisi can do it, but I did not figure out how to do it and I spent a week reading and testing.
The quickstart.rst says to instal pyisy
pip3 install pyisy
After that I check with pip list and pyisy is installed.
The quick guide says that once it is installed run python3 -m pyisy http://your-isy-url:80 username password.
This does not work for me. I got error that there is missing my.py.
I figure out that I need to have installed all the files from this repository.
I installed it in developer mode and then when I run the command python3 -m pyisy http://your-isy-url:80 username password. from within the folder where it is installed it works like a charm. It lists in my terminal all the values from all my nodes. It will continue to do until I exit but when it is running I cannot run from the terminal any commands. When I exit it stops.

I thought that once the pyisy is installed I would be able to access directly any node using address directly.
I copied a code from connection.py (addedd my credentials) to connect to my ISY than added the code example from quickstart.rst - changing address to address of my switch
NODE = '22 5C EB 1'
node = isy.nodes[NODE]
await node.turn_off()
sleep(5)
await node.turn_on()

It does not work
The errors are showing that something is missing. I add the code to import the misisng code from other files in this repository and then another code is missing. There are so many dependencies that it seems endless and that the whole pyISY nodeserved has to run.
Obviously I do not know how to do it.
Any help or suggestions.

@shbatm
Copy link
Collaborator

shbatm commented Sep 8, 2023

The command line is really just for testing, it's not really any sort of interface. If you want to actually perform tasks with the module, you can start by copying __main__.py and adding in what you want to do around line 70 -- that will essentially connect to the ISY, initialize, and leave you with a variable isy that has the node information you want to manage. __main__.py is what runs when you run the module from command line, if that helps you figure out how it works.

This can be moved to whatever part of your program you choose. The most important part is providing the connection info and then initializing the connection before trying to connect to anything. The docs for this module are hit-or-miss--I was never able to fully update them from Version 1 to Version 3, so some detail is out of date.

As far as testing: I tested on a fresh virtual environment and all I ran was the following and it connected:

python3 -m venv pyisy-test  # Creates a new virtual enviornment for a clean set of dependencies
cd pyisy-test && source bin/activate # Activate the virtual env.
python3 -m pip install pyisy # install PyISY
python3 -m pyisy http://polisy.local:8080 admin "password" 

Two gotchas: double check that your port number is correct and put your password in quotes if it has any special characters in it.

Finally, I would recommend before you get too deep into this that you check out https://github.com/shbatm/pyisyox instead. That is going to ultimately be the replacement for this module and it has better support for Node Servers built in. Again __main__.py is going to be your starting point to build your own code.

@Kajtek13
Copy link
Author

Thank you very much for taking time and helpning me. I do not have polisy but eisy and now have to use polyglot 3.
I tried pysyox and eisy that I use has python 3.9 and pyisyox rtequires 3.10. I asked universal device and now there is no option to upgrade to newer version. I will stick to pyisy whioch works for me from command line when after I SSH to eisy and run
python3 -m pyisy https://192.168.2.219:8443 myusername mypassword - That this is all what I achieved.
It will run only from the nodeserver that includes folder pyisy from your repository https://github.com/automicus/PyISY/tree/v3.x.x
If this folder does not exist even after
python3 -m venv pyisy-test
python3 -m pip install pyisy
I will get message error No module named pyisy.main; 'pyisy' is a package and cannot be directly executed
Copying just main.py does not work. The whole pyisy folder has to exist
I am going now to add the code to main.py and experiment. The command line works so it means that I can connect to ISY.
Now I will try to write a nodeserver. Thanks again

You wrote "start by copying main.py and adding in what you want to do around line 70 -- that will essentially connect to the ISY, initialize, and leave you with a variable isy that has the node information you want to manage. This can be moved to whatever part of your program you choose. The most important part is providing the connection info and then initializing the connection before trying to connect to anything"

Do I understand correctly that I can copy the first 70 lines from main_.py to my first executable file to establish connection and then write the rest of the code or main_.py has to be unchanged and left in pyisy folder and the rest of the code to use the ISY variable in a nodeserver can be in other files.

@jimboca
Copy link
Collaborator

jimboca commented Sep 10, 2023

My HarmonyHub and ELK node server both use PyISY, and ELK uses the one built into PG3x.

@Kajtek13
Copy link
Author

Thank you jimboca. I found the references in ELK controller.py but this is too complex for my knowledge level. It would take me a month to analyze it. I found your examples at https://github.com/jimboca/PyISYExamples much easier to understand but even with that clear examples I cannot make them to work for me. I started with your test.py substitting my credentials and switch address. When i tried to run this from command line I get error
Traceback (most recent call last):
File "/var/polyglot/pg3/ns/0021b90260fc_33/test.py", line 6, in
import PyISY
ModuleNotFoundError: No module named 'PyISY'

I suspected that I have change PyISY to pyisy
When i run now [polyglot@eisy ~/pg3/ns/0021b90260fc_33]$ python3 test.py
I get
Starting PyISY: ADDR=192.168.2.219 PORT=8443 LOG=test.log

/var/polyglot/pg3/ns/0021b90260fc_33/test.py(30)()
-> isy = PyISY.ISY(ADDR, PORT, USER, PASS, False, "1.1", logger)
(Pdb)

and it stays as that
when I disable #import pdb; pdb.set_trace()
I get
NameError: name 'PyISY' is not defined

when I change PyISY to isy = pyisy.ISY(ADDR, PORT, USER, PASS, False, "1.1", logger)
I get
Starting PyISY: ADDR=192.168.2.219 PORT=8443 LOG=test.log
Traceback (most recent call last):
File "/var/polyglot/pg3/ns/0021b90260fc_33/test.py", line 30, in
isy = pyisy.ISY(ADDR, PORT, USER, PASS, False, "1.1", logger)
File "/var/polyglot/pg3/ns/0021b90260fc_33/pyisy/isy.py", line 83, in init
self.conn = Connection(
File "/var/polyglot/pg3/ns/0021b90260fc_33/pyisy/connection.py", line 78, in init
self._webroot = webroot.rstrip("/")
AttributeError: 'RootLogger' object has no attribute 'rstrip'

and I am stack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants