Devices and some settings are now loaded from the config file instead of hardcoded

This commit is contained in:
2022-12-27 04:00:35 +01:00
parent c45ef583b1
commit f735216dc4
8 changed files with 100 additions and 87 deletions

View File

@@ -1,21 +1,56 @@
use std::{fs, error::Error};
use std::{fs, error::Error, collections::HashMap};
use log::debug;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Config {
pub mqtt: MQTT,
pub mqtt: MQTTConfig,
pub fullfillment: FullfillmentConfig,
#[serde(default)]
pub devices: HashMap<String, Device>
}
#[derive(Debug, Deserialize)]
pub struct MQTT {
pub struct MQTTConfig {
pub host: String,
pub port: u16,
pub username: String,
pub password: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct FullfillmentConfig {
pub port: u16,
pub username: String,
}
#[derive(Debug, Deserialize)]
pub struct InfoConfig {
pub name: String,
pub room: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct ZigbeeDeviceConfig {
pub topic: String,
}
#[derive(Debug, Deserialize)]
pub struct KettleConfig {
// @TODO Add options for the kettle
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum Device {
IkeaOutlet {
info: InfoConfig,
zigbee: ZigbeeDeviceConfig,
kettle: Option<KettleConfig>,
},
}
impl Config {
pub fn build(filename: &str) -> Result<Self, Box<dyn Error>> {
debug!("Loading config: {filename}");