Skip to main content

hive_client/client/wrapper/
device.rs

1use crate::devices::Device;
2use crate::{ApiError, Client};
3
4impl Client {
5    /// Get all of the devices associated with the Hive account.
6    ///
7    /// This can include Hubs, Thermostats, Boilers, and other devices.
8    ///
9    /// # Examples
10    ///
11    /// ```no_run
12    /// use hive_client::authentication::{TrustedDevice, User};
13    /// use hive_client::products::{Product, ProductData, State, States};
14    ///
15    /// # tokio_test::block_on(async {
16    /// let client = hive_client::Client::new("Home Automation");
17    ///
18    /// let trusted_device = Some(TrustedDevice::new(
19    ///     "device_password",
20    ///     "device_group_key",
21    ///     "device_key"
22    /// ));
23    ///
24    /// let attempt = client.login(User::new("example@example.com", "example"), trusted_device).await;
25    ///
26    /// if let Ok(_) = attempt {
27    ///     // Login was successful
28    ///
29    ///     let devices = client.get_devices()
30    ///         .await
31    ///         .expect("Devices should be retrieved");
32    ///     
33    ///     println!("{:?}", devices);
34    /// }
35    /// # })
36    /// ```
37    ///
38    /// # Errors
39    ///
40    /// Returns an error if the list of devices could not be retrieved.
41    pub async fn get_devices(&self) -> Result<Vec<Device>, ApiError> {
42        self.api
43            .get_devices(&*self.refresh_tokens_if_needed().await?)
44            .await
45            .map(|data| data.into_iter().map(Device::new).collect())
46    }
47}