use serde_json::{json, Map, Value as Json};
use zigbee_host::Device;
use zigbee_primitives::Ieee;
use zigbee_zcl::{attr, cluster_id, Value};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Convert {
Scale(f64, u32),
Lux,
OnOff,
Bit(u8),
Raw,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Component {
Sensor,
BinarySensor,
Switch,
}
impl Component {
fn name(self) -> &'static str {
match self {
Component::Sensor => "sensor",
Component::BinarySensor => "binary_sensor",
Component::Switch => "switch",
}
}
}
#[derive(Debug)]
pub struct Expose {
pub cluster: u16,
pub attribute: u16,
pub field: &'static str,
pub convert: Convert,
pub unit: Option<&'static str>,
pub device_class: Option<&'static str>,
pub state_class: Option<&'static str>,
pub component: Component,
pub diagnostic: bool,
}
macro_rules! expose {
($cluster:expr, $attr:expr, $field:literal, $convert:expr, $unit:expr, $class:expr, $state:expr, $component:expr, $diag:expr) => {
Expose {
cluster: $cluster,
attribute: $attr,
field: $field,
convert: $convert,
unit: $unit,
device_class: $class,
state_class: $state,
component: $component,
diagnostic: $diag,
}
};
}
const M: Option<&str> = Some("measurement");
pub static EXPOSES: &[Expose] = &[
expose!(
cluster_id::TEMPERATURE_MEASUREMENT,
attr::temperature_measurement::MEASURED_VALUE,
"temperature",
Convert::Scale(0.01, 2),
Some("°C"),
Some("temperature"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::RELATIVE_HUMIDITY_MEASUREMENT,
attr::relative_humidity_measurement::MEASURED_VALUE,
"humidity",
Convert::Scale(0.01, 2),
Some("%"),
Some("humidity"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::PRESSURE_MEASUREMENT,
attr::pressure_measurement::MEASURED_VALUE,
"pressure",
Convert::Scale(1.0, 1),
Some("hPa"),
Some("pressure"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::ILLUMINANCE_MEASUREMENT,
attr::illuminance_measurement::MEASURED_VALUE,
"illuminance",
Convert::Lux,
Some("lx"),
Some("illuminance"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::FLOW_MEASUREMENT,
attr::flow_measurement::MEASURED_VALUE,
"flow",
Convert::Scale(0.1, 1),
Some("m³/h"),
None,
M,
Component::Sensor,
false
),
expose!(
cluster_id::SOIL_MOISTURE_MEASUREMENT,
attr::soil_moisture_measurement::MEASURED_VALUE,
"soil_moisture",
Convert::Scale(0.01, 2),
Some("%"),
Some("moisture"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::CARBON_DIOXIDE_MEASUREMENT,
attr::carbon_dioxide_measurement::MEASURED_VALUE,
"co2",
Convert::Scale(1e6, 0),
Some("ppm"),
Some("carbon_dioxide"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::PM25_MEASUREMENT,
attr::pm25_measurement::MEASURED_VALUE,
"pm25",
Convert::Scale(1.0, 1),
Some("µg/m³"),
Some("pm25"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::OCCUPANCY_SENSING,
attr::occupancy_sensing::OCCUPANCY,
"occupancy",
Convert::Bit(0),
None,
Some("occupancy"),
None,
Component::BinarySensor,
false
),
expose!(
cluster_id::ON_OFF,
attr::on_off::ON_OFF,
"state",
Convert::OnOff,
None,
None,
None,
Component::Switch,
false
),
expose!(
cluster_id::POWER_CONFIGURATION,
attr::power_configuration::BATTERY_PERCENTAGE_REMAINING,
"battery",
Convert::Scale(0.5, 0),
Some("%"),
Some("battery"),
M,
Component::Sensor,
true
),
expose!(
cluster_id::POWER_CONFIGURATION,
attr::power_configuration::BATTERY_VOLTAGE,
"battery_voltage",
Convert::Scale(100.0, 0),
Some("mV"),
Some("voltage"),
M,
Component::Sensor,
true
),
expose!(
cluster_id::ELECTRICAL_MEASUREMENT,
attr::electrical_measurement::ACTIVE_POWER,
"power",
Convert::Scale(1.0, 1),
Some("W"),
Some("power"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::ELECTRICAL_MEASUREMENT,
attr::electrical_measurement::RMS_VOLTAGE,
"voltage",
Convert::Scale(1.0, 1),
Some("V"),
Some("voltage"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::ELECTRICAL_MEASUREMENT,
attr::electrical_measurement::RMS_CURRENT,
"current",
Convert::Scale(0.001, 3),
Some("A"),
Some("current"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::METERING,
attr::metering::CURRENT_SUMMATION_DELIVERED,
"energy",
Convert::Scale(0.001, 3),
Some("kWh"),
Some("energy"),
Some("total_increasing"),
Component::Sensor,
false
),
expose!(
cluster_id::THERMOSTAT,
attr::thermostat::LOCAL_TEMPERATURE,
"local_temperature",
Convert::Scale(0.01, 2),
Some("°C"),
Some("temperature"),
M,
Component::Sensor,
false
),
expose!(
cluster_id::DEVICE_TEMPERATURE_CONFIGURATION,
attr::device_temperature_configuration::CURRENT_TEMPERATURE,
"device_temperature",
Convert::Raw,
Some("°C"),
Some("temperature"),
M,
Component::Sensor,
true
),
];
pub fn find(cluster: u16, attribute: u16) -> Option<&'static Expose> {
EXPOSES
.iter()
.find(|e| e.cluster == cluster && e.attribute == attribute)
}
fn round(v: f64, decimals: u32) -> f64 {
let f = 10f64.powi(decimals as i32);
(v * f).round() / f
}
pub fn convert(expose: &Expose, value: &Value) -> Option<Json> {
match expose.convert {
Convert::OnOff => value
.as_bool()
.map(|on| json!(if on { "ON" } else { "OFF" })),
Convert::Bit(bit) => value.as_u64().map(|v| json!(v & (1 << bit) != 0)),
Convert::Lux => value.as_f64().map(|v| {
json!(round(
if v <= 0.0 {
0.0
} else {
10f64.powf((v - 1.0) / 10000.0)
},
0
))
}),
Convert::Scale(factor, decimals) => {
value.as_f64().map(|v| json!(round(v * factor, decimals)))
}
Convert::Raw => serde_json::to_value(value).ok().filter(|j| !j.is_null()),
}
}
pub fn field_name(device: &Device, endpoint: u8, expose: &Expose) -> String {
match device
.endpoints
.iter()
.find(|e| e.input_clusters.contains(&expose.cluster))
{
Some(first) if first.endpoint != endpoint => format!("{}_{endpoint}", expose.field),
_ => expose.field.to_string(),
}
}
pub fn ias_fields(zone_type: Option<u16>, status: u16) -> Vec<(&'static str, Json)> {
let alarm = status & 0x0001 != 0;
let main = match zone_type {
Some(0x0015) => ("contact", json!(!alarm)),
Some(0x000D) => ("occupancy", json!(alarm)),
Some(0x002A) => ("water_leak", json!(alarm)),
Some(0x0028) => ("smoke", json!(alarm)),
Some(0x002B) => ("carbon_monoxide", json!(alarm)),
Some(0x002D) => ("vibration", json!(alarm)),
_ => ("alarm", json!(alarm)),
};
vec![
main,
("tamper", json!(status & 0x0004 != 0)),
("battery_low", json!(status & 0x0008 != 0)),
]
}
fn ias_class(field: &str) -> Option<&'static str> {
Some(match field {
"contact" => "door",
"occupancy" => "motion",
"water_leak" => "moisture",
"smoke" => "smoke",
"carbon_monoxide" => "carbon_monoxide",
"vibration" => "vibration",
"tamper" => "tamper",
"battery_low" => "battery",
_ => "safety",
})
}
pub struct DiscoveryContext<'a> {
pub prefix: &'a str,
pub base: &'a str,
pub version: &'a str,
}
fn title(field: &str) -> String {
let mut out = String::new();
for (i, word) in field.split('_').enumerate() {
if i > 0 {
out.push(' ');
}
let mut chars = word.chars();
if let Some(c) = chars.next() {
out.extend(c.to_uppercase());
out.push_str(chars.as_str());
}
}
out.replace("Co2", "CO2").replace("Pm25", "PM2.5")
}
fn id(ieee: Ieee) -> String {
ieee.to_prefixed_hex()
}
pub fn discovery(
ctx: &DiscoveryContext<'_>,
device: &Device,
friendly: &str,
) -> Vec<(String, Json)> {
let ieee = id(device.ieee);
let state_topic = format!("{}/{friendly}", ctx.base);
let device_info = {
let mut d = Map::new();
d.insert(
"identifiers".into(),
json!([format!("zigbee-bridge_{ieee}")]),
);
d.insert("name".into(), json!(friendly));
if let Some(m) = &device.manufacturer {
d.insert("manufacturer".into(), json!(m));
}
if let Some(m) = &device.model {
d.insert("model".into(), json!(m));
}
if let Some(v) = &device.sw_build_id {
d.insert("sw_version".into(), json!(v));
}
Json::Object(d)
};
let base_payload = |field: &str, name: String| {
json!({
"name": name,
"unique_id": format!("{ieee}_{field}_zigbee-bridge"),
"object_id": format!("{}_{field}", friendly.replace(['/', ' '], "_")),
"state_topic": state_topic,
"value_template": format!("{{{{ value_json.{field} }}}}"),
"availability": [{"topic": format!("{}/bridge/state", ctx.base), "value_template": "{{ value_json.state }}"}],
"device": device_info,
"origin": {"name": "zigbee-bridge", "sw_version": ctx.version},
})
};
let config_topic = |component: &str, field: &str| {
format!(
"{}/{component}/{}/{field}/config",
ctx.prefix,
ieee.trim_start_matches("0x")
)
};
let mut out = Vec::new();
for ep in &device.endpoints {
let has = |c: u16| ep.input_clusters.contains(&c);
let light = has(cluster_id::ON_OFF) && has(cluster_id::LEVEL_CONTROL);
if light {
let field = match device
.endpoints
.iter()
.find(|e| e.input_clusters.contains(&cluster_id::ON_OFF))
{
Some(first) if first.endpoint != ep.endpoint => format!("light_{}", ep.endpoint),
_ => "light".to_string(),
};
let mut p = base_payload(&field, "Light".into());
if let Some(o) = p.as_object_mut() {
o.remove("value_template");
o.insert("schema".into(), json!("json"));
o.insert("command_topic".into(), json!(format!("{state_topic}/set")));
o.insert("brightness".into(), json!(true));
o.insert("brightness_scale".into(), json!(254));
if has(cluster_id::COLOR_CONTROL) {
o.insert("supported_color_modes".into(), json!(["color_temp"]));
}
}
out.push((config_topic("light", &field), p));
}
for e in EXPOSES.iter().filter(|e| has(e.cluster)) {
if light && e.cluster == cluster_id::ON_OFF {
continue;
}
if e.cluster == cluster_id::POWER_CONFIGURATION
&& device
.power_source
.is_some_and(|p| p & 0x7F == 0x01 || p & 0x7F == 0x04)
{
continue;
}
let field = field_name(device, ep.endpoint, e);
let mut p = base_payload(&field, title(&field));
if let Some(o) = p.as_object_mut() {
if let Some(u) = e.unit {
o.insert("unit_of_measurement".into(), json!(u));
}
if let Some(c) = e.device_class {
o.insert("device_class".into(), json!(c));
}
if let Some(s) = e.state_class {
o.insert("state_class".into(), json!(s));
}
if e.diagnostic {
o.insert("entity_category".into(), json!("diagnostic"));
}
match e.component {
Component::BinarySensor => {
o.insert("payload_on".into(), json!(true));
o.insert("payload_off".into(), json!(false));
}
Component::Switch => {
o.insert("command_topic".into(), json!(format!("{state_topic}/set")));
o.insert("payload_on".into(), json!(r#"{"state":"ON"}"#));
o.insert("payload_off".into(), json!(r#"{"state":"OFF"}"#));
o.insert("state_on".into(), json!("ON"));
o.insert("state_off".into(), json!("OFF"));
}
Component::Sensor => {}
}
}
out.push((config_topic(e.component.name(), &field), p));
}
if has(cluster_id::IAS_ZONE) {
for (field, _) in ias_fields(device.ias_zone_type, 0) {
let mut p = base_payload(field, title(field));
if let Some(o) = p.as_object_mut() {
o.insert("payload_on".into(), json!(field != "contact"));
o.insert("payload_off".into(), json!(field == "contact"));
if let Some(c) = ias_class(field) {
o.insert("device_class".into(), json!(c));
}
if field == "contact" {
o.insert("payload_on".into(), json!(false));
o.insert("payload_off".into(), json!(true));
}
if matches!(field, "tamper" | "battery_low") {
o.insert("entity_category".into(), json!("diagnostic"));
}
}
out.push((config_topic("binary_sensor", field), p));
}
}
}
let mut lq = base_payload("linkquality", "Link quality".into());
if let Some(o) = lq.as_object_mut() {
o.insert("unit_of_measurement".into(), json!("lqi"));
o.insert("state_class".into(), json!("measurement"));
o.insert("entity_category".into(), json!("diagnostic"));
o.insert("icon".into(), json!("mdi:signal"));
}
out.push((config_topic("sensor", "linkquality"), lq));
out
}
#[cfg(test)]
mod tests {
use super::*;
use zigbee_zdo::SimpleDescriptor;
fn device() -> Device {
let mut d = Device::new(Ieee(0x00124B0000000001), 0x1234, 0);
d.model = Some("SIM-TH1".into());
d.power_source = Some(3);
d.endpoints = vec![SimpleDescriptor {
endpoint: 1,
profile_id: 0x0104,
device_id: 0x0302,
device_version: 1,
input_clusters: vec![0, 1, 0x0402, 0x0405],
output_clusters: vec![],
}];
d
}
#[test]
fn converts_standard_readings() {
let t = find(cluster_id::TEMPERATURE_MEASUREMENT, 0).unwrap();
assert_eq!(convert(t, &Value::i16(2150)), Some(json!(21.5)));
assert_eq!(
convert(t, &Value::Int(zigbee_zcl::DataType::Int16, -32768)),
None
);
let b = find(
cluster_id::POWER_CONFIGURATION,
attr::power_configuration::BATTERY_PERCENTAGE_REMAINING,
)
.unwrap();
assert_eq!(convert(b, &Value::u8(200)), Some(json!(100.0)));
let l = find(cluster_id::ILLUMINANCE_MEASUREMENT, 0).unwrap();
assert_eq!(convert(l, &Value::u16(10001)), Some(json!(10.0)));
let o = find(cluster_id::ON_OFF, 0).unwrap();
assert_eq!(convert(o, &Value::bool(true)), Some(json!("ON")));
assert_eq!(
ias_fields(Some(0x0015), 0x0001)[0],
("contact", json!(false))
);
}
#[test]
fn discovery_describes_each_reading() {
let d = device();
let ctx = DiscoveryContext {
prefix: "homeassistant",
base: "zigbee",
version: "0.1.0",
};
let messages = discovery(&ctx, &d, "0x00124b0000000001");
let topics: Vec<&str> = messages.iter().map(|(t, _)| t.as_str()).collect();
assert!(topics.contains(&"homeassistant/sensor/00124b0000000001/temperature/config"));
assert!(topics.contains(&"homeassistant/sensor/00124b0000000001/battery/config"));
assert!(topics.contains(&"homeassistant/sensor/00124b0000000001/linkquality/config"));
let (_, t) = messages
.iter()
.find(|(t, _)| t.ends_with("/temperature/config"))
.unwrap();
assert_eq!(t["state_topic"], "zigbee/0x00124b0000000001");
assert_eq!(t["value_template"], "{{ value_json.temperature }}");
assert_eq!(t["device_class"], "temperature");
assert_eq!(
t["unique_id"],
"0x00124b0000000001_temperature_zigbee-bridge"
);
}
}