Skip to main content

Event-driven Home Assistant automations in Python

Project description

hassreactor

Event-driven Home Assistant automations in Python. No YAML, no Node-RED, no AppDaemon — just Python.

Why

Home Assistant has a powerful automation engine, but it lives in YAML or a UI. Sometimes you just want to write a Python script:

  • "If living room temp > 28°C, turn on fan"
  • "If front door opens, send me a Telegram message"
  • "Every hour, log the temperature"

hassreactor lets you write these as plain Python files using WebSocket events — no polling, no complex setup.

Install

pip install hassreactor

Quick Start

Create a file automations.py:

from hassreactor import Reactor

app = Reactor("http://homeassistant:8123", "your-long-lived-token")

@app.when("sensor.temperatura_salotto", above=28)
async def accendi_ventilatore(event):
    """When temp goes above 28°C, turn on the fan."""
    await app.fan.turn_on(entity_id="fan.ventilatore")

@app.when("binary_sensor.porta_ingresso", to="on")
async def porta_aperta(event):
    """When front door opens, notify."""
    await app.notify.telegram(message="Porta d'ingresso aperta!")

@app.schedule("every 1h")
async def report():
    temp = await app.get_state("sensor.temperatura_salotto")
    print(f"Current temperature: {temp}°C")

if __name__ == "__main__":
    app.run()

Run it:

python automations.py

Trigger Types

Trigger Description
@app.when(entity, above=N) Numeric value crosses ABOVE threshold
@app.when(entity, below=N) Numeric value crosses BELOW threshold
@app.when(entity, to="on") State changes TO an exact value
@app.when(entity, changes=True) ANY state change
@app.schedule("every 30m") Run every 30 minutes
@app.schedule("every 2h") Run every 2 hours
@app.schedule("0 9 * * *") Cron expression (every day at 9am)

Calling Services

Any HA service is available as a method on the domain:

await app.light.turn_on(entity_id="light.kitchen", brightness=128)
await app.climate.set_temperature(entity_id="climate.home", temperature=22)
await app.switch.toggle(entity_id="switch.pump")
await app.notify.telegram(message="Hello!")

How It Works

hassreactor connects to Home Assistant via WebSocket and subscribes to state_changed events. When an entity you're watching changes state, your function runs instantly — no polling, no sleep loops.

Service calls use the REST API.

Only dependency: aiohttp.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hassreactor-0.2.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hassreactor-0.2.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file hassreactor-0.2.0.tar.gz.

File metadata

  • Download URL: hassreactor-0.2.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for hassreactor-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f7eddb61c1f309cbade8733fd1d315e88adeeeb221b490ca5a2cdc6242902608
MD5 104fb48e0f262b39e08f8a9011d41a14
BLAKE2b-256 59c57d69b16b43f20dd4e74b5f91e6a26169af12d2740ea32b473ae6f577ae99

See more details on using hashes here.

File details

Details for the file hassreactor-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: hassreactor-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for hassreactor-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1daf1a0617d5f6627d19a8b68e9fde9b2ff05934193516c1264d7aed1556a2d3
MD5 30cd87164205507b64f1a0002638cf43
BLAKE2b-256 48913946eaf7f79dde075361a12c1fb1c423b89b81f3cf3eea294afc08854711

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page