hass_rs/types/
response.rs

1use crate::types::HassEvent;
2use crate::HassResult;
3
4use serde::Deserialize;
5use serde_json::Value;
6
7///The tag identifying which variant we are dealing with is inside of the content,
8/// next to any other fields of the variant.
9#[derive(Debug, Deserialize)]
10#[serde(tag = "type", rename_all = "snake_case")]
11pub enum Response {
12    //request to autheticate
13    AuthRequired(AuthRequired),
14    //authetication suceeded
15    #[allow(unused)]
16    AuthOk(AuthOk),
17    //authetication failed
18    AuthInvalid(AuthInvalid),
19    //general response from server
20    Result(WSResult),
21    //response to ping request
22    Pong(WSPong),
23    //received when subscribed to event
24    Event(WSEvent),
25    //when the server close the websocket connection
26    #[allow(unused)]
27    Close(String),
28}
29
30impl Response {
31    pub fn id(&self) -> Option<u64> {
32        match self {
33            Self::AuthRequired(_) | Self::AuthOk(_) | Self::AuthInvalid(_) | Self::Close(_) => None,
34            Self::Pong(pong) => Some(pong.id),
35            Self::Result(result) => Some(result.id),
36            Self::Event(event) => Some(event.id),
37        }
38    }
39}
40
41// this is the first message received from websocket,
42// that ask to provide a authetication method
43#[derive(Debug, Deserialize, PartialEq)]
44#[serde(rename_all = "snake_case")]
45pub struct AuthRequired {
46    pub ha_version: String,
47}
48
49// this is received when the service successfully autheticate
50#[derive(Debug, Deserialize, PartialEq)]
51#[serde(rename_all = "snake_case")]
52pub struct AuthOk {
53    pub ha_version: String,
54}
55
56// this is received if the authetication failed
57#[derive(Debug, Deserialize, PartialEq)]
58#[serde(rename_all = "snake_case")]
59pub struct AuthInvalid {
60    pub message: String,
61}
62
63// this is received as a response to a ping request
64#[derive(Debug, Deserialize, PartialEq)]
65pub struct WSPong {
66    pub id: u64,
67}
68
69///	This object represents the Home Assistant Event
70///
71/// received when the client is subscribed to
72/// [Subscribe to events](https://developers.home-assistant.io/docs/api/websocket/#subscribe-to-events)
73#[derive(Debug, Deserialize, PartialEq, Clone)]
74pub struct WSEvent {
75    pub id: u64,
76    pub event: HassEvent,
77}
78
79///this is the general response from the Websocket server when a requesthas been sent
80///
81/// if "success" is true, then the "result" can be checked
82/// if "suceess" is false, then the "error" should be further explored
83#[derive(Debug, Deserialize, PartialEq)]
84pub struct WSResult {
85    pub id: u64,
86    success: bool,
87    result: Option<Value>,
88    error: Option<ErrorCode>,
89}
90
91impl WSResult {
92    pub fn is_ok(&self) -> bool {
93        self.success
94    }
95
96    pub fn is_err(&self) -> bool {
97        !self.success
98    }
99
100    pub fn result(self) -> HassResult<Value> {
101        if self.success {
102            if let Some(result) = self.result {
103                return Ok(result);
104            }
105        }
106        Err(crate::HassError::ResponseError(self))
107    }
108}
109
110#[derive(Debug, Deserialize, PartialEq)]
111pub struct ErrorCode {
112    pub code: String,
113    pub message: String,
114}