hive_client/lib.rs
1#![deny(missing_docs)]
2#![deny(clippy::all)]
3#![warn(clippy::pedantic)]
4#![warn(clippy::nursery)]
5#![warn(missing_debug_implementations, rust_2018_idioms, rustdoc::all)]
6#![allow(rustdoc::private_doc_tests)]
7#![forbid(unsafe_code)]
8#![deny(clippy::clone_on_ref_ptr)]
9
10//! # Hive Client
11//!
12//! The Hive Client crate provides a client for interfacing with [Hive](https://www.hivehome.com/) smart home systems.
13//!
14//! ## Usage
15//! ```toml
16//! [dependencies]
17//! hive-client = "0.0.6"
18//! ```
19//!
20//! ## Capabilities
21//!
22//! 1. Authenticate with Hive.
23//! 2. Setup trusted devices in a Hive account.
24//! 3. Request live Weather information directly from Hive.
25//! 4. List [Quick Actions](https://www.hivehome.com/ie/support/Help_Using_Hive/HUH_General/What-are-Quick-Actions) configured in a Hive account.
26//! 5. Activate [Quick Actions](https://www.hivehome.com/ie/support/Help_Using_Hive/HUH_General/What-are-Quick-Actions) on demand.
27//! 6. List Devices associated with a Hive account (Hubs, Thermostats, Hubs, etc).
28//! 7. List Products currently running in a Hive account.
29//! 8. Change state of Products in a Hive account (Boost heating/hot water, change target
30//! temperature, etc).
31//!
32//! ## Examples
33//!
34//! More in-depth examples can be found in documentation comments on the Client methods.
35//!
36//! ### Trigger a Quick Action
37//!
38//! ```no_run
39//! use hive_client::authentication::{TrustedDevice, User};
40//!
41//! # tokio_test::block_on(async {
42//! let client = hive_client::Client::new("Home Automation");
43//!
44//! let trusted_device = Some(TrustedDevice::new(
45//! "device_password",
46//! "device_group_key",
47//! "device_key"
48//! ));
49//!
50//! let attempt = client.login(User::new("example@example.com", "example"), trusted_device).await;
51//!
52//! if let Ok(_) = attempt {
53//! // Login was successful
54//!
55//! let mut actions = client.get_actions()
56//! .await
57//! .expect("Quick action should be retrieved");
58//!
59//! if let Some(mut first_action) = actions.first_mut() {
60//! let was_activated = first_action.activate()
61//! .await
62//! .expect("Quick action should be activated");
63//! # assert!(was_activated);
64//! }
65//! }
66//! # })
67//! ```
68//!
69//! ### Set Target Temperature of Heating
70//!
71//! ```no_run
72//! use hive_client::authentication::{TrustedDevice, User};
73//! use hive_client::products::{Product, ProductData, State, States};
74//!
75//! # tokio_test::block_on(async {
76//! let client = hive_client::Client::new("Home Automation");
77//!
78//! let trusted_device = Some(TrustedDevice::new(
79//! "device_password",
80//! "device_group_key",
81//! "device_key"
82//! ));
83//!
84//! let attempt = client.login(User::new("example@example.com", "example"), trusted_device).await;
85//!
86//! if let Ok(_) = attempt {
87//! // Login was successful
88//!
89//! let products = client.get_products()
90//! .await
91//! .expect("Products should be retrieved");
92//!
93//! if let Some(mut heating) = products.into_iter().find(|Product { data, .. }| matches!(data, ProductData::Heating { .. })) {
94//! let was_set = heating.set_state(States(vec!(State::TargetTemperature(18.0))))
95//! .await
96//! .expect("Product state should be set");
97//! # assert!(was_set);
98//! }
99//! }
100//! # })
101//! ```
102//!
103//! ### Retrieve Current Weather
104//!
105//! ```no_run
106//! use hive_client::authentication::{TrustedDevice, User};
107//! use hive_client::products::{Product, ProductData, State, States};
108//!
109//! # tokio_test::block_on(async {
110//! let client = hive_client::Client::new("Home Automation");
111//!
112//! let trusted_device = Some(TrustedDevice::new(
113//! "device_password",
114//! "device_group_key",
115//! "device_key"
116//! ));
117//!
118//! let attempt = client.login(User::new("example@example.com", "example"), trusted_device).await;
119//!
120//! if let Ok(_) = attempt {
121//! // Login was successful
122//!
123//! let postcode = "SW1A 1AA";
124//!
125//! let weather = client.get_weather(postcode)
126//! .await
127//! .expect("Weather should be retrieved");
128//!
129//! // Example: It is currently 18.0C with clear skies at Buckingham Palace
130//! println!("It is currently {} with {} at Buckingham Palace", weather.data.temperature, weather.data.description);
131//! }
132//! # })
133//! ```
134//!
135//! ## Contributing
136//!
137//! There are _tons_ of features which could be added to the crate. If you'd like to contribute, please
138//! feel free to open an issue or a pull request.
139//!
140//! Examples of features which could be added:
141//! 1. Better parity between the Hive API and the structs.
142//! 2. Support for controlling Holiday Mode.
143//! 3. Support for modifying the schedule of a Hive Device.
144//! 4. Support for other Hive products (e.g. Hive Lights, Smart Plugs, Motion Sensors, etc).
145//!
146//! ### Testing
147//! Many of the tests require that an AWS Cognito User Pool, configured with SRP authentication and device
148//! tracking, be available to act as a mock server.
149//!
150//! The setup of a suitable User Pool is fully automated with Terraform (see the [`infrastructure`](infrastructure/) folder).
151//!
152//! #### Set up
153//!
154//! In order to setup the test environment, Terraform needs to be installed and AWS credentials need to be
155//! configured locally.
156//!
157//! Once this is done, running `apply` should setup the Terraform backend, and the user pool and app client in the correct state:
158//! ```sh
159//! # Setup state backend
160//! cd infrastructure/state && terraform init && terraform apply
161//!
162//! # Setup user pool
163//! cd infrastructure/tests && terraform init --backend-config="./local.config" && terraform apply
164//! ```
165//!
166//! After the user pool is set up, multiple environment variables need to be set in a `.env` file.
167//!
168//! The `.env` file can be created by using `.env.example` as a template:
169//! ```sh
170//! cp .env.example .env
171//! ```
172//!
173//! #### Running tests
174//!
175//! The tests can be run with:
176//! ```sh
177//! cargo test
178//! ```
179//!
180//! #### Tear down
181//!
182//! The test environment can be torn down at any point with:
183//! ```sh
184//! cd infrastructure/tests && terraform destroy
185//! ```
186
187mod client;
188mod constants;
189mod helper;
190
191pub use client::*;