1use serde::{Deserialize, Serialize};
15use serde_json::{Map, Value as Json};
16
17#[cfg(feature = "unstable-asyncapi-docs")]
18use asyncapi_rust::ToAsyncApiMessage;
19#[cfg(feature = "unstable-asyncapi-docs")]
20use schemars::JsonSchema;
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
28#[cfg_attr(
29 feature = "unstable-asyncapi-docs",
30 derive(JsonSchema, ToAsyncApiMessage)
31)]
32#[cfg_attr(
33 feature = "unstable-asyncapi-docs",
34 asyncapi(summary = "The bridge is online or offline")
35)]
36pub struct BridgeState {
37 pub state: Availability,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
44#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
45#[serde(rename_all = "snake_case")]
46pub enum Availability {
47 Online,
49 Offline,
51}
52
53impl BridgeState {
54 pub fn payload(state: Availability) -> Vec<u8> {
56 serde_json::to_vec(&BridgeState { state }).unwrap_or_default()
57 }
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
66#[cfg_attr(
67 feature = "unstable-asyncapi-docs",
68 derive(JsonSchema, ToAsyncApiMessage)
69)]
70#[cfg_attr(
71 feature = "unstable-asyncapi-docs",
72 asyncapi(summary = "The bridge's version, coordinator, network and join state")
73)]
74pub struct BridgeInfo {
75 pub version: String,
77 pub coordinator: CoordinatorInfo,
79 pub network: NetworkInfo,
81 pub permit_join: bool,
83 pub permit_join_timeout: u8,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
89#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
90pub struct CoordinatorInfo {
91 pub ieee_address: String,
93 #[serde(rename = "type")]
95 pub kind: String,
96 pub firmware: String,
98 pub protocol: u8,
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize)]
104#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
105pub struct NetworkInfo {
106 pub channel: u8,
108 pub pan_id: u16,
110 pub extended_pan_id: String,
112 pub update_id: u8,
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
119#[cfg_attr(
120 feature = "unstable-asyncapi-docs",
121 derive(JsonSchema, ToAsyncApiMessage)
122)]
123#[cfg_attr(
124 feature = "unstable-asyncapi-docs",
125 asyncapi(summary = "Every device and its interview")
126)]
127#[serde(transparent)]
128pub struct BridgeDevices(pub Vec<DeviceSummary>);
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
132#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
133pub struct DeviceSummary {
134 pub ieee_address: String,
136 pub network_address: u16,
138 pub friendly_name: String,
141 pub manufacturer: Option<String>,
143 pub model: Option<String>,
145 pub power_source: Option<u8>,
148 pub software_build_id: Option<String>,
150 pub date_code: Option<String>,
152 #[serde(rename = "type")]
155 pub kind: Option<LogicalType>,
156 pub interview: InterviewSummary,
158 pub unique_link_key: bool,
160 pub endpoints: Vec<EndpointSummary>,
162}
163
164#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
166#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
167#[serde(rename_all = "snake_case")]
168pub enum LogicalType {
169 Coordinator,
171 Router,
173 EndDevice,
175 Unknown,
177}
178
179impl From<zigbee_zdo::LogicalType> for LogicalType {
180 fn from(t: zigbee_zdo::LogicalType) -> Self {
181 match t {
182 zigbee_zdo::LogicalType::Coordinator => LogicalType::Coordinator,
183 zigbee_zdo::LogicalType::Router => LogicalType::Router,
184 zigbee_zdo::LogicalType::EndDevice => LogicalType::EndDevice,
185 zigbee_zdo::LogicalType::Reserved(_) => LogicalType::Unknown,
186 }
187 }
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
192#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
193pub struct InterviewSummary {
194 pub stage: InterviewStage,
196 pub done: bool,
198 pub error: Option<String>,
201}
202
203#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
205#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
206#[serde(rename_all = "snake_case")]
207pub enum InterviewStage {
208 NodeDescriptor,
210 ActiveEndpoints,
212 SimpleDescriptors,
214 Basic,
216 Configure,
218 Done,
220}
221
222impl From<zigbee_host::InterviewStage> for InterviewStage {
223 fn from(s: zigbee_host::InterviewStage) -> Self {
224 use zigbee_host::InterviewStage as S;
225 match s {
226 S::NodeDescriptor => InterviewStage::NodeDescriptor,
227 S::ActiveEndpoints => InterviewStage::ActiveEndpoints,
228 S::SimpleDescriptors => InterviewStage::SimpleDescriptors,
229 S::Basic => InterviewStage::Basic,
230 S::Configure => InterviewStage::Configure,
231 S::Done => InterviewStage::Done,
232 }
233 }
234}
235
236#[derive(Debug, Clone, Serialize, Deserialize)]
238#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
239pub struct EndpointSummary {
240 pub endpoint: u8,
242 pub profile_id: u16,
244 pub device_id: u16,
246 pub input_clusters: Vec<String>,
249 pub output_clusters: Vec<String>,
251}
252
253#[derive(Debug, Clone, Serialize, Deserialize)]
259#[cfg_attr(
260 feature = "unstable-asyncapi-docs",
261 derive(JsonSchema, ToAsyncApiMessage)
262)]
263#[serde(tag = "type", content = "data")]
264pub enum BridgeEvent {
265 #[serde(rename = "permit_join")]
267 #[cfg_attr(
268 feature = "unstable-asyncapi-docs",
269 asyncapi(summary = "Joining opened or closed")
270 )]
271 PermitJoin {
272 time: u8,
274 device: Option<String>,
276 },
277 #[serde(rename = "device_joined")]
279 #[cfg_attr(
280 feature = "unstable-asyncapi-docs",
281 asyncapi(summary = "A device joined")
282 )]
283 DeviceJoined {
284 ieee_address: String,
286 network_address: u16,
288 rejoin: bool,
290 },
291 #[serde(rename = "device_announce")]
293 #[cfg_attr(
294 feature = "unstable-asyncapi-docs",
295 asyncapi(summary = "A device announced itself")
296 )]
297 DeviceAnnounce {
298 ieee_address: String,
300 network_address: u16,
302 },
303 #[serde(rename = "device_leave")]
305 #[cfg_attr(
306 feature = "unstable-asyncapi-docs",
307 asyncapi(summary = "A device left")
308 )]
309 DeviceLeave {
310 ieee_address: String,
312 },
313 #[serde(rename = "device_removed")]
315 #[cfg_attr(
316 feature = "unstable-asyncapi-docs",
317 asyncapi(summary = "The bridge removed a device")
318 )]
319 DeviceRemoved {
320 ieee_address: String,
322 reason: String,
324 },
325 #[serde(rename = "device_interview")]
327 #[cfg_attr(
328 feature = "unstable-asyncapi-docs",
329 asyncapi(summary = "An interview started, finished or failed")
330 )]
331 DeviceInterview(InterviewEvent),
332 #[serde(rename = "ota_available")]
335 #[cfg_attr(
336 feature = "unstable-asyncapi-docs",
337 asyncapi(summary = "A firmware update is available")
338 )]
339 OtaAvailable {
340 device: String,
342 ieee_address: String,
344 installed_version: u32,
346 latest_version: u32,
348 },
349 #[serde(rename = "ota_progress")]
351 #[cfg_attr(
352 feature = "unstable-asyncapi-docs",
353 asyncapi(summary = "A firmware download's progress")
354 )]
355 OtaProgress {
356 device: String,
358 ieee_address: String,
360 progress: u8,
362 },
363 #[serde(rename = "ota_finished")]
365 #[cfg_attr(
366 feature = "unstable-asyncapi-docs",
367 asyncapi(summary = "A firmware update ended")
368 )]
369 OtaFinished {
370 device: String,
372 ieee_address: String,
374 from_version: u32,
376 to_version: u32,
378 success: bool,
380 status: u8,
382 },
383 #[serde(rename = "channel_changed")]
385 #[cfg_attr(
386 feature = "unstable-asyncapi-docs",
387 asyncapi(summary = "The network changed channel")
388 )]
389 ChannelChanged {
390 channel: u8,
392 update_id: u8,
394 },
395}
396
397#[derive(Debug, Clone, Serialize, Deserialize)]
399#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
400#[serde(tag = "status")]
401pub enum InterviewEvent {
402 #[serde(rename = "started")]
404 Started {
405 ieee_address: String,
407 },
408 #[serde(rename = "successful")]
410 Successful {
411 ieee_address: String,
413 manufacturer: Option<String>,
415 model: Option<String>,
417 definition: Box<DeviceSummary>,
419 },
420 #[serde(rename = "failed")]
422 Failed {
423 ieee_address: String,
425 stage: InterviewStage,
427 error: String,
429 },
430}
431
432#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
443#[cfg_attr(feature = "unstable-asyncapi-docs", derive(ToAsyncApiMessage))]
444#[cfg_attr(
445 feature = "unstable-asyncapi-docs",
446 asyncapi(summary = "A device's readings")
447)]
448#[serde(transparent)]
449pub struct DeviceState(pub Map<String, Json>);
450
451#[cfg(feature = "unstable-asyncapi-docs")]
452impl JsonSchema for DeviceState {
453 fn schema_name() -> std::borrow::Cow<'static, str> {
454 "DeviceState".into()
455 }
456
457 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
460 use crate::exposes::{self, Convert, EXPOSES};
461 use serde_json::json;
462 let mut properties = Map::new();
463 properties.insert(
464 "state".into(),
465 json!({"description": "On/off cluster: the switch or light is on.", "enum": ["ON", "OFF"]}),
466 );
467 properties.insert(
468 "brightness".into(),
469 json!({"description": "Level control cluster: the current level.", "type": "integer", "minimum": 0, "maximum": 254}),
470 );
471 for e in EXPOSES {
472 let cluster = zigbee_zcl::cluster(e.cluster)
473 .map(|c| c.name.replace('_', " "))
474 .unwrap_or_else(|| format!("{:#06x}", e.cluster));
475 let unit = e.unit.map(|u| format!(", in {u}")).unwrap_or_default();
476 let schema = match e.convert {
477 Convert::OnOff => json!({"enum": ["ON", "OFF"]}),
478 Convert::Bit(_) => json!({"type": "boolean"}),
479 Convert::Raw => json!({"type": "integer"}),
480 Convert::Scale(..) | Convert::Lux => json!({"type": "number"}),
481 };
482 let mut schema = schema.as_object().cloned().unwrap_or_default();
483 let description = format!("{}{unit}.", capitalize(&cluster));
484 schema.insert("description".into(), description.into());
485 properties
486 .entry(e.field.to_string())
487 .or_insert(Json::Object(schema));
488 }
489 for zone_type in [
490 None,
491 Some(0x0015),
492 Some(0x000D),
493 Some(0x002A),
494 Some(0x0028),
495 Some(0x002B),
496 Some(0x002D),
497 ] {
498 for (field, _) in exposes::ias_fields(zone_type, 0) {
499 properties.entry(field.to_string()).or_insert_with(|| {
500 json!({"description": format!("IAS zone: {}.", field.replace('_', " ")), "type": "boolean"})
501 });
502 }
503 }
504 properties.insert(
505 "linkquality".into(),
506 json!({"description": "The link quality of the device's last message, 0 to 255.", "type": "integer", "minimum": 0, "maximum": 255}),
507 );
508 schemars::json_schema!({
509 "type": "object",
510 "description": "The device's readings. Which keys appear depends on its clusters; a reading from a second endpoint with the same cluster has the endpoint number as a suffix, as in `temperature_2`.",
511 "properties": properties,
512 "additionalProperties": true,
513 })
514 }
515}
516
517#[cfg(feature = "unstable-asyncapi-docs")]
518fn capitalize(s: &str) -> String {
519 let mut c = s.chars();
520 match c.next() {
521 Some(f) => f.to_uppercase().chain(c).collect(),
522 None => String::new(),
523 }
524}
525
526#[derive(Debug, Clone, Default, Serialize, Deserialize)]
528#[cfg_attr(
529 feature = "unstable-asyncapi-docs",
530 derive(JsonSchema, ToAsyncApiMessage)
531)]
532#[cfg_attr(
533 feature = "unstable-asyncapi-docs",
534 asyncapi(summary = "Switch, dim or tune a device")
535)]
536pub struct SetRequest {
537 #[serde(default, skip_serializing_if = "Option::is_none")]
540 pub state: Option<String>,
541 #[serde(default, skip_serializing_if = "Option::is_none")]
543 pub brightness: Option<u64>,
544 #[serde(default, skip_serializing_if = "Option::is_none")]
546 pub color_temp: Option<u64>,
547 #[serde(default, skip_serializing_if = "Option::is_none")]
550 pub transition: Option<f64>,
551}
552
553#[derive(Debug, Clone, Serialize, Deserialize)]
555#[cfg_attr(
556 feature = "unstable-asyncapi-docs",
557 derive(JsonSchema, ToAsyncApiMessage)
558)]
559#[cfg_attr(
560 feature = "unstable-asyncapi-docs",
561 asyncapi(summary = "Every attribute report, raw, by cluster and attribute name")
562)]
563pub struct AttributeReport {
564 pub endpoint: u8,
566 pub cluster: String,
568 pub attributes: Map<String, Json>,
570}
571
572#[derive(Debug, Clone, Serialize, Deserialize)]
575#[cfg_attr(
576 feature = "unstable-asyncapi-docs",
577 derive(JsonSchema, ToAsyncApiMessage)
578)]
579#[cfg_attr(
580 feature = "unstable-asyncapi-docs",
581 asyncapi(summary = "A command the device sent: a button press, an alarm")
582)]
583pub struct DeviceAction {
584 pub endpoint: u8,
586 pub cluster: String,
588 pub command: String,
590 pub fields: Map<String, Json>,
592}
593
594#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
599#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
600#[serde(rename_all = "snake_case")]
601pub enum Status {
602 Ok,
604 Error,
606}
607
608macro_rules! response {
609 ($(#[$doc:meta])* $name:ident, $data:ty, $summary:literal) => {
610 $(#[$doc])*
611 #[derive(Debug, Clone, Serialize, Deserialize)]
615 #[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema, ToAsyncApiMessage))]
616 #[cfg_attr(feature = "unstable-asyncapi-docs", asyncapi(summary = $summary))]
617 pub struct $name {
618 pub status: Status,
620 #[serde(default, skip_serializing_if = "Option::is_none")]
622 pub data: Option<$data>,
623 #[serde(default, skip_serializing_if = "Option::is_none")]
625 pub error: Option<String>,
626 #[serde(default, skip_serializing_if = "Option::is_none")]
628 pub transaction: Option<Json>,
629 }
630
631 impl $name {
632 pub fn new(result: Result<$data, String>, transaction: Option<Json>) -> Self {
634 match result {
635 Ok(data) => $name { status: Status::Ok, data: Some(data), error: None, transaction },
636 Err(error) => $name { status: Status::Error, data: None, error: Some(error), transaction },
637 }
638 }
639 }
640 };
641}
642
643macro_rules! request {
644 ($(#[$doc:meta])* $name:ident, $summary:literal { $($(#[$fdoc:meta])* $field:ident : $ty:ty),* $(,)? }) => {
645 $(#[$doc])*
646 #[derive(Debug, Clone, Default, Serialize, Deserialize)]
647 #[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema, ToAsyncApiMessage))]
648 #[cfg_attr(feature = "unstable-asyncapi-docs", asyncapi(summary = $summary))]
649 pub struct $name {
650 $($(#[$fdoc])* #[serde(default)] pub $field: $ty,)*
651 #[serde(default, skip_serializing_if = "Option::is_none")]
654 pub transaction: Option<Json>,
655 }
656 };
657}
658
659request!(
660 PermitJoinRequest, "Open or close joining" {
662 time: Option<u64>,
665 value: Option<bool>,
668 device: Option<String>,
670 }
671);
672
673#[derive(Debug, Clone, Serialize, Deserialize)]
675#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
676pub struct PermitJoinData {
677 pub time: u8,
679}
680
681response!(
682 PermitJoinResponse, PermitJoinData, "Joining is open or closed"
684);
685
686request!(
687 RemoveRequest, "Remove a device" {
689 #[serde(alias = "id")]
691 device: Option<String>,
692 force: bool,
694 }
695);
696
697#[derive(Debug, Clone, Serialize, Deserialize)]
699#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
700pub struct DeviceData {
701 pub device: String,
703}
704
705response!(
706 RemoveResponse, DeviceData, "The device is removed"
708);
709
710request!(
711 RenameRequest, "Rename a device" {
714 #[serde(alias = "device")]
716 from: Option<String>,
717 #[serde(alias = "name")]
720 to: Option<String>,
721 }
722);
723
724#[derive(Debug, Clone, Serialize, Deserialize)]
726#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
727pub struct RenameData {
728 pub from: String,
730 pub to: String,
732}
733
734response!(
735 RenameResponse, RenameData, "The device is renamed"
737);
738
739request!(
740 InstallCodeRequest, "Let a device join with its install code" {
743 #[serde(alias = "device")]
745 ieee_address: Option<String>,
746 code: Option<String>,
749 }
750);
751
752#[derive(Debug, Clone, Serialize, Deserialize)]
754#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
755pub struct IeeeData {
756 pub ieee_address: String,
758}
759
760response!(
761 InstallCodeResponse, IeeeData, "The install code is added"
763);
764
765request!(
766 InterviewRequest, "Interview a device again" {
768 device: Option<String>,
770 }
771);
772
773response!(
774 InterviewResponse, DeviceData, "The interview is queued"
776);
777
778request!(
779 BackupRequest, "Back up the network" {}
783);
784
785#[derive(Debug, Clone, Serialize, Deserialize)]
787#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
788pub struct BackupData {
789 pub path: String,
791 pub devices: usize,
793 pub frame_counter: u32,
795}
796
797response!(
798 BackupResponse, BackupData, "The backup is written"
800);
801
802request!(
803 TopologyRequest, "The network's links" {}
806);
807
808#[derive(Debug, Clone, Serialize, Deserialize)]
810#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
811pub struct TopologyData {
812 pub links: Vec<TopologyLink>,
814}
815
816#[derive(Debug, Clone, Serialize, Deserialize)]
819#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
820pub struct TopologyLink {
821 pub from: Option<String>,
823 pub to: Option<String>,
825 pub from_network_address: u16,
827 pub to_network_address: u16,
829 pub lqi: Option<u8>,
831 pub rssi: Option<i8>,
833 pub source: LinkSource,
835 pub seen: u64,
837}
838
839#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
841#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
842#[serde(rename_all = "snake_case")]
843pub enum LinkSource {
844 LastHop,
846 RouteRecord,
848 NeighborTable,
850}
851
852impl From<zigbee_host::topology::LinkSource> for LinkSource {
853 fn from(s: zigbee_host::topology::LinkSource) -> Self {
854 use zigbee_host::topology::LinkSource as S;
855 match s {
856 S::LastHop => LinkSource::LastHop,
857 S::RouteRecord => LinkSource::RouteRecord,
858 S::NeighborTable => LinkSource::NeighborTable,
859 }
860 }
861}
862
863response!(
864 TopologyResponse, TopologyData, "The network's links"
866);
867
868request!(
869 ScanNeighborsRequest, "Read a router's neighbor table" {
872 device: Option<String>,
874 }
875);
876
877#[derive(Debug, Clone, Serialize, Deserialize)]
879#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
880pub struct ScanNeighborsData {
881 pub neighbors: Vec<Neighbor>,
883}
884
885#[derive(Debug, Clone, Serialize, Deserialize)]
887#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
888pub struct Neighbor {
889 pub extended_pan_id: String,
891 pub ieee: String,
893 pub nwk: u16,
895 pub flags: u8,
898 pub permit_joining: u8,
900 pub depth: u8,
902 pub lqi: u8,
904}
905
906impl From<zigbee_zdo::Neighbor> for Neighbor {
907 fn from(n: zigbee_zdo::Neighbor) -> Self {
908 Neighbor {
909 extended_pan_id: n.extended_pan_id.to_string(),
910 ieee: n.ieee.to_string(),
911 nwk: n.nwk,
912 flags: n.flags,
913 permit_joining: n.permit_joining,
914 depth: n.depth,
915 lqi: n.lqi,
916 }
917 }
918}
919
920response!(
921 ScanNeighborsResponse, ScanNeighborsData, "The router's neighbors"
923);
924
925request!(
926 OtaCheckRequest, "Check a device for a firmware update" {
930 device: Option<String>,
932 }
933);
934
935#[derive(Debug, Clone, Serialize, Deserialize)]
937#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
938pub struct OtaData {
939 pub device: String,
941 pub installed_version: u32,
943 pub latest_version: Option<u32>,
945 pub size: Option<u32>,
947 pub updating: bool,
949}
950
951response!(
952 OtaCheckResponse, OtaData, "Whether a firmware update is available"
954);
955
956request!(
957 OtaUpdateRequest, "Update a device's firmware" {
961 device: Option<String>,
963 }
964);
965
966response!(
967 OtaUpdateResponse, OtaData, "The firmware update started"
969);
970
971request!(
972 ChangeChannelRequest, "Move the network to another channel" {
975 channel: Option<u64>,
977 }
978);
979
980#[derive(Debug, Clone, Serialize, Deserialize)]
982#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
983pub struct ChannelData {
984 pub channel: u8,
986 pub update_id: u8,
988}
989
990response!(
991 ChangeChannelResponse, ChannelData, "The network changed channel"
993);
994
995request!(
996 EnergyScanRequest, "Measure the noise on every channel" {}
998);
999
1000#[derive(Debug, Clone, Serialize, Deserialize)]
1002#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
1003pub struct EnergyScanData {
1004 pub channels: Vec<ChannelEnergy>,
1006}
1007
1008#[derive(Debug, Clone, Serialize, Deserialize)]
1010#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
1011pub struct ChannelEnergy {
1012 pub channel: u8,
1014 pub max_rssi: i8,
1016}
1017
1018response!(
1019 EnergyScanResponse, EnergyScanData, "Each channel's noise"
1021);
1022
1023request!(
1024 DevicesRequest, "Publish the device list again" {}
1026);
1027
1028#[derive(Debug, Clone, Default, Serialize, Deserialize)]
1030#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
1031pub struct Empty {}
1032
1033response!(
1034 DevicesResponse, Empty, "The device list is published"
1036);
1037
1038response!(
1039 UnknownResponse, Empty, "The request is unknown"
1042);
1043
1044#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1052#[cfg_attr(
1053 feature = "unstable-asyncapi-docs",
1054 derive(JsonSchema, ToAsyncApiMessage)
1055)]
1056#[cfg_attr(
1057 feature = "unstable-asyncapi-docs",
1058 asyncapi(
1059 summary = "Home Assistant started or stopped",
1060 content_type = "text/plain"
1061 )
1062)]
1063#[serde(transparent)]
1064pub struct HomeAssistantStatus(pub HomeAssistantState);
1065
1066#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1068#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
1069#[serde(rename_all = "snake_case")]
1070pub enum HomeAssistantState {
1071 Online,
1073 Offline,
1075}
1076
1077impl HomeAssistantStatus {
1078 pub fn parse(payload: &[u8]) -> Option<HomeAssistantState> {
1080 match payload {
1081 b"online" => Some(HomeAssistantState::Online),
1082 b"offline" => Some(HomeAssistantState::Offline),
1083 _ => None,
1084 }
1085 }
1086}
1087
1088#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
1093#[cfg_attr(
1094 feature = "unstable-asyncapi-docs",
1095 derive(JsonSchema, ToAsyncApiMessage)
1096)]
1097#[cfg_attr(
1098 feature = "unstable-asyncapi-docs",
1099 asyncapi(summary = "A Home Assistant entity for one of a device's readings")
1100)]
1101pub struct DiscoveryConfig {
1102 pub name: String,
1104 pub unique_id: String,
1106 pub object_id: String,
1108 pub state_topic: String,
1110 #[serde(default, skip_serializing_if = "Option::is_none")]
1112 pub value_template: Option<String>,
1113 pub availability: Vec<DiscoveryAvailability>,
1115 pub device: DiscoveryDevice,
1117 pub origin: DiscoveryOrigin,
1119 #[serde(default, skip_serializing_if = "Option::is_none")]
1121 pub schema: Option<String>,
1122 #[serde(default, skip_serializing_if = "Option::is_none")]
1124 pub command_topic: Option<String>,
1125 #[serde(default, skip_serializing_if = "Option::is_none")]
1127 pub brightness: Option<bool>,
1128 #[serde(default, skip_serializing_if = "Option::is_none")]
1130 pub brightness_scale: Option<u16>,
1131 #[serde(default, skip_serializing_if = "Option::is_none")]
1133 pub supported_color_modes: Option<Vec<String>>,
1134 #[serde(default, skip_serializing_if = "Option::is_none")]
1136 pub unit_of_measurement: Option<String>,
1137 #[serde(default, skip_serializing_if = "Option::is_none")]
1139 pub device_class: Option<String>,
1140 #[serde(default, skip_serializing_if = "Option::is_none")]
1142 pub state_class: Option<String>,
1143 #[serde(default, skip_serializing_if = "Option::is_none")]
1145 pub entity_category: Option<String>,
1146 #[serde(default, skip_serializing_if = "Option::is_none")]
1149 pub payload_on: Option<DiscoveryPayload>,
1150 #[serde(default, skip_serializing_if = "Option::is_none")]
1153 pub payload_off: Option<DiscoveryPayload>,
1154 #[serde(default, skip_serializing_if = "Option::is_none")]
1156 pub state_on: Option<String>,
1157 #[serde(default, skip_serializing_if = "Option::is_none")]
1159 pub state_off: Option<String>,
1160 #[serde(default, skip_serializing_if = "Option::is_none")]
1162 pub icon: Option<String>,
1163}
1164
1165#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1167#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
1168#[serde(untagged)]
1169pub enum DiscoveryPayload {
1170 Bool(bool),
1172 Text(String),
1174}
1175
1176#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
1178#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
1179pub struct DiscoveryAvailability {
1180 pub topic: String,
1182 pub value_template: String,
1184}
1185
1186#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
1188#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
1189pub struct DiscoveryDevice {
1190 pub identifiers: Vec<String>,
1192 pub name: String,
1194 #[serde(default, skip_serializing_if = "Option::is_none")]
1196 pub manufacturer: Option<String>,
1197 #[serde(default, skip_serializing_if = "Option::is_none")]
1199 pub model: Option<String>,
1200 #[serde(default, skip_serializing_if = "Option::is_none")]
1202 pub sw_version: Option<String>,
1203}
1204
1205#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
1207#[cfg_attr(feature = "unstable-asyncapi-docs", derive(JsonSchema))]
1208pub struct DiscoveryOrigin {
1209 pub name: String,
1211 pub sw_version: String,
1213}