Control lights from python program

I want to set the color of RGB lights from within a python program (running outside HA). How can I do that? Is there a library which I can import into python? Or do I have the call the REST api described here ?

RestAPI is certainly the most straight forward and most portable. Just import “requests” library and it should be all you need. Generate a Long Lived Access Token for this app and use that one (so you can revoke permissions if the key gets leaked)

If you are wanting something more powerful, you can use AppDaemon:

AppDaemon is more like Node Red in that it is a separate thing that gets events directly from Home Assistant and your little “apps” handle these events, the same way Node Red flows handle events in their own way.

AppDaemon is a subsystem to complement Home Assistant’s Automation and Scripting components. AppDaemon, is a Python daemon that consumes events from Home Assistant and feeds them to snippets of Python code called Apps . An App is a Python class that is instantiated possibly multiple times from AppDaemon and registers callbacks for various system events. It is also able to inspect and set state and call services. The API provides a rich environment suited to home automation tasks that can also leverage all the power of Python.

But, this thing is essentially a service that is running…somewhere…getting updates from all of Home Assistant at all times. It is most certainly overkill for “turning a light a certain color”, but the most powerful when it comes to creating your own custom things without having to create an actual custom_component.

1 Like