| Crates.io | ha-rs |
| lib.rs | ha-rs |
| version | 0.1.1 |
| created_at | 2025-12-26 11:42:25.595123+00 |
| updated_at | 2025-12-27 16:29:23.604759+00 |
| description | A command-line interface (CLI) tool for interacting with Home Assistant. |
| homepage | |
| repository | https://github.com/sachinkum0009/ha-rs.git |
| max_upload_size | |
| id | 2005611 |
| size | 58,661 |
A Rust library and command-line interface (CLI) tool for interacting with Home Assistant.
Add this to your Cargo.toml:
[dependencies]
ha-rs = "0.1.0"
tokio = { version = "1.48", features = ["full"] }
cargo install --path . --bin ha_cli
Or build from source:
cargo build --release
./target/release/ha_cli --help
use ha_rs::client::HaClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize the client
let ha_url = "http://homeassistant.local:8123";
let ha_token = "your_long_lived_access_token";
let client = HaClient::new(ha_url.to_string(), ha_token.to_string());
// Get entity state
let entity = client.get_state("switch.smart_plug").await?;
println!("Status: {}", entity.state);
println!("Entity ID: {}", entity.entity_id);
println!("Last updated: {}", entity.last_updated);
// Turn on a device
client.set_state("switch.smart_plug", true).await?;
println!("Device turned on");
// Turn off a device
client.set_state("switch.smart_plug", false).await?;
println!("Device turned off");
Ok(())
}
Set required environment variables:
export HA_URL="http://homeassistant.local:8123"
export HA_TOKEN="your_long_lived_access_token"
Check device status:
ha_cli status
Toggle device state:
ha_cli toggle
HaClientThe main client for interacting with Home Assistant.
new(base_url: String, token: String) -> SelfCreates a new Home Assistant client.
Parameters:
base_url: The base URL of your Home Assistant instance (e.g., "http://homeassistant.local:8123")token: Your Home Assistant long-lived access tokenExample:
let client = HaClient::new(
"http://homeassistant.local:8123".to_string(),
"your_token".to_string()
);
async get_state(&self, entity_id: &str) -> Result<Entity, Error>Retrieves the current state of an entity.
Parameters:
entity_id: The entity ID (e.g., "switch.smart_plug", "light.bedroom")Returns:
Ok(Entity): Entity information including state, attributes, and timestampsErr(Error): Network or API errorExample:
let entity = client.get_state("switch.smart_plug").await?;
println!("State: {}", entity.state);
async set_state(&self, entity_id: &str, turn_on: bool) -> Result<String, Error>Controls a device by turning it on or off.
Parameters:
entity_id: The entity ID (e.g., "switch.smart_plug", "light.bedroom")turn_on: true to turn on, false to turn offReturns:
Ok(String): Response from Home Assistant APIErr(Error): Network or API errorExample:
// Turn on
client.set_state("switch.smart_plug", true).await?;
// Turn off
client.set_state("switch.smart_plug", false).await?;
EntityRepresents a Home Assistant entity with its current state and metadata.
Fields:
entity_id: String - The entity identifierstate: String - Current state (e.g., "on", "off", "unavailable")attributes: HashMap<String, serde_json::Value> - Entity attributeslast_changed: String - Timestamp of last state changelast_reported: String - Timestamp of last reportlast_updated: String - Timestamp of last updatecontext: Context - Context informationMIT
Sachin Kumar sachinkumar.ar97@gmail.com