hive_client/client/api/
devices.rs1use std::collections::HashMap;
2
3use crate::client::api::error::ApiError;
4use crate::client::api::HiveApi;
5use crate::client::authentication::Tokens;
6use crate::helper::url::{get_base_url, Url};
7use chrono::{serde::ts_milliseconds, DateTime, Utc};
8use serde::Deserialize;
9use serde_json::Value;
10
11#[derive(Deserialize, Debug)]
12#[serde(rename_all = "lowercase")]
13#[allow(missing_docs)]
14pub enum PowerType {
15 Battery,
17
18 Mains,
20}
21
22#[derive(Deserialize, Debug)]
23#[non_exhaustive]
24#[allow(missing_docs)]
25pub struct Properties {
26 #[serde(rename = "online")]
27 pub is_online: bool,
29
30 pub power: Option<PowerType>,
32
33 #[serde(rename = "battery")]
34 pub battery_percentage: Option<i32>,
36
37 #[serde(rename = "zone")]
38 pub zone_id: Option<String>,
40
41 #[serde(flatten)]
42 #[allow(missing_docs)]
43 pub extra: HashMap<String, Value>,
44}
45
46#[derive(Deserialize, Debug)]
47#[serde(rename_all = "camelCase")]
48#[non_exhaustive]
49#[allow(missing_docs)]
50pub struct State {
51 pub name: String,
53
54 pub zone_name: Option<String>,
56}
57
58#[derive(Deserialize, Debug)]
59#[serde(rename_all = "camelCase")]
60#[non_exhaustive]
61pub struct Thermostat {
63 pub id: String,
65
66 #[serde(with = "ts_milliseconds")]
67 pub last_seen: DateTime<Utc>,
69
70 #[serde(with = "ts_milliseconds")]
71 #[serde(rename = "created")]
72 pub created_at: DateTime<Utc>,
74
75 #[serde(rename = "props")]
76 pub properties: Properties,
78
79 pub state: State,
81
82 #[serde(flatten)]
83 #[allow(missing_docs)]
84 pub extra: HashMap<String, Value>,
85}
86
87#[derive(Deserialize, Debug)]
88#[serde(rename_all = "camelCase")]
89#[non_exhaustive]
90pub struct Hub {
92 pub id: String,
94
95 #[serde(with = "ts_milliseconds")]
96 pub last_seen: DateTime<Utc>,
98
99 #[serde(with = "ts_milliseconds")]
100 #[serde(rename = "created")]
101 pub created_at: DateTime<Utc>,
103
104 #[serde(rename = "props")]
105 pub properties: Properties,
107
108 pub state: State,
110
111 #[serde(flatten)]
112 #[allow(missing_docs)]
113 pub extra: HashMap<String, Value>,
114}
115
116#[derive(Deserialize, Debug)]
117#[serde(rename_all = "camelCase")]
118#[non_exhaustive]
119pub struct BoilerModule {
121 pub id: String,
123
124 #[serde(with = "ts_milliseconds")]
125 pub last_seen: DateTime<Utc>,
127
128 #[serde(with = "ts_milliseconds")]
129 #[serde(rename = "created")]
130 pub created_at: DateTime<Utc>,
132
133 #[serde(rename = "props")]
134 pub properties: Properties,
136
137 pub state: State,
139
140 #[serde(flatten)]
141 #[allow(missing_docs)]
142 pub extra: HashMap<String, Value>,
143}
144
145#[derive(Deserialize, Debug)]
146#[serde(rename_all = "lowercase")]
147#[serde(tag = "type")]
148#[non_exhaustive]
149#[allow(missing_docs)]
150pub enum DeviceData {
151 #[serde(rename = "thermostatui")]
152 Thermostat(Thermostat),
154
155 Hub(Hub),
157
158 BoilerModule(BoilerModule),
160
161 #[serde(other)]
162 Unknown,
164}
165
166#[derive(Debug)]
170pub struct Device {
171 #[allow(missing_docs)]
172 pub data: DeviceData,
173}
174
175impl Device {
176 pub(crate) const fn new(data: DeviceData) -> Self {
177 Self { data }
178 }
179}
180
181impl HiveApi {
182 pub(crate) async fn get_devices(&self, tokens: &Tokens) -> Result<Vec<DeviceData>, ApiError> {
183 let response = self
184 .client
185 .get(get_base_url(&Url::Device))
186 .header("Authorization", &tokens.id_token)
187 .send()
188 .await;
189
190 let body = response?.text().await?;
191
192 Ok(serde_json::from_str(&body)?)
193 }
194}