Good way to develop and test python scripts

I am a web developer by day and I’m interested in learning some python so I can do some advanced stuff with Home Assistant. I cobbled together my first script today and got it to work but it’s only 6 lines and is very simple.

I was wondering if there was a way to run python scripts from an ssh session or something rather than calling the service in the developer console in the HA UI.

1 Like

Appdaemon?

From the AppDeamon description…

AppDaemon is a loosely coupled, multithreaded, sandboxed python execution
environment for writing automation apps for Home Assistant home automation
software. It also provides a configurable dashboard (HADashboard) suitable
for wall mounted tablets.

That seems like more than I want and if I am new to python, is that pure python or another thing to learn on top of python?

If you just want to learn Python I’d suggest Googling for an online tutorial site. There are lots of good ones. That’s how I learned. Just make sure it’s Python 3.

1 Like

It’s pure python. And you could write simple code it the beginning to learn the language then make more and more advanced automations as you learn more python.

Yes of course, just like any other python script.

So I finished my first Python course on PluralSight this morning and what I’m looking for is a REPL.

I want an interactive shell where I could type hass.states.get('binary_sensor.home_mode') and have it return the state or try out an if or a for loop with access to states and services.

Something like the Python console in Blender

Is that possible?

Or is my only option to create script, reboot HA so it sees script, add log statements to script, call script service in the developer console, review log, modify script, repeat…

I know it has been suggested before, but I do think you should reconsider using appdaemon. It is not interactive, but the app re-loads automatically as soon as the file is saved, so there is none of this restart HA every time you make a change to the script.

It is an extra learning curve, but definitely one worth making.

The equivalent to hass.states.get('binary_sensor.home_mode') is hass.get_state('binary_sensor.home_mode'), which is far simpler to understand - see http://appdaemon.readthedocs.io/en/latest/APIREFERENCE.html#get-state

The best tutorial to start with AD is probably here

1 Like