The device create function is now standarized using a trait
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -4,8 +4,8 @@ use rumqttc::AsyncClient;
|
||||
use serde::Deserialize;
|
||||
use tracing::{debug, error, trace, warn};
|
||||
|
||||
use crate::config::{self, MqttDeviceConfig};
|
||||
use crate::error::DeviceCreateError;
|
||||
use crate::config::{self, CreateDevice, MqttDeviceConfig};
|
||||
use crate::error::CreateDeviceError;
|
||||
use crate::mqtt::{OnMqtt, RemoteAction, RemoteMessage};
|
||||
use crate::presence::OnPresence;
|
||||
|
||||
@@ -28,25 +28,26 @@ pub struct AudioSetup {
|
||||
speakers: Box<dyn traits::OnOff>,
|
||||
}
|
||||
|
||||
impl AudioSetup {
|
||||
pub fn create(
|
||||
impl CreateDevice for AudioSetup {
|
||||
type Config = AudioSetupConfig;
|
||||
|
||||
fn create(
|
||||
identifier: &str,
|
||||
config: AudioSetupConfig,
|
||||
config: Self::Config,
|
||||
client: AsyncClient,
|
||||
// We only need to pass this in because constructing children
|
||||
presence_topic: &str, // Not a big fan of passing in the global config
|
||||
) -> Result<Self, DeviceCreateError> {
|
||||
presence_topic: &str,
|
||||
) -> Result<Self, CreateDeviceError> {
|
||||
trace!(id = identifier, "Setting up AudioSetup");
|
||||
|
||||
// Create the child devices
|
||||
let mixer_id = format!("{}.mixer", identifier);
|
||||
let mixer = (*config.mixer).create(&mixer_id, client.clone(), presence_topic)?;
|
||||
let mixer = As::consume(mixer).ok_or(DeviceCreateError::OnOffExpected(mixer_id))?;
|
||||
let mixer = As::consume(mixer).ok_or(CreateDeviceError::OnOffExpected(mixer_id))?;
|
||||
|
||||
let speakers_id = format!("{}.speakers", identifier);
|
||||
let speakers = (*config.speakers).create(&speakers_id, client, presence_topic)?;
|
||||
let speakers =
|
||||
As::consume(speakers).ok_or(DeviceCreateError::OnOffExpected(speakers_id))?;
|
||||
As::consume(speakers).ok_or(CreateDeviceError::OnOffExpected(speakers_id))?;
|
||||
|
||||
Ok(Self {
|
||||
identifier: identifier.to_owned(),
|
||||
|
||||
@@ -7,8 +7,8 @@ use tokio::task::JoinHandle;
|
||||
use tracing::{debug, error, trace, warn};
|
||||
|
||||
use crate::{
|
||||
config::MqttDeviceConfig,
|
||||
error::{DeviceCreateError, MissingWildcard},
|
||||
config::{CreateDevice, MqttDeviceConfig},
|
||||
error::{CreateDeviceError, MissingWildcard},
|
||||
mqtt::{ContactMessage, OnMqtt, PresenceMessage},
|
||||
presence::OnPresence,
|
||||
};
|
||||
@@ -69,13 +69,15 @@ pub struct ContactSensor {
|
||||
handle: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl ContactSensor {
|
||||
pub fn create(
|
||||
impl CreateDevice for ContactSensor {
|
||||
type Config = ContactSensorConfig;
|
||||
|
||||
fn create(
|
||||
identifier: &str,
|
||||
config: ContactSensorConfig,
|
||||
config: Self::Config,
|
||||
client: AsyncClient,
|
||||
presence_topic: &str,
|
||||
) -> Result<Self, DeviceCreateError> {
|
||||
) -> Result<Self, CreateDeviceError> {
|
||||
trace!(id = identifier, "Setting up ContactSensor");
|
||||
|
||||
let presence = config
|
||||
|
||||
@@ -13,9 +13,9 @@ use std::time::Duration;
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::{debug, error, trace, warn};
|
||||
|
||||
use crate::config::{InfoConfig, MqttDeviceConfig};
|
||||
use crate::config::{CreateDevice, InfoConfig, MqttDeviceConfig};
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceCreateError;
|
||||
use crate::error::CreateDeviceError;
|
||||
use crate::mqtt::{OnMqtt, OnOffMessage};
|
||||
use crate::presence::OnPresence;
|
||||
|
||||
@@ -54,12 +54,15 @@ pub struct IkeaOutlet {
|
||||
handle: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl IkeaOutlet {
|
||||
pub fn create(
|
||||
impl CreateDevice for IkeaOutlet {
|
||||
type Config = IkeaOutletConfig;
|
||||
|
||||
fn create(
|
||||
identifier: &str,
|
||||
config: IkeaOutletConfig,
|
||||
config: Self::Config,
|
||||
client: AsyncClient,
|
||||
) -> Result<Self, DeviceCreateError> {
|
||||
_presence_topic: &str, // Not a big fan of passing in the global config
|
||||
) -> Result<Self, CreateDeviceError> {
|
||||
trace!(
|
||||
id = identifier,
|
||||
name = config.info.name,
|
||||
|
||||
@@ -9,11 +9,12 @@ use google_home::{
|
||||
errors::{self, DeviceError},
|
||||
traits,
|
||||
};
|
||||
use rumqttc::AsyncClient;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
use tracing::trace;
|
||||
|
||||
use crate::error::DeviceCreateError;
|
||||
use crate::{config::CreateDevice, error::CreateDeviceError};
|
||||
|
||||
use super::Device;
|
||||
|
||||
@@ -28,8 +29,15 @@ pub struct KasaOutlet {
|
||||
addr: SocketAddr,
|
||||
}
|
||||
|
||||
impl KasaOutlet {
|
||||
pub fn create(identifier: &str, config: KasaOutletConfig) -> Result<Self, DeviceCreateError> {
|
||||
impl CreateDevice for KasaOutlet {
|
||||
type Config = KasaOutletConfig;
|
||||
|
||||
fn create(
|
||||
identifier: &str,
|
||||
config: Self::Config,
|
||||
_client: AsyncClient,
|
||||
_presence_topic: &str,
|
||||
) -> Result<Self, CreateDeviceError> {
|
||||
trace!(id = identifier, "Setting up KasaOutlet");
|
||||
|
||||
Ok(Self {
|
||||
|
||||
@@ -9,13 +9,13 @@ use google_home::{
|
||||
types::Type,
|
||||
GoogleHomeDevice,
|
||||
};
|
||||
use rumqttc::Publish;
|
||||
use rumqttc::{AsyncClient, Publish};
|
||||
use serde::Deserialize;
|
||||
use tracing::{debug, error, trace};
|
||||
|
||||
use crate::{
|
||||
config::{InfoConfig, MqttDeviceConfig},
|
||||
error::DeviceCreateError,
|
||||
config::{CreateDevice, InfoConfig, MqttDeviceConfig},
|
||||
error::CreateDeviceError,
|
||||
mqtt::{ActivateMessage, OnMqtt},
|
||||
};
|
||||
|
||||
@@ -45,8 +45,15 @@ pub struct WakeOnLAN {
|
||||
broadcast_ip: Ipv4Addr,
|
||||
}
|
||||
|
||||
impl WakeOnLAN {
|
||||
pub fn create(identifier: &str, config: WakeOnLANConfig) -> Result<Self, DeviceCreateError> {
|
||||
impl CreateDevice for WakeOnLAN {
|
||||
type Config = WakeOnLANConfig;
|
||||
|
||||
fn create(
|
||||
identifier: &str,
|
||||
config: Self::Config,
|
||||
_client: AsyncClient,
|
||||
_presence_topic: &str,
|
||||
) -> Result<Self, CreateDeviceError> {
|
||||
trace!(
|
||||
id = identifier,
|
||||
name = config.info.name,
|
||||
|
||||
Reference in New Issue
Block a user