by dingledork69 · 3y ago · 0 comments
An example script:
@state_trigger("security.rear_motion == '1' or security.side_motion == '1'")
@time_active("range(sunset - 20min, sunrise + 20min)")
def motion_light_rear():
"""Turn on rear light for 5 minutes when there is motion and it's dark"""
task.unique("motion_light_rear")
log.info(f"triggered; turning on the light")
if light.outside_rear != "on":
light.turn_on(entity_id="light.outside_rear", brightness=255)
task.sleep(300)
light.turn_off(entity_id="light.outside_rear")
It's what made me ditch node red after going down the same path.The Home Assistant community seems to have the entire gambit of people who can't code at all, the people who never coded professionally and make riduclously complex things like the fabled Excel accountants, to the professional developers doing stuff for fun, but nobody actually shares anything worthwhile outside of awful YAML and youtubes about awful YAML.
But i have a nagging feeling that i should probably use PyScript instead.. If anyone has a more thorough analysis i'm open to hear it..
Auto complete for discovery just refuses to work in PyCharm and VSCode for me. The way PyScript is setup seems to be a strange bespoke system that kind of breaks the import system between files, type safety only sort of works sometimes, and just other quirks where I seem to just have to get used to all of the red underlines if I want to use an IDE as part of my development process.
But it still seems to be best option for allowing me to use any libraries in the ecosystem, gets rid of boilerplate, and the author is actively maintaining it. I didn't really find any other viable options outside of Python that would let me actually code my automations without yaml or a GUI.
I was very excited when I found a Kotlin implementation, KHome, but by the time I found it, it was already a seemingly dead project.
My PyCharm just shows those types as unknown, not as errors. I get auto completion & type checking for all the code I write myself.
It's also important to note that PyScript isn't regular python. Instead it's an interpreted script[1] that is compatible with the Python syntax. This is unfortunate, but enables most of the magic of PyScript which otherwise would not be practical.
[1] https://github.com/custom-components/pyscript/blob/master/cu...
Even if I use a UI for making a modification sometimes, I want to be able to commit a clean diff like:
sensorLightBackdoor:
- timeActive: range(sunset - 20min, sunrise + 20min)
+ timeActive: range(sunset, sunrise)
Or whatever. Which obviously I can do with python, as long as it's stored neatly in config in a .py file, or even if embedded in HA yaml or something?In a `pyscript/*.py` file, so you can open it with a full blown IDE and use all your usual comforts, and commit the results to git.