1use std::path::{Path, PathBuf};
4
5use serde::Deserialize;
6
7#[derive(Debug, Clone, Default, Deserialize)]
9#[cfg_attr(feature = "schema", derive(serde::Serialize, schemars::JsonSchema))]
10#[serde(deny_unknown_fields)]
11pub struct Config {
12 #[serde(default)]
14 pub mqtt: MqttConfig,
15 #[serde(default)]
17 pub homeassistant: HomeAssistantConfig,
18 #[serde(default)]
20 pub matter: MatterConfig,
21 #[serde(default)]
23 pub data: DataConfig,
24}
25
26impl Config {
27 pub fn load(path: &Path) -> Result<Self, String> {
29 let text = std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
30 toml::from_str(&text).map_err(|e| format!("{}: {e}", path.display()))
31 }
32}
33
34#[derive(Debug, Clone, Deserialize)]
36#[cfg_attr(feature = "schema", derive(serde::Serialize, schemars::JsonSchema))]
37#[serde(deny_unknown_fields, default)]
38pub struct MqttConfig {
39 pub host: String,
41 pub port: u16,
43 pub username: Option<String>,
45 pub password_file: Option<PathBuf>,
47 pub client_id: String,
49 pub base_topic: String,
51 pub keepalive: u64,
53}
54
55impl Default for MqttConfig {
56 fn default() -> Self {
57 MqttConfig {
58 host: "localhost".into(),
59 port: 1883,
60 username: None,
61 password_file: None,
62 client_id: "matter-bridge".into(),
63 base_topic: "matter".into(),
64 keepalive: 30,
65 }
66 }
67}
68
69#[derive(Debug, Clone, Deserialize)]
71#[cfg_attr(feature = "schema", derive(serde::Serialize, schemars::JsonSchema))]
72#[serde(deny_unknown_fields, default)]
73pub struct HomeAssistantConfig {
74 pub enabled: bool,
76 pub discovery_prefix: String,
78 pub status_topic: String,
80}
81
82impl Default for HomeAssistantConfig {
83 fn default() -> Self {
84 HomeAssistantConfig {
85 enabled: true,
86 discovery_prefix: "homeassistant".into(),
87 status_topic: "homeassistant/status".into(),
88 }
89 }
90}
91
92#[derive(Debug, Clone, Deserialize)]
94#[cfg_attr(feature = "schema", derive(serde::Serialize, schemars::JsonSchema))]
95#[serde(deny_unknown_fields, default)]
96pub struct MatterConfig {
97 pub port: u16,
99 pub interface: Option<String>,
101 pub fabric_id: Option<u64>,
104 pub vendor_id: u16,
106 pub allow_unverified_attestation: bool,
109 pub max_report_interval: u16,
111 pub discovery_timeout: u16,
113 pub bluetooth: Option<String>,
116 pub thread_dataset_url: Option<String>,
120 pub ota_dir: Option<PathBuf>,
123}
124
125impl Default for MatterConfig {
126 fn default() -> Self {
127 let c = matter_controller::Config::new("");
128 MatterConfig {
129 port: c.port,
130 interface: None,
131 fabric_id: None,
132 vendor_id: c.vendor_id,
133 allow_unverified_attestation: c.allow_unverified_attestation,
134 max_report_interval: c.max_report_interval,
135 discovery_timeout: 30,
136 bluetooth: None,
137 thread_dataset_url: None,
138 ota_dir: None,
139 }
140 }
141}
142
143#[derive(Debug, Clone, Deserialize)]
145#[cfg_attr(feature = "schema", derive(serde::Serialize, schemars::JsonSchema))]
146#[serde(deny_unknown_fields, default)]
147pub struct DataConfig {
148 pub dir: PathBuf,
151}
152
153impl Default for DataConfig {
154 fn default() -> Self {
155 DataConfig {
156 dir: "/var/lib/matter-bridge".into(),
157 }
158 }
159}
160
161impl Config {
162 pub fn controller(&self) -> matter_controller::Config {
164 let mut c = matter_controller::Config::new(&self.data.dir);
165 c.port = self.matter.port;
166 c.interface = self.matter.interface.clone();
167 c.fabric_id = self.matter.fabric_id;
168 c.vendor_id = self.matter.vendor_id;
169 c.allow_unverified_attestation = self.matter.allow_unverified_attestation;
170 c.max_report_interval = self.matter.max_report_interval;
171 c.bluetooth = self.matter.bluetooth.clone();
172 if let Some(dir) = &self.matter.ota_dir {
173 c.ota_dir = dir.clone();
174 }
175 c
176 }
177}
178
179#[cfg(test)]
180mod tests {
181 use super::*;
182
183 #[test]
184 fn example_parses() {
185 let text = include_str!("../../../config.example.toml");
186 let config: Config = toml::from_str(text).unwrap();
187 assert_eq!(config.mqtt.base_topic, "matter");
188 assert!(config.homeassistant.enabled);
189 }
190
191 #[test]
192 fn empty_is_all_defaults() {
193 let config: Config = toml::from_str("").unwrap();
194 assert_eq!(config.mqtt.port, 1883);
195 assert_eq!(config.matter.port, 5541);
196 }
197
198 #[test]
199 fn unknown_keys_are_refused() {
200 assert!(toml::from_str::<Config>("[mqtt]\nhots = \"x\"").is_err());
201 }
202}