Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use HA Scripts as Button Entity #20

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/client/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl HomeAssistantClient {
let entity_change = match entity_type {
"light" => light_event_to_entity_change(event.data),
"switch" | "input_boolean" => switch_event_to_entity_change(event.data),
"button" | "input_button" => {
// the button entity is stateless and the remote doesn't need to be notified when the button was pressed externally
"button" | "input_button" | "script" => {
// the button & script entity is stateless and the remote doesn't need to be notified when the button was pressed externally
return Ok(());
}
"cover" => cover_event_to_entity_change(event.data),
Expand Down
1 change: 1 addition & 0 deletions src/client/get_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl HomeAssistantClient {
"input_boolean" => "switch",
"binary_sensor" => "sensor",
"input_button" => "button",
"script" => "button",
v => v,
},
};
Expand Down
27 changes: 27 additions & 0 deletions src/client/service/button.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2022 Unfolded Circle ApS, Markus Zehnder <markus.z@unfoldedcircle.com>
// SPDX-License-Identifier: MPL-2.0

//! Button entity specific HA service call logic.

use crate::client::service::cmd_from_str;
use crate::errors::ServiceError;
use serde_json::Value;
use uc_api::intg::EntityCommand;
use uc_api::ButtonCommand;

pub(crate) fn handle_button(msg: &EntityCommand) -> Result<(String, Option<Value>), ServiceError> {
let cmd: ButtonCommand = cmd_from_str(&msg.cmd_id)?;

let entity: Vec<&str> = msg.entity_id.split('.').collect();

let service_call: &str = match entity[0] {
"script" => entity[1],
&_ => "press",
};

let result = match cmd {
ButtonCommand::Push => (service_call.into(), None),
};

Ok(result)
}
3 changes: 2 additions & 1 deletion src/client/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use serde_json::{Map, Value};
use uc_api::intg::EntityCommand;
use uc_api::EntityType;

mod button;
mod climate;
mod cover;
mod light;
Expand All @@ -41,7 +42,7 @@ impl Handler<CallService> for HomeAssistantClient {

// map Remote Two command name & parameters to HA service name and service_data payload
let (service, service_data) = match msg.command.entity_type {
EntityType::Button => Ok(("press".to_string(), None)),
EntityType::Button => button::handle_button(&msg.command),
EntityType::Switch => switch::handle_switch(&msg.command),
EntityType::Climate => climate::handle_climate(&msg.command),
EntityType::Cover => cover::handle_cover(&msg.command),
Expand Down