|
| 1 | +-- Copyright © 2025 SmartThings, Inc. |
| 2 | +-- Licensed under the Apache License, Version 2.0 |
| 3 | + |
| 4 | +local capabilities = require "st.capabilities" |
| 5 | +local clusters = require "st.matter.clusters" |
| 6 | +local embedded_cluster_utils = require "sensor_utils.embedded_cluster_utils" |
| 7 | +local fields = require "sub_drivers.air_quality_sensor.air_quality_sensor_utils.fields" |
| 8 | + |
| 9 | + |
| 10 | +local AirQualitySensorUtils = {} |
| 11 | + |
| 12 | +function AirQualitySensorUtils.is_matter_air_quality_sensor(opts, driver, device) |
| 13 | + for _, ep in ipairs(device.endpoints) do |
| 14 | + for _, dt in ipairs(ep.device_types) do |
| 15 | + if dt.device_type_id == fields.AIR_QUALITY_SENSOR_DEVICE_TYPE_ID then |
| 16 | + return true |
| 17 | + end |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + return false |
| 22 | + end |
| 23 | + |
| 24 | +function AirQualitySensorUtils.supports_capability_by_id_modular(device, capability, component) |
| 25 | + if not device:get_field(fields.SUPPORTED_COMPONENT_CAPABILITIES) then |
| 26 | + device.log.warn_with({hub_logs = true}, "Device has overriden supports_capability_by_id, but does not have supported capabilities set.") |
| 27 | + return false |
| 28 | + end |
| 29 | + for _, component_capabilities in ipairs(device:get_field(fields.SUPPORTED_COMPONENT_CAPABILITIES)) do |
| 30 | + local comp_id = component_capabilities[1] |
| 31 | + local capability_ids = component_capabilities[2] |
| 32 | + if (component == nil) or (component == comp_id) then |
| 33 | + for _, cap in ipairs(capability_ids) do |
| 34 | + if cap == capability then |
| 35 | + return true |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + return false |
| 41 | +end |
| 42 | + |
| 43 | +function AirQualitySensorUtils.unit_conversion(device, value, from_unit, to_unit) |
| 44 | + local conversion_function = fields.conversion_tables[from_unit][to_unit] |
| 45 | + if conversion_function == nil then |
| 46 | + device.log.info_with( {hub_logs = true} , string.format("Unsupported unit conversion from %s to %s", fields.unit_strings[from_unit], fields.unit_strings[to_unit])) |
| 47 | + return 1 |
| 48 | + end |
| 49 | + |
| 50 | + if value == nil then |
| 51 | + device.log.info_with( {hub_logs = true} , "unit conversion value is nil") |
| 52 | + return 1 |
| 53 | + end |
| 54 | + return conversion_function(value) |
| 55 | +end |
| 56 | + |
| 57 | +local function get_supported_health_concern_values_for_air_quality(device) |
| 58 | + local health_concern_datatype = capabilities.airQualityHealthConcern.airQualityHealthConcern |
| 59 | + local supported_values = {health_concern_datatype.unknown.NAME, health_concern_datatype.good.NAME, health_concern_datatype.unhealthy.NAME} |
| 60 | + if #embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID, { feature_bitmap = clusters.AirQuality.types.Feature.FAIR }) > 0 then |
| 61 | + table.insert(supported_values, health_concern_datatype.moderate.NAME) |
| 62 | + end |
| 63 | + if #embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID, { feature_bitmap = clusters.AirQuality.types.Feature.MODERATE }) > 0 then |
| 64 | + table.insert(supported_values, health_concern_datatype.slightlyUnhealthy.NAME) |
| 65 | + end |
| 66 | + if #embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID, { feature_bitmap = clusters.AirQuality.types.Feature.VERY_POOR }) > 0 then |
| 67 | + table.insert(supported_values, health_concern_datatype.veryUnhealthy.NAME) |
| 68 | + end |
| 69 | + if #embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID, { feature_bitmap = clusters.AirQuality.types.Feature.EXTREMELY_POOR }) > 0 then |
| 70 | + table.insert(supported_values, health_concern_datatype.hazardous.NAME) |
| 71 | + end |
| 72 | + return supported_values |
| 73 | +end |
| 74 | + |
| 75 | +local function get_supported_health_concern_values_for_concentration_cluster(device, cluster) |
| 76 | + -- note: health_concern_datatype is generic since all the healthConcern capabilities' datatypes are equivalent to those in airQualityHealthConcern |
| 77 | + local health_concern_datatype = capabilities.airQualityHealthConcern.airQualityHealthConcern |
| 78 | + local supported_values = {health_concern_datatype.unknown.NAME, health_concern_datatype.good.NAME, health_concern_datatype.unhealthy.NAME} |
| 79 | + if #embedded_cluster_utils.get_endpoints(device, cluster.ID, { feature_bitmap = cluster.types.Feature.MEDIUM_LEVEL }) > 0 then |
| 80 | + table.insert(supported_values, health_concern_datatype.moderate.NAME) |
| 81 | + end |
| 82 | + if #embedded_cluster_utils.get_endpoints(device, cluster.ID, { feature_bitmap = cluster.types.Feature.CRITICAL_LEVEL }) > 0 then |
| 83 | + table.insert(supported_values, health_concern_datatype.hazardous.NAME) |
| 84 | + end |
| 85 | + return supported_values |
| 86 | +end |
| 87 | + |
| 88 | +function AirQualitySensorUtils.set_supported_health_concern_values(device) |
| 89 | + -- handle AQ Health Concern, since this is a mandatory capability |
| 90 | + local supported_aqs_values = get_supported_health_concern_values_for_air_quality(device) |
| 91 | + local aqs_ep_ids = embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID) or {} |
| 92 | + device:emit_event_for_endpoint(aqs_ep_ids[1], capabilities.airQualityHealthConcern.supportedAirQualityValues(supported_aqs_values, { visibility = { displayed = false }})) |
| 93 | + |
| 94 | + for _, capability in ipairs(fields.CONCENTRATION_MEASUREMENT_PROFILE_ORDERING) do |
| 95 | + -- all of these capabilities are optional, and capabilities stored in this field are for either a HealthConcern or a Measurement/Sensor |
| 96 | + if device:supports_capability_by_id(capability.ID) and capability.ID:match("HealthConcern$") then |
| 97 | + local cluster_info = fields.CONCENTRATION_MEASUREMENT_MAP[capability][2] |
| 98 | + local supported_values_setter = fields.CONCENTRATION_MEASUREMENT_MAP[capability][3] |
| 99 | + local supported_values = get_supported_health_concern_values_for_concentration_cluster(device, cluster_info) |
| 100 | + local cluster_ep_ids = embedded_cluster_utils.get_endpoints(device, cluster_info.ID, { feature_bitmap = cluster_info.types.Feature.LEVEL_INDICATION }) or {} -- cluster associated with the supported capability |
| 101 | + device:emit_event_for_endpoint(cluster_ep_ids[1], supported_values_setter(supported_values, { visibility = { displayed = false }})) |
| 102 | + end |
| 103 | + end |
| 104 | +end |
| 105 | + |
| 106 | +return AirQualitySensorUtils |
0 commit comments