-
Notifications
You must be signed in to change notification settings - Fork 523
Matter AQS: Break AQS files further apart, add supported air quality sensor value handling #2587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0b72b1
Break AQS files apart, add supported air quality sensor value handling
hcarter-775 b6fad71
remove minor changes
hcarter-775 994d6f8
move attribute handlers to unique file
hcarter-775 171fd57
move setter out of configure, into init
hcarter-775 cdc760a
remove indexing
hcarter-775 c10b5ec
move unit_conversion to utils
hcarter-775 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
...sor/src/sub_drivers/air_quality_sensor/air_quality_sensor_handlers/attribute_handlers.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| -- Copyright © 2025 SmartThings, Inc. | ||
| -- Licensed under the Apache License, Version 2.0 | ||
|
|
||
| local st_utils = require "st.utils" | ||
| local capabilities = require "st.capabilities" | ||
| local aqs_utils = require "sub_drivers.air_quality_sensor.air_quality_sensor_utils.utils" | ||
| local aqs_fields = require "sub_drivers.air_quality_sensor.air_quality_sensor_utils.fields" | ||
|
|
||
| local AirQualitySensorAttributeHandlers = {} | ||
|
|
||
|
|
||
| -- [[ GENERIC CONCENTRATION MEASUREMENT CLUSTER ATTRIBUTES ]] | ||
|
|
||
| function AirQualitySensorAttributeHandlers.measurement_unit_factory(capability_name) | ||
| return function(driver, device, ib, response) | ||
| device:set_field(capability_name.."_unit", ib.data.value, {persist = true}) | ||
| end | ||
| end | ||
|
|
||
| function AirQualitySensorAttributeHandlers.level_value_factory(attribute) | ||
| return function(driver, device, ib, response) | ||
| device:emit_event_for_endpoint(ib.endpoint_id, attribute(aqs_fields.level_strings[ib.data.value])) | ||
| end | ||
| end | ||
|
|
||
| function AirQualitySensorAttributeHandlers.measured_value_factory(capability_name, attribute, target_unit) | ||
| return function(driver, device, ib, response) | ||
| local reporting_unit = device:get_field(capability_name.."_unit") | ||
|
|
||
| if reporting_unit == nil then | ||
| reporting_unit = aqs_fields.unit_default[capability_name] | ||
| device:set_field(capability_name.."_unit", reporting_unit, {persist = true}) | ||
| end | ||
|
|
||
| if reporting_unit then | ||
| local value = aqs_utils.unit_conversion(device, ib.data.value, reporting_unit, target_unit) | ||
| device:emit_event_for_endpoint(ib.endpoint_id, attribute({value = value, unit = aqs_fields.unit_strings[target_unit]})) | ||
|
|
||
| -- handle case where device profile supports both fineDustLevel and dustLevel | ||
| if capability_name == capabilities.fineDustSensor.NAME and device:supports_capability(capabilities.dustSensor) then | ||
| device:emit_event_for_endpoint(ib.endpoint_id, capabilities.dustSensor.fineDustLevel({value = value, unit = aqs_fields.unit_strings[target_unit]})) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
| -- [[ AIR QUALITY CLUSTER ATTRIBUTES ]] -- | ||
|
|
||
| function AirQualitySensorAttributeHandlers.air_quality_handler(driver, device, ib, response) | ||
| local state = ib.data.value | ||
| if state == 0 then -- Unknown | ||
| device:emit_event_for_endpoint(ib.endpoint_id, capabilities.airQualityHealthConcern.airQualityHealthConcern.unknown()) | ||
| elseif state == 1 then -- Good | ||
| device:emit_event_for_endpoint(ib.endpoint_id, capabilities.airQualityHealthConcern.airQualityHealthConcern.good()) | ||
| elseif state == 2 then -- Fair | ||
| device:emit_event_for_endpoint(ib.endpoint_id, capabilities.airQualityHealthConcern.airQualityHealthConcern.moderate()) | ||
| elseif state == 3 then -- Moderate | ||
| device:emit_event_for_endpoint(ib.endpoint_id, capabilities.airQualityHealthConcern.airQualityHealthConcern.slightlyUnhealthy()) | ||
| elseif state == 4 then -- Poor | ||
| device:emit_event_for_endpoint(ib.endpoint_id, capabilities.airQualityHealthConcern.airQualityHealthConcern.unhealthy()) | ||
| elseif state == 5 then -- VeryPoor | ||
| device:emit_event_for_endpoint(ib.endpoint_id, capabilities.airQualityHealthConcern.airQualityHealthConcern.veryUnhealthy()) | ||
| elseif state == 6 then -- ExtremelyPoor | ||
| device:emit_event_for_endpoint(ib.endpoint_id, capabilities.airQualityHealthConcern.airQualityHealthConcern.hazardous()) | ||
| end | ||
| end | ||
|
|
||
|
|
||
| -- [[ PRESSURE MEASUREMENT CLUSTER ATTRIBUTES ]] -- | ||
|
|
||
| function AirQualitySensorAttributeHandlers.pressure_measured_value_handler(driver, device, ib, response) | ||
| local pressure = st_utils.round(ib.data.value / 10.0) | ||
| device:emit_event_for_endpoint(ib.endpoint_id, capabilities.atmosphericPressureMeasurement.atmosphericPressure(pressure)) | ||
| end | ||
|
|
||
| return AirQualitySensorAttributeHandlers | ||
4 changes: 2 additions & 2 deletions
4
...r_quality_sensor/device_configuration.lua → ...ity_sensor_utils/device_configuration.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...sub_drivers/air_quality_sensor/fields.lua → ...ensor/air_quality_sensor_utils/fields.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...hings/matter-sensor/src/sub_drivers/air_quality_sensor/air_quality_sensor_utils/utils.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| -- Copyright © 2025 SmartThings, Inc. | ||
| -- Licensed under the Apache License, Version 2.0 | ||
|
|
||
| local capabilities = require "st.capabilities" | ||
| local clusters = require "st.matter.clusters" | ||
| local embedded_cluster_utils = require "sensor_utils.embedded_cluster_utils" | ||
| local fields = require "sub_drivers.air_quality_sensor.air_quality_sensor_utils.fields" | ||
|
|
||
|
|
||
| local AirQualitySensorUtils = {} | ||
|
|
||
| function AirQualitySensorUtils.is_matter_air_quality_sensor(opts, driver, device) | ||
| for _, ep in ipairs(device.endpoints) do | ||
| for _, dt in ipairs(ep.device_types) do | ||
| if dt.device_type_id == fields.AIR_QUALITY_SENSOR_DEVICE_TYPE_ID then | ||
| return true | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return false | ||
| end | ||
|
|
||
| function AirQualitySensorUtils.supports_capability_by_id_modular(device, capability, component) | ||
| if not device:get_field(fields.SUPPORTED_COMPONENT_CAPABILITIES) then | ||
| device.log.warn_with({hub_logs = true}, "Device has overriden supports_capability_by_id, but does not have supported capabilities set.") | ||
| return false | ||
| end | ||
| for _, component_capabilities in ipairs(device:get_field(fields.SUPPORTED_COMPONENT_CAPABILITIES)) do | ||
| local comp_id = component_capabilities[1] | ||
| local capability_ids = component_capabilities[2] | ||
| if (component == nil) or (component == comp_id) then | ||
| for _, cap in ipairs(capability_ids) do | ||
| if cap == capability then | ||
| return true | ||
| end | ||
| end | ||
| end | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| function AirQualitySensorUtils.unit_conversion(device, value, from_unit, to_unit) | ||
| local conversion_function = fields.conversion_tables[from_unit][to_unit] | ||
| if conversion_function == nil then | ||
| 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])) | ||
| return 1 | ||
| end | ||
|
|
||
| if value == nil then | ||
| device.log.info_with( {hub_logs = true} , "unit conversion value is nil") | ||
| return 1 | ||
| end | ||
| return conversion_function(value) | ||
| end | ||
|
|
||
| local function get_supported_health_concern_values_for_air_quality(device) | ||
| local health_concern_datatype = capabilities.airQualityHealthConcern.airQualityHealthConcern | ||
| local supported_values = {health_concern_datatype.unknown.NAME, health_concern_datatype.good.NAME, health_concern_datatype.unhealthy.NAME} | ||
| if #embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID, { feature_bitmap = clusters.AirQuality.types.Feature.FAIR }) > 0 then | ||
| table.insert(supported_values, health_concern_datatype.moderate.NAME) | ||
| end | ||
| if #embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID, { feature_bitmap = clusters.AirQuality.types.Feature.MODERATE }) > 0 then | ||
| table.insert(supported_values, health_concern_datatype.slightlyUnhealthy.NAME) | ||
| end | ||
| if #embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID, { feature_bitmap = clusters.AirQuality.types.Feature.VERY_POOR }) > 0 then | ||
| table.insert(supported_values, health_concern_datatype.veryUnhealthy.NAME) | ||
| end | ||
| if #embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID, { feature_bitmap = clusters.AirQuality.types.Feature.EXTREMELY_POOR }) > 0 then | ||
| table.insert(supported_values, health_concern_datatype.hazardous.NAME) | ||
| end | ||
| return supported_values | ||
| end | ||
|
|
||
| local function get_supported_health_concern_values_for_concentration_cluster(device, cluster) | ||
ctowns marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| -- note: health_concern_datatype is generic since all the healthConcern capabilities' datatypes are equivalent to those in airQualityHealthConcern | ||
| local health_concern_datatype = capabilities.airQualityHealthConcern.airQualityHealthConcern | ||
| local supported_values = {health_concern_datatype.unknown.NAME, health_concern_datatype.good.NAME, health_concern_datatype.unhealthy.NAME} | ||
| if #embedded_cluster_utils.get_endpoints(device, cluster.ID, { feature_bitmap = cluster.types.Feature.MEDIUM_LEVEL }) > 0 then | ||
| table.insert(supported_values, health_concern_datatype.moderate.NAME) | ||
| end | ||
| if #embedded_cluster_utils.get_endpoints(device, cluster.ID, { feature_bitmap = cluster.types.Feature.CRITICAL_LEVEL }) > 0 then | ||
| table.insert(supported_values, health_concern_datatype.hazardous.NAME) | ||
| end | ||
| return supported_values | ||
| end | ||
|
|
||
| function AirQualitySensorUtils.set_supported_health_concern_values(device) | ||
| -- handle AQ Health Concern, since this is a mandatory capability | ||
| local supported_aqs_values = get_supported_health_concern_values_for_air_quality(device) | ||
| local aqs_ep_ids = embedded_cluster_utils.get_endpoints(device, clusters.AirQuality.ID) or {} | ||
| device:emit_event_for_endpoint(aqs_ep_ids[1], capabilities.airQualityHealthConcern.supportedAirQualityValues(supported_aqs_values, { visibility = { displayed = false }})) | ||
|
|
||
| for _, capability in ipairs(fields.CONCENTRATION_MEASUREMENT_PROFILE_ORDERING) do | ||
| -- all of these capabilities are optional, and capabilities stored in this field are for either a HealthConcern or a Measurement/Sensor | ||
| if device:supports_capability_by_id(capability.ID) and capability.ID:match("HealthConcern$") then | ||
| local cluster_info = fields.CONCENTRATION_MEASUREMENT_MAP[capability][2] | ||
| local supported_values_setter = fields.CONCENTRATION_MEASUREMENT_MAP[capability][3] | ||
| local supported_values = get_supported_health_concern_values_for_concentration_cluster(device, cluster_info) | ||
| 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 | ||
| device:emit_event_for_endpoint(cluster_ep_ids[1], supported_values_setter(supported_values, { visibility = { displayed = false }})) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return AirQualitySensorUtils | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handlers unchanged, just moved