1
2
3
4
5
6
7
8
9
10
11
12
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
83
84
85
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
150
151
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use clap::{CommandFactory, Parser, Subcommand};
use homeassistant_cli::output::{OutputConfig, OutputFormat, exit_codes};
use homeassistant_cli::{api, commands};
#[derive(Parser)]
#[command(
name = "ha",
version,
about = "CLI for Home Assistant",
arg_required_else_help = true
)]
struct Cli {
/// Config profile to use [env: HA_PROFILE]
#[arg(long, env = "HA_PROFILE", global = true)]
profile: Option<String>,
/// Output format [env: HA_OUTPUT]
#[arg(long, value_enum, env = "HA_OUTPUT", global = true)]
output: Option<OutputFormat>,
/// Suppress non-data output
#[arg(long, global = true)]
quiet: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Read and watch entity states
#[command(subcommand, arg_required_else_help = true)]
Entity(EntityCommand),
/// Call and list services
#[command(subcommand, arg_required_else_help = true)]
Service(ServiceCommand),
/// Fire and watch events
#[command(subcommand, arg_required_else_help = true)]
Event(EventCommand),
/// Set up credentials interactively (or print JSON schema for agents)
Init {
#[arg(long)]
profile: Option<String>,
},
/// Manage configuration
#[command(subcommand, arg_required_else_help = true)]
Config(ConfigCommand),
/// Print machine-readable schema of all commands
Schema,
/// Generate shell completions
Completions {
/// Shell to generate completions for
shell: clap_complete::Shell,
},
}
#[derive(Subcommand)]
enum EntityCommand {
/// Get the current state of an entity
Get { entity_id: String },
/// List all entities, optionally filtered by domain, state, or count
List {
#[arg(long)]
domain: Option<String>,
/// Filter by state value (e.g. on, off, unavailable)
#[arg(long)]
state: Option<String>,
/// Maximum number of results to show
#[arg(long)]
limit: Option<usize>,
},
/// Stream state changes for an entity
Watch { entity_id: String },
}
#[derive(Subcommand)]
enum ServiceCommand {
/// Call a service
Call {
/// Service in domain.service format (e.g. light.turn_on)
service: String,
/// Target entity ID
#[arg(long)]
entity: Option<String>,
/// Additional service data as JSON
#[arg(long)]
data: Option<String>,
},
/// List available services
List {
#[arg(long)]
domain: Option<String>,
},
}
#[derive(Subcommand)]
enum EventCommand {
/// Fire an event
Fire {
event_type: String,
/// Event data as JSON
#[arg(long)]
data: Option<String>,
},
/// Stream events
Watch {
/// Filter by event type
event_type: Option<String>,
},
}
#[derive(Subcommand)]
enum ConfigCommand {
/// Show current configuration
Show,
/// Set a config value
Set { key: String, value: String },
}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
let out = OutputConfig::new(cli.output, cli.quiet);
match cli.command {
Command::Init { profile } => {
commands::init::init(profile).await;
}
Command::Schema => {
commands::schema::print_schema();
}
Command::Completions { shell } => {
clap_complete::generate(shell, &mut Cli::command(), "ha", &mut std::io::stdout());
}
Command::Config(cmd) => match cmd {
ConfigCommand::Show => {
commands::config::show(&out, cli.profile.as_deref());
}
ConfigCommand::Set { key, value } => {
commands::config::set(&out, cli.profile.as_deref(), &key, &value);
}
},
command => {
let cfg = match homeassistant_cli::config::Config::load(cli.profile.clone()) {
Ok(c) => c,
Err(e) => {
eprintln!("{e}");
std::process::exit(exit_codes::CONFIG_ERROR);
}
};
let client = api::HaClient::new(&cfg.url, &cfg.token);
let result = match command {
Command::Entity(cmd) => match cmd {
EntityCommand::Get { entity_id } => {
commands::entity::get(&out, &client, &entity_id).await
}
EntityCommand::List {
domain,
state,
limit,
} => {
commands::entity::list(
&out,
&client,
domain.as_deref(),
state.as_deref(),
limit,
)
.await
}
EntityCommand::Watch { entity_id } => {
commands::entity::watch(&out, &client, &entity_id).await
}
},
Command::Service(cmd) => match cmd {
ServiceCommand::Call {
service,
entity,
data,
} => {
commands::service::call(
&out,
&client,
&service,
entity.as_deref(),
data.as_deref(),
)
.await
}
ServiceCommand::List { domain } => {
commands::service::list(&out, &client, domain.as_deref()).await
}
},
Command::Event(cmd) => match cmd {
EventCommand::Fire { event_type, data } => {
commands::event::fire(&out, &client, &event_type, data.as_deref()).await
}
EventCommand::Watch { event_type } => {
commands::event::watch(&out, &client, event_type.as_deref()).await
}
},
Command::Init { .. }
| Command::Schema
| Command::Config(_)
| Command::Completions { .. } => unreachable!(),
};
if let Err(e) = result {
let code = exit_codes::for_error(&e);
out.print_error(&e);
std::process::exit(code);
}
}
}
}