hassapi — Home Assistant Python API Client
A Python client for the Home Assistant REST API and WebSocket API.
Installation
pip install hassapi
Quick Start
from hassapi import Hass
hass = Hass(hassurl="http://192.168.1.10:8123", token="YOUR_LONG_LIVED_TOKEN")
# Control devices
hass.turn_on("light.living_room")
hass.turn_off("switch.coffee_machine")
hass.toggle("light.bedroom")
# Read state
state = hass.get_state("sensor.temperature")
print(state.state) # e.g. "21.5"
print(state.attributes) # dict of entity attributes
print(state.last_changed) # datetime
# Run a script
hass.run_script("good_morning")
Configuration
Constructor arguments
hass = Hass(
hassurl="http://IP_ADDRESS:8123",
token="YOUR_HASS_TOKEN",
verify=True, # Set to False to skip TLS certificate verification (e.g. self-signed certs)
timeout=3, # Request timeout in seconds
)
Environment variables
If hassurl or token are not passed, the client reads from environment variables:
export HASS_URL=http://192.168.1.10:8123
export HASS_TOKEN=your_long_lived_token
hass = Hass() # reads from env
API Reference
States
# Get the state of a single entity
state: State = hass.get_state("light.living_room")
# Get states of all entities
states: StateList = hass.get_states()
# Set state and optionally attributes of an entity
state: State = hass.set_state("sensor.my_sensor", state="42", attributes={"unit": "°C"})
State object fields:
| Field | Type | Description |
|---|---|---|
entity_id |
str |
Entity identifier |
state |
str |
Current state value |
attributes |
dict |
Entity attributes |
last_changed |
datetime |
When state last changed |
last_updated |
datetime |
When state was last updated |
context |
Context |
Context (id, parent_id, user_id) |
Services
# Call any service
hass.call_service("turn_on", entity_id="light.living_room", brightness=200)
# Convenience methods
hass.turn_on("light.living_room")
hass.turn_off("switch.fan")
hass.toggle("light.bedroom")
# Covers
hass.open_cover("cover.garage")
hass.close_cover("cover.garage")
hass.set_cover_position("cover.garage", position=50)
# Input entities
hass.select_option("input_select.mode", option="Away")
hass.set_value("input_number.brightness", value=75)
# Scripts and shell commands
hass.run_script("good_morning")
hass.run_shell_command("backup")
# List all available services
services: ServiceList = hass.get_services()
Events
# List all registered event listeners
events: EventList = hass.get_events()
# Fire an event
hass.fire_event("MY_CUSTOM_EVENT")
hass.fire_event("MY_CUSTOM_EVENT", event_data={"key": "value"})
Real-time events (WebSocket)
Subscriptions are non-blocking — they start a background thread and return immediately.
import time
def on_change(event):
data = event["data"]
print(data["entity_id"], data["old_state"]["state"], "->", data["new_state"]["state"])
# Watch a specific entity for state changes
hass.subscribe_to_state_changes(on_change, entity_id="light.living_room")
# Watch all state changes
hass.subscribe_to_state_changes(on_change)
# Watch a specific event type
hass.subscribe_to_events(on_change, event_type="call_service")
# Watch all events
hass.subscribe_to_events(on_change)
# Keep the main thread alive while listening
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
hass.unsubscribe()
The client reconnects automatically on dropped connections using exponential backoff, and gives up after 5 consecutive failures.
Templates
# Render a Jinja2 template
result: str = hass.render_template("{{ states('sensor.temperature') }}")
Getting a Long-Lived Access Token
- Open Home Assistant in your browser
- Go to your profile: Settings → Your profile → Security
- Scroll to Long-lived access tokens and create one
Contributing
# Install dev dependencies and run all checks
pip install tox
tox
Individual environments:
tox -e pytest # run tests
tox -e flake8 # lint
tox -e mypy # type check
tox -e format # auto-format code
tox -e format-check # check formatting without modifying
License
Metadata
Release files for hassapi 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hassapi-0.3.0.tar.gz | 13.8 kB | Without provenance | Details for hassapi-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hassapi-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for hassapi-0.3.0-py3-none-any.whl |
Total release size: 28.9 kB
Release files / hassapi-0.3.0.tar.gz
| Download URL | hassapi-0.3.0.tar.gz |
|---|---|
| Size | 13.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
27aab84c036014bc95a02db5b0cc41b7cf5b35d16b406a8b5f0c2294b29dcef5
|
|
BLAKE2b-256 checksum How to use checksums |
4feb9865d286c083abce3e2e4b1be68fce893b2af39bed954e925c568e25c033
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / hassapi-0.3.0-py3-none-any.whl
| Download URL | hassapi-0.3.0-py3-none-any.whl |
|---|---|
| Size | 15.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9aeb4e63f175dda703bb488cfe5a90ac3ff8a6015e6570bd31a56e86b22124cf
|
|
BLAKE2b-256 checksum How to use checksums |
5400477dc8f60e3c2f4143c889dbed2774f151031635a3e3165e620cd7f7b4dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Security for hassapi 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.