Skip to main content

hive_client/client/wrapper/
action.rs

1use crate::actions::Action;
2use crate::{ApiError, Client};
3
4impl Client {
5    /// Get all of the [Quick Actions](https://www.hivehome.com/ie/support/Help_Using_Hive/HUH_General/What-are-Quick-Actions) setup in the Hive account.
6    ///
7    /// # Examples
8    ///
9    /// ```no_run
10    /// use hive_client::authentication::{TrustedDevice, User};
11    ///
12    /// # tokio_test::block_on(async {
13    /// let client = hive_client::Client::new("Home Automation");
14    ///
15    /// let trusted_device = Some(TrustedDevice::new(
16    ///     "device_password",
17    ///     "device_group_key",
18    ///     "device_key"
19    /// ));
20    ///
21    /// client.login(User::new("example@example.com", "example"), trusted_device)
22    ///     .await
23    ///     .expect("Login should succeed");
24    ///
25    /// let actions = client.get_actions()
26    ///     .await
27    ///     .expect("Quick action should be retrieved");
28    ///
29    /// // Activate a quick action
30    /// let mut turn_off_heating = actions.into_iter()
31    ///     .find(|action| action.data.id == "1234-5678-000-0000")
32    ///     .expect("Quick action to turn off heating should exist");
33    ///
34    /// let activated = turn_off_heating.activate().await;
35    ///
36    /// assert!(activated.is_ok());
37    /// # })
38    /// ```
39    ///
40    /// # Errors
41    ///
42    /// Returns an error if the list of [Quick Actions](https://www.hivehome.com/ie/support/Help_Using_Hive/HUH_General/What-are-Quick-Actions) could not be retrieved.
43    pub async fn get_actions(&self) -> Result<Vec<Action<'_>>, ApiError> {
44        self.api
45            .get_actions_data(&*self.refresh_tokens_if_needed().await?)
46            .await
47            .map(|actions| {
48                actions
49                    .into_iter()
50                    .map(|data| Action::new(self, data))
51                    .collect()
52            })
53    }
54
55    /// Activate a Quick Action by a given ID.
56    ///
57    /// Wrapped by [`Action::activate`] to activate a returned Quick Action.
58    pub(crate) async fn activate_action(&self, action_id: &str) -> Result<bool, ApiError> {
59        self.api
60            .activate_action(&*self.refresh_tokens_if_needed().await?, action_id)
61            .await
62    }
63}