Framework for building easy Home Assistant devices
Project description
jhomeassistant
A Python library for Home Assistant MQTT device discovery based on jmqtt connections.
Installation
pip install jhomeassistant
Requirements
- Python
>=3.8 jmqttpython-slugify
Quickstart
from jmqtt import MQTTBuilderV3
from jhomeassistant import HomeAssistantConnection, HomeAssistantDevice
from jhomeassistant.entities import HomeAssistantEntityBase
from jhomeassistant.types import Component
# 1) Build MQTT connection
connection = (
MQTTBuilderV3(host="localhost", app_name="my-tool")
.instance_id("main")
.availability("my-tool/status")
.fast_build()
)
# 2) Wrap it for Home Assistant discovery
ha = HomeAssistantConnection(connection)
ha.origin.name = "my-tool"
# 3) Define an entity by subclassing HomeAssistantEntityBase
class TemperatureEntity(HomeAssistantEntityBase):
def __init__(self):
super().__init__(Component.SENSOR, "Temperature")
entity = TemperatureEntity()
device = HomeAssistantDevice("Kitchen Sensor").add_entities(entity)
# 4) Register and run discovery + scheduler loop
ha.add_devices(device)
# Blocking mode (current default behavior)
ha.run()
# Non-blocking mode with runtime handle (alternative)
# runtime = ha.run(blocking=False)
# print(runtime.is_running)
# runtime.stop(timeout=5.0)
# runtime.join(timeout=5.0)
Core Concepts
HomeAssistantConnection
- Holds a
jmqttconnection (MQTTConnectionV3orMQTTConnectionV5) - Builds and publishes Home Assistant device discovery payloads
- Subscribes to
homeassistant/statusand dispatches birth/death hooks to entities
Main configuration:
discovery_prefix("homeassistant")origin.name,origin.sw_version,origin.urlavailability(connection-level availability inheritance)qos,encoding(connection-level defaults inherited by devices)
HomeAssistantDevice
- Represents one Home Assistant device block in discovery
- Auto-detects runtime device facts using
jmqtt.client_identity.facts - Derives stable identifiers for discovery and entity unique IDs
- Supports
prevent_device_merge=Trueto avoid connection-based merge identifiers
Common fields:
manufacturer,model,model_idhw_version,sw_versionvia_device,configuration_urlqos,encodingavailability
HomeAssistantEntityBase
- Base class for entities that belong to a
HomeAssistantDevice - Provides deterministic
identifierand discovery payload base - Supports scheduled publisher functions via
add_schedule(interval, function) - Lifecycle hooks:
home_assistant_birth(connection)home_assistant_death(connection)
Discovery Output
HomeAssistantConnection.discovery_text() returns formatted discovery topics + payloads,
useful for debugging and tests.
Runtime Control
HomeAssistantConnection.run(...) supports both runtime styles:
ha.run()orha.run(blocking=True): runs discovery + HA status subscription + scheduler in the caller thread.runtime = ha.run(blocking=False): starts the same runtime in a background thread and returnsHomeAssistantRuntime.
Runtime handle API:
runtime.is_runningruntime.last_errorruntime.stop(timeout=...)runtime.join(timeout=...) -> bool
Notes
origin.nameis required by device-based discovery payload validation.- If not set explicitly, device name is used as fallback.
- Entity names should be unique within a device to avoid unique_id collisions.
License
MIT (see LICENSE).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jhomeassistant-0.3.2.tar.gz.
File metadata
- Download URL: jhomeassistant-0.3.2.tar.gz
- Upload date:
- Size: 33.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66fa9e1c0202b469cccac0337195cb65a397e90419babec0b6c2cf748e9c25d6
|
|
| MD5 |
cef53dd1d4b31b0ae6b24c0ea4d679f1
|
|
| BLAKE2b-256 |
b385c744cadb0cd2bb68fe900ff3f903aaf25b1d3471cc53e9ede20f6e2ecb86
|
File details
Details for the file jhomeassistant-0.3.2-py3-none-any.whl.
File metadata
- Download URL: jhomeassistant-0.3.2-py3-none-any.whl
- Upload date:
- Size: 65.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80b7595b2bb28aaeb490766735c4f844facda9f4b2913b530188f1e0f648f839
|
|
| MD5 |
ab4f98c9b7323f070f8af48d4a94df0f
|
|
| BLAKE2b-256 |
3e921c385ccb2c36c14f23c0fdcc58a649dee6f3ef7375183ff279b64a11f2c3
|