|
| 1 | +-- Copyright 2025 SmartThings |
| 2 | +-- |
| 3 | +-- Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +-- you may not use this file except in compliance with the License. |
| 5 | +-- You may obtain a copy of the License at |
| 6 | +-- |
| 7 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +-- |
| 9 | +-- Unless required by applicable law or agreed to in writing, software |
| 10 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +-- See the License for the specific language governing permissions and |
| 13 | +-- limitations under the License. |
| 14 | + |
| 15 | +local clusters = require "st.zigbee.zcl.clusters" |
| 16 | +local capabilities = require "st.capabilities" |
| 17 | +local custom_clusters = require "MultiIR/custom_clusters" |
| 18 | +local cluster_base = require "st.zigbee.cluster_base" |
| 19 | + |
| 20 | +local RelativeHumidity = clusters.RelativeHumidity |
| 21 | +local TemperatureMeasurement = clusters.TemperatureMeasurement |
| 22 | + |
| 23 | +local MultiIR_SENSOR_FINGERPRINTS = { |
| 24 | + { mfr = "MultiIR", model = "PMT1006S-SGM-ZTN" }--This is not a sleep end device |
| 25 | +} |
| 26 | + |
| 27 | +local function can_handle_MultiIR_sensor(opts, driver, device) |
| 28 | + for _, fingerprint in ipairs(MultiIR_SENSOR_FINGERPRINTS) do |
| 29 | + if device:get_manufacturer() == fingerprint.mfr and device:get_model() == fingerprint.model then |
| 30 | + return true |
| 31 | + end |
| 32 | + end |
| 33 | + return false |
| 34 | +end |
| 35 | + |
| 36 | +local function send_read_attr_request(device, cluster, attr) |
| 37 | + device:send( |
| 38 | + cluster_base.read_manufacturer_specific_attribute( |
| 39 | + device, |
| 40 | + cluster.id, |
| 41 | + attr.id, |
| 42 | + cluster.mfg_specific_code |
| 43 | + ) |
| 44 | + ) |
| 45 | +end |
| 46 | + |
| 47 | +local function do_refresh(driver, device) |
| 48 | + device:send(RelativeHumidity.attributes.MeasuredValue:read(device):to_endpoint(0x01)) |
| 49 | + device:send(TemperatureMeasurement.attributes.MeasuredValue:read(device):to_endpoint(0x01)) |
| 50 | + send_read_attr_request(device, custom_clusters.particulate_matter, custom_clusters.particulate_matter.attributes.pm2_5_MeasuredValue) |
| 51 | + send_read_attr_request(device, custom_clusters.particulate_matter, custom_clusters.particulate_matter.attributes.pm1_0_MeasuredValue) |
| 52 | + send_read_attr_request(device, custom_clusters.particulate_matter, custom_clusters.particulate_matter.attributes.pm10_MeasuredValue) |
| 53 | + send_read_attr_request(device, custom_clusters.unhealthy_gas, custom_clusters.unhealthy_gas.attributes.CH2O_MeasuredValue) |
| 54 | + send_read_attr_request(device, custom_clusters.unhealthy_gas, custom_clusters.unhealthy_gas.attributes.tvoc_MeasuredValue) |
| 55 | + send_read_attr_request(device, custom_clusters.carbonDioxide, custom_clusters.carbonDioxide.attributes.measured_value) |
| 56 | + send_read_attr_request(device, custom_clusters.AQI, custom_clusters.AQI.attributes.AQI_value) |
| 57 | +end |
| 58 | + |
| 59 | +local function airQualityHealthConcern_attr_handler(driver, device, value, zb_rx) |
| 60 | + local airQuality_level = "good" |
| 61 | + if value.value >= 51 then |
| 62 | + airQuality_level = "moderate" |
| 63 | + end |
| 64 | + if value.value >= 101 then |
| 65 | + airQuality_level = "slightlyUnhealthy" |
| 66 | + end |
| 67 | + if value.value >= 151 then |
| 68 | + airQuality_level = "unhealthy" |
| 69 | + end |
| 70 | + if value.value >= 201 then |
| 71 | + airQuality_level = "veryUnhealthy" |
| 72 | + end |
| 73 | + if value.value >= 301 then |
| 74 | + airQuality_level = "hazardous" |
| 75 | + end |
| 76 | + device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, capabilities.airQualityHealthConcern.airQualityHealthConcern({value = airQuality_level})) |
| 77 | +end |
| 78 | + |
| 79 | +local function carbonDioxide_attr_handler(driver, device, value, zb_rx) |
| 80 | + local level = "unhealthy" |
| 81 | + device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, capabilities.carbonDioxideMeasurement.carbonDioxide({value = value.value, unit = "ppm"})) |
| 82 | + if value.value <= 1500 then |
| 83 | + level = "good" |
| 84 | + elseif value.value >= 1501 and value.value <= 2500 then |
| 85 | + level = "moderate" |
| 86 | + end |
| 87 | + device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, capabilities.carbonDioxideHealthConcern.carbonDioxideHealthConcern({value = level})) |
| 88 | +end |
| 89 | + |
| 90 | +local function particulate_matter_attr_handler(cap,Concern,good,bad) |
| 91 | + return function(driver, device, value, zb_rx) |
| 92 | + local level = "unhealthy" |
| 93 | + device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, cap({value = value.value})) |
| 94 | + if value.value <= good then |
| 95 | + level = "good" |
| 96 | + elseif bad > 0 and value.value > good and value.value < bad then |
| 97 | + level = "moderate" |
| 98 | + end |
| 99 | + device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, Concern({value = level})) |
| 100 | + end |
| 101 | +end |
| 102 | + |
| 103 | +local function CH2O_attr_handler(driver, device, value, zb_rx) |
| 104 | + device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, capabilities.formaldehydeMeasurement.formaldehydeLevel({value = value.value, unit = "mg/m^3"})) |
| 105 | +end |
| 106 | + |
| 107 | +local function tvoc_attr_handler(driver, device, value, zb_rx) |
| 108 | + device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, capabilities.tvocMeasurement.tvocLevel({value = value.value, unit = "ug/m3"})) |
| 109 | + local level = "unhealthy" |
| 110 | + if value.value < 600.0 then |
| 111 | + level = "good" |
| 112 | + end |
| 113 | + device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, capabilities.tvocHealthConcern.tvocHealthConcern({value = level})) |
| 114 | +end |
| 115 | + |
| 116 | +local function added_handler(self, device) |
| 117 | + do_refresh() |
| 118 | +end |
| 119 | + |
| 120 | +local MultiIR_sensor = { |
| 121 | + NAME = "MultiIR air quality detector", |
| 122 | + lifecycle_handlers = { |
| 123 | + added = added_handler |
| 124 | + }, |
| 125 | + zigbee_handlers = { |
| 126 | + attr = { |
| 127 | + [custom_clusters.carbonDioxide.id] = { |
| 128 | + [custom_clusters.carbonDioxide.attributes.measured_value.id] = carbonDioxide_attr_handler |
| 129 | + }, |
| 130 | + [custom_clusters.particulate_matter.id] = { |
| 131 | + [custom_clusters.particulate_matter.attributes.pm2_5_MeasuredValue.id] = particulate_matter_attr_handler(capabilities.fineDustSensor.fineDustLevel,capabilities.fineDustHealthConcern.fineDustHealthConcern,75,115),--75 115 is a comparative value of good moderate unhealthy, and 0 is no comparison |
| 132 | + [custom_clusters.particulate_matter.attributes.pm1_0_MeasuredValue.id] = particulate_matter_attr_handler(capabilities.veryFineDustSensor.veryFineDustLevel,capabilities.veryFineDustHealthConcern.veryFineDustHealthConcern,100,0), |
| 133 | + [custom_clusters.particulate_matter.attributes.pm10_MeasuredValue.id] = particulate_matter_attr_handler(capabilities.dustSensor.dustLevel,capabilities.dustHealthConcern.dustHealthConcern,150,0) |
| 134 | + }, |
| 135 | + [custom_clusters.unhealthy_gas.id] = { |
| 136 | + [custom_clusters.unhealthy_gas.attributes.CH2O_MeasuredValue.id] = CH2O_attr_handler, |
| 137 | + [custom_clusters.unhealthy_gas.attributes.tvoc_MeasuredValue.id] = tvoc_attr_handler |
| 138 | + }, |
| 139 | + [custom_clusters.AQI.id] = { |
| 140 | + [custom_clusters.AQI.attributes.AQI_value.id] = airQualityHealthConcern_attr_handler |
| 141 | + } |
| 142 | + } |
| 143 | + }, |
| 144 | + capability_handlers = { |
| 145 | + [capabilities.refresh.ID] = { |
| 146 | + [capabilities.refresh.commands.refresh.NAME] = do_refresh, |
| 147 | + } |
| 148 | + }, |
| 149 | + can_handle = can_handle_MultiIR_sensor |
| 150 | +} |
| 151 | + |
| 152 | +return MultiIR_sensor |
0 commit comments