API Reference
This reference documentation is automatically generated from the source code docstrings.
CLI Application
configure(host=typer.Option(None, help='MQTT Broker Host'), port=typer.Option(None, help='MQTT Broker Port'), username=typer.Option(None, help='MQTT Username'), password=typer.Option(None, help='MQTT Password'), reset=typer.Option(False, '--reset', help='Reset configuration to defaults'), show=typer.Option(False, '--show', help='Show current configuration'))
Configure the MQTT broker connection settings.
Interactive prompts are used if no arguments are provided.
Saved configuration is stored in ~/.config/ha-cli/config.json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
Optional[str]
|
MQTT Broker Host. |
Option(None, help='MQTT Broker Host')
|
port
|
Optional[int]
|
MQTT Broker Port. |
Option(None, help='MQTT Broker Port')
|
username
|
Optional[str]
|
MQTT Username. |
Option(None, help='MQTT Username')
|
password
|
Optional[str]
|
MQTT Password. |
Option(None, help='MQTT Password')
|
reset
|
bool
|
Reset configuration to defaults. |
Option(False, '--reset', help='Reset configuration to defaults')
|
show
|
bool
|
Show current configuration. |
Option(False, '--show', help='Show current configuration')
|
Source code in src/hamqtt/main.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
register(unique_id=typer.Option(..., help='Unique ID for the entity'), name=typer.Option(..., help='Readable name for the entity'), device_class=typer.Option(None, help='Device class (e.g., temperature, humidity)'), unit=typer.Option(None, help='Unit of measurement'), component=typer.Option('sensor', help='HA Component (sensor, binary_sensor, etc.)'), state_topic=typer.Option(None, help='Custom state topic. Defaults to ha-cli/<unique_id>/state'), device_name=typer.Option(None, help='Device Name for grouping'), dry_run=typer.Option(False, help='Print payload instead of sending'))
Register a new entity with Home Assistant via MQTT Discovery.
Publishes a discovery configuration payload to the configured discovery topic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unique_id
|
str
|
Unique ID for the entity. |
Option(..., help='Unique ID for the entity')
|
name
|
str
|
Readable name for the entity. |
Option(..., help='Readable name for the entity')
|
device_class
|
Optional[str]
|
Device class (e.g., temperature, humidity). |
Option(None, help='Device class (e.g., temperature, humidity)')
|
unit
|
Optional[str]
|
Unit of measurement. |
Option(None, help='Unit of measurement')
|
component
|
str
|
HA Component (sensor, binary_sensor, etc.). |
Option('sensor', help='HA Component (sensor, binary_sensor, etc.)')
|
state_topic
|
Optional[str]
|
Custom state topic. Defaults to ha-cli/ |
Option(None, help='Custom state topic. Defaults to ha-cli/<unique_id>/state')
|
device_name
|
Optional[str]
|
Device Name for grouping entities. |
Option(None, help='Device Name for grouping')
|
dry_run
|
bool
|
If True, print payload instead of sending. |
Option(False, help='Print payload instead of sending')
|
Source code in src/hamqtt/main.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
send(unique_id=typer.Option(..., help='Unique ID of the entity'), state=typer.Option(..., help='State/Value to send'), topic=typer.Option(None, help='Override state topic. Defaults to ha-cli/<unique_id>/state'), dry_run=typer.Option(False, help='Print payload instead of sending'))
Send a state update for a registered entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unique_id
|
str
|
Unique ID of the entity. |
Option(..., help='Unique ID of the entity')
|
state
|
str
|
State/Value to send. |
Option(..., help='State/Value to send')
|
topic
|
Optional[str]
|
Override state topic. Defaults to ha-cli/ |
Option(None, help='Override state topic. Defaults to ha-cli/<unique_id>/state')
|
dry_run
|
bool
|
If True, print payload instead of sending. |
Option(False, help='Print payload instead of sending')
|
Source code in src/hamqtt/main.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
Configuration
Config
Bases: BaseModel
Global application configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
mqtt |
MqttConfig
|
The MQTT connection configuration. |
Source code in src/hamqtt/config.py
33 34 35 36 37 38 39 40 41 | |
MqttConfig
Bases: BaseModel
Configuration for the MQTT connection.
Attributes:
| Name | Type | Description |
|---|---|---|
host |
str
|
The hostname or IP address of the MQTT broker. |
port |
int
|
The port number of the MQTT broker. Defaults to 1883. |
username |
Optional[str]
|
The username for authentication. Defaults to None. |
password |
Optional[str]
|
The password for authentication. Defaults to None. |
discovery_prefix |
str
|
The prefix used for Home Assistant discovery topics. Defaults to "homeassistant". |
Source code in src/hamqtt/config.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
delete_config()
Delete the configuration file.
Source code in src/hamqtt/config.py
108 109 110 111 112 113 | |
load_config()
Load the application configuration from the config file.
If the configuration file does not exist, it attempts to migrate from the legacy location. If migration is not possible or file doesn't exist, a default configuration is returned with 'localhost' as the MQTT host.
Returns:
| Name | Type | Description |
|---|---|---|
Config |
Config
|
The loaded or default configuration. |
Source code in src/hamqtt/config.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
save_config(config)
Save the application configuration to the config file.
Creates the configuration directory if it does not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Config
|
The configuration object to save. |
required |
Source code in src/hamqtt/config.py
94 95 96 97 98 99 100 101 102 103 104 105 | |
Discovery Models
DeviceInfo
Bases: BaseModel
Information about the device associated with the entity.
Attributes:
| Name | Type | Description |
|---|---|---|
identifiers |
list[str]
|
A list of IDs that uniquely identify the device. |
name |
Optional[str]
|
The name of the device. |
model |
Optional[str]
|
The model of the device. |
manufacturer |
Optional[str]
|
The manufacturer of the device. |
sw_version |
Optional[str]
|
The software version of the device. |
Source code in src/hamqtt/discovery.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
DiscoveryEntity
Bases: BaseModel
Represents an entity for Home Assistant MQTT Discovery.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Optional[str]
|
The name of the entity. |
unique_id |
str
|
A unique identifier for the entity. |
object_id |
Optional[str]
|
Used to generate the entity ID in Home Assistant. |
device_class |
Optional[str]
|
The class of the device (e.g., 'temperature'). |
state_topic |
str
|
The MQTT topic where the entity's state is published. |
unit_of_measurement |
Optional[str]
|
The unit of measurement (e.g., '°C'). |
device |
Optional[DeviceInfo]
|
Information about the device the entity belongs to. |
icon |
Optional[str]
|
The icon to display in the frontend. |
value_template |
Optional[str]
|
Template to extract the value from the payload. |
payload_on |
Optional[str]
|
The payload that represents the 'on' state. |
payload_off |
Optional[str]
|
The payload that represents the 'off' state. |
Source code in src/hamqtt/discovery.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
get_topic(prefix='homeassistant', component='sensor')
Generates the discovery topic.
Format:
Source code in src/hamqtt/discovery.py
56 57 58 59 60 61 62 63 64 | |
MQTT Client
MqttClient
A wrapper around paho.mqtt.client for handling MQTT connections and publishing.
This class supports usage as a context manager for automatic connection and disconnection.
Source code in src/hamqtt/mqtt_client.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
__init__(config)
Initialize the MQTT client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
MqttConfig
|
The MQTT configuration. |
required |
Source code in src/hamqtt/mqtt_client.py
20 21 22 23 24 25 26 27 28 29 30 31 | |
connect()
Connect to the MQTT broker and start the network loop.
Raises:
| Type | Description |
|---|---|
error
|
If the connection fails. |
OSError
|
If the connection fails. |
Source code in src/hamqtt/mqtt_client.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
disconnect()
Stop the network loop and disconnect from the MQTT broker.
Source code in src/hamqtt/mqtt_client.py
48 49 50 51 52 53 | |
publish(topic, payload, retain=False)
Publish a message to an MQTT topic.
If the payload is not a string, it is serialized to JSON. This method blocks until the message is published.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
topic
|
str
|
The MQTT topic to publish to. |
required |
payload
|
Any
|
The message payload. |
required |
retain
|
bool
|
Whether to retain the message. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
mqtt.MQTTMessageInfo: Information about the published message. |
Source code in src/hamqtt/mqtt_client.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |