From 00c32e8993237783fa76e8c4903447b90840878b Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Tue, 12 May 2026 04:26:42 +0200 Subject: [PATCH] feat: Added all_on function to HueGroup --- automation_devices/src/hue_group.rs | 47 +++++++++++++++++++++++++++++ definitions/automation:devices.lua | 3 ++ 2 files changed, 50 insertions(+) diff --git a/automation_devices/src/hue_group.rs b/automation_devices/src/hue_group.rs index e2ae5d2..ab10b00 100644 --- a/automation_devices/src/hue_group.rs +++ b/automation_devices/src/hue_group.rs @@ -2,6 +2,7 @@ use std::net::SocketAddr; use anyhow::Result; use async_trait::async_trait; +use automation_lib::lua::traits::PartialUserData; use automation_macro::{Device, LuaDeviceConfig}; use google_home::errors::ErrorCode; use google_home::traits::OnOff; @@ -25,6 +26,7 @@ crate::register_type!(Config); #[derive(Debug, Clone, Device)] #[device(traits(OnOff))] +#[device(extra_user_data = AllOn)] pub struct HueGroup { config: Config, } @@ -122,6 +124,47 @@ impl OnOff for HueGroup { } } +struct AllOn; +impl PartialUserData for AllOn { + fn add_methods>(methods: &mut M) { + methods.add_async_method("all_on", async |_lua, this, ()| { + let res = reqwest::Client::new() + .get(this.url_get_state()) + .send() + .await; + + match res { + Ok(res) => { + let status = res.status(); + if !status.is_success() { + warn!(id = this.get_id(), "Status code is not success: {status}"); + } + + let on = match res.json::().await { + Ok(info) => info.all_on(), + Err(err) => { + error!(id = this.get_id(), "Failed to parse message: {err}"); + return Ok(false); + } + }; + + return Ok(on); + } + Err(err) => error!(id = this.get_id(), "Error: {err}"), + } + + Ok(false) + }); + } + + fn definitions() -> Option { + Some(format!( + "---@async\n---@return boolean\nfunction {}:all_on() end\n", + ::type_name(), + )) + } +} + mod message { use serde::{Deserialize, Serialize}; @@ -164,5 +207,9 @@ mod message { pub fn any_on(&self) -> bool { self.state.any_on } + + pub fn all_on(&self) -> bool { + self.state.all_on + } } } diff --git a/definitions/automation:devices.lua b/definitions/automation:devices.lua index 06b0667..a182df4 100644 --- a/definitions/automation:devices.lua +++ b/definitions/automation:devices.lua @@ -116,6 +116,9 @@ devices.HueGroup = {} ---@param config HueGroupConfig ---@return HueGroup function devices.HueGroup.new(config) end +---@async +---@return boolean +function HueGroup:all_on() end ---@class HueGroupConfig ---@field identifier string