homelander/traits/
sensor_state.rs

1use crate::CombinedDeviceError;
2use serde::Serialize;
3
4/// Each object represents sensor state capabilities supported by this specific device.
5/// Each sensor must have at least a descriptive or numeric capability.
6/// Sensors can also report both, in which case the numeric value will be preferred.
7#[derive(Debug, PartialEq, Serialize)]
8#[serde(rename_all = "camelCase")]
9pub struct SupportedSensorState {
10    /// Supported sensor type.
11    /// Refer to [the Google docs](https://developers.google.com/assistant/smarthome/traits/sensorstate#supported-sensors) for supported values.
12    pub name: String,
13    /// A description of the sensor's capabilities.
14    pub descriptive_capabilities: Option<DescriptiveCapabilities>,
15    /// Describes the possible numerical values that the sensor can report.
16    pub numeric_capabilities: Option<NumericCapabilities>,
17}
18
19/// A description of the sensor's capabilities.
20#[derive(Debug, PartialEq, Serialize)]
21#[serde(rename_all = "camelCase")]
22pub struct DescriptiveCapabilities {
23    /// List of the available states for the device. The "unknown" state is implicitly supported when the sensor does not return a value.
24    /// Requires at least 1 item.
25    /// Refer to [the Google docs](https://developers.google.com/assistant/smarthome/traits/sensorstate#supported-sensors) for supported values.
26    pub available_states: Vec<String>,
27}
28
29/// Describes the possible numerical values that the sensor can report.
30#[derive(Debug, PartialEq, Serialize)]
31#[serde(rename_all = "camelCase")]
32pub struct NumericCapabilities {
33    /// Supported numerical unit.
34    /// Refer to [the Google docs](https://developers.google.com/assistant/smarthome/traits/sensorstate#supported-sensors) for supported values.
35    pub raw_value_unit: String,
36}
37
38/// Current sensor state.
39#[derive(Debug, PartialEq, Serialize)]
40#[serde(rename_all = "camelCase")]
41pub struct CurrentSensorState {
42    /// Sensor state name. Matches a value from sensorStatesSupported.
43    pub name: String,
44    /// Current descriptive state value. Matches a value from sensorStatesSupported.
45    pub current_sensor_state: Option<String>,
46    /// Current numeric sensor value.
47    pub raw_value: Option<f32>,
48}
49
50/// This trait covers both quantitative measurement (for example,
51/// air quality index or smoke level) and qualitative state (for example, whether the air quality is healthy
52/// or whether the smoke level is low or high).
53///
54/// ## See also
55/// <https://developers.google.com/assistant/smarthome/traits/sensorstate>
56pub trait SensorState {
57    /// Each object represents sensor state capabilities supported by this specific device.
58    /// Each sensor must have at least a descriptive or numeric capability.
59    /// Sensors can also report both, in which case the numeric value will be preferred.
60    fn get_supported_sensor_states(&self) -> Result<Vec<SupportedSensorState>, CombinedDeviceError>;
61
62    /// List of current sensor states.
63    fn get_current_sensor_states(&self) -> Result<Vec<CurrentSensorState>, CombinedDeviceError>;
64}