honeywell_tcc/
lib.rs

1use crate::api::locations::get_locations;
2use crate::api::login::{login, LoggedIn, LoginError};
3use crate::api::zones::set_zone_temperature;
4use crate::api::USER_AGENT;
5use reqwest::Client;
6
7mod api;
8
9pub use api::locations::{Location, Zone};
10
11/// The honeywell TCC api
12pub struct TccApi {
13    client: Client,
14    login: LoggedIn,
15}
16
17impl TccApi {
18    /// Create a new API client and log in with Honeywell
19    ///
20    /// # Errors
21    ///
22    /// If the request fails
23    pub async fn new_with_login<S1: AsRef<str>, S2: AsRef<str>>(
24        email: S1,
25        password: S2,
26    ) -> Result<Self, LoginError> {
27        let client = Client::builder()
28            .user_agent(USER_AGENT)
29            .cookie_store(true)
30            .build()?;
31
32        let login = login(&client, email, password).await?;
33
34        Ok(Self { client, login })
35    }
36
37    /// Get the display name of the logged in user
38    pub fn display_name(&self) -> &str {
39        &self.login.display_name
40    }
41
42    /// Get the ID of the logged in user
43    pub fn user_id(&self) -> &str {
44        &self.login.user_id
45    }
46
47    /// Get locations
48    ///
49    /// # Errors
50    ///
51    /// If the request fails
52    pub async fn get_locations(&self) -> Result<Vec<Location>, reqwest::Error> {
53        get_locations(&self.client).await
54    }
55
56    /// Set the temperature of a zone.
57    /// The temperature may only be a whole integer or half of an integer. E.g. `13.5` and `13.0` are fine, `13.7` is not.
58    ///
59    /// # Errors
60    ///
61    /// If the request fails
62    pub async fn set_zone_temperature(
63        &self,
64        zone_id: impl Into<String>,
65        temperature: f32,
66    ) -> Result<(), reqwest::Error> {
67        set_zone_temperature(&self.client, zone_id, temperature).await
68    }
69}