Giter VIP home page Giter VIP logo

Comments (1)

ewilken avatar ewilken commented on August 17, 2024

An alternative I see is to handle my own shared state somewhere else and use an on_read callback to update the Homekit state when its actually attempting to be read.

This is something you can do, but, as you pointed out, not really practical for things like sensors where you want event notifications on value changes.

I was expecting that set_value would be "sending out" status update messages to the system so they could be triggering notifications, automations, etc.

It does! (Or at least should do. I hope I didn't break it since I last tested.)

The other idea I had but haven't followed through with yet is trying to use the pointer::Acessory returned from add_accessory to try and get to the characteristic to set values on it. Is that what I'm supposed to be doing?

Yes. That's what I'd do. You're totally right about the lack of documentation for that. Will try to document it properly.

Just added a (lightly tested) example for a motion sensor that gets triggered every two seconds after one minute after program start. The interesting part is this one:

let sensor_ptr = server.add_accessory(sensor).await.unwrap();

let handle = server.run_handle();

let value_set_interval = async move {
    let mut interval = tokio::time::interval(std::time::Duration::from_secs(2));

    tokio::time::delay_for(std::time::Duration::from_secs(60)).await;

    loop {
        interval.tick().await;

        let mut motion_sensor_accessory = sensor_ptr.lock().await;
        let motion_sensor_service = motion_sensor_accessory.get_mut_service(HapType::MotionSensor).unwrap();
        let motion_detected_characteristic = motion_sensor_service
            .get_mut_characteristic(HapType::MotionDetected)
            .unwrap();

        motion_detected_characteristic
            .set_value(Value::Bool(true))
            .await
            .unwrap();

        tokio::time::delay_for(std::time::Duration::from_secs(1)).await;

        motion_detected_characteristic
            .set_value(Value::Bool(false))
            .await
            .unwrap();
    }
};

std::env::set_var("RUST_LOG", "hap=debug");
env_logger::init();

futures::join!(handle, value_set_interval);
cargo run --example setting_values_after_server_start --release

Apparently the --release build is needed for the pairing process to be fast enough for the iOS controller not to lose patience. Still a lot of stuff left to look into here.

Thanks a lot for caring! Feel free to reach out for further questions anytime. And contributions to help finishing this crate are always welcome, should you find it useful.

from hap-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.