hue_bridge/api/resources/
light.rs

1// Copyright 2022  Palade Ionut Victor
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::ResourceTypes;
16use crate::HueBridge;
17use crate::Result;
18use reqwest::Method;
19use serde::{Deserialize, Serialize};
20
21#[allow(missing_docs)]
22#[derive(Debug, Default)]
23/// <https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_light>
24pub struct Light {
25    endpoint: String,
26    bridge: HueBridge,
27}
28
29impl Light {
30    /// Light resource constructor.
31    pub fn new(bridge: HueBridge) -> Self {
32        Self {
33            endpoint: "clip/v2/resource/light".to_string(),
34            bridge,
35            ..Default::default()
36        }
37    }
38    /// List all the available light resources
39    pub async fn list(self) -> Result<Vec<LightPayload>> {
40        let resp: LightResponse = self
41            .bridge
42            .fetch_resource(Method::GET, &self.endpoint)
43            .await?;
44
45        if let Some(err) = resp.error {
46            Err(err[0].description.to_string().into())
47        } else {
48            Ok(resp.data)
49        }
50    }
51}
52
53#[allow(missing_docs)]
54#[derive(Debug, Serialize, Deserialize)]
55struct LightResponse {
56    data: Vec<LightPayload>,
57    error: Option<Vec<ResourceErr>>,
58}
59
60#[allow(missing_docs)]
61#[derive(Debug, Serialize, Deserialize)]
62pub struct LightPayload {
63    pub id: String,
64    pub on: On,
65    pub dimming: Dimming,
66    pub dimming_delta: Option<DimmingDelta>,
67    pub color_temperature: ColorTemperature,
68    r#type: ResourceTypes,
69}
70
71#[allow(missing_docs)]
72#[derive(Debug, Serialize, Deserialize)]
73pub struct On {
74    pub on: bool,
75}
76
77#[allow(missing_docs)]
78#[derive(Debug, Serialize, Deserialize)]
79struct ResourceErr {
80    description: String,
81}
82
83#[allow(missing_docs)]
84#[derive(Debug, Default, Clone, Serialize, Deserialize)]
85pub struct Dimming {
86    pub brightness: f32,
87}
88
89#[allow(missing_docs)]
90#[derive(Debug, Clone, Serialize, Deserialize)]
91#[serde(rename_all = "lowercase")]
92pub enum Action {
93    Up,
94    Down,
95    Stop,
96}
97
98impl Default for Action {
99    fn default() -> Self {
100        Action::Up
101    }
102}
103
104#[allow(missing_docs)]
105#[derive(Debug, Default, Clone, Serialize, Deserialize)]
106pub struct DimmingDelta {
107    pub action: Option<Action>,
108}
109
110#[allow(missing_docs)]
111#[derive(Debug, Default, Clone, Serialize, Deserialize)]
112pub struct ColorTemperature {
113    pub mirek: u16,
114}
115
116#[allow(missing_docs)]
117#[derive(Debug, Default, Clone, Serialize, Deserialize)]
118pub struct ColorTemperatureDelta {
119    pub action: Action,
120    pub mirek_delta: u16,
121}