1786288245
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
#[allow(clippy::all)]
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum AppError {
|
pub enum AppError {
|
||||||
#[error("HASS_URL is not set")]
|
#[error("HASS_URL is not set")]
|
||||||
@@ -16,7 +17,7 @@ pub enum AppError {
|
|||||||
SlintPlatform(#[from] slint::PlatformError),
|
SlintPlatform(#[from] slint::PlatformError),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
HomeAssistant(#[from] Box<hass_rs::HassError>),
|
HomeAssistant(#[from] hass_rs::HassError),
|
||||||
|
|
||||||
#[error("Entity {0} not found")]
|
#[error("Entity {0} not found")]
|
||||||
NoEntity(String),
|
NoEntity(String),
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ async fn handle_command(
|
|||||||
client
|
client
|
||||||
.call_service("light".into(), service.into(), Some(payload))
|
.call_service("light".into(), service.into(), Some(payload))
|
||||||
.await
|
.await
|
||||||
.map_err(Box::new)
|
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
} else {
|
} else {
|
||||||
Err(AppError::NoEntity(entity_id))
|
Err(AppError::NoEntity(entity_id))
|
||||||
@@ -39,7 +38,6 @@ async fn handle_command(
|
|||||||
client
|
client
|
||||||
.call_service("climate".into(), service.into(), Some(payload))
|
.call_service("climate".into(), service.into(), Some(payload))
|
||||||
.await
|
.await
|
||||||
.map_err(Box::new)
|
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
} else {
|
} else {
|
||||||
Err(AppError::NoEntity(entity_id))
|
Err(AppError::NoEntity(entity_id))
|
||||||
@@ -56,7 +54,6 @@ async fn handle_command(
|
|||||||
client
|
client
|
||||||
.call_service("climate".into(), "set_temperature".into(), Some(payload))
|
.call_service("climate".into(), "set_temperature".into(), Some(payload))
|
||||||
.await
|
.await
|
||||||
.map_err(Box::new)
|
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,14 +83,11 @@ pub async fn hass(
|
|||||||
|
|
||||||
let url = format!("{}://{}/api/websocket", scheme, url.authority());
|
let url = format!("{}://{}/api/websocket", scheme, url.authority());
|
||||||
|
|
||||||
let mut client = HassClient::new(&url).await.map_err(Box::new)?;
|
let mut client = HassClient::new(&url).await?;
|
||||||
client
|
client.auth_with_longlivedtoken(&hass_token).await?;
|
||||||
.auth_with_longlivedtoken(&hass_token)
|
|
||||||
.await
|
|
||||||
.map_err(Box::new)?;
|
|
||||||
|
|
||||||
let mut state = HashMap::<String, HassEntity>::new();
|
let mut state = HashMap::<String, HassEntity>::new();
|
||||||
let states = client.get_states().await.map_err(Box::new)?;
|
let states = client.get_states().await?;
|
||||||
|
|
||||||
states.iter().for_each(|entity| {
|
states.iter().for_each(|entity| {
|
||||||
state.insert(entity.entity_id.clone(), entity.clone());
|
state.insert(entity.entity_id.clone(), entity.clone());
|
||||||
@@ -103,10 +97,7 @@ pub async fn hass(
|
|||||||
initial_state.sort_by(|a, b| a.entity_id.cmp(&b.entity_id));
|
initial_state.sort_by(|a, b| a.entity_id.cmp(&b.entity_id));
|
||||||
s2c_tx.send(EventMessage::InitialState(initial_state)).ok();
|
s2c_tx.send(EventMessage::InitialState(initial_state)).ok();
|
||||||
|
|
||||||
let mut event_receiver = client
|
let mut event_receiver = client.subscribe_event("state_changed").await?;
|
||||||
.subscribe_event("state_changed")
|
|
||||||
.await
|
|
||||||
.map_err(Box::new)?;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ use crate::{
|
|||||||
messages::{CommandMessage, EventMessage},
|
messages::{CommandMessage, EventMessage},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[allow(clippy::result_large_err)]
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), AppError> {
|
async fn main() -> Result<(), AppError> {
|
||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
|
|||||||
Reference in New Issue
Block a user