Crate homelander

Source
Expand description

§Homelander

Homelander is a Google Home integration framework. It provides serialization and deserialization for fulfillment requests. It also handles translation between Google Home traits and Rust traits. Furthermore it provides error handling and translating between Rust errors and errors accepted by Google Home.

Homelander does not provide an OAuth2 server or a web server.

§Getting started

To get started, you’ll first have to create your own OAuth2 server or use an existing implementation. Refer to the Google documentation for details.

After you’ve done this, you’ve presumably also configured your web server. You can then easily get started with Homelander. Create a Device like so:

use std::sync::{Arc, Mutex};
use homelander::{Device, DeviceType, Homelander};
use homelander::traits::{CombinedDeviceError, DeviceInfo, DeviceName, GoogleHomeDevice};
use homelander::traits::on_off::OnOff;

#[derive(Debug)]
struct MyDevice(bool);

// Implement the basic GoogleHomeDevice trait,
// This gives the basic information required for every device
impl GoogleHomeDevice for MyDevice {
    fn get_device_info(&self) -> DeviceInfo {
        DeviceInfo {
            model: "mydevice".to_string(),
            manufacturer: "mydevice company".to_string(),
            hw: "0.1.0".to_string(),
            sw: "0.1.0".to_string(),
        }
    }

    fn will_report_state(&self) -> bool {
        // Will this Device be reporting state to Google?
        // Note that as of August 6 2022, this isn't implemented in Homelander yet,
        // Until it is, this should *always* be false.
        false
    }

    fn get_device_name(&self) -> DeviceName {
        DeviceName {
            name: "MyDevice".to_string(),
            default_names: Vec::new(),
            nicknames: Vec::new(),
        }
    }

    fn is_online(&self) -> bool {
        true
    }

    fn disconnect(&mut self) {
        // Handle your disconnect here
    }
}

// Implement a device specific trait. E.g. OnOff
impl OnOff for MyDevice {
    fn is_on(&self) -> Result<bool, CombinedDeviceError> {
        Ok(self.0)
    }

    fn set_on(&mut self, on: bool) -> Result<(), CombinedDeviceError> {
        self.0 = on;
        Ok(())
    }
}

// Create the device
let mut device = Device::new(MyDevice(false), DeviceType::Outlet, "my_id".to_string());
// Register the OnOff traitr
device.set_on_off();

// Create the Homelander struct
let mut homelander = Homelander::new("my_user_id".to_string());
homelander.add_device(device);

This will create a basic setup. You can now register a fulfillment route with your webserver. This route should take a JSON payload: Request. This request can then be passed to Homelander:


// Retrieve the Homelander for the user,
// The user can be identified through the OAuth2 token provided by Google
let mut homelander = get_homelander("my_user_id".to_string());
// Let homelander handle the request and create a response
// The response can then be returned to Google as JSON
let the_request = get_incoming_request(); // Usually you'd get this from your web framework
let response = homelander.handle_request(the_request);

Modules§

traits

Structs§

Device
A Google Home device with its traits
Homelander
Keeps track of all devices owned by a specific user.
Request
Response
SerializableError

Enums§

DeviceType

Traits§

DeviceTraits
ToStringError