feat: Add proper type definition for devices
All checks were successful
Build and deploy / build (push) Successful in 10m38s
Build and deploy / Deploy container (push) Has been skipped

Depending on the implemented traits the lua class will inherit from the
associated interface class.

It also specifies the constructor function for each of the devices.
This commit is contained in:
2025-10-10 03:50:47 +02:00
parent 8010f7a404
commit 1e9bd300a6
3 changed files with 68 additions and 1 deletions

View File

@@ -44,8 +44,8 @@ where
#[serde(default)] #[serde(default)]
pub callback: ActionCallback<(Light<T>, T)>, pub callback: ActionCallback<(Light<T>, T)>,
#[serde(default)]
#[device_config(from_lua)] #[device_config(from_lua)]
#[serde(default)]
pub client: WrappedAsyncClient, pub client: WrappedAsyncClient,
} }
crate::register_type!(Config<StateOnOff>); crate::register_type!(Config<StateOnOff>);

View File

@@ -138,6 +138,12 @@ impl quote::ToTokens for Implementation {
add_methods, add_methods,
} = &self; } = &self;
let interfaces: String = traits
.0
.iter()
.map(|tr| format!(", Interface{tr}"))
.collect();
tokens.extend(quote! { tokens.extend(quote! {
impl mlua::UserData for #name { impl mlua::UserData for #name {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) { fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
@@ -168,6 +174,25 @@ impl quote::ToTokens for Implementation {
fn type_name() -> String { fn type_name() -> String {
stringify!(#name).into() stringify!(#name).into()
} }
fn generate_header() -> std::option::Option<::std::string::String> {
let type_name = <Self as ::lua_typed::Typed>::type_name();
Some(format!("---@class {type_name}: InterfaceDevice{}\nlocal {type_name}\n", #interfaces))
}
fn generate_members() -> Option<String> {
let mut output = String::new();
let type_name = <Self as ::lua_typed::Typed>::type_name();
output += &format!("devices.{type_name} = {{}}\n");
let config_name = <<Self as ::automation_lib::device::LuaDeviceCreate>::Config as ::lua_typed::Typed>::type_name();
output += &format!("---@param config {config_name}\n");
output += &format!("---@return {type_name}\n");
output += &format!("function devices.{type_name}.new(config) end\n");
Some(output)
}
} }
}); });
} }

View File

@@ -0,0 +1,42 @@
--- @meta
---@class InterfaceDevice
local InterfaceDevice
---@return string
function InterfaceDevice:get_id() end
---@class InterfaceOnOff: InterfaceDevice
local InterfaceOnOff
---@async
---@param on boolean
function InterfaceOnOff:set_on(on) end
---@async
---@return boolean
function InterfaceOnOff:on() end
---@class InterfaceBrightness: InterfaceDevice
local InterfaceBrightness
---@async
---@param brightness integer
function InterfaceBrightness:set_brightness(brightness) end
---@async
---@return integer
function InterfaceBrightness:brightness() end
---@class InterfaceColorSetting: InterfaceDevice
local InterfaceColorSetting
---@async
---@param temperature integer
function InterfaceColorSetting:set_color_temperature(temperature) end
---@async
---@return integer
function InterfaceColorSetting:color_temperature() end
---@class InterfaceOpenClose: InterfaceDevice
local InterfaceOpenClose
---@async
---@param open_percent integer
function InterfaceOpenClose:set_open_percent(open_percent) end
---@async
---@return integer
function InterfaceOpenClose:open_percent() end