hass_rs/types/
services.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use std::collections::HashMap;
4use std::fmt;
5
6/// This object represents the collection of Home Assistant Services
7///
8/// This will get a dump of the current services in Home Assistant.
9/// [Fetch Services](https://developers.home-assistant.io/docs/api/websocket/#fetching-services)
10#[derive(Debug, Serialize, Deserialize, PartialEq)]
11pub struct HassServices(pub Domain);
12
13/// This is part of HassServices
14pub type Domain = HashMap<String, ServiceName>;
15
16/// This is part of HassServices
17pub type ServiceName = HashMap<String, HassService>;
18
19/// This object represents the Home Assistant Service
20///
21/// This will get a dump of the current services in Home Assistant.
22/// [Fetch Services](https://developers.home-assistant.io/docs/api/websocket/#fetching-services)
23#[derive(Debug, Serialize, Deserialize, PartialEq)]
24pub struct HassService {
25    pub name: Option<String>,
26    pub description: Option<String>,
27    pub fields: FieldName,
28    //pub response: Option<bool>,
29}
30
31/// This is part of HassService
32pub type FieldName = HashMap<String, Field>;
33
34///This is part of HassService
35#[derive(Debug, Serialize, Deserialize, PartialEq)]
36pub struct Field {
37    pub name: Option<String>,
38    pub description: Option<String>,
39    pub example: Option<Value>,
40}
41
42impl fmt::Display for HassServices {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "HassServices {{\n")?;
45        write!(f, "  domain: {{\n")?;
46        for (domain_name, service_name) in &self.0 {
47            write!(f, "    {}: {{\n", domain_name)?;
48            for (service_name, hass_service) in service_name {
49                write!(f, "      {}: {{\n", service_name)?;
50                write!(f, "        name: {:?},\n", hass_service.name)?;
51                write!(f, "        description: {:?},\n", hass_service.description)?;
52                write!(f, "        fields: {{\n")?;
53                for (field_name, field) in &hass_service.fields {
54                    write!(f, "          {}: {{\n", field_name)?;
55                    write!(f, "            name: {:?},\n", field.name)?;
56                    write!(f, "            description: {:?},\n", field.description)?;
57                    write!(f, "            example: {:?},\n", field.example)?;
58                    write!(f, "          }},\n")?;
59                }
60                write!(f, "        }},\n")?;
61                write!(f, "      }},\n")?;
62            }
63            write!(f, "    }},\n")?;
64        }
65        write!(f, "  }},\n")?;
66        write!(f, "}}")?;
67        Ok(())
68    }
69}
70
71impl fmt::Display for HassService {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        write!(f, "    name: {:?},\n", self.name)?;
74        write!(f, "    description: {:?},\n", self.description)?;
75        write!(f, "    fields: {{\n")?;
76        for (field_name, field) in &self.fields {
77            write!(f, "      {}: {{\n", field_name)?;
78            write!(f, "          name: {:?},\n", field.name)?;
79            write!(f, "          description: {:?},\n", field.description)?;
80            write!(f, "          example: {:?},\n", field.example)?;
81            write!(f, "          }},\n")?;
82        }
83        Ok(())
84    }
85}
86
87impl HassServices {
88    pub fn list_domains(&self) -> Vec<String> {
89        self.0.keys().cloned().collect()
90    }
91
92    pub fn list_services(&self, user_provided_domain: &str) -> Option<Vec<(String, &HassService)>> {
93        self.0.get(user_provided_domain).map(|service_name| {
94            service_name
95                .iter()
96                .map(|(name, hass_service)| (name.clone(), hass_service))
97                .collect()
98        })
99    }
100}