streamdeck_homeassistant/config.rs
1//! Configuration types and functions for the StreamDeck HomeAssistant integration.
2
3use serde::{Deserialize, Serialize};
4
5/// Main configuration for the HomeAssistant integration.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(deny_unknown_fields, rename_all = "snake_case")]
8pub struct HomeAssistantConfig {
9 /// WebSocket URL for the HomeAssistant instance (e.g., "ws://192.168.0.1:8123/api/websocket")
10 pub url: String,
11 /// Root menu configuration
12 pub menu: HomeAssistantMenu,
13}
14
15/// Represents a menu in the StreamDeck interface.
16#[derive(Debug, Clone, Serialize, Deserialize)]
17#[serde(deny_unknown_fields, rename_all = "snake_case")]
18pub struct HomeAssistantMenu {
19 /// Display name for the menu
20 pub name: String,
21 /// List of buttons in this menu
22 pub buttons: Vec<HomeAssistantButton>,
23}
24
25/// Represents different types of buttons that can be placed on the StreamDeck.
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(tag = "type", rename_all = "snake_case")]
28pub enum HomeAssistantButton {
29 /// A simple on/off switch
30 Switch { entity_id: String, name: String },
31 /// An RGB light with color control
32 RgbLight { entity_id: String, name: String },
33 /// A submenu containing more buttons
34 Menu(HomeAssistantMenu),
35}
36
37/// Loads a configuration from a YAML file.
38///
39/// # Arguments
40///
41/// * `arg` - Path to the YAML configuration file
42///
43/// # Returns
44///
45/// The parsed configuration or an error
46///
47/// # Example
48///
49/// ```no_run
50/// use streamdeck_homeassistant::config;
51///
52/// let config = config::load_config("config.yaml").expect("Failed to load config");
53/// println!("Connected to HomeAssistant at: {}", config.url);
54/// ```
55pub fn load_config<S: Into<String>>(
56 arg: S,
57) -> Result<HomeAssistantConfig, Box<dyn std::error::Error>> {
58 let file = std::fs::File::open(arg.into())?;
59 let reader = std::io::BufReader::new(file);
60 let config: HomeAssistantConfig = serde_yaml::from_reader(reader)?;
61 Ok(config)
62}